Skip to content

Commit

Permalink
add tokenIsTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 19, 2021
1 parent 491c921 commit 1859669
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
27 changes: 15 additions & 12 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -27,6 +27,7 @@ import {
tokenIsPostfix,
tokenIsPrefix,
tokenIsRightAssociative,
tokenIsTemplate,
tokenKeywordOrIdentifierIsKeyword,
tokenLabelName,
tokenOperatorPrecedence,
Expand Down Expand Up @@ -706,9 +707,10 @@ export default class ExpressionParser extends LValParser {
noCalls: ?boolean,
state: N.ParseSubscriptState,
): N.Expression {
if (!noCalls && this.eat(tt.doubleColon)) {
const { type } = this.state;
if (!noCalls && type === tt.doubleColon) {
return this.parseBind(base, startPos, startLoc, noCalls, state);
} else if (this.match(tt.templateNonTail) || this.match(tt.templateTail)) {
} else if (tokenIsTemplate(type)) {
return this.parseTaggedTemplateExpression(
base,
startPos,
Expand All @@ -719,7 +721,7 @@ export default class ExpressionParser extends LValParser {

let optional = false;

if (this.match(tt.questionDot)) {
if (type === tt.questionDot) {
if (noCalls && this.lookaheadCharCode() === charCodes.leftParenthesis) {
// stop at `?.` when parsing `new a?.()`
state.stop = true;
Expand Down Expand Up @@ -801,6 +803,7 @@ export default class ExpressionParser extends LValParser {
): N.Expression {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
this.next(); // eat '::'
node.callee = this.parseNoCallExpr();
state.stop = true;
return this.parseSubscripts(
Expand Down Expand Up @@ -2691,22 +2694,22 @@ export default class ExpressionParser extends LValParser {
}

isAmbiguousAwait(): boolean {
if (this.hasPrecedingLineBreak()) return true;
const { type } = this.state;
return (
this.hasPrecedingLineBreak() ||
// All the following expressions are ambiguous:
// await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await ``
this.match(tt.plusMin) ||
this.match(tt.parenL) ||
this.match(tt.bracketL) ||
this.match(tt.templateNonTail) ||
this.match(tt.templateTail) ||
type === tt.plusMin ||
type === tt.parenL ||
type === tt.bracketL ||
tokenIsTemplate(type) ||
// Sometimes the tokenizer generates tt.slash for regexps, and this is
// handler by parseExprAtom
this.match(tt.regexp) ||
this.match(tt.slash) ||
type === tt.regexp ||
type === tt.slash ||
// This code could be parsed both as a modulo operator or as an intrinsic:
// await %x(0)
(this.hasPlugin("v8intrinsic") && this.match(tt.modulo))
(this.hasPlugin("v8intrinsic") && type === tt.modulo)
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-parser/src/parser/statement.js
Expand Up @@ -4,6 +4,7 @@ import * as N from "../types";
import {
tokenIsIdentifier,
tokenIsLoop,
tokenIsTemplate,
tt,
type TokenType,
getExportedToken,
Expand Down Expand Up @@ -96,7 +97,7 @@ function babel7CompatTokens(tokens) {
continue;
}
}
if (type === tt.templateNonTail || type === tt.templateTail) {
if (tokenIsTemplate(type)) {
if (!process.env.BABEL_8_BREAKING) {
const { loc, start, value, end } = token;
const backquoteEnd = start + 1;
Expand Down
6 changes: 2 additions & 4 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -14,6 +14,7 @@ import {
tokenIsKeywordOrIdentifier,
tt,
type TokenType,
tokenIsTemplate,
} from "../../tokenizer/types";
import { types as tc } from "../../tokenizer/context";
import * as N from "../../types";
Expand Down Expand Up @@ -2197,10 +2198,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

return this.finishCallExpression(node, state.optionalChainMember);
} else if (
this.match(tt.templateNonTail) ||
this.match(tt.templateTail)
) {
} else if (tokenIsTemplate(this.state.type)) {
const result = this.parseTaggedTemplateExpression(
base,
startPos,
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-parser/src/tokenizer/types.js
Expand Up @@ -158,8 +158,10 @@ export const tt: { [name: string]: TokenType } = {
ellipsis: createToken("...", { beforeExpr }),
backQuote: createToken("`", { startsExpr }),
dollarBraceL: createToken("${", { beforeExpr, startsExpr }),
// start: isTemplate
templateTail: createToken("...`", { startsExpr }),
templateNonTail: createToken("...${", { beforeExpr, startsExpr }),
// end: isTemplate
at: createToken("@"),
hash: createToken("#", { startsExpr }),

Expand Down Expand Up @@ -404,6 +406,10 @@ export function tokenIsRightAssociative(token: TokenType): boolean {
return token === tt.exponent;
}

export function tokenIsTemplate(token: TokenType): boolean {
return token >= tt.templateTail && token <= tt.templateNonTail;
}

export function getExportedToken(token: TokenType): ExportedTokenType {
return tokenTypes[token];
}
Expand Down

0 comments on commit 1859669

Please sign in to comment.