This repository was archived by the owner on Aug 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 209
Use @babel/core#parse #594
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,42 @@ | ||
"use strict"; | ||
|
||
var babylonToEspree = require("./babylon-to-espree"); | ||
var parse = require("babylon").parse; | ||
var tt = require("babylon").tokTypes; | ||
var babel = require("@babel/core"); | ||
var tt = require("@babel/parser").tokTypes; | ||
var traverse = require("@babel/traverse").default; | ||
var codeFrameColumns = require("@babel/code-frame").codeFrameColumns; | ||
var babelToEstree = require("./babel-to-estree"); | ||
|
||
var ESLINT_DEFAULT_FILEPATH = "<text>"; | ||
|
||
module.exports = function(code, options) { | ||
var 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", | ||
"jsx", | ||
"estree", | ||
"asyncFunctions", | ||
"asyncGenerators", | ||
"classConstructorCall", | ||
"classProperties", | ||
"decorators", | ||
"doExpressions", | ||
"exponentiationOperator", | ||
"exportDefaultFrom", | ||
"exportNamespaceFrom", | ||
"functionBind", | ||
"functionSent", | ||
"objectRestSpread", | ||
"trailingFunctionCommas", | ||
"dynamicImport", | ||
"numericSeparator", | ||
"optionalChaining", | ||
"importMeta", | ||
"classPrivateProperties", | ||
"bigInt", | ||
"optionalCatchBinding", | ||
"throwExpressions", | ||
"pipelineOperator", | ||
"nullishCoalescingOperator", | ||
], | ||
filename: | ||
options.filePath && options.filePath !== ESLINT_DEFAULT_FILEPATH | ||
? options.filePath | ||
: undefined, | ||
parserOpts: { | ||
sourceType: options.sourceType, | ||
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with Espree | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
ranges: true, | ||
tokens: true, | ||
}, | ||
}; | ||
|
||
var ast; | ||
|
||
try { | ||
ast = parse(code, opts); | ||
ast = babel.parse(code, opts); | ||
} catch (err) { | ||
if (err instanceof SyntaxError) { | ||
if (err instanceof SyntaxError && err.loc) { | ||
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; | ||
} | ||
|
||
babylonToEspree(ast, traverse, tt, code); | ||
babelToEstree(ast, traverse, tt, code); | ||
|
||
return ast; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,19 +10,11 @@ | |
"type": "git", | ||
"url": "https://github.com/babel/babel-eslint.git" | ||
}, | ||
"dependencies": { | ||
"@babel/code-frame": "7.0.0-beta.44", | ||
"@babel/traverse": "7.0.0-beta.44", | ||
"@babel/types": "7.0.0-beta.44", | ||
"babylon": "7.0.0-beta.44", | ||
"eslint-scope": "~3.7.1", | ||
"eslint-visitor-keys": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run lint && npm run test-only", | ||
"test-only": "mocha && mocha --require test/fixtures/preprocess-to-patch.js && mocha --require test/fixtures/use-eslint-old.js", | ||
"lint": "eslint index.js babylon-to-espree test", | ||
"fix": "eslint index.js babylon-to-espree test --fix", | ||
"lint": "eslint index.js babel-to-estree test", | ||
"fix": "npm run lint -- --fix", | ||
"precommit": "lint-staged", | ||
"preversion": "npm test", | ||
"changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:' * %s (%an)' | grep -v 'Merge pull request'" | ||
|
@@ -36,8 +28,17 @@ | |
"url": "https://github.com/babel/babel-eslint/issues" | ||
}, | ||
"homepage": "https://github.com/babel/babel-eslint", | ||
"peerDependencies": { | ||
"@babel/core": ">=7.0.0-beta.51", | ||
"eslint": ">=4.14.0" | ||
}, | ||
"dependencies": { | ||
"@babel/core": ">=7.0.0-beta.51", | ||
"eslint-scope": "~3.7.1", | ||
"eslint-visitor-keys": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-eslint": "^8.0.0", | ||
"babel-eslint": "^8.2.3", | ||
"dedent": "^0.7.0", | ||
"eslint": "npm:[email protected]", | ||
"eslint-config-babel": "^7.0.1", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 a lot of peer deps; eslint and babel-core makes sense, but aren’t the other two addressed by babel-core?
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.
Yeah code-frame and babylon are dependencies of babel/core that's true. So if you are on npm 3 it should be fine?
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.
Since we rely on those packages directly and they could potentially be removed from @babel/core's dependencies I thought it would be safer this way. Can definitely remove them if we think that's being overprotective!
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.
The goal is to set up the deps such that when the user runs
npm install && npm ls
, everything exits zero.Since we rely on them directly, I'd rather them be deps. However we could make them both deps and peerDeps, and then anyone on npm3+ should get them autoinstalled and satisfy the peer dependency, and any conflicts due to changes in babel-core would cause
npm ls
to fail.