Search in sources :

Example 1 with ComputedPropertyMemberVariableTree

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

the class Parser method parseClassMemberDeclaration.

private ParseTree parseClassMemberDeclaration(PartialClassElement partial) {
    boolean isGenerator = eatOpt(TokenType.STAR) != null;
    ParseTree nameExpr;
    IdentifierToken name;
    if (peekPropertyName(0)) {
        if (peekIdOrKeyword()) {
            nameExpr = null;
            name = eatIdOrKeywordAsId();
            if (Keywords.isKeyword(name.value)) {
                features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
            }
        } else {
            // { 'str'() {} }
            // { 123() {} }
            // Treat these as if they were computed properties.
            name = null;
            nameExpr = parseLiteralExpression();
        }
    } else {
        if (config.parseTypeSyntax && peekIndexSignature()) {
            ParseTree indexSignature = parseIndexSignature();
            eatPossibleImplicitSemiColon();
            return indexSignature;
        }
        nameExpr = parseComputedPropertyName();
        name = null;
    }
    // Member variables are supported only when parsing type syntax
    if (!config.parseTypeSyntax || peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
        // Member function.
        FunctionDeclarationTree.Kind kind;
        TokenType accessOnFunction;
        if (nameExpr == null) {
            kind = FunctionDeclarationTree.Kind.MEMBER;
            accessOnFunction = partial.accessModifier;
        } else {
            kind = FunctionDeclarationTree.Kind.EXPRESSION;
            // Accessibility modifier goes on the ComputedPropertyMethodTree
            accessOnFunction = null;
        }
        ParseTree function;
        if (partial.isAmbient) {
            function = parseMethodSignature(partial, name, isGenerator, /* isOptional */
            false);
            eatPossibleImplicitSemiColon();
        } else {
            FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(kind).setName(name).setStatic(partial.isStatic).setAccess(accessOnFunction);
            parseFunctionTail(builder, isGenerator ? FunctionFlavor.GENERATOR : FunctionFlavor.NORMAL);
            function = builder.build(getTreeLocation(partial.start));
        }
        if (kind == FunctionDeclarationTree.Kind.MEMBER) {
            return function;
        } else {
            return new ComputedPropertyMethodTree(getTreeLocation(partial.start), partial.accessModifier, nameExpr, function);
        }
    } else {
        // Member variable.
        if (isGenerator) {
            reportError("Member variable cannot be prefixed by '*' (generator function)");
        }
        ParseTree declaredType = maybeParseColonType();
        if (peek(TokenType.EQUAL)) {
            reportError("Member variable initializers ('=') are not supported");
        }
        eatPossibleImplicitSemiColon();
        if (nameExpr == null) {
            return new MemberVariableTree(getTreeLocation(partial.start), name, partial.isStatic, false, partial.accessModifier, declaredType);
        } else {
            return new ComputedPropertyMemberVariableTree(getTreeLocation(partial.start), nameExpr, partial.isStatic, partial.accessModifier, declaredType);
        }
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) ComputedPropertyMemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) ComputedPropertyMemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree) MemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree)

Aggregations

ComputedPropertyMemberVariableTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree)1 ComputedPropertyMethodTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree)1 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)1 MemberVariableTree (com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1