Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint/babel-eslint-parser/README.md
Expand Up @@ -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.
- `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"`.
- `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**

Expand Down
21 changes: 5 additions & 16 deletions eslint/babel-eslint-parser/src/configuration.js
Expand Up @@ -6,6 +6,7 @@ export function normalizeESLintConfig(options) {
ecmaVersion: 2020,
sourceType: "module",
allowImportExportEverywhere: false,
requireConfigFile: true,
};

return Object.assign(defaultOptions, options);
Expand All @@ -15,31 +16,19 @@ 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,
...options.babelOptions,
parserOpts: {
allowImportExportEverywhere: options.allowImportExportEverywhere,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
...options.babelOptions.parserOpts,
plugins: ["estree", ...(options.babelOptions.parserOpts?.plugins ?? [])],
ranges: true,
tokens: true,
plugins: ["estree"],
},
caller: {
name: "@babel/eslint-parser",
...options.babelOptions.caller,
},
};

Expand Down
50 changes: 41 additions & 9 deletions eslint/babel-eslint-parser/src/index.js
@@ -1,8 +1,16 @@
import semver from "semver";
import { version as CURRENT_BABEL_VERSION } from "@babel/core";
import parseWithScope from "./parse-with-scope";
import { normalizeESLintConfig } from "./configuration";
import {
version as CURRENT_BABEL_VERSION,
parseSync as babelParse,
} from "@babel/core";
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 =
packageJson.peerDependencies["@babel/core"];
Expand All @@ -11,16 +19,40 @@ const IS_RUNNING_SUPPORTED_VERSION = semver.satisfies(
SUPPORTED_BABEL_VERSION_RANGE,
);

export function parse(code, options) {
return parseForESLint(code, options).ast;
}

export function parseForESLint(code, options = {}) {
function baseParse(code, options) {
if (!IS_RUNNING_SUPPORTED_VERSION) {
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}`,
);
}

return parseWithScope(code, normalizeESLintConfig(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;
}

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 };
}
10 changes: 0 additions & 10 deletions eslint/babel-eslint-parser/src/parse-with-scope.js

This file was deleted.

23 changes: 0 additions & 23 deletions eslint/babel-eslint-parser/src/parse.js

This file was deleted.