Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-vars] check if any variable definition…
Browse files Browse the repository at this point in the history
… is exported (#6873)

* fix #6188

* optimize

* add tests

* nit: simplify to .some

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
Maxim-Mazurok and JoshuaKGoldberg committed Jul 17, 2023
1 parent c8abb09 commit 587ac30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
7 changes: 2 additions & 5 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
Expand Up @@ -426,9 +426,7 @@ function isMergableExported(variable: TSESLint.Scope.Variable): boolean {
* @returns True if the variable is exported, false if not.
*/
function isExported(variable: TSESLint.Scope.Variable): boolean {
const definition = variable.defs[0];

if (definition) {
return variable.defs.some(definition => {
let node = definition.node;

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

return node.parent!.type.indexOf('Export') === 0;
}
return false;
});
}

/**
Expand Down
Expand Up @@ -1080,6 +1080,18 @@ export class Foo {
typescript: '4.4',
},
},
`
interface Foo {
bar: string;
}
export const Foo = 'bar';
`,
`
export const Foo = 'bar';
interface Foo {
bar: string;
}
`,
],

invalid: [
Expand Down Expand Up @@ -1805,5 +1817,25 @@ x = foo(x);
},
],
},
{
code: `
interface Foo {
bar: string;
}
const Foo = 'bar';
`,
errors: [
{
messageId: 'unusedVar',
line: 5,
column: 7,
data: {
varName: 'Foo',
action: 'assigned a value',
additional: '',
},
},
],
},
],
});

0 comments on commit 587ac30

Please sign in to comment.