Skip to content

Commit

Permalink
Only read eslint version dynamically during dev
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 22, 2021
1 parent 9ab25e8 commit 53e4b51
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
33 changes: 33 additions & 0 deletions babel.config.js
Expand Up @@ -58,6 +58,7 @@ module.exports = function (api) {
let ignoreLib = true;
let includeRegeneratorRuntime = false;
let needsPolyfillsForOldNode = false;
let dynamicESLintVersionCheck = false;

let transformRuntimeOptions;

Expand Down Expand Up @@ -109,6 +110,7 @@ module.exports = function (api) {
// fall through
case "test":
targets = { node: "current" };
dynamicESLintVersionCheck = true;
break;
}

Expand Down Expand Up @@ -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),
};

Expand Down Expand Up @@ -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}
`);
},
},
};
}
8 changes: 4 additions & 4 deletions 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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions 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;
Expand Down Expand Up @@ -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];

Expand Down
1 change: 1 addition & 0 deletions eslint/babel-eslint-parser/src/utils/eslint-version.cjs
@@ -0,0 +1 @@
module.exports = parseInt(require("eslint/package.json").version, 10);
6 changes: 0 additions & 6 deletions eslint/babel-eslint-parser/src/utils/get-eslint-version.cjs

This file was deleted.

4 changes: 2 additions & 2 deletions 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
Expand All @@ -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]);
Expand Down

0 comments on commit 53e4b51

Please sign in to comment.