diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 08f4492aaea..e3d2db7aeb9 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -446,7 +446,19 @@ module.exports = { reportCount += nodesToReport.length; - shouldFix = shouldFix && (reportCount === varDeclParent.declarations.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 === totalDeclarationsCount); } } diff --git a/tests/lib/rules/prefer-const.js b/tests/lib/rules/prefer-const.js index 0a44b0bd017..c65252cb0bf 100644 --- a/tests/lib/rules/prefer-const.js +++ b/tests/lib/rules/prefer-const.js @@ -684,6 +684,78 @@ 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 } = {}, + 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 ] = [], + 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" } + ] + }, + { + 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" } + ] } ] });