Skip to content

Commit

Permalink
Add option to disable code frame. (babel#446)
Browse files Browse the repository at this point in the history
* Add option to disable code hightlight.

* Rename codeHighlight with codeFrame

* Add codeFrame tests

* Remove colors from test assertions
  • Loading branch information
Couto authored and hzoo committed Mar 20, 2017
1 parent ce66e73 commit 515adef
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ exports.parse = function (code, options) {

exports.parseNoPatch = function (code, options) {
var opts = {
codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true,
sourceType: options.sourceType,
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
allowReturnOutsideFunction: true,
Expand Down Expand Up @@ -394,14 +395,20 @@ exports.parseNoPatch = function (code, options) {
ast = parse(code, opts);
} catch (err) {
if (err instanceof SyntaxError) {

err.lineNumber = err.loc.line;
err.column = err.loc.column + 1;
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" +
codeFrame(code, err.lineNumber, err.column, { highlightCode: true });
// 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" +
codeFrame(code, err.lineNumber, err.column, { highlightCode: true });
}
}

throw err;
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/rules/syntax-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ClassName {
constructor() {

},
aMethod() {}
}
48 changes: 48 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,52 @@ function strictSuite () {
// it
});
// describe
describe("When \"codeFrame\"", () => {
// Strip chalk colors, these are not relevant for the test
const stripAnsi = (str) => str.replace(
/[\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();
});
});
});
}

0 comments on commit 515adef

Please sign in to comment.