Skip to content

Commit ac60621

Browse files
yeonjuankaicataldo
authored andcommitted
Fix: unexpected autofix in prefer-const (fixes #12514) (#12521)
1 parent 990065e commit ac60621

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/rules/prefer-const.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ module.exports = {
356356
const ignoreReadBeforeAssign = options.ignoreReadBeforeAssign === true;
357357
const variables = [];
358358
let reportCount = 0;
359-
let name = "";
359+
let checkedId = null;
360+
let checkedName = "";
361+
360362

361363
/**
362364
* Reports given identifier nodes if all of the nodes should be declared
@@ -387,25 +389,30 @@ module.exports = {
387389
/*
388390
* First we check the declaration type and then depending on
389391
* if the type is a "VariableDeclarator" or its an "ObjectPattern"
390-
* we compare the name from the first identifier, if the names are different
391-
* we assign the new name and reset the count of reportCount and nodeCount in
392+
* we compare the name and id from the first identifier, if the names are different
393+
* we assign the new name, id and reset the count of reportCount and nodeCount in
392394
* order to check each block for the number of reported errors and base our fix
393395
* based on comparing nodes.length and nodesToReport.length.
394396
*/
395397

396398
if (firstDecParent.type === "VariableDeclarator") {
397399

398-
if (firstDecParent.id.name !== name) {
399-
name = firstDecParent.id.name;
400+
if (firstDecParent.id.name !== checkedName) {
401+
checkedName = firstDecParent.id.name;
400402
reportCount = 0;
401403
}
402404

403405
if (firstDecParent.id.type === "ObjectPattern") {
404-
if (firstDecParent.init.name !== name) {
405-
name = firstDecParent.init.name;
406+
if (firstDecParent.init.name !== checkedName) {
407+
checkedName = firstDecParent.init.name;
406408
reportCount = 0;
407409
}
408410
}
411+
412+
if (firstDecParent.id !== checkedId) {
413+
checkedId = firstDecParent.id;
414+
reportCount = 0;
415+
}
409416
}
410417
}
411418
}

tests/lib/rules/prefer-const.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,25 @@ ruleTester.run("prefer-const", rule, {
527527
{ messageId: "useConst", data: { name: "b" }, type: "Identifier" },
528528
{ messageId: "useConst", data: { name: "c" }, type: "Identifier" }
529529
]
530+
},
531+
{
532+
code: [
533+
"function a() {",
534+
"let foo = 0,",
535+
" bar = 1;",
536+
"foo = 1;",
537+
"}",
538+
"function b() {",
539+
"let foo = 0,",
540+
" bar = 2;",
541+
"foo = 2;",
542+
"}"
543+
].join("\n"),
544+
output: null,
545+
errors: [
546+
{ message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" },
547+
{ message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" }
548+
]
530549
}
531550
]
532551
});

0 commit comments

Comments
 (0)