Skip to content
Permalink
Browse files
fix: improve autofix for the prefer-const rule (#16292)
* 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 734b54eb9c6c4839c0f99ebe18dc5695754aac1d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
@@ -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);
}
}

@@ -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.