Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-vars] clear error report range (#8640)
Browse files Browse the repository at this point in the history
* fix: clear error code range

* test: add test code that cover end line

* refactor: rename variable name and delete not effect property

* test: add test case

* fix: remove type property

* fix: remove type property

* fix: remove type property

---------

Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>
  • Loading branch information
developer-bandi and kirkwaiblinger committed Apr 28, 2024
1 parent 216d1b0 commit 8127873
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
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
? 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,
},
],
},
],
});

0 comments on commit 8127873

Please sign in to comment.