feature: babel-eslint-parser passes through config options (#11639)

* Refactor

* feature: babel-eslint-plugin passes through config options

* Address feedback

* Remove unnecessary temporary variable
This commit is contained in:
Kai Cataldo 2020-05-29 15:00:12 -04:00 committed by GitHub
parent 6b7a6dccd2
commit 69198beee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 59 deletions

View File

@ -56,7 +56,7 @@ Additional configuration options can be set in your ESLint configuration under t
- `sourceType` can be set to `"module"`(default) or `"script"` if your code isn't using ECMAScript modules. - `sourceType` can be set to `"module"`(default) or `"script"` if your code isn't using ECMAScript modules.
- `allowImportExportEverywhere` (default `false`) can be set to `true` to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. Otherwise import and export declarations can only appear at a program's top level. - `allowImportExportEverywhere` (default `false`) can be set to `true` to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. Otherwise import and export declarations can only appear at a program's top level.
- `ecmaFeatures.globalReturn` (default `false`) allow return statements in the global scope when used with `sourceType: "script"`. - `ecmaFeatures.globalReturn` (default `false`) allow return statements in the global scope when used with `sourceType: "script"`.
- `babelOptions` passes through Babel's configuration [loading](https://babeljs.io/docs/en/options#config-loading-options) and [merging](https://babeljs.io/docs/en/options#config-merging-options) options (for instance, in case of a monorepo). When not defined, @babel/eslint-parser will use Babel's default configuration file resolution logic. - `babelOptions` is an object containing Babel configuration [options](https://babeljs.io/docs/en/options) that are passed to Babel's parser at runtime. For cases where users might not want to use a Babel configuration file or are running Babel through another tool (such as Webpack with `babel-loader`).
**.eslintrc.js** **.eslintrc.js**

View File

@ -6,6 +6,7 @@ export function normalizeESLintConfig(options) {
ecmaVersion: 2020, ecmaVersion: 2020,
sourceType: "module", sourceType: "module",
allowImportExportEverywhere: false, allowImportExportEverywhere: false,
requireConfigFile: true,
}; };
return Object.assign(defaultOptions, options); return Object.assign(defaultOptions, options);
@ -15,31 +16,19 @@ export function normalizeBabelParseConfig(options) {
const parseOptions = { const parseOptions = {
sourceType: options.sourceType, sourceType: options.sourceType,
filename: options.filePath, filename: options.filePath,
cwd: options.babelOptions.cwd, ...options.babelOptions,
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: { parserOpts: {
allowImportExportEverywhere: options.allowImportExportEverywhere, allowImportExportEverywhere: options.allowImportExportEverywhere,
allowReturnOutsideFunction: true, allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true, allowSuperOutsideMethod: true,
...options.babelOptions.parserOpts,
plugins: ["estree", ...(options.babelOptions.parserOpts?.plugins ?? [])],
ranges: true, ranges: true,
tokens: true, tokens: true,
plugins: ["estree"],
}, },
caller: { caller: {
name: "@babel/eslint-parser", name: "@babel/eslint-parser",
...options.babelOptions.caller,
}, },
}; };

View File

@ -1,8 +1,16 @@
import semver from "semver"; import semver from "semver";
import { version as CURRENT_BABEL_VERSION } from "@babel/core"; import {
import parseWithScope from "./parse-with-scope"; version as CURRENT_BABEL_VERSION,
import { normalizeESLintConfig } from "./configuration"; parseSync as babelParse,
} from "@babel/core";
import packageJson from "../package.json"; import packageJson from "../package.json";
import {
normalizeBabelParseConfig,
normalizeESLintConfig,
} from "./configuration";
import convert from "./convert";
import analyzeScope from "./analyze-scope";
import visitorKeys from "./visitor-keys";
const SUPPORTED_BABEL_VERSION_RANGE = const SUPPORTED_BABEL_VERSION_RANGE =
packageJson.peerDependencies["@babel/core"]; packageJson.peerDependencies["@babel/core"];
@ -11,16 +19,39 @@ const IS_RUNNING_SUPPORTED_VERSION = semver.satisfies(
SUPPORTED_BABEL_VERSION_RANGE, SUPPORTED_BABEL_VERSION_RANGE,
); );
export function parse(code, options) { function baseParse(code, options) {
return parseForESLint(code, options).ast;
}
export function parseForESLint(code, options = {}) {
if (!IS_RUNNING_SUPPORTED_VERSION) { if (!IS_RUNNING_SUPPORTED_VERSION) {
throw new Error( throw new Error(
`babel-eslint@${packageJson.version} does not support @babel/core@${CURRENT_BABEL_VERSION}. Please downgrade to babel-eslint@^10 or upgrade to @babel/core@${SUPPORTED_BABEL_VERSION_RANGE}`, `babel-eslint@${packageJson.version} does not support @babel/core@${CURRENT_BABEL_VERSION}. Please downgrade to babel-eslint@^10 or upgrade to @babel/core@${SUPPORTED_BABEL_VERSION_RANGE}`,
); );
} }
return parseWithScope(code, normalizeESLintConfig(options)); let ast;
try {
ast = babelParse(code, normalizeBabelParseConfig(options));
} catch (err) {
if (err instanceof SyntaxError) {
err.lineNumber = err.loc.line;
err.column = err.loc.column;
}
throw err;
}
convert(ast, code);
return ast;
}
export function parse(code, options = {}) {
return baseParse(code, normalizeESLintConfig(options));
}
export function parseForESLint(code, options = {}) {
const normalizedOptions = normalizeESLintConfig(options);
const ast = baseParse(code, normalizedOptions);
const scopeManager = analyzeScope(ast, normalizedOptions);
return { ast, scopeManager, visitorKeys };
} }

View File

@ -1,10 +0,0 @@
import visitorKeys from "./visitor-keys";
import analyzeScope from "./analyze-scope";
import parse from "./parse";
export default function parseWithScope(code, options) {
const ast = parse(code, options);
const scopeManager = analyzeScope(ast, options);
return { ast, scopeManager, visitorKeys };
}

View File

@ -1,23 +0,0 @@
import { parseSync as babelParse } from "@babel/core";
import convert from "./convert";
import { normalizeBabelParseConfig } from "./configuration";
export default function parse(code, options) {
const parseOptions = normalizeBabelParseConfig(options);
let ast;
try {
ast = babelParse(code, parseOptions);
} catch (err) {
if (err instanceof SyntaxError) {
err.lineNumber = err.loc.line;
err.column = err.loc.column;
}
throw err;
}
convert(ast, code);
return ast;
}