Skip to content

Commit

Permalink
Fix: no-unused-vars updated location to last reference (fixes #13181) (
Browse files Browse the repository at this point in the history
…#13354)

* Fix: changed reporter loc to last reference

* Chore: added some tests

* Chore: fixed no-unused loc for linter tests

* Update: refactored code using ternary exp
  • Loading branch information
anikethsaha committed Jun 1, 2020
1 parent cb50b69 commit 426088c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/rules/no-unused-vars.js
Expand Up @@ -619,7 +619,9 @@ module.exports = {
// Report the first declaration.
if (unusedVar.defs.length > 0) {
context.report({
node: unusedVar.identifiers[0],
node: unusedVar.references.length ? unusedVar.references[
unusedVar.references.length - 1
].identifier : unusedVar.identifiers[0],
messageId: "unusedVar",
data: unusedVar.references.some(ref => ref.isWrite())
? getAssignedMessageData(unusedVar)
Expand Down
20 changes: 10 additions & 10 deletions tests/lib/linter/linter.js
Expand Up @@ -3918,9 +3918,9 @@ var a = "test2";
messages,
[{
column: 25,
endLine: 3,
endLine: 4,
endColumn: 28,
line: 3,
line: 4,
message: "'aaa' is assigned a value but never used.",
messageId: "unusedVar",
nodeType: "Identifier",
Expand Down Expand Up @@ -3966,9 +3966,9 @@ var a = "test2";
messages,
[{
column: 25,
endLine: 2,
endLine: 3,
endColumn: 28,
line: 2,
line: 3,
message: "'aaa' is assigned a value but never used.",
messageId: "unusedVar",
nodeType: "Identifier",
Expand All @@ -3989,9 +3989,9 @@ var a = "test2";
messages,
[{
column: 25,
endLine: 2,
endLine: 3,
endColumn: 28,
line: 2,
line: 3,
message: "'aaa' is assigned a value but never used.",
messageId: "unusedVar",
nodeType: "Identifier",
Expand All @@ -4014,9 +4014,9 @@ var a = "test2";
messages,
[{
column: 25,
endLine: 3,
endLine: 5,
endColumn: 28,
line: 3,
line: 5,
message: "'aaa' is assigned a value but never used.",
messageId: "unusedVar",
nodeType: "Identifier",
Expand All @@ -4039,9 +4039,9 @@ var a = "test2";
messages,
[{
column: 25,
endLine: 3,
endLine: 5,
endColumn: 28,
line: 3,
line: 5,
message: "'aaa' is assigned a value but never used.",
messageId: "unusedVar",
nodeType: "Identifier",
Expand Down
60 changes: 58 additions & 2 deletions tests/lib/rules/no-unused-vars.js
Expand Up @@ -444,7 +444,7 @@ ruleTester.run("no-unused-vars", rule, {
code: "(function(obj) { var name; for ( name in obj ) { i(); return; } })({});",
errors: [{
line: 1,
column: 22,
column: 34,
messageId: "unusedVar",
data: {
varName: "name",
Expand All @@ -457,7 +457,7 @@ ruleTester.run("no-unused-vars", rule, {
code: "(function(obj) { var name; for ( name in obj ) { } })({});",
errors: [{
line: 1,
column: 22,
column: 34,
messageId: "unusedVar",
data: {
varName: "name",
Expand Down Expand Up @@ -1021,6 +1021,62 @@ ruleTester.run("no-unused-vars", rule, {
code: "const a = () => () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [assignedError("a")]
},
{
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 }]
},
{
code: "const a = 1; a += 1;",
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("a"), line: 1, column: 14 }]
},
{
code: "var a = function() { a(); };",
errors: [{ ...assignedError("a"), line: 1, column: 22 }]
},
{
code: "var a = function(){ return function() { a(); } };",
errors: [{ ...assignedError("a"), line: 1, column: 41 }]
},
{
code: "const a = () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("a"), line: 1, column: 19 }]
},
{
code: "const a = () => () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [{ ...assignedError("a"), line: 1, column: 25 }]
},
{

code: `let a = 'a';
a = 10;
function foo(){
a = 11;
a = () => {
a = 13
}
}`,
parserOptions: { ecmaVersion: 2020 },
errors: [{ ...definedError("foo"), line: 3, column: 22 }, { ...assignedError("a"), line: 6, column: 21 }]
},
{
code: `let c = 'c'
c = 10
function foo1() {
c = 11
c = () => {
c = 13
}
}
c = foo1`,
parserOptions: { ecmaVersion: 2020 },
errors: [{ ...assignedError("c"), line: 10, column: 1 }]
}
]
});

0 comments on commit 426088c

Please sign in to comment.