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 no-unused-collection: should not fire if collection is exported #244

Merged
merged 7 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/rules/no-unused-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ function collectUnusedCollections(
});
}

function isExported(variable: TSESLint.Scope.Variable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a lot of point in all this logic here. I suggest following:

  const definition = variable.defs[0];
  return definition && definition.node.parent?.parent?.type.startsWith('Export');

const definition = variable.defs[0];
return definition && definition.node.parent?.parent?.type.startsWith('Export');
}

function isUnusedCollection(variable: TSESLint.Scope.Variable) {
if (isExported(variable)) {
return false;
}
if (variable.references.length <= 1) {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/rules/no-unused-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ ruleTester.run('Primitive return types should be used.', rule, {
console.log(i);
}`,
},
{
code: `
export const array = [];

array.push(1);
`,
},
{
code: `export const collection = new Map()`,
},
],
invalid: [
{
Expand Down