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:
Huáng Jùnliàng
2021-06-21 10:51:00 -04:00
committed by GitHub
parent 8c229e7657
commit 5145c98a73
5 changed files with 29 additions and 46 deletions

View File

@@ -2847,9 +2847,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// by parsing `jsxTagStart` to stop the JSX plugin from
// messing with the tokens
const { context } = this.state;
if (context[context.length - 1] === tc.j_oTag) {
const curContext = context[context.length - 1];
if (curContext === tc.j_oTag) {
context.length -= 2;
} else if (context[context.length - 1] === tc.j_expr) {
} else if (curContext === tc.j_expr) {
context.length -= 1;
}
}

View File

@@ -55,8 +55,10 @@ tt.jsxTagStart = new TokenType("jsxTagStart", { startsExpr: true });
tt.jsxTagEnd = new TokenType("jsxTagEnd");
tt.jsxTagStart.updateContext = context => {
context.push(tc.j_expr); // treat as beginning of JSX expression
context.push(tc.j_oTag); // start opening tag context
context.push(
tc.j_expr, // treat as beginning of JSX expression
tc.j_oTag, // start opening tag context
);
};
function isFragment(object: ?N.JSXElement): boolean {
@@ -616,17 +618,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
updateContext(prevType: TokenType): void {
super.updateContext(prevType);
const { context, type } = this.state;
if (type === tt.braceL) {
const curContext = context[context.length - 1];
if (curContext === tc.j_oTag) {
context.push(tc.brace);
} else if (curContext === tc.j_expr) {
context.push(tc.templateQuasi);
}
this.state.exprAllowed = true;
} else if (type === tt.slash && prevType === tt.jsxTagStart) {
context.length -= 2; // do not consider JSX expr -> JSX open tag -> ... anymore
context.push(tc.j_cTag); // reconsider as closing tag context
if (type === tt.slash && prevType === tt.jsxTagStart) {
// do not consider JSX expr -> JSX open tag -> ... anymore
// reconsider as closing tag context
context.splice(-2, 2, tc.j_cTag);
this.state.exprAllowed = false;
} else if (type === tt.jsxTagEnd) {
const out = context.pop();