Skip to content
Permalink
Browse files
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
  • Loading branch information
snitin315 committed Apr 23, 2021
1 parent 0023872 commit 5df5e4a9976964fcf4dc67e241d4e22ec1370fe0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
@@ -624,10 +624,18 @@ module.exports = {

// Report the first declaration.
if (unusedVar.defs.length > 0) {

// report last write reference, https://github.com/eslint/eslint/issues/14324
const writeReferences = unusedVar.references.filter(ref => ref.isWrite() && ref.from.variableScope === unusedVar.scope.variableScope);

let referenceToReport;

if (writeReferences.length > 0) {
referenceToReport = writeReferences[writeReferences.length - 1];
}

context.report({
node: unusedVar.references.length ? unusedVar.references[
unusedVar.references.length - 1
].identifier : unusedVar.identifiers[0],
node: referenceToReport ? referenceToReport.identifier : unusedVar.identifiers[0],
messageId: "unusedVar",
data: unusedVar.references.some(ref => ref.isWrite())
? getAssignedMessageData(unusedVar)
@@ -1062,7 +1062,7 @@ ruleTester.run("no-unused-vars", rule, {
code: `let myArray = [1,2,3,4].filter((x) => x == 0);
myArray = myArray.filter((x) => x == 1);`,
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("myArray"), line: 2, column: 15 }]
errors: [{ ...assignedError("myArray"), line: 2, column: 5 }]
},
{
code: "const a = 1; a += 1;",
@@ -1071,21 +1071,28 @@ ruleTester.run("no-unused-vars", rule, {
},
{
code: "var a = function() { a(); };",
errors: [{ ...assignedError("a"), line: 1, column: 22 }]
errors: [{ ...assignedError("a"), line: 1, column: 5 }]
},
{
code: "var a = function(){ return function() { a(); } };",
errors: [{ ...assignedError("a"), line: 1, column: 41 }]
errors: [{ ...assignedError("a"), line: 1, column: 5 }]
},
{
code: "const a = () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("a"), line: 1, column: 19 }]
errors: [{ ...assignedError("a"), line: 1, column: 7 }]
},
{
code: "const a = () => () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("a"), line: 1, column: 25 }]
errors: [{ ...assignedError("a"), line: 1, column: 7 }]
},

// https://github.com/eslint/eslint/issues/14324
{
code: "let x = [];\nx = x.concat(x);",
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("x"), line: 2, column: 1 }]
},
{

@@ -1098,7 +1105,25 @@ ruleTester.run("no-unused-vars", rule, {
}
}`,
parserOptions: { ecmaVersion: 2020 },
errors: [{ ...definedError("foo"), line: 3, column: 22 }, { ...assignedError("a"), line: 6, column: 21 }]
errors: [{ ...assignedError("a"), line: 2, column: 13 }, { ...definedError("foo"), line: 3, column: 22 }]
},
{
code: `let foo;
init();
foo = foo + 2;
function init() {
foo = 1;
}`,
parserOptions: { ecmaVersion: 2020 },
errors: [{ ...assignedError("foo"), line: 3, column: 13 }]
},
{
code: `function foo(n) {
if (n < 2) return 1;
return n * foo(n - 1);
}`,
parserOptions: { ecmaVersion: 2020 },
errors: [{ ...definedError("foo"), line: 1, column: 10 }]
},
{
code: `let c = 'c'

0 comments on commit 5df5e4a

Please sign in to comment.