@babel/eslint-parser: refactor configuration logic (#10860)
This commit is contained in:
parent
7b54a94389
commit
25f7e6808e
61
eslint/babel-eslint-parser/src/configuration.js
Normal file
61
eslint/babel-eslint-parser/src/configuration.js
Normal file
@ -0,0 +1,61 @@
|
||||
import { loadPartialConfig } from "@babel/core";
|
||||
|
||||
export function normalizeESLintConfig(options) {
|
||||
const defaultOptions = {
|
||||
babelOptions: {},
|
||||
ecmaVersion: 2020,
|
||||
sourceType: "module",
|
||||
allowImportExportEverywhere: false,
|
||||
};
|
||||
|
||||
return Object.assign(defaultOptions, options);
|
||||
}
|
||||
|
||||
export function normalizeBabelParseConfig(options) {
|
||||
const parseOptions = {
|
||||
sourceType: options.sourceType,
|
||||
filename: options.filePath,
|
||||
cwd: options.babelOptions.cwd,
|
||||
root: options.babelOptions.root,
|
||||
rootMode: options.babelOptions.rootMode,
|
||||
envName: options.babelOptions.envName,
|
||||
configFile: options.babelOptions.configFile,
|
||||
babelrc: options.babelOptions.babelrc,
|
||||
babelrcRoots: options.babelOptions.babelrcRoots,
|
||||
extends: options.babelOptions.extends,
|
||||
env: options.babelOptions.env,
|
||||
overrides: options.babelOptions.overrides,
|
||||
test: options.babelOptions.test,
|
||||
include: options.babelOptions.include,
|
||||
exclude: options.babelOptions.exclude,
|
||||
ignore: options.babelOptions.ignore,
|
||||
only: options.babelOptions.only,
|
||||
parserOpts: {
|
||||
allowImportExportEverywhere: options.allowImportExportEverywhere,
|
||||
allowReturnOutsideFunction: true,
|
||||
allowSuperOutsideMethod: true,
|
||||
ranges: true,
|
||||
tokens: true,
|
||||
plugins: ["estree"],
|
||||
},
|
||||
caller: {
|
||||
name: "@babel/eslint-parser",
|
||||
},
|
||||
};
|
||||
|
||||
if (options.requireConfigFile !== false) {
|
||||
const config = loadPartialConfig(parseOptions);
|
||||
|
||||
if (config !== null) {
|
||||
if (!config.hasFilesystemConfig()) {
|
||||
throw new Error(
|
||||
`No Babel config file detected for ${config.options.filename}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.`,
|
||||
);
|
||||
}
|
||||
|
||||
return config.options;
|
||||
}
|
||||
}
|
||||
|
||||
return parseOptions;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
import semver from "semver";
|
||||
import { version as CURRENT_BABEL_VERSION } from "@babel/core";
|
||||
import parseWithScope from "./parse-with-scope";
|
||||
import { normalizeESLintConfig } from "./configuration";
|
||||
import packageJson from "../package.json";
|
||||
|
||||
const SUPPORTED_BABEL_VERSION_RANGE =
|
||||
@ -21,11 +22,5 @@ export function parseForESLint(code, options = {}) {
|
||||
);
|
||||
}
|
||||
|
||||
options.babelOptions = options.babelOptions || {};
|
||||
options.ecmaVersion = options.ecmaVersion || 2018;
|
||||
options.sourceType = options.sourceType || "module";
|
||||
options.allowImportExportEverywhere =
|
||||
options.allowImportExportEverywhere || false;
|
||||
|
||||
return parseWithScope(code, options);
|
||||
return parseWithScope(code, normalizeESLintConfig(options));
|
||||
}
|
||||
|
||||
@ -1,61 +1,13 @@
|
||||
import { parseSync as babelParse, tokTypes as tt, traverse } from "@babel/core";
|
||||
import babylonToEspree from "./babylon-to-espree";
|
||||
import {
|
||||
parseSync as parse,
|
||||
tokTypes as tt,
|
||||
traverse,
|
||||
loadPartialConfig,
|
||||
} from "@babel/core";
|
||||
|
||||
export default function(code, options) {
|
||||
let opts = {
|
||||
sourceType: options.sourceType,
|
||||
filename: options.filePath,
|
||||
cwd: options.babelOptions.cwd,
|
||||
root: options.babelOptions.root,
|
||||
rootMode: options.babelOptions.rootMode,
|
||||
envName: options.babelOptions.envName,
|
||||
configFile: options.babelOptions.configFile,
|
||||
babelrc: options.babelOptions.babelrc,
|
||||
babelrcRoots: options.babelOptions.babelrcRoots,
|
||||
extends: options.babelOptions.extends,
|
||||
env: options.babelOptions.env,
|
||||
overrides: options.babelOptions.overrides,
|
||||
test: options.babelOptions.test,
|
||||
include: options.babelOptions.include,
|
||||
exclude: options.babelOptions.exclude,
|
||||
ignore: options.babelOptions.ignore,
|
||||
only: options.babelOptions.only,
|
||||
parserOpts: {
|
||||
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
|
||||
allowReturnOutsideFunction: true,
|
||||
allowSuperOutsideMethod: true,
|
||||
ranges: true,
|
||||
tokens: true,
|
||||
plugins: ["estree"],
|
||||
},
|
||||
caller: {
|
||||
name: "@babel/eslint-parser",
|
||||
},
|
||||
};
|
||||
|
||||
if (options.requireConfigFile !== false) {
|
||||
const config = loadPartialConfig(opts);
|
||||
|
||||
if (config !== null) {
|
||||
if (!config.hasFilesystemConfig()) {
|
||||
throw new Error(
|
||||
`No Babel config file detected for ${config.options.filename}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.`,
|
||||
);
|
||||
}
|
||||
|
||||
opts = config.options;
|
||||
}
|
||||
}
|
||||
import { normalizeBabelParseConfig } from "./configuration";
|
||||
|
||||
export default function parse(code, options) {
|
||||
const parseOptions = normalizeBabelParseConfig(options);
|
||||
let ast;
|
||||
|
||||
try {
|
||||
ast = parse(code, opts);
|
||||
ast = babelParse(code, parseOptions);
|
||||
} catch (err) {
|
||||
if (err instanceof SyntaxError) {
|
||||
err.lineNumber = err.loc.line;
|
||||
|
||||
@ -1614,7 +1614,7 @@ describe("verify", () => {
|
||||
"var leakedGlobal = 1;",
|
||||
{ "no-implicit-globals": 1 },
|
||||
[],
|
||||
null,
|
||||
undefined,
|
||||
{
|
||||
env: {},
|
||||
parserOptions: { ecmaVersion: 6 },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user