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(eslint-plugin): [no-unused-vars] highlight last write reference #5267

Merged
merged 6 commits into from Jul 18, 2022
8 changes: 4 additions & 4 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Expand Up @@ -422,13 +422,13 @@ export default util.createRule<Options, MessageIds>({
for (const unusedVar of unusedVars) {
// Report the first declaration.
if (unusedVar.defs.length > 0) {
const writeRef = unusedVar.references.filter(ref => ref.isWrite());
context.report({
node: unusedVar.references.length
? unusedVar.references[unusedVar.references.length - 1]
.identifier
node: writeRef.length
? writeRef[writeRef.length - 1].identifier
: unusedVar.identifiers[0],
messageId: 'unusedVar',
data: unusedVar.references.some(ref => ref.isWrite())
data: writeRef.length
? getAssignedMessageData(unusedVar)
: getDefinedMessageData(unusedVar),
});
Expand Down
Expand Up @@ -2601,7 +2601,7 @@ myArray = myArray.filter(x => x == 1);
{
...assignedError('myArray'),
line: 3,
column: 11,
column: 1,
armano2 marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
Expand Down
Expand Up @@ -1458,8 +1458,8 @@ namespace Foo {
action: 'defined',
additional: '',
},
line: 4,
column: 15,
line: 2,
column: 11,
},
],
},
Expand Down Expand Up @@ -1490,8 +1490,8 @@ namespace Foo {
action: 'defined',
additional: '',
},
line: 5,
column: 17,
line: 3,
column: 13,
},
],
},
Expand All @@ -1506,7 +1506,8 @@ interface Foo {
errors: [
{
messageId: 'unusedVar',
line: 4,
line: 2,
column: 11,
data: {
varName: 'Foo',
action: 'defined',
Expand All @@ -1523,6 +1524,7 @@ type Foo = Array<Foo>;
{
messageId: 'unusedVar',
line: 2,
column: 6,
armano2 marked this conversation as resolved.
Show resolved Hide resolved
data: {
varName: 'Foo',
action: 'defined',
Expand Down Expand Up @@ -1550,6 +1552,7 @@ export const ComponentFoo = () => {
{
messageId: 'unusedVar',
line: 3,
column: 10,
data: {
varName: 'Fragment',
action: 'defined',
Expand Down Expand Up @@ -1577,6 +1580,7 @@ export const ComponentFoo = () => {
{
messageId: 'unusedVar',
line: 2,
column: 8,
data: {
varName: 'React',
action: 'defined',
Expand Down Expand Up @@ -1604,6 +1608,7 @@ export const ComponentFoo = () => {
{
messageId: 'unusedVar',
line: 2,
column: 8,
data: {
varName: 'React',
action: 'defined',
Expand All @@ -1624,6 +1629,7 @@ declare module 'foo' {
{
messageId: 'unusedVar',
line: 3,
column: 8,
data: {
varName: 'Test',
action: 'defined',
Expand All @@ -1649,6 +1655,7 @@ export namespace Foo {
{
messageId: 'unusedVar',
line: 4,
column: 13,
data: {
varName: 'Bar',
action: 'defined',
Expand All @@ -1658,6 +1665,7 @@ export namespace Foo {
{
messageId: 'unusedVar',
line: 5,
column: 15,
data: {
varName: 'Baz',
action: 'defined',
Expand All @@ -1667,6 +1675,7 @@ export namespace Foo {
{
messageId: 'unusedVar',
line: 6,
column: 17,
data: {
varName: 'Bam',
action: 'defined',
Expand All @@ -1676,6 +1685,7 @@ export namespace Foo {
{
messageId: 'unusedVar',
line: 7,
column: 15,
data: {
varName: 'x',
action: 'assigned a value',
Expand All @@ -1696,7 +1706,8 @@ interface Foo {
errors: [
{
messageId: 'unusedVar',
line: 6,
line: 2,
column: 11,
data: {
varName: 'Foo',
action: 'defined',
Expand All @@ -1705,5 +1716,23 @@ interface Foo {
},
],
},
{
code: `
let x = null;
x = foo(x);
`,
errors: [
{
messageId: 'unusedVar',
line: 3,
column: 1,
data: {
varName: 'x',
action: 'assigned a value',
additional: '',
},
},
],
},
],
});