@babel/eslint-parser: refactor configuration logic (#10860)

This commit is contained in:
Kai Cataldo 2019-12-11 18:06:37 -05:00 committed by GitHub
parent 7b54a94389
commit 25f7e6808e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 61 deletions

View 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;
}

View File

@ -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));
}

View File

@ -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;

View File

@ -1614,7 +1614,7 @@ describe("verify", () => {
"var leakedGlobal = 1;",
{ "no-implicit-globals": 1 },
[],
null,
undefined,
{
env: {},
parserOptions: { ecmaVersion: 6 },