From 2a78150cd42d62d9d1cfb17b5d0eddf7dde3b85c Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 10 Sep 2022 16:16:01 +0530 Subject: [PATCH 1/4] fix: avoid incorrect autofix in the `prefer-const` rule --- lib/rules/prefer-const.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 08f4492aaea..09d1efc4885 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -439,12 +439,14 @@ module.exports = { if (varDeclParent && varDeclParent.declarations && varDeclParent.declarations.length >= 1) { - /* - * Add nodesToReport.length to a count, then comparing the count to the length - * of the declarations in the current block. - */ + if (!varDeclParent.declarations.some(declaration => declaration.id.type === "ObjectPattern")) { - reportCount += nodesToReport.length; + /* + * Add nodesToReport.length to a count, then comparing the count to the length + * of the declarations in the current block. + */ + reportCount += nodesToReport.length; + } shouldFix = shouldFix && (reportCount === varDeclParent.declarations.length); } From b5a6df77c712103f503618fd4ecab71bcb16fcdc Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 10 Sep 2022 16:23:55 +0530 Subject: [PATCH 2/4] test: add more test cases --- tests/lib/rules/prefer-const.js | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/lib/rules/prefer-const.js b/tests/lib/rules/prefer-const.js index 0a44b0bd017..5d0bc0bf8bb 100644 --- a/tests/lib/rules/prefer-const.js +++ b/tests/lib/rules/prefer-const.js @@ -684,6 +684,40 @@ ruleTester.run("prefer-const", rule, { errors: [ { messageId: "useConst", data: { name: "a" }, type: "Identifier" } ] + }, + + // https://github.com/eslint/eslint/issues/16266 + { + code: ` + let { itemId, list } = {}, + obj = [], + total = 0; + total = 9; + console.log(itemId, list, obj, total); + `, + output: null, + options: [{ destructuring: "any", ignoreReadBeforeAssign: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "useConst", data: { name: "itemId" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "list" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "obj" }, type: "Identifier" } + ] + }, + { + code: ` + let { itemId, list } = {}, + total = 0; + total = 9; + console.log(itemId, list, total); + `, + output: null, + options: [{ destructuring: "any", ignoreReadBeforeAssign: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "useConst", data: { name: "itemId" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "list" }, type: "Identifier" } + ] } ] }); From ea61c1428d75b19d20a7846485a126dc8bea7da3 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 10 Sep 2022 16:30:44 +0530 Subject: [PATCH 3/4] test: add more cases --- lib/rules/prefer-const.js | 2 +- tests/lib/rules/prefer-const.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 09d1efc4885..8907ec8d3fb 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -439,7 +439,7 @@ module.exports = { if (varDeclParent && varDeclParent.declarations && varDeclParent.declarations.length >= 1) { - if (!varDeclParent.declarations.some(declaration => declaration.id.type === "ObjectPattern")) { + if (varDeclParent.declarations.every(declaration => declaration.id.type === "Identifier")) { /* * Add nodesToReport.length to a count, then comparing the count to the length diff --git a/tests/lib/rules/prefer-const.js b/tests/lib/rules/prefer-const.js index 5d0bc0bf8bb..b1ca6b9fb8b 100644 --- a/tests/lib/rules/prefer-const.js +++ b/tests/lib/rules/prefer-const.js @@ -706,7 +706,7 @@ ruleTester.run("prefer-const", rule, { }, { code: ` - let { itemId, list } = {}, + let [ itemId, list ] = [], total = 0; total = 9; console.log(itemId, list, total); From 0ab08645899270fc58bb16519c209b2e399a52ab Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 10 Sep 2022 17:02:44 +0530 Subject: [PATCH 4/4] fix: improve autofix and add more cases --- lib/rules/prefer-const.js | 26 +++++++++++++++------- tests/lib/rules/prefer-const.js | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 8907ec8d3fb..e3d2db7aeb9 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -439,16 +439,26 @@ module.exports = { if (varDeclParent && varDeclParent.declarations && varDeclParent.declarations.length >= 1) { - if (varDeclParent.declarations.every(declaration => declaration.id.type === "Identifier")) { + /* + * Add nodesToReport.length to a count, then comparing the count to the length + * of the declarations in the current block. + */ - /* - * Add nodesToReport.length to a count, then comparing the count to the length - * of the declarations in the current block. - */ - reportCount += nodesToReport.length; - } + reportCount += nodesToReport.length; + + let totalDeclarationsCount = 0; + + varDeclParent.declarations.forEach(declaration => { + if (declaration.id.type === "ObjectPattern") { + totalDeclarationsCount += declaration.id.properties.length; + } else if (declaration.id.type === "ArrayPattern") { + totalDeclarationsCount += declaration.id.elements.length; + } else { + totalDeclarationsCount += 1; + } + }); - shouldFix = shouldFix && (reportCount === varDeclParent.declarations.length); + shouldFix = shouldFix && (reportCount === totalDeclarationsCount); } } diff --git a/tests/lib/rules/prefer-const.js b/tests/lib/rules/prefer-const.js index b1ca6b9fb8b..c65252cb0bf 100644 --- a/tests/lib/rules/prefer-const.js +++ b/tests/lib/rules/prefer-const.js @@ -704,6 +704,25 @@ ruleTester.run("prefer-const", rule, { { messageId: "useConst", data: { name: "obj" }, type: "Identifier" } ] }, + { + code: ` + let { itemId, list } = {}, + obj = []; + console.log(itemId, list, obj); + `, + output: ` + const { itemId, list } = {}, + obj = []; + console.log(itemId, list, obj); + `, + options: [{ destructuring: "any", ignoreReadBeforeAssign: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "useConst", data: { name: "itemId" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "list" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "obj" }, type: "Identifier" } + ] + }, { code: ` let [ itemId, list ] = [], @@ -718,6 +737,25 @@ ruleTester.run("prefer-const", rule, { { messageId: "useConst", data: { name: "itemId" }, type: "Identifier" }, { messageId: "useConst", data: { name: "list" }, type: "Identifier" } ] + }, + { + code: ` + let [ itemId, list ] = [], + obj = []; + console.log(itemId, list, obj); + `, + output: ` + const [ itemId, list ] = [], + obj = []; + console.log(itemId, list, obj); + `, + options: [{ destructuring: "any", ignoreReadBeforeAssign: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "useConst", data: { name: "itemId" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "list" }, type: "Identifier" }, + { messageId: "useConst", data: { name: "obj" }, type: "Identifier" } + ] } ] });