Skip to content

Commit 587ac30

Browse files
fix(eslint-plugin): [no-unused-vars] check if any variable definition is exported (#6873)
* fix #6188 * optimize * add tests * nit: simplify to .some --------- Co-authored-by: Josh Goldberg <[email protected]>
1 parent c8abb09 commit 587ac30

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Diff for: packages/eslint-plugin/src/util/collectUnusedVariables.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,7 @@ function isMergableExported(variable: TSESLint.Scope.Variable): boolean {
426426
* @returns True if the variable is exported, false if not.
427427
*/
428428
function isExported(variable: TSESLint.Scope.Variable): boolean {
429-
const definition = variable.defs[0];
430-
431-
if (definition) {
429+
return variable.defs.some(definition => {
432430
let node = definition.node;
433431

434432
if (node.type === AST_NODE_TYPES.VariableDeclarator) {
@@ -438,8 +436,7 @@ function isExported(variable: TSESLint.Scope.Variable): boolean {
438436
}
439437

440438
return node.parent!.type.indexOf('Export') === 0;
441-
}
442-
return false;
439+
});
443440
}
444441

445442
/**

Diff for: packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,18 @@ export class Foo {
10801080
typescript: '4.4',
10811081
},
10821082
},
1083+
`
1084+
interface Foo {
1085+
bar: string;
1086+
}
1087+
export const Foo = 'bar';
1088+
`,
1089+
`
1090+
export const Foo = 'bar';
1091+
interface Foo {
1092+
bar: string;
1093+
}
1094+
`,
10831095
],
10841096

10851097
invalid: [
@@ -1805,5 +1817,25 @@ x = foo(x);
18051817
},
18061818
],
18071819
},
1820+
{
1821+
code: `
1822+
interface Foo {
1823+
bar: string;
1824+
}
1825+
const Foo = 'bar';
1826+
`,
1827+
errors: [
1828+
{
1829+
messageId: 'unusedVar',
1830+
line: 5,
1831+
column: 7,
1832+
data: {
1833+
varName: 'Foo',
1834+
action: 'assigned a value',
1835+
additional: '',
1836+
},
1837+
},
1838+
],
1839+
},
18081840
],
18091841
});

0 commit comments

Comments
 (0)