From 96712cd37849328dfa47062b32eecac2826e0acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 2 Nov 2021 12:01:26 -0400 Subject: [PATCH] refactor: move JSX context to context.js --- packages/babel-parser/src/plugins/jsx/index.js | 6 ------ packages/babel-parser/src/tokenizer/context.js | 18 +++++++++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/babel-parser/src/plugins/jsx/index.js b/packages/babel-parser/src/plugins/jsx/index.js index 5844426cb1cb..d2ecb7306e88 100644 --- a/packages/babel-parser/src/plugins/jsx/index.js +++ b/packages/babel-parser/src/plugins/jsx/index.js @@ -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("...", true); - function isFragment(object: ?N.JSXElement): boolean { return object ? object.type === "JSXOpeningFragment" || diff --git a/packages/babel-parser/src/tokenizer/context.js b/packages/babel-parser/src/tokenizer/context.js index ebf4793afc33..6b985901814b 100644 --- a/packages/babel-parser/src/tokenizer/context.js +++ b/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) { @@ -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("...", true), // JSX expressions }; + +if (!process.env.BABEL_8_BREAKING) { + types.template = new TokContext("`", true); +} + +export { types };