Skip to content

Commit

Permalink
Update: include ruleId in error logs (fixes eslint#15037)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Sep 12, 2021
1 parent 143a598 commit de4f783
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
29 changes: 26 additions & 3 deletions lib/linter/linter.js
Expand Up @@ -955,13 +955,31 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser

const ruleListeners = createRuleListeners(rule, ruleContext);

/**
* Include `ruleId` in error logs
* @param {Function} ruleListener The factory of the rule.
* @returns {Function} ruleListener wrapped in error handler
*/
function addRuleErrorHandler(ruleListener) {
return function ruleErrorHandler(...listenerArgs) {
try {
return ruleListener(...listenerArgs);
} catch (e) {
e.ruleId = ruleId;
throw e;
}
};
}

// add all the selectors from the rule as listeners
Object.keys(ruleListeners).forEach(selector => {
const ruleListener = timing.enabled
? timing.time(ruleId, ruleListeners[selector])
: ruleListeners[selector];

emitter.on(
selector,
timing.enabled
? timing.time(ruleId, ruleListeners[selector])
: ruleListeners[selector]
addRuleErrorHandler(ruleListener)
);
});
});
Expand Down Expand Up @@ -1223,6 +1241,11 @@ class Linter {
debug("Parser Options:", parserOptions);
debug("Parser Path:", parserName);
debug("Settings:", settings);

if (err.ruleId) {
err.message += `\nRule: "${err.ruleId}"`;
}

throw err;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/linter/linter.js
Expand Up @@ -87,7 +87,7 @@ describe("Linter", () => {

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

it("does not call rule listeners with a `this` value", () => {
Expand Down Expand Up @@ -5300,7 +5300,7 @@ var a = "test2";

assert.throws(() => {
linter.verify("0", { rules: { "test-rule": "error" } });
}, /Fixable rules must set the `meta\.fixable` property to "code" or "whitespace".\nOccurred while linting <input>:1$/u);
}, /Fixable rules must set the `meta\.fixable` property to "code" or "whitespace".\nOccurred while linting <input>:1\nRule: "test-rule"$/u);
});

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

0 comments on commit de4f783

Please sign in to comment.