Add option to disable code frame. (babel/babel-eslint#446)

* Add option to disable code hightlight.

* Rename codeHighlight with codeFrame

* Add codeFrame tests

* Remove colors from test assertions
This commit is contained in:
Luís Couto 2017-03-20 21:45:11 +00:00
parent dccd5a7593
commit 2bee348c9a
3 changed files with 68 additions and 7 deletions

View File

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

View File

@ -0,0 +1,6 @@
class ClassName {
constructor() {
},
aMethod() {}
}

View File

@ -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();
});
});
});
}