From 8d4e578d6b5b55ed4ff1845f89c16cca5394a0b8 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 30 Oct 2022 07:08:53 +0530 Subject: [PATCH 1/4] feat: suggest to add comment in empty BlockStatements --- docs/src/_data/rules.json | 2 +- docs/src/_data/rules_meta.json | 1 + lib/rules/no-empty.js | 21 +++++++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/src/_data/rules.json b/docs/src/_data/rules.json index c0f724ffaf7..48c2a9866ba 100644 --- a/docs/src/_data/rules.json +++ b/docs/src/_data/rules.json @@ -731,7 +731,7 @@ "description": "Disallow empty block statements", "recommended": true, "fixable": false, - "hasSuggestions": false + "hasSuggestions": true }, { "name": "no-empty-function", diff --git a/docs/src/_data/rules_meta.json b/docs/src/_data/rules_meta.json index 72044eb74ab..8f1f839e86f 100644 --- a/docs/src/_data/rules_meta.json +++ b/docs/src/_data/rules_meta.json @@ -938,6 +938,7 @@ }, "no-empty": { "type": "suggestion", + "hasSuggestions": true, "docs": { "description": "Disallow empty block statements", "recommended": true, diff --git a/lib/rules/no-empty.js b/lib/rules/no-empty.js index 459140a2e70..242e50efea4 100644 --- a/lib/rules/no-empty.js +++ b/lib/rules/no-empty.js @@ -17,6 +17,7 @@ const astUtils = require("./utils/ast-utils"); /** @type {import('../shared/types').Rule} */ module.exports = { meta: { + hasSuggestions: true, type: "suggestion", docs: { @@ -39,7 +40,8 @@ module.exports = { ], messages: { - unexpected: "Empty {{type}} statement." + unexpected: "Empty {{type}} statement.", + suggestComment: "Add comment inside empty {{type}} statement." } }, @@ -71,7 +73,22 @@ module.exports = { return; } - context.report({ node, messageId: "unexpected", data: { type: "block" } }); + context.report({ + node, + messageId: "unexpected", + data: { type: "block" }, + suggest: [ + { + messageId: "suggestComment", + data: { type: "block" }, + fix(fixer) { + const range = [node.range[0] + 1, node.range[1] - 1]; + + return fixer.replaceTextRange(range, " /* empty */ "); + } + } + ] + }); }, SwitchStatement(node) { From d3451b233d8b577a048324d357216eb9ee6aa20f Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 30 Oct 2022 07:23:52 +0530 Subject: [PATCH 2/4] test: update inavlid test cases with suggestions --- tests/lib/rules/no-empty.js | 163 +++++++++++++++++++++++++++++++++--- 1 file changed, 150 insertions(+), 13 deletions(-) diff --git a/tests/lib/rules/no-empty.js b/tests/lib/rules/no-empty.js index 98651a41256..001001b640c 100644 --- a/tests/lib/rules/no-empty.js +++ b/tests/lib/rules/no-empty.js @@ -44,36 +44,173 @@ ruleTester.run("no-empty", rule, { { code: "try { foo(); } catch (ex) {} finally { bar(); }", options: [{ allowEmptyCatch: true }] } ], invalid: [ - { code: "try {} catch (ex) {throw ex}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] }, - { code: "try { foo() } catch (ex) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] }, - { code: "try { foo() } catch (ex) {throw ex} finally {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] }, - { code: "if (foo) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] }, - { code: "while (foo) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] }, - { code: "for (;foo;) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] }, - { code: "switch(foo) {}", errors: [{ messageId: "unexpected", data: { type: "switch" }, type: "SwitchStatement" }] }, + { + code: "try {} catch (ex) {throw ex}", + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "try { /* empty */ } catch (ex) {throw ex}" + }] + }] + }, + { + code: "try { foo() } catch (ex) {}", + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "try { foo() } catch (ex) { /* empty */ }" + }] + }] + }, + { + code: "try { foo() } catch (ex) {throw ex} finally {}", + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "try { foo() } catch (ex) {throw ex} finally { /* empty */ }" + }] + }] + }, + { + code: "if (foo) {}", + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "if (foo) { /* empty */ }" + }] + }] + }, + { + code: "while (foo) {}", + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "while (foo) { /* empty */ }" + }] + }] + }, + { + code: "for (;foo;) {}", + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "for (;foo;) { /* empty */ }" + }] + }] + }, + { + code: "switch(foo) {}", + errors: [{ + messageId: "unexpected", + data: { type: "switch" }, + type: "SwitchStatement" + }] + }, + { + code: "switch (foo) { /* empty */ }", + errors: [{ + messageId: "unexpected", + data: { type: "switch" }, + type: "SwitchStatement" + }] + }, { code: "try {} catch (ex) {}", options: [{ allowEmptyCatch: true }], - errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "try { /* empty */ } catch (ex) {}" + }] + }] }, { code: "try { foo(); } catch (ex) {} finally {}", options: [{ allowEmptyCatch: true }], - errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] + errors: [{ + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [{ + messageId: "suggestComment", + output: "try { foo(); } catch (ex) {} finally { /* empty */ }" + }] + }] }, { code: "try {} catch (ex) {} finally {}", options: [{ allowEmptyCatch: true }], errors: [ - { messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }, - { messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" } + { + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [ + { + messageId: "suggestComment", + output: "try { /* empty */ } catch (ex) {} finally {}" + } + ] + }, + { + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [ + { + messageId: "suggestComment", + output: "try {} catch (ex) {} finally { /* empty */ }" + } + ] + } ] }, { code: "try { foo(); } catch (ex) {} finally {}", errors: [ - { messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }, - { messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" } + { + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [ + { + messageId: "suggestComment", + output: "try { foo(); } catch (ex) { /* empty */ } finally {}" + } + ] + }, + { + messageId: "unexpected", + data: { type: "block" }, + type: "BlockStatement", + suggestions: [ + { + messageId: "suggestComment", + output: "try { foo(); } catch (ex) {} finally { /* empty */ }" + } + ] + } ] } ] From 473eaeda4a9c765d893275e9dc4975090f5163b0 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 30 Oct 2022 16:46:31 +0530 Subject: [PATCH 3/4] test: more strict assertions --- tests/lib/rules/no-empty.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/lib/rules/no-empty.js b/tests/lib/rules/no-empty.js index 001001b640c..f9c5ec05144 100644 --- a/tests/lib/rules/no-empty.js +++ b/tests/lib/rules/no-empty.js @@ -52,6 +52,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "try { /* empty */ } catch (ex) {throw ex}" }] }] @@ -64,6 +65,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "try { foo() } catch (ex) { /* empty */ }" }] }] @@ -76,6 +78,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "try { foo() } catch (ex) {throw ex} finally { /* empty */ }" }] }] @@ -88,6 +91,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "if (foo) { /* empty */ }" }] }] @@ -100,6 +104,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "while (foo) { /* empty */ }" }] }] @@ -112,6 +117,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "for (;foo;) { /* empty */ }" }] }] @@ -129,7 +135,8 @@ ruleTester.run("no-empty", rule, { errors: [{ messageId: "unexpected", data: { type: "switch" }, - type: "SwitchStatement" + type: "SwitchStatement", + suggestions: null }] }, { @@ -141,6 +148,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "try { /* empty */ } catch (ex) {}" }] }] @@ -154,6 +162,7 @@ ruleTester.run("no-empty", rule, { type: "BlockStatement", suggestions: [{ messageId: "suggestComment", + data: { type: "block" }, output: "try { foo(); } catch (ex) {} finally { /* empty */ }" }] }] @@ -169,6 +178,7 @@ ruleTester.run("no-empty", rule, { suggestions: [ { messageId: "suggestComment", + data: { type: "block" }, output: "try { /* empty */ } catch (ex) {} finally {}" } ] @@ -180,6 +190,7 @@ ruleTester.run("no-empty", rule, { suggestions: [ { messageId: "suggestComment", + data: { type: "block" }, output: "try {} catch (ex) {} finally { /* empty */ }" } ] @@ -196,6 +207,7 @@ ruleTester.run("no-empty", rule, { suggestions: [ { messageId: "suggestComment", + data: { type: "block" }, output: "try { foo(); } catch (ex) { /* empty */ } finally {}" } ] @@ -207,6 +219,7 @@ ruleTester.run("no-empty", rule, { suggestions: [ { messageId: "suggestComment", + data: { type: "block" }, output: "try { foo(); } catch (ex) {} finally { /* empty */ }" } ] From a9e760a9bb8aec99cc7719a1b89320f5a268c334 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 30 Oct 2022 17:28:24 +0530 Subject: [PATCH 4/4] test: assert for suggestions Co-authored-by: Milos Djermanovic --- tests/lib/rules/no-empty.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/lib/rules/no-empty.js b/tests/lib/rules/no-empty.js index f9c5ec05144..812aa8de994 100644 --- a/tests/lib/rules/no-empty.js +++ b/tests/lib/rules/no-empty.js @@ -127,7 +127,8 @@ ruleTester.run("no-empty", rule, { errors: [{ messageId: "unexpected", data: { type: "switch" }, - type: "SwitchStatement" + type: "SwitchStatement", + suggestions: null }] }, {