Search in sources :

Example 6 with FormalParameterListTree

use of com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree in project closure-compiler by google.

the class Parser method parseFormalParameterList.

private FormalParameterListTree parseFormalParameterList() {
    SourcePosition listStart = getTreeStartLocation();
    eat(TokenType.OPEN_PAREN);
    ImmutableList.Builder<ParseTree> result = ImmutableList.builder();
    boolean trailingComma = false;
    ImmutableList.Builder<SourcePosition> commaPositions = ImmutableList.builder();
    while (peekParameter()) {
        result.add(parseParameter());
        if (!peek(TokenType.CLOSE_PAREN)) {
            Token comma = eat(TokenType.COMMA);
            if (comma != null) {
                commaPositions.add(comma.getStart());
            } else {
                // semi-arbitrary comma position in case the code is syntactially invalid & missing one
                commaPositions.add(getTreeEndLocation());
            }
            if (peek(TokenType.CLOSE_PAREN)) {
                recordFeatureUsed(Feature.TRAILING_COMMA_IN_PARAM_LIST);
                if (!config.atLeast8) {
                    reportError(comma, "Invalid trailing comma in formal parameter list");
                }
                trailingComma = true;
            }
        }
    }
    eat(TokenType.CLOSE_PAREN);
    return new FormalParameterListTree(getTreeLocation(listStart), result.build(), trailingComma, commaPositions.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)

Example 7 with FormalParameterListTree

use of com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree in project closure-compiler by google.

the class Parser method parseSetAccessor.

private ParseTree parseSetAccessor(PartialClassElement partial) {
    eatPredefinedString(PredefinedName.SET);
    if (peekPropertyName(0)) {
        Token propertyName = eatObjectLiteralPropertyName();
        FormalParameterListTree parameter = parseSetterParameterList();
        BlockTree body = parseFunctionBody();
        recordFeatureUsed(Feature.SETTER);
        return new SetAccessorTree(getTreeLocation(partial.start), propertyName, partial.isStatic, parameter, body);
    } else {
        ParseTree property = parseComputedPropertyName();
        FormalParameterListTree parameter = parseSetterParameterList();
        BlockTree body = parseFunctionBody();
        recordFeatureUsed(Feature.SETTER);
        return new ComputedPropertySetterTree(getTreeLocation(partial.start), property, partial.isStatic, parameter, body);
    }
}
Also used : SetAccessorTree(com.google.javascript.jscomp.parsing.parser.trees.SetAccessorTree) BlockTree(com.google.javascript.jscomp.parsing.parser.trees.BlockTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) ComputedPropertySetterTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertySetterTree)

Example 8 with FormalParameterListTree

use of com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree in project closure-compiler by google.

the class Parser method completeArrowFunctionParseAtArrow.

private ParseTree completeArrowFunctionParseAtArrow(ParseTree leftOfArrow, Expression expressionIn) {
    FormalParameterListTree arrowFormalParameters = transformToArrowFormalParameters(leftOfArrow);
    if (peekImplicitSemiColon()) {
        reportError("No newline allowed before '=>'");
    }
    eat(TokenType.ARROW);
    ParseTree arrowFunctionBody = parseArrowFunctionBody(expressionIn, FunctionFlavor.NORMAL);
    FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.ARROW).setFormalParameterList(arrowFormalParameters).setFunctionBody(arrowFunctionBody);
    return builder.build(getTreeLocation(arrowFormalParameters.location.start));
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 9 with FormalParameterListTree

use of com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree in project closure-compiler by google.

the class Parser method parseAsyncArrowFunction.

private ParseTree parseAsyncArrowFunction(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    eatPredefinedString(ASYNC);
    if (peekImplicitSemiColon()) {
        reportError("No newline allowed between `async` and arrow function parameter list");
    }
    FormalParameterListTree arrowParameterList = null;
    if (peek(TokenType.OPEN_PAREN)) {
        // async (...) =>
        arrowParameterList = parseFormalParameterList();
    } else {
        // async arg =>
        final IdentifierExpressionTree singleParameter = parseIdentifierExpression();
        arrowParameterList = new FormalParameterListTree(singleParameter.location, ImmutableList.<ParseTree>of(singleParameter), /* hasTrailingComma= */
        false, ImmutableList.<SourcePosition>of());
    }
    if (peekImplicitSemiColon()) {
        reportError("No newline allowed before '=>'");
    }
    eat(TokenType.ARROW);
    ParseTree arrowFunctionBody = parseArrowFunctionBody(expressionIn, FunctionFlavor.ASYNCHRONOUS);
    FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.ARROW).setAsync(true).setFormalParameterList(arrowParameterList).setFunctionBody(arrowFunctionBody);
    return builder.build(getTreeLocation(start));
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) IdentifierExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)9 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)9 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)7 ImmutableList (com.google.common.collect.ImmutableList)2 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)2 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)1 CallSignatureTree (com.google.javascript.jscomp.parsing.parser.trees.CallSignatureTree)1 CommaExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree)1 ComputedPropertySetterTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertySetterTree)1 FunctionTypeTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionTypeTree)1 GenericTypeListTree (com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree)1 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)1 MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)1 ParenExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ParenExpressionTree)1 SetAccessorTree (com.google.javascript.jscomp.parsing.parser.trees.SetAccessorTree)1