Skip to content

Commit

Permalink
refactor: simplify JSX context tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 2, 2021
1 parent 722429f commit 614b17a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
8 changes: 3 additions & 5 deletions packages/babel-parser/src/plugins/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2812,11 +2812,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// by parsing `jsxTagStart` to stop the JSX plugin from
// messing with the tokens
const { context } = this.state;
const curContext = context[context.length - 1];
if (curContext === tc.j_oTag) {
context.length -= 2;
} else if (curContext === tc.j_expr) {
context.length -= 1;
const currentContext = context[context.length - 1];
if (currentContext === tc.j_oTag || currentContext === tc.j_expr) {
context.pop();
}
}

Expand Down
10 changes: 4 additions & 6 deletions packages/babel-parser/src/plugins/jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,25 +617,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

updateContext(prevType: TokenType): void {
super.updateContext(prevType);
const { context, type } = this.state;
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.canStartJSXElement = false;
} else if (type === tt.jsxTagStart) {
context.push(
tc.j_expr, // treat as beginning of JSX expression
tc.j_oTag, // start opening tag context
);
// start opening tag context
context.push(tc.j_oTag);
} else if (type === tt.jsxTagEnd) {
const out = context.pop();
const out = context[context.length - 1];
if ((out === tc.j_oTag && prevType === tt.slash) || out === tc.j_cTag) {
context.pop();
this.state.canStartJSXElement =
context[context.length - 1] === tc.j_expr;
} else {
this.setContext(tc.j_expr);
this.state.canStartJSXElement = true;
}
} else {
Expand Down
11 changes: 5 additions & 6 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
tt,
type TokenType,
} from "../../tokenizer/types";
import { types as ct } from "../../tokenizer/context";
import { types as tc } from "../../tokenizer/context";
import * as N from "../../types";
import type { Position } from "../../util/location";
import type Parser from "../../parser";
Expand Down Expand Up @@ -2876,14 +2876,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
/*:: invariant(jsx.node != null) */
if (!jsx.error) return jsx.node;

// Remove `tc.j_expr` and `tc.j_oTag` from context added
// Remove `tc.j_expr` or `tc.j_oTag` from context added
// by parsing `jsxTagStart` to stop the JSX plugin from
// messing with the tokens
const { context } = this.state;
if (context[context.length - 1] === ct.j_oTag) {
context.length -= 2;
} else if (context[context.length - 1] === ct.j_expr) {
context.length -= 1;
const currentContext = context[context.length - 1];
if (currentContext === tc.j_oTag || currentContext === tc.j_expr) {
context.pop();
}
}

Expand Down

0 comments on commit 614b17a

Please sign in to comment.