-
-
Notifications
You must be signed in to change notification settings - Fork 239
Use @babel/core#parse #711
Changes from 19 commits
99be0bd
60554f1
fac8793
e550a4c
abcec07
e4fa4f8
6100fe1
f97954a
c1bb141
fae5a5d
cd7b549
1c39155
f92c9ad
3ec6148
0477350
59aed40
759ca4c
823500d
b50c2fb
37ff19e
2ab1a61
eca618d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
!.*.js | ||
test/fixtures |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,38 @@ | ||
"use strict"; | ||
|
||
const babylonToEspree = require("./babylon-to-espree"); | ||
const parse = require("@babel/parser").parse; | ||
const tt = require("@babel/parser").tokTypes; | ||
const traverse = require("@babel/traverse").default; | ||
const codeFrameColumns = require("@babel/code-frame").codeFrameColumns; | ||
const { parseSync: parse, tokTypes: tt, traverse } = require("@babel/core"); | ||
|
||
module.exports = function(code, options) { | ||
const legacyDecorators = | ||
options.ecmaFeatures && options.ecmaFeatures.legacyDecorators; | ||
|
||
const opts = { | ||
codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true, | ||
sourceType: options.sourceType, | ||
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
ranges: true, | ||
tokens: true, | ||
plugins: [ | ||
["flow", { all: true }], | ||
"jsx", | ||
"estree", | ||
"asyncFunctions", | ||
"asyncGenerators", | ||
"classConstructorCall", | ||
"classProperties", | ||
legacyDecorators | ||
? "decorators-legacy" | ||
: ["decorators", { decoratorsBeforeExport: false }], | ||
"doExpressions", | ||
"exponentiationOperator", | ||
"exportDefaultFrom", | ||
"exportNamespaceFrom", | ||
"functionBind", | ||
"functionSent", | ||
"objectRestSpread", | ||
"trailingFunctionCommas", | ||
"dynamicImport", | ||
"numericSeparator", | ||
"optionalChaining", | ||
"importMeta", | ||
"classPrivateProperties", | ||
"bigInt", | ||
"optionalCatchBinding", | ||
"throwExpressions", | ||
["pipelineOperator", { proposal: "minimal" }], | ||
"nullishCoalescingOperator", | ||
"logicalAssignment", | ||
], | ||
filename: options.filePath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should probably pass through all the options that can directly influence a config:
It would be great to also set
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated this, but I'm wondering if we do really want the config merging options here - shouldn't we be able to just provide the config loading options that would point to the config file that could then contain the config merging options? This would avoid duplication in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
are important to have since they affect the actual process of loading config files. The rest are not strictly necessary, but it seems like support for them might as well be included. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally would prefer to start with fewer rather than more options (especially since this project is already difficult to maintain), but am happy to defer to what you think is best. 👍 |
||
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.babelrRoots, | ||
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", | ||
}, | ||
}; | ||
|
||
let ast; | ||
|
@@ -58,30 +42,6 @@ module.exports = function(code, options) { | |
if (err instanceof SyntaxError) { | ||
err.lineNumber = err.loc.line; | ||
err.column = err.loc.column; | ||
|
||
if (opts.codeFrame) { | ||
err.lineNumber = err.loc.line; | ||
err.column = err.loc.column + 1; | ||
|
||
// remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start | ||
err.message = | ||
"Line " + | ||
err.lineNumber + | ||
": " + | ||
err.message.replace(/ \((\d+):(\d+)\)$/, "") + | ||
// add codeframe | ||
"\n\n" + | ||
codeFrameColumns( | ||
code, | ||
{ | ||
start: { | ||
line: err.lineNumber, | ||
column: err.column, | ||
}, | ||
}, | ||
{ highlightCode: true } | ||
); | ||
} | ||
} | ||
|
||
throw err; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic. Very important for toolkits that encapsulate eslint config. Thank you for adding this feature 💯