Skip to content

Commit

Permalink
Chore: improve crash reporting (fixes #11304) (#11463)
Browse files Browse the repository at this point in the history
Add line number to the output in the event of a rule crash
  • Loading branch information
Alex Zherdev authored and btmills committed Mar 15, 2019
1 parent 0f56dc6 commit 448e8da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/linter.js
Expand Up @@ -747,10 +747,15 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser
nodeQueue.forEach(traversalInfo => {
currentNode = traversalInfo.node;

if (traversalInfo.isEntering) {
eventGenerator.enterNode(currentNode);
} else {
eventGenerator.leaveNode(currentNode);
try {
if (traversalInfo.isEntering) {
eventGenerator.enterNode(currentNode);
} else {
eventGenerator.leaveNode(currentNode);
}
} catch (err) {
err.currentNode = currentNode;
throw err;
}
});

Expand Down Expand Up @@ -901,8 +906,15 @@ module.exports = class Linter {
options.filename
);
} catch (err) {
err.message += `\nOccurred while linting ${options.filename}`;
debug("An error occurred while traversing");
debug("Filename:", options.filename);
if (err.currentNode) {
const { line } = err.currentNode.loc.start;

debug("Line:", line);
err.message += `:${line}`;
}
debug("Parser Options:", parserOptions);
debug("Parser Path:", parserName);
debug("Settings:", settings);
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/linter.js
Expand Up @@ -99,7 +99,7 @@ describe("Linter", () => {

assert.throws(() => {
linter.verify(code, config, filename);
}, "Intentional error.");
}, `Intentional error.\nOccurred while linting ${filename}:1`);
});

it("does not call rule listeners with a `this` value", () => {
Expand Down Expand Up @@ -4381,7 +4381,7 @@ describe("Linter", () => {

assert.throws(() => {
linter.verify("0", { rules: { "test-rule": "error" } });
}, /Fixable rules should export a `meta\.fixable` property.$/u);
}, /Fixable rules should export a `meta\.fixable` property.\nOccurred while linting <input>:1$/u);
});

it("should not throw an error if fix is passed and there is no metadata", () => {
Expand Down

0 comments on commit 448e8da

Please sign in to comment.