From 53250b4c4c16b108e5ecc1269d919c8345f64f70 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Thu, 30 Jun 2022 15:28:59 -0400 Subject: [PATCH 1/5] fix: throw helpful exception when rule has wrong return type --- lib/linter/linter.js | 4 ++++ tests/lib/linter/linter.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index bd1bbb7ca82..56ee8d6d397 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 !== "object" || Array.isArray(ruleListeners) || 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..ec529d6d5a4 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -7000,6 +7000,28 @@ 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."); + + linter.defineRule("checker", () => []); // returns an array + + assert.throws(() => { + linter.verify("abc", config, filename); + }, "The create() function for rule checker did not return an object."); + }); }); describe("Custom parser", () => { From c8c4cdcacdcf6a69c0f98468dab1c92adf182136 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Fri, 1 Jul 2022 11:57:13 -0400 Subject: [PATCH 2/5] do not throw when rule returns array --- lib/linter/linter.js | 2 +- tests/lib/linter/linter.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 56ee8d6d397..06114961bf6 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -1119,7 +1119,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageO }; } - if (typeof ruleListeners !== "object" || Array.isArray(ruleListeners) || ruleListeners === null) { + if (typeof ruleListeners !== "object" || ruleListeners === null) { throw new Error(`The create() function for rule ${ruleId} did not return an object.`); } diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index ec529d6d5a4..23c5ac9cdf4 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -7018,9 +7018,8 @@ var a = "test2"; linter.defineRule("checker", () => []); // returns an array - assert.throws(() => { - linter.verify("abc", config, filename); - }, "The create() function for rule checker did not return an object."); + // array should not throw + linter.verify("abc", config, filename); }); }); From b4cd77c6abb3223e937372a09aed21b74779c4b3 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Fri, 1 Jul 2022 14:41:21 -0400 Subject: [PATCH 3/5] Update lib/linter/linter.js Co-authored-by: Milos Djermanovic --- lib/linter/linter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 06114961bf6..4a432c0d049 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -1119,7 +1119,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageO }; } - if (typeof ruleListeners !== "object" || ruleListeners === null) { + if (typeof ruleListeners === "undefined" || ruleListeners === null) { throw new Error(`The create() function for rule ${ruleId} did not return an object.`); } From 2245d8264d5b8e43e7bd2a919299eb0761b13244 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Fri, 1 Jul 2022 14:41:53 -0400 Subject: [PATCH 4/5] Update lib/linter/linter.js Co-authored-by: Milos Djermanovic --- lib/linter/linter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 4a432c0d049..95a83366a37 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -1120,7 +1120,7 @@ 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.`); + throw new Error(`The create() function for rule '${ruleId}' did not return an object.`); } // add all the selectors from the rule as listeners From 4e509f0f6e79122c8a95b074c1caf1d05b41c987 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Fri, 1 Jul 2022 14:46:15 -0400 Subject: [PATCH 5/5] update tests --- tests/lib/linter/linter.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index 23c5ac9cdf4..8239abd2086 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -7008,18 +7008,13 @@ var a = "test2"; assert.throws(() => { linter.verify("abc", config, filename); - }, "The create() function for rule checker did not return an object."); + }, "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."); - - linter.defineRule("checker", () => []); // returns an array - - // array should not throw - linter.verify("abc", config, filename); + }, "The create() function for rule 'checker' did not return an object."); }); });