* @babel/eslint-parser: fix ImportExpression node to match ESTree spec * Update caller name for @babel/core.parseSync * Move logic into estree plugin * Add estree plugin tests * Fix Flow error * Fix flow * Remove extra properties on ImportExpression node * Incorporate review feedback
72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
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;
|
|
}
|
|
}
|
|
|
|
let ast;
|
|
|
|
try {
|
|
ast = parse(code, opts);
|
|
} catch (err) {
|
|
if (err instanceof SyntaxError) {
|
|
err.lineNumber = err.loc.line;
|
|
err.column = err.loc.column;
|
|
}
|
|
|
|
throw err;
|
|
}
|
|
|
|
babylonToEspree(ast, traverse, tt, code);
|
|
|
|
return ast;
|
|
}
|