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:
parent
dccd5a7593
commit
2bee348c9a
@ -366,6 +366,7 @@ exports.parse = function (code, options) {
|
|||||||
|
|
||||||
exports.parseNoPatch = function (code, options) {
|
exports.parseNoPatch = function (code, options) {
|
||||||
var opts = {
|
var opts = {
|
||||||
|
codeFrame: options.hasOwnProperty("codeFrame") ? options.codeFrame : true,
|
||||||
sourceType: options.sourceType,
|
sourceType: options.sourceType,
|
||||||
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
|
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
|
||||||
allowReturnOutsideFunction: true,
|
allowReturnOutsideFunction: true,
|
||||||
@ -394,6 +395,11 @@ exports.parseNoPatch = function (code, options) {
|
|||||||
ast = parse(code, opts);
|
ast = parse(code, opts);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof SyntaxError) {
|
if (err instanceof SyntaxError) {
|
||||||
|
|
||||||
|
err.lineNumber = err.loc.line;
|
||||||
|
err.column = err.loc.column;
|
||||||
|
|
||||||
|
if (opts.codeFrame) {
|
||||||
err.lineNumber = err.loc.line;
|
err.lineNumber = err.loc.line;
|
||||||
err.column = err.loc.column + 1;
|
err.column = err.loc.column + 1;
|
||||||
|
|
||||||
@ -403,6 +409,7 @@ exports.parseNoPatch = function (code, options) {
|
|||||||
"\n\n" +
|
"\n\n" +
|
||||||
codeFrame(code, err.lineNumber, err.column, { highlightCode: true });
|
codeFrame(code, err.lineNumber, err.column, { highlightCode: true });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|||||||
6
eslint/babel-eslint-parser/test/fixtures/rules/syntax-error.js
vendored
Normal file
6
eslint/babel-eslint-parser/test/fixtures/rules/syntax-error.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class ClassName {
|
||||||
|
constructor() {
|
||||||
|
|
||||||
|
},
|
||||||
|
aMethod() {}
|
||||||
|
}
|
||||||
@ -200,4 +200,52 @@ function strictSuite () {
|
|||||||
// it
|
// it
|
||||||
});
|
});
|
||||||
// describe
|
// 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user