Search in sources :

Example 1 with ComputedPropertyDefinitionTree

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

the class Parser method parseObjectPatternField.

private ParseTree parseObjectPatternField(PatternKind kind) {
    SourcePosition start = getTreeStartLocation();
    if (peekType() == TokenType.OPEN_SQUARE) {
        ParseTree key = parseComputedPropertyName();
        eat(TokenType.COLON);
        ParseTree value = parsePatternAssignmentTarget(kind);
        return new ComputedPropertyDefinitionTree(getTreeLocation(start), key, value);
    }
    Token name;
    if (peekIdOrKeyword()) {
        name = eatIdOrKeywordAsId();
        if (!peek(TokenType.COLON)) {
            IdentifierToken idToken = (IdentifierToken) name;
            if (Keywords.isKeyword(idToken.value)) {
                reportError("cannot use keyword '%s' here.", name);
            }
            if (peek(TokenType.EQUAL)) {
                IdentifierExpressionTree idTree = new IdentifierExpressionTree(getTreeLocation(start), idToken);
                eat(TokenType.EQUAL);
                ParseTree defaultValue = parseAssignmentExpression();
                return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue);
            }
            return new PropertyNameAssignmentTree(getTreeLocation(start), name, null);
        }
    } else {
        name = parseLiteralExpression().literalToken;
    }
    eat(TokenType.COLON);
    ParseTree value = parsePatternAssignmentTarget(kind);
    return new PropertyNameAssignmentTree(getTreeLocation(start), name, value);
}
Also used : ComputedPropertyDefinitionTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyDefinitionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) IdentifierExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree) PropertyNameAssignmentTree(com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

ComputedPropertyDefinitionTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyDefinitionTree)1 DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)1 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 PropertyNameAssignmentTree (com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree)1 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)1