diff --git a/babel.config.js b/babel.config.js index 176e83ebae21..69255fe2df06 100644 --- a/babel.config.js +++ b/babel.config.js @@ -58,6 +58,7 @@ module.exports = function (api) { let ignoreLib = true; let includeRegeneratorRuntime = false; let needsPolyfillsForOldNode = false; + let dynamicESLintVersionCheck = false; let transformRuntimeOptions; @@ -109,6 +110,7 @@ module.exports = function (api) { // fall through case "test": targets = { node: "current" }; + dynamicESLintVersionCheck = true; break; } @@ -216,6 +218,10 @@ module.exports = function (api) { exclude: /regenerator-runtime/, plugins: [["@babel/transform-runtime", transformRuntimeOptions]], }, + dynamicESLintVersionCheck && { + test: ["./eslint/*/src"].map(normalize), + plugins: [pluginDynamicESLintVersionCheck], + }, ].filter(Boolean), }; @@ -652,3 +658,30 @@ function pluginBabelParserTokenType({ tokenTypesMapping.set(tokenTypesDefinition[i].key.name, i); } })(); + +// Transforms +// ESLINT_VERSION +// to +// process.env.ESLINT_VERSION_FOR_BABEL +// ? parseInt(process.env.ESLINT_VERSION_FOR_BABEL, 10) +// : ESLINT_VERSION +function pluginDynamicESLintVersionCheck({ template }) { + const transformed = new WeakSet(); + + return { + visitor: { + ReferencedIdentifier(path) { + if (path.node.name !== "ESLINT_VERSION") return; + + if (transformed.has(path.node)) return; + transformed.add(path.node); + + path.replaceWith(template.expression.ast` + process.env.ESLINT_VERSION_FOR_BABEL + ? parseInt(process.env.ESLINT_VERSION_FOR_BABEL, 10) + : ${path.node} + `); + }, + }, + }; +} diff --git a/eslint/babel-eslint-parser/src/convert/convertAST.cjs b/eslint/babel-eslint-parser/src/convert/convertAST.cjs index 2a7b4962d88f..1bc12d077e1d 100644 --- a/eslint/babel-eslint-parser/src/convert/convertAST.cjs +++ b/eslint/babel-eslint-parser/src/convert/convertAST.cjs @@ -1,4 +1,4 @@ -const getEslintVersion = require("../utils/get-eslint-version.cjs"); +const ESLINT_VERSION = require("../utils/eslint-version.cjs"); function* it(children) { if (Array.isArray(children)) yield* children; @@ -92,7 +92,7 @@ const convertNodesVisitor = { q.loc.end.column += 2; } - if (getEslintVersion() >= 8) { + if (ESLINT_VERSION >= 8) { q.start -= 1; if (q.tail) { q.end += 1; @@ -129,7 +129,7 @@ function convertProgramNode(ast) { ast.loc.end.line = lastToken.loc.end.line; ast.loc.end.column = lastToken.loc.end.column; - if (getEslintVersion() >= 8) { + if (ESLINT_VERSION >= 8) { ast.end = lastToken.end; } } @@ -145,7 +145,7 @@ function convertProgramNode(ast) { ast.loc.start.line = ast.body[0].loc.start.line; ast.range[0] = ast.body[0].start; - if (getEslintVersion() >= 8) { + if (ESLINT_VERSION >= 8) { ast.start = ast.body[0].start; } } diff --git a/eslint/babel-eslint-parser/src/convert/convertTokens.cjs b/eslint/babel-eslint-parser/src/convert/convertTokens.cjs index b9851e949836..c69f85b0c5e9 100644 --- a/eslint/babel-eslint-parser/src/convert/convertTokens.cjs +++ b/eslint/babel-eslint-parser/src/convert/convertTokens.cjs @@ -1,4 +1,4 @@ -const getEslintVersion = require("../utils/get-eslint-version.cjs"); +const ESLINT_VERSION = require("../utils/eslint-version.cjs"); function convertTemplateType(tokens, tl) { let curlyBrace = null; @@ -206,7 +206,7 @@ module.exports = function convertTokens(tokens, code, tl) { if (!process.env.BABEL_8_BREAKING) { // Babel 8 already produces a single token - if (getEslintVersion() >= 8 && token.type.label === tl.hash) { + if (ESLINT_VERSION >= 8 && token.type.label === tl.hash) { i++; token = withoutComments[i]; diff --git a/eslint/babel-eslint-parser/src/utils/eslint-version.cjs b/eslint/babel-eslint-parser/src/utils/eslint-version.cjs new file mode 100644 index 000000000000..2afb5fc97550 --- /dev/null +++ b/eslint/babel-eslint-parser/src/utils/eslint-version.cjs @@ -0,0 +1 @@ +module.exports = parseInt(require("eslint/package.json").version, 10); diff --git a/eslint/babel-eslint-parser/src/utils/get-eslint-version.cjs b/eslint/babel-eslint-parser/src/utils/get-eslint-version.cjs deleted file mode 100644 index db33b71f9d84..000000000000 --- a/eslint/babel-eslint-parser/src/utils/get-eslint-version.cjs +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = function eslintVersion() { - const version = - process.env.ESLINT_VERSION_FOR_BABEL || - require("eslint/package.json").version; - return parseInt(version, 10); -}; diff --git a/eslint/babel-eslint-parser/src/worker/configuration.cjs b/eslint/babel-eslint-parser/src/worker/configuration.cjs index 3319d2e9c7d0..28b1294b04fc 100644 --- a/eslint/babel-eslint-parser/src/worker/configuration.cjs +++ b/eslint/babel-eslint-parser/src/worker/configuration.cjs @@ -1,5 +1,5 @@ const babel = require("./babel-core.cjs"); -const getEslintVersion = require("../utils/get-eslint-version.cjs"); +const ESLINT_VERSION = require("../utils/eslint-version.cjs"); /** * Merge user supplied estree plugin options to default estree plugin options @@ -9,7 +9,7 @@ const getEslintVersion = require("../utils/get-eslint-version.cjs"); */ function getParserPlugins(babelOptions) { const babelParserPlugins = babelOptions.parserOpts?.plugins ?? []; - const estreeOptions = { classFeatures: getEslintVersion() >= 8 }; + const estreeOptions = { classFeatures: ESLINT_VERSION >= 8 }; for (const plugin of babelParserPlugins) { if (Array.isArray(plugin) && plugin[0] === "estree") { Object.assign(estreeOptions, plugin[1]);