Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve autofix for the prefer-const rule #16292

Merged
merged 4 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" }
]
}
]
});