private void checkArgumentsMatchParameters ( Node call , FunctionType functionType , Iterator < Node > arguments , Iterator < Node > parameters , int firstParameterIndex ) { int spreadArgumentCount = 0 ; int normalArgumentCount = firstParameterIndex ; boolean checkArgumentTypeAgainstParameter = true ; Node parameter = null ; Node argument = null ; while ( arguments . hasNext ( ) ) { // get the next argument argument = arguments . next ( ) ; // Count normal & spread arguments. if ( argument . isSpread ( ) ) { // we have some form of this case // someCall(arg1, arg2, ...firstSpreadExpression, argN, ...secondSpreadExpression) spreadArgumentCount ++ ; // Once we see a spread parameter, we can no longer match up arguments with parameters. checkArgumentTypeAgainstParameter = false ; } else { normalArgumentCount ++ ; } // Get the next parameter, if we're still matching parameters and arguments. if ( checkArgumentTypeAgainstParameter ) { if ( parameters . hasNext ( ) ) { parameter = parameters . next ( ) ; } else if ( parameter != null && parameter . isVarArgs ( ) ) { // use varargs for all remaining parameters } else { // else we ran out of parameters and will report that after this loop parameter = null ; checkArgumentTypeAgainstParameter = false ; } } if ( checkArgumentTypeAgainstParameter ) { validator . expectArgumentMatchesParameter ( argument , getJSType ( argument ) , getJSType ( parameter ) , call , normalArgumentCount ) ; } } int minArity = functionType . getMinArity ( ) ; int maxArity = functionType . getMaxArity ( ) ; if ( spreadArgumentCount > 0 ) { if ( normalArgumentCount > maxArity ) { // We cannot reliably check whether the total argument count is wrong, but we can at // least tell if there are more arguments than the function can handle even ignoring the // spreads. report ( call , WRONG_ARGUMENT_COUNT , typeRegistry . getReadableTypeNameNoDeref ( call . getFirstChild ( ) ) , "at least " + String . valueOf ( normalArgumentCount ) , String . valueOf ( minArity ) , maxArity == Integer . MAX_VALUE ? "" : " and no more than " + maxArity + " argument(s)" ) ; } } else { if ( minArity > normalArgumentCount || maxArity < normalArgumentCount ) { report ( call , WRONG_ARGUMENT_COUNT , typeRegistry . getReadableTypeNameNoDeref ( call . getFirstChild ( ) ) , String . valueOf ( normalArgumentCount ) , String . valueOf ( minArity ) , maxArity == Integer . MAX_VALUE ? "" : " and no more than " + maxArity + " argument(s)" ) ; } } }
Checks that a list of arguments match a list of formal parameters