Skip to content

Commit

Permalink
refactor: move JSX context to context.js
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 2, 2021
1 parent 0b1ec5a commit 96712cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 0 additions & 6 deletions packages/babel-parser/src/plugins/jsx/index.js
Expand Up @@ -46,12 +46,6 @@ const JsxErrors = makeErrorTemplates(
);
/* eslint-disable sort-keys */

// Be aware that this file is always executed and not only when the plugin is enabled.
// Therefore the contexts do always exist.
tc.j_oTag = new TokContext("<tag");
tc.j_cTag = new TokContext("</tag");
tc.j_expr = new TokContext("<tag>...</tag>", true);

function isFragment(object: ?N.JSXElement): boolean {
return object
? object.type === "JSXOpeningFragment" ||
Expand Down
18 changes: 13 additions & 5 deletions packages/babel-parser/src/tokenizer/context.js
@@ -1,7 +1,7 @@
// @flow

// The token context is used to track whether the apostrophe "`"
// starts or ends a string template
// The token context is used in JSX plugin to track
// jsx tag / jsx text / normal JavaScript expression

export class TokContext {
constructor(token: string, preserveSpace?: boolean) {
Expand All @@ -13,9 +13,17 @@ export class TokContext {
preserveSpace: boolean;
}

export const types: {
const types: {
[key: string]: TokContext,
} = {
brace: new TokContext("{"),
template: new TokContext("`", true),
brace: new TokContext("{"), // normal JavaScript expression
j_oTag: new TokContext("<tag"), // JSX openning tag
j_cTag: new TokContext("</tag"), // JSX closing tag
j_expr: new TokContext("<tag>...</tag>", true), // JSX expressions
};

if (!process.env.BABEL_8_BREAKING) {
types.template = new TokContext("`", true);
}

export { types };

0 comments on commit 96712cd

Please sign in to comment.