Skip to content

Commit

Permalink
Fixes no-unused-collections on exported members (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro authored and yassin-kammoun-sonarsource committed Jun 30, 2021
1 parent 345151b commit a91e540
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/rules/no-unused-collection.ts
Expand Up @@ -61,7 +61,26 @@ function collectUnusedCollections(
});
}

// Determines if a given variable is being exported from a module.
function isExported(variable: TSESLint.Scope.Variable) {
const definition = variable.defs[0] as TSESLint.Scope.Definition;

if (definition) {
let {node} = definition;
if (node.type === "VariableDeclarator") {
node = node.parent;
} else if (definition.type === "Parameter") {
return false;
}
return node.parent.type.indexOf("Export") === 0;
}
return false;
}

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
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

0 comments on commit a91e540

Please sign in to comment.