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] clear error report range #8640

Merged
19 changes: 16 additions & 3 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,23 @@ export default createRule<Options, MessageIds>({
ref.from.variableScope === unusedVar.scope.variableScope,
);

const id = writeReferences.length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though these branches collapse to do the same thing, I think it could be good to test both branches, in case one day they get separated (for example, to report different error messages)

What do you think about adding test cases with all 4 report loc fields for some cases like the following?

let foo: number;
let foo: number = 1;
foo = 2;
foo = 3;

? writeReferences[writeReferences.length - 1].identifier
: unusedVar.identifiers[0];

const { start } = id.loc;
const idLength = id.name.length;

const loc = {
start,
end: {
line: start.line,
column: start.column + idLength,
},
};

context.report({
node: writeReferences.length
? writeReferences[writeReferences.length - 1].identifier
: unusedVar.identifiers[0],
loc,
messageId: 'unusedVar',
data: unusedVar.references.some(ref => ref.isWrite())
? getAssignedMessageData(unusedVar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import { RuleTester } from '@typescript-eslint/rule-tester';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import type { MessageIds } from '../../../src/rules/no-unused-vars';
import rule from '../../../src/rules/no-unused-vars';
Expand Down Expand Up @@ -40,7 +39,6 @@ ruleTester.defineRule('use-every-a', context => {
function definedError(
varName: string,
additional = '',
type = AST_NODE_TYPES.Identifier,
): TSESLint.TestCaseError<MessageIds> {
return {
messageId: 'unusedVar',
Expand All @@ -49,7 +47,6 @@ function definedError(
action: 'defined',
additional,
},
type,
};
}

Expand All @@ -63,7 +60,6 @@ function definedError(
function assignedError(
varName: string,
additional = '',
type = AST_NODE_TYPES.Identifier,
): TSESLint.TestCaseError<MessageIds> {
return {
messageId: 'unusedVar',
Expand All @@ -72,7 +68,6 @@ function assignedError(
action: 'assigned a value',
additional,
},
type,
};
}

Expand Down Expand Up @@ -1232,7 +1227,7 @@ function f() {
},
{
code: '/*global a */',
errors: [definedError('a', '', AST_NODE_TYPES.Program)],
errors: [definedError('a', '')],
},
{
code: `
Expand Down Expand Up @@ -1341,7 +1336,6 @@ function foo() {
messageId: 'unusedVar',
data: { varName: 'foo', action: 'defined', additional: '' },
line: 2,
type: AST_NODE_TYPES.Identifier,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,9 @@ export class Foo {}
additional: '',
},
line: 2,
endLine: 2,
column: 10,
endColumn: 31,
},
],
},
Expand Down Expand Up @@ -1744,6 +1746,8 @@ declare module 'foo' {
messageId: 'unusedVar',
line: 3,
column: 8,
endLine: 3,
endColumn: 12,
data: {
varName: 'Test',
action: 'defined',
Expand Down Expand Up @@ -1840,6 +1844,8 @@ x = foo(x);
messageId: 'unusedVar',
line: 3,
column: 1,
endLine: 3,
endColumn: 2,
data: {
varName: 'x',
action: 'assigned a value',
Expand Down Expand Up @@ -1950,5 +1956,24 @@ export namespace Bar {
},
],
},
{
code: `
const foo: number = 1;
`,
errors: [
{
messageId: 'unusedVar',
data: {
varName: 'foo',
action: 'assigned a value',
additional: '',
},
line: 2,
column: 7,
endLine: 2,
endColumn: 10,
},
],
},
],
});