Skip to content

Commit

Permalink
fix: improve autofix for the prefer-const rule (#16292)
Browse files Browse the repository at this point in the history
* fix: avoid incorrect autofix in the `prefer-const` rule

* test: add more test cases

* test: add more cases

* fix: improve autofix and add more cases
  • Loading branch information
snitin315 committed Sep 12, 2022
1 parent 6a923ff commit 734b54e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/rules/prefer-const.js
Expand Up @@ -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);
}
}

Expand Down
72 changes: 72 additions & 0 deletions tests/lib/rules/prefer-const.js
Expand Up @@ -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" }
]
}
]
});

0 comments on commit 734b54e

Please sign in to comment.