Simplify token context (#13450)
* refactor: unify tc.brace and tc.templateQuasi * refactor: remove unused context check * perf: reduce arrayPrototype call and hoist variables
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
// @flow
|
||||
|
||||
// The token context is used to track whether `}` matches
|
||||
// a template quasi `${` or other tokens containing `{`:
|
||||
// namely tt.braceL `{` and tt.braceHashL `#{`
|
||||
// The token context is used to track whether the apostrophe "`"
|
||||
// starts or ends a string template
|
||||
|
||||
import { types as tt } from "./types";
|
||||
|
||||
@@ -20,7 +19,6 @@ export const types: {
|
||||
[key: string]: TokContext,
|
||||
} = {
|
||||
brace: new TokContext("{"),
|
||||
templateQuasi: new TokContext("${"),
|
||||
template: new TokContext("`", true),
|
||||
};
|
||||
|
||||
@@ -35,19 +33,22 @@ export const types: {
|
||||
// `this.prodParam` still has `[Yield]` production because it is not yet updated
|
||||
|
||||
tt.braceR.updateContext = context => {
|
||||
if (context.length > 1) {
|
||||
context.pop();
|
||||
}
|
||||
context.pop();
|
||||
};
|
||||
|
||||
// we don't need to update context for tt.braceBarL because we do not pop context for tt.braceBarR
|
||||
tt.braceL.updateContext = tt.braceHashL.updateContext = context => {
|
||||
context.push(types.brace);
|
||||
};
|
||||
|
||||
tt.dollarBraceL.updateContext = context => {
|
||||
context.push(types.templateQuasi);
|
||||
};
|
||||
// ideally only dollarBraceL "${" needs a non-template context
|
||||
// in order to indicate that the last "`" in `${`" starts a new string template
|
||||
// inside a template element within outer string template.
|
||||
// but when we popped such context in `}`, we lost track of whether this
|
||||
// `}` matches a `${` or other tokens matching `}`, so we have to push
|
||||
// such context in every token that `}` will match.
|
||||
tt.braceL.updateContext =
|
||||
tt.braceHashL.updateContext =
|
||||
tt.dollarBraceL.updateContext =
|
||||
context => {
|
||||
context.push(types.brace);
|
||||
};
|
||||
|
||||
tt.backQuote.updateContext = context => {
|
||||
if (context[context.length - 1] === types.template) {
|
||||
|
||||
@@ -127,10 +127,10 @@ export default class State {
|
||||
lastTokStart: number = 0;
|
||||
lastTokEnd: number = 0;
|
||||
|
||||
// The context stack is used to superficially track syntactic
|
||||
// context to predict whether a regular expression is allowed in a
|
||||
// given position.
|
||||
// The context stack is used to track whether the apostrophe "`" starts
|
||||
// or ends a string template
|
||||
context: Array<TokContext> = [ct.brace];
|
||||
// Used to track whether a JSX element is allowed to form
|
||||
exprAllowed: boolean = true;
|
||||
|
||||
// Used to signal to callers of `readWord1` whether the word
|
||||
|
||||
Reference in New Issue
Block a user