Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Use @babel/core#parse #594

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock = false
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ Check out the [ESLint docs](http://eslint.org/docs/rules/) for all possible rule

`sourceType` can be set to `'module'`(default) or `'script'` if your code isn't using ECMAScript modules.
`allowImportExportEverywhere` can be set to true to allow import and export declarations to appear anywhere a statement is allowed if your build environment supports that. By default, import and export declarations can only appear at a program's top level.
`codeFrame` can be set to false to disable the code frame in the reporter. This is useful since some eslint formatters don't play well with it.

**.eslintrc**

```json
Expand All @@ -91,7 +89,6 @@ Check out the [ESLint docs](http://eslint.org/docs/rules/) for all possible rule
"parserOptions": {
"sourceType": "module",
"allowImportExportEverywhere": false,
"codeFrame": false
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion lib/analyze-scope.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const t = require("@babel/types");
const t = require("@babel/core").types;
const escope = require("eslint-scope");
const Definition = require("eslint-scope/lib/definition").Definition;
const OriginalPatternVisitor = require("eslint-scope/lib/pattern-visitor");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var t = require("@babel/types");
var t = require("@babel/core").types;
var convertComments = require("./convertComments");

module.exports = function(ast, traverse, code) {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.parse = function(code, options) {

exports.parseForESLint = function(code, options) {
options = options || {};
options.ecmaVersion = options.ecmaVersion || 2018;
options.ecmaVersion = options.ecmaVersion || 2019;
options.sourceType = options.sourceType || "module";
options.allowImportExportEverywhere =
options.allowImportExportEverywhere || false;
Expand Down
87 changes: 21 additions & 66 deletions lib/parse.js
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;
};
2 changes: 1 addition & 1 deletion lib/patch-eslint-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var Module = require("module");
var path = require("path");
var t = require("@babel/types");
var t = require("@babel/core").types;

function getModules() {
try {
Expand Down
2 changes: 1 addition & 1 deletion lib/visitor-keys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const BABEL_VISITOR_KEYS = require("@babel/types").VISITOR_KEYS;
const BABEL_VISITOR_KEYS = require("@babel/core").types.VISITOR_KEYS;
const ESLINT_VISITOR_KEYS = require("eslint-visitor-keys").KEYS;

module.exports = Object.assign(
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand All @@ -36,8 +28,17 @@
"url": "https://github.com/babel/babel-eslint/issues"
},
"homepage": "https://github.com/babel/babel-eslint",
"peerDependencies": {
Copy link
Member

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?

Copy link
Member

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?

Copy link
Member Author

@kaicataldo kaicataldo Feb 21, 2018

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!

Copy link
Member

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.

"@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",
Expand Down
22 changes: 11 additions & 11 deletions test/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ function parseAndAssertSame(code) {
ecmaVersion: 2018,
sourceType: "module",
});
var babylonAST = babelEslint.parseForESLint(code, {
var babelAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
try {
assertImplementsAST(esAST, babylonAST);
assertImplementsAST(esAST, babelAST);
} catch (err) {
var traversal = err.message.slice(3, err.message.indexOf(":"));
err.message += unpad(`
Expand All @@ -55,17 +55,17 @@ function parseAndAssertSame(code) {
colors: true,
})}
babel-eslint:
${util.inspect(lookup(babylonAST, traversal, 2), {
${util.inspect(lookup(babelAST, traversal, 2), {
depth: err.depth,
colors: true,
})}
`);
throw err;
}
// assert.equal(esAST, babylonAST);
// assert.equal(esAST, babAST);
}

describe("babylon-to-espree", () => {
describe("babel-to-estree", () => {
describe("compatibility", () => {
it("should allow ast.analyze to be called without options", function() {
var esAST = babelEslint.parseForESLint("`test`", {
Expand Down Expand Up @@ -259,31 +259,31 @@ describe("babylon-to-espree", () => {
// Espree doesn't support the optional chaining operator yet
it("optional chaining operator (token)", () => {
const code = "foo?.bar";
var babylonAST = babelEslint.parseForESLint(code, {
var babelAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
assert.strictEqual(babelAST.tokens[1].type, "Punctuator");
});

// Espree doesn't support the nullish coalescing operator yet
it("nullish coalescing operator (token)", () => {
const code = "foo ?? bar";
var babylonAST = babelEslint.parseForESLint(code, {
var babelAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
assert.strictEqual(babelAST.tokens[1].type, "Punctuator");
});

// Espree doesn't support the pipeline operator yet
it("pipeline operator (token)", () => {
const code = "foo |> bar";
var babylonAST = babelEslint.parseForESLint(code, {
var babelAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
assert.strictEqual(babelAST.tokens[1].type, "Punctuator");
});

it.skip("empty program with line comment", () => {
Expand Down
Loading