Search in sources :

Example 1 with TemplateLiteralPortionTree

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

the class Parser method parseTemplateLiteral.

/**
 * Constructs a template literal expression tree. "operand" is used to handle
 * the case like "foo`bar`", which is a CallExpression or MemberExpression that
 * calls the function foo() with the template literal as the argument (with extra
 * handling). In this case, operand would be "foo", which is the callsite.
 *
 * <p>We store this operand in the TemplateLiteralExpressionTree and
 * generate a TAGGED_TEMPLATELIT node if it's not null later when transpiling.
 *
 * @param operand A non-null value would represent the callsite
 * @return The template literal expression
 */
private TemplateLiteralExpressionTree parseTemplateLiteral(ParseTree operand) {
    SourcePosition start = operand == null ? getTreeStartLocation() : operand.location.start;
    Token token = nextToken();
    ImmutableList.Builder<ParseTree> elements = ImmutableList.builder();
    elements.add(new TemplateLiteralPortionTree(token.location, token));
    if (token.type == TokenType.NO_SUBSTITUTION_TEMPLATE) {
        return new TemplateLiteralExpressionTree(getTreeLocation(start), operand, elements.build());
    }
    // `abc${
    ParseTree expression = parseExpression();
    elements.add(new TemplateSubstitutionTree(expression.location, expression));
    while (!errorReporter.hadError()) {
        token = nextTemplateLiteralToken();
        if (token.type == TokenType.ERROR || token.type == TokenType.END_OF_FILE) {
            break;
        }
        elements.add(new TemplateLiteralPortionTree(token.location, token));
        if (token.type == TokenType.TEMPLATE_TAIL) {
            break;
        }
        expression = parseExpression();
        elements.add(new TemplateSubstitutionTree(expression.location, expression));
    }
    return new TemplateLiteralExpressionTree(getTreeLocation(start), operand, elements.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) TemplateLiteralPortionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralPortionTree) TemplateLiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) TemplateSubstitutionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateSubstitutionTree)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 TemplateLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree)1 TemplateLiteralPortionTree (com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralPortionTree)1 TemplateSubstitutionTree (com.google.javascript.jscomp.parsing.parser.trees.TemplateSubstitutionTree)1 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)1