From fc81848238ee0f6ff93615875ea4b8e95dc09249 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Fri, 1 Jul 2022 20:34:40 -0400 Subject: [PATCH] fix: throw helpful exception when rule has wrong return type (#16075) * 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 * Update lib/linter/linter.js Co-authored-by: Milos Djermanovic * update tests Co-authored-by: Milos Djermanovic --- lib/linter/linter.js | 4 ++++ tests/lib/linter/linter.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index bd1bbb7ca82..95a83366a37 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -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 diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index 05cb6d0f1d9..8239abd2086 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -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", () => {