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

Commit 515adef

Browse files
coutohzoo
authored andcommitted
Add option to disable code frame. (#446)
* Add option to disable code hightlight. * Rename codeHighlight with codeFrame * Add codeFrame tests * Remove colors from test assertions
1 parent ce66e73 commit 515adef

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ exports.parse = function (code, options) {
366366

367367
exports.parseNoPatch = function (code, options) {
368368
var opts = {
369+
codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true,
369370
sourceType: options.sourceType,
370371
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
371372
allowReturnOutsideFunction: true,
@@ -394,14 +395,20 @@ exports.parseNoPatch = function (code, options) {
394395
ast = parse(code, opts);
395396
} catch (err) {
396397
if (err instanceof SyntaxError) {
398+
397399
err.lineNumber = err.loc.line;
398-
err.column = err.loc.column + 1;
400+
err.column = err.loc.column;
401+
402+
if (opts.codeFrame) {
403+
err.lineNumber = err.loc.line;
404+
err.column = err.loc.column + 1;
399405

400-
// remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start
401-
err.message = "Line " + err.lineNumber + ": " + err.message.replace(/ \((\d+):(\d+)\)$/, "") +
402-
// add codeframe
403-
"\n\n" +
404-
codeFrame(code, err.lineNumber, err.column, { highlightCode: true });
406+
// remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start
407+
err.message = "Line " + err.lineNumber + ": " + err.message.replace(/ \((\d+):(\d+)\)$/, "") +
408+
// add codeframe
409+
"\n\n" +
410+
codeFrame(code, err.lineNumber, err.column, { highlightCode: true });
411+
}
405412
}
406413

407414
throw err;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ClassName {
2+
constructor() {
3+
4+
},
5+
aMethod() {}
6+
}

test/integration.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,52 @@ function strictSuite () {
200200
// it
201201
});
202202
// describe
203+
describe("When \"codeFrame\"", () => {
204+
// Strip chalk colors, these are not relevant for the test
205+
const stripAnsi = (str) => str.replace(
206+
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
207+
""
208+
);
209+
210+
it("should display codeFrame when option is absent", (done) => {
211+
lint({
212+
fixture: ["syntax-error"],
213+
eslint: baseEslintOpts
214+
}, (err, report) => {
215+
if (err) return done(err);
216+
assert(stripAnsi(report[0].message).indexOf("^\n 5 |") > -1);
217+
done();
218+
});
219+
});
220+
221+
it("should display codeFrame when option is true", (done) => {
222+
lint({
223+
fixture: ["syntax-error"],
224+
eslint: Object.assign({}, baseEslintOpts, {
225+
parserOptions: {
226+
codeFrame: true
227+
}
228+
})
229+
}, (err, report) => {
230+
if (err) return done(err);
231+
assert(stripAnsi(report[0].message).indexOf("^\n 5 |") > -1);
232+
done();
233+
});
234+
});
235+
236+
it("should not display codeFrame when option is false", (done) => {
237+
lint({
238+
fixture: ["syntax-error"],
239+
eslint: Object.assign({}, baseEslintOpts, {
240+
parserOptions: {
241+
codeFrame: false
242+
}
243+
})
244+
}, (err, report) => {
245+
if (err) return done(err);
246+
assert(stripAnsi(report[0].message).indexOf("^\n 5 |") === -1);
247+
done();
248+
});
249+
});
250+
});
203251
}

0 commit comments

Comments
 (0)