Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: include ruleId in error logs of crash (fixes #15037) #15053

Merged
merged 1 commit into from Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 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)
);
});
});
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