Skip to content
Permalink
Browse files
Update: include ruleId in error logs (fixes #15037) (#15053)
  • Loading branch information
AriPerkkio committed Sep 21, 2021
1 parent 47be800 commit 0a1a850575ca75db017051abe5e931f0f9c8012b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
@@ -955,13 +955,31 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser

const ruleListeners = createRuleListeners(rule, ruleContext);

/**
* Include `ruleId` in error logs
* @param {Function} ruleListener A rule method that listens for a node.
* @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;
}

@@ -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 <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", () => {

0 comments on commit 0a1a850

Please sign in to comment.