Skip to content

Commit 5df5e4a

Browse files
authored
Update: highlight last write reference for no-unused-vars (fixes #14324) (#14335)
* Fix: highlight last write reference for no-unused-vars (fixes #14324) * test: updates * Chore: add test case * Fix: apply suggestions * Chore: update tests * Chore: more tests
1 parent 0023872 commit 5df5e4a

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

lib/rules/no-unused-vars.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,18 @@ module.exports = {
624624

625625
// Report the first declaration.
626626
if (unusedVar.defs.length > 0) {
627+
628+
// report last write reference, https://github.com/eslint/eslint/issues/14324
629+
const writeReferences = unusedVar.references.filter(ref => ref.isWrite() && ref.from.variableScope === unusedVar.scope.variableScope);
630+
631+
let referenceToReport;
632+
633+
if (writeReferences.length > 0) {
634+
referenceToReport = writeReferences[writeReferences.length - 1];
635+
}
636+
627637
context.report({
628-
node: unusedVar.references.length ? unusedVar.references[
629-
unusedVar.references.length - 1
630-
].identifier : unusedVar.identifiers[0],
638+
node: referenceToReport ? referenceToReport.identifier : unusedVar.identifiers[0],
631639
messageId: "unusedVar",
632640
data: unusedVar.references.some(ref => ref.isWrite())
633641
? getAssignedMessageData(unusedVar)

tests/lib/rules/no-unused-vars.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ ruleTester.run("no-unused-vars", rule, {
10621062
code: `let myArray = [1,2,3,4].filter((x) => x == 0);
10631063
myArray = myArray.filter((x) => x == 1);`,
10641064
parserOptions: { ecmaVersion: 2015 },
1065-
errors: [{ ...assignedError("myArray"), line: 2, column: 15 }]
1065+
errors: [{ ...assignedError("myArray"), line: 2, column: 5 }]
10661066
},
10671067
{
10681068
code: "const a = 1; a += 1;",
@@ -1071,21 +1071,28 @@ ruleTester.run("no-unused-vars", rule, {
10711071
},
10721072
{
10731073
code: "var a = function() { a(); };",
1074-
errors: [{ ...assignedError("a"), line: 1, column: 22 }]
1074+
errors: [{ ...assignedError("a"), line: 1, column: 5 }]
10751075
},
10761076
{
10771077
code: "var a = function(){ return function() { a(); } };",
1078-
errors: [{ ...assignedError("a"), line: 1, column: 41 }]
1078+
errors: [{ ...assignedError("a"), line: 1, column: 5 }]
10791079
},
10801080
{
10811081
code: "const a = () => { a(); };",
10821082
parserOptions: { ecmaVersion: 2015 },
1083-
errors: [{ ...assignedError("a"), line: 1, column: 19 }]
1083+
errors: [{ ...assignedError("a"), line: 1, column: 7 }]
10841084
},
10851085
{
10861086
code: "const a = () => () => { a(); };",
10871087
parserOptions: { ecmaVersion: 2015 },
1088-
errors: [{ ...assignedError("a"), line: 1, column: 25 }]
1088+
errors: [{ ...assignedError("a"), line: 1, column: 7 }]
1089+
},
1090+
1091+
// https://github.com/eslint/eslint/issues/14324
1092+
{
1093+
code: "let x = [];\nx = x.concat(x);",
1094+
parserOptions: { ecmaVersion: 2015 },
1095+
errors: [{ ...assignedError("x"), line: 2, column: 1 }]
10891096
},
10901097
{
10911098

@@ -1098,7 +1105,25 @@ ruleTester.run("no-unused-vars", rule, {
10981105
}
10991106
}`,
11001107
parserOptions: { ecmaVersion: 2020 },
1101-
errors: [{ ...definedError("foo"), line: 3, column: 22 }, { ...assignedError("a"), line: 6, column: 21 }]
1108+
errors: [{ ...assignedError("a"), line: 2, column: 13 }, { ...definedError("foo"), line: 3, column: 22 }]
1109+
},
1110+
{
1111+
code: `let foo;
1112+
init();
1113+
foo = foo + 2;
1114+
function init() {
1115+
foo = 1;
1116+
}`,
1117+
parserOptions: { ecmaVersion: 2020 },
1118+
errors: [{ ...assignedError("foo"), line: 3, column: 13 }]
1119+
},
1120+
{
1121+
code: `function foo(n) {
1122+
if (n < 2) return 1;
1123+
return n * foo(n - 1);
1124+
}`,
1125+
parserOptions: { ecmaVersion: 2020 },
1126+
errors: [{ ...definedError("foo"), line: 1, column: 10 }]
11021127
},
11031128
{
11041129
code: `let c = 'c'

0 commit comments

Comments
 (0)