Search in sources :

Example 1 with VariableDeclarationTree

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

the class Parser method parseForStatement.

// 12.6.3 The for Statement
// 12.6.4 The for-in Statement
// The for-of Statement
// The for-await-of Statement
private ParseTree parseForStatement() {
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.FOR);
    boolean awaited = peekPredefinedString(AWAIT);
    if (awaited) {
        eatPredefinedString(AWAIT);
    }
    eat(TokenType.OPEN_PAREN);
    if (peekVariableDeclarationList()) {
        VariableDeclarationListTree variables = parseVariableDeclarationListNoIn();
        if (peek(TokenType.IN)) {
            if (awaited) {
                reportError("for-await-of is the only allowed asynchronous iteration");
            }
            // for-in: only one declaration allowed
            if (variables.declarations.size() > 1) {
                reportError("for-in statement may not have more than one variable declaration");
            }
            VariableDeclarationTree declaration = variables.declarations.get(0);
            if (declaration.initializer != null) {
                // http://esdiscuss.org/topic/initializer-expression-on-for-in-syntax-subject
                if (config.atLeast6) {
                    reportError("for-in statement may not have initializer");
                } else {
                    errorReporter.reportWarning(declaration.location.start, "for-in statement should not have initializer");
                }
            }
            return parseForInStatement(start, variables);
        } else if (peekPredefinedString(PredefinedName.OF)) {
            // for-of: only one declaration allowed
            if (variables.declarations.size() > 1) {
                if (awaited) {
                    reportError("for-await-of statement may not have more than one variable declaration");
                } else {
                    reportError("for-of statement may not have more than one variable declaration");
                }
            }
            // for-of: initializer is illegal
            VariableDeclarationTree declaration = variables.declarations.get(0);
            if (declaration.initializer != null) {
                if (awaited) {
                    reportError("for-await-of statement may not have initializer");
                } else {
                    reportError("for-of statement may not have initializer");
                }
            }
            if (awaited) {
                return parseForAwaitOfStatement(start, variables);
            } else {
                return parseForOfStatement(start, variables);
            }
        } else {
            // "Vanilla" for statement: const/destructuring must have initializer
            checkVanillaForInitializers(variables);
            return parseForStatement(start, variables);
        }
    }
    if (peek(TokenType.SEMI_COLON)) {
        return parseForStatement(start, null);
    }
    ParseTree initializer = parseExpressionNoIn();
    if (peek(TokenType.IN) || peek(TokenType.EQUAL) || peekPredefinedString(PredefinedName.OF)) {
        initializer = transformLeftHandSideExpression(initializer);
        if (!initializer.isValidAssignmentTarget()) {
            reportError("invalid assignment target");
        }
    }
    if (peek(TokenType.IN) || peekPredefinedString(PredefinedName.OF)) {
        if (initializer.type != ParseTreeType.BINARY_OPERATOR && initializer.type != ParseTreeType.COMMA_EXPRESSION) {
            if (peek(TokenType.IN)) {
                return parseForInStatement(start, initializer);
            } else {
                // for {await}? ( _ of _ )
                if (awaited) {
                    return parseForAwaitOfStatement(start, initializer);
                } else {
                    return parseForOfStatement(start, initializer);
                }
            }
        }
    }
    return parseForStatement(start, initializer);
}
Also used : VariableDeclarationListTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree) VariableDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 2 with VariableDeclarationTree

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

the class Parser method parseVariableDeclarationList.

private VariableDeclarationListTree parseVariableDeclarationList(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    TokenType token = peekType();
    switch(token) {
        case CONST:
        case LET:
        case VAR:
            eat(token);
            break;
        default:
            reportError(peekToken(), "expected declaration");
            return null;
    }
    ImmutableList.Builder<VariableDeclarationTree> declarations = ImmutableList.builder();
    declarations.add(parseVariableDeclaration(token, expressionIn));
    while (peek(TokenType.COMMA)) {
        eat(TokenType.COMMA);
        declarations.add(parseVariableDeclaration(token, expressionIn));
    }
    return new VariableDeclarationListTree(getTreeLocation(start), token, declarations.build());
}
Also used : VariableDeclarationListTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree) VariableDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition)

Example 3 with VariableDeclarationTree

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

the class Parser method parseVariableDeclaration.

private VariableDeclarationTree parseVariableDeclaration(final TokenType binding, Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    ParseTree lvalue;
    if (peekPatternStart()) {
        lvalue = parsePattern(PatternKind.INITIALIZER);
    } else {
        lvalue = parseIdentifierExpression();
    }
    ParseTree initializer = null;
    if (peek(TokenType.EQUAL)) {
        initializer = parseInitializer(expressionIn);
    } else if (expressionIn != Expression.NO_IN) {
        // NOTE(blickly): this is a bit of a hack, declarations outside of for statements allow "in",
        // and by chance, also must have initializers for const/destructuring. Vanilla for loops
        // also require intializers, but are handled separately in checkVanillaForInitializers
        maybeReportNoInitializer(binding, lvalue);
    }
    return new VariableDeclarationTree(getTreeLocation(start), lvalue, initializer);
}
Also used : VariableDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

VariableDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree)3 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)3 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)2 VariableDeclarationListTree (com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree)2 ImmutableList (com.google.common.collect.ImmutableList)1