Skip to content

Commit

Permalink
@babel/eslint-parser: refactor configuration logic (#10860)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Dec 11, 2019
1 parent 7b54a94 commit 25f7e68
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 61 deletions.
61 changes: 61 additions & 0 deletions eslint/babel-eslint-parser/src/configuration.js
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 2 additions & 7 deletions eslint/babel-eslint-parser/src/index.js
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -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));
}
58 changes: 5 additions & 53 deletions eslint/babel-eslint-parser/src/parse.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion eslint/babel-eslint-parser/test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ describe("verify", () => {
"var leakedGlobal = 1;",
{ "no-implicit-globals": 1 },
[],
null,
undefined,
{
env: {},
parserOptions: { ecmaVersion: 6 },
Expand Down

0 comments on commit 25f7e68

Please sign in to comment.