diff --git a/lib/linter/linter.js b/lib/linter/linter.js
index 4b28e196a76f..da6b1f6408bc 100644
--- a/lib/linter/linter.js
+++ b/lib/linter/linter.js
@@ -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)
);
});
});
@@ -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;
}
diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js
index d277ce1ea92b..8eedeabd6c21 100644
--- a/tests/lib/linter/linter.js
+++ b/tests/lib/linter/linter.js
@@ -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", () => {
@@ -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 :1$/u);
+ }, /Fixable rules must set the `meta\.fixable` property to "code" or "whitespace".\nOccurred while linting :1\nRule: "test-rule"$/u);
});
it("should throw an error if fix is passed and there is no metadata", () => {