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

Commit

Permalink
Use @babel/core#parse
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Nov 7, 2018
1 parent ef1fc0e commit 820c3aa
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 128 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +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` (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.
- `codeFrame` (default `true`) 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**

Expand All @@ -91,7 +90,6 @@ Check out the [ESLint docs](http://eslint.org/docs/rules/) for all possible rule
"parserOptions": {
"sourceType": "module",
"allowImportExportEverywhere": false,
"codeFrame": true
}
}
```
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
2 changes: 1 addition & 1 deletion lib/babylon-to-espree/toAST.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 convertComments = require("./convertComments");

module.exports = function(ast, traverse, code) {
Expand Down
28 changes: 1 addition & 27 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

const babylonToEspree = require("./babylon-to-espree");
const parse = require("@babel/parser").parse;
const traverse = require("@babel/core").traverse;
const tt = require("@babel/parser").tokTypes;
const traverse = require("@babel/traverse").default;
const codeFrameColumns = require("@babel/code-frame").codeFrameColumns;

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,
Expand Down Expand Up @@ -58,30 +56,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;
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
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"url": "https://github.com/babel/babel-eslint.git"
},
"dependencies": {
"@babel/code-frame": "^7.0.0",
"@babel/parser": "^7.0.0",
"@babel/traverse": "^7.0.0",
"@babel/types": "^7.0.0",
"@babel/core": "^7.1.5",
"@babel/parser": "^7.1.5",
"eslint-scope": "3.7.1",
"eslint-visitor-keys": "^1.0.0"
},
Expand Down
60 changes: 0 additions & 60 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,64 +222,4 @@ function strictSuite() {
});
// it
});
// describe
describe('When "codeFrame"', () => {
// Strip chalk colors, these are not relevant for the test
const stripAnsi = str =>
str.replace(
// eslint-disable-next-line no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
""
);

it("should display codeFrame when option is absent", done => {
lint(
{
fixture: ["syntax-error"],
eslint: baseEslintOpts,
},
(err, report) => {
if (err) return done(err);
assert(stripAnsi(report[0].message).indexOf("^\n 5 |") > -1);
done();
}
);
});

it("should display codeFrame when option is true", done => {
lint(
{
fixture: ["syntax-error"],
eslint: Object.assign({}, baseEslintOpts, {
parserOptions: {
codeFrame: true,
},
}),
},
(err, report) => {
if (err) return done(err);
assert(stripAnsi(report[0].message).indexOf("^\n 5 |") > -1);
done();
}
);
});

it("should not display codeFrame when option is false", done => {
lint(
{
fixture: ["syntax-error"],
eslint: Object.assign({}, baseEslintOpts, {
parserOptions: {
codeFrame: false,
},
}),
},
(err, report) => {
if (err) return done(err);
assert(stripAnsi(report[0].message).indexOf("^\n 5 |") === -1);
done();
}
);
});
});
}
Loading

0 comments on commit 820c3aa

Please sign in to comment.