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

fix: throw helpful exception when rule has wrong return type #16075

Merged
merged 5 commits into from Jul 2, 2022
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
4 changes: 4 additions & 0 deletions lib/linter/linter.js
Expand Up @@ -1119,6 +1119,10 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageO
};
}

if (typeof ruleListeners === "undefined" || ruleListeners === null) {
throw new Error(`The create() function for rule '${ruleId}' did not return an object.`);
}

// add all the selectors from the rule as listeners
Object.keys(ruleListeners).forEach(selector => {
const ruleListener = timing.enabled
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -7000,6 +7000,22 @@ var a = "test2";

assert(ok);
});

it("should throw when rule's create() function does not return an object", () => {
const config = { rules: { checker: "error" } };

linter.defineRule("checker", () => null); // returns null

assert.throws(() => {
linter.verify("abc", config, filename);
}, "The create() function for rule 'checker' did not return an object.");

linter.defineRule("checker", () => {}); // returns undefined

assert.throws(() => {
linter.verify("abc", config, filename);
}, "The create() function for rule 'checker' did not return an object.");
});
});

describe("Custom parser", () => {
Expand Down