Skip to content

Commit

Permalink
fix: throw helpful exception when rule has wrong return type (#16075)
Browse files Browse the repository at this point in the history
* fix: throw helpful exception when rule has wrong return type

* do not throw when rule returns array

* Update lib/linter/linter.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/linter/linter.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* update tests

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
bmish and mdjermanovic committed Jul 2, 2022
1 parent e884933 commit fc81848
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit fc81848

Please sign in to comment.