Search in sources :

Example 1 with ComputedPropertyMethodTree

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

the class Parser method parseAsyncMethod.

private ParseTree parseAsyncMethod(PartialClassElement partial) {
    eatPredefinedString(ASYNC);
    boolean generator = peek(TokenType.STAR);
    if (generator) {
        eat(TokenType.STAR);
    }
    if (peekPropertyName(0)) {
        if (peekIdOrKeyword()) {
            IdentifierToken name = eatIdOrKeywordAsId();
            FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.MEMBER).setAsync(true).setGenerator(generator).setStatic(partial.isStatic).setName(name);
            parseFunctionTail(builder, generator ? FunctionFlavor.ASYNCHRONOUS_GENERATOR : FunctionFlavor.ASYNCHRONOUS);
            return builder.build(getTreeLocation(name.getStart()));
        } else {
            // { 'str'() {} }
            // { 123() {} }
            // Treat these as if they were computed properties.
            ParseTree nameExpr = parseLiteralExpression();
            FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.EXPRESSION).setAsync(true).setGenerator(generator).setStatic(partial.isStatic);
            parseFunctionTail(builder, generator ? FunctionFlavor.ASYNCHRONOUS_GENERATOR : FunctionFlavor.ASYNCHRONOUS);
            ParseTree function = builder.build(getTreeLocation(nameExpr.getStart()));
            return new ComputedPropertyMethodTree(getTreeLocation(nameExpr.getStart()), nameExpr, function);
        }
    } else {
        // expect '[' to start computed property name
        ParseTree nameExpr = parseComputedPropertyName();
        FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.EXPRESSION).setAsync(true).setGenerator(generator).setStatic(partial.isStatic);
        parseFunctionTail(builder, generator ? FunctionFlavor.ASYNCHRONOUS_GENERATOR : FunctionFlavor.ASYNCHRONOUS);
        ParseTree function = builder.build(getTreeLocation(nameExpr.getStart()));
        return new ComputedPropertyMethodTree(getTreeLocation(nameExpr.getStart()), nameExpr, function);
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 2 with ComputedPropertyMethodTree

use of com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree 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)

Example 3 with ComputedPropertyMethodTree

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

the class Parser method parsePropertyAssignmentGenerator.

private ParseTree parsePropertyAssignmentGenerator() {
    TokenType type = peekType(1);
    if (type == TokenType.STRING || type == TokenType.NUMBER || type == TokenType.IDENTIFIER || Keywords.isKeyword(type)) {
        // parseMethodDeclaration will consume the '*'.
        return parseMethodDeclaration();
    } else {
        SourcePosition start = getTreeStartLocation();
        eat(TokenType.STAR);
        ParseTree name = parseComputedPropertyName();
        FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.EXPRESSION);
        parseFunctionTail(builder, FunctionFlavor.GENERATOR);
        ParseTree value = builder.build(getTreeLocation(start));
        return new ComputedPropertyMethodTree(getTreeLocation(start), name, value);
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 4 with ComputedPropertyMethodTree

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

the class Parser method parseMethodDefinition.

private ParseTree parseMethodDefinition(PartialClassElement partial, boolean isGenerator) {
    FunctionDeclarationTree.Kind kind;
    if (partial.getNameExpr() == null) {
        kind = FunctionDeclarationTree.Kind.MEMBER;
    } else {
        kind = FunctionDeclarationTree.Kind.EXPRESSION;
    }
    FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(kind).setName(partial.getName()).setStatic(partial.isStatic);
    parseFunctionTail(builder, isGenerator ? FunctionFlavor.GENERATOR : FunctionFlavor.NORMAL);
    ParseTree function = builder.build(getTreeLocation(partial.start));
    if (kind == FunctionDeclarationTree.Kind.MEMBER) {
        return function;
    } else {
        return new ComputedPropertyMethodTree(getTreeLocation(partial.start), partial.getNameExpr(), function);
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

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