Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Make no-unused-collection not trigger if writing to elements of said …
Browse files Browse the repository at this point in the history
…collection (#452)
  • Loading branch information
zglicz authored Mar 18, 2024
1 parent 710b5f1 commit e8d2d08
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/rules/no-unused-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ function isElementWrite(statement: TSESTree.ExpressionStatement, ref: TSESLint.S
}

function isMemberExpressionReference(lhs: TSESTree.Node, ref: TSESLint.Scope.Reference): boolean {
return (
lhs.type === 'MemberExpression' &&
(isReferenceTo(ref, lhs.object) || isMemberExpressionReference(lhs.object, ref))
);
return lhs.type === 'MemberExpression' && isReferenceTo(ref, lhs.object);
}

function isIdentifier(node: TSESTree.Node, ...values: string[]): node is TSESTree.Identifier {
Expand Down
14 changes: 8 additions & 6 deletions tests/rules/no-unused-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function invalidTest(code: string) {
};
}

ruleTester.run('Primitive return types should be used.', rule, {
ruleTester.run('Collection contents should be used', rule, {
valid: [
{
code: `
Expand Down Expand Up @@ -169,6 +169,13 @@ ruleTester.run('Primitive return types should be used.', rule, {
{
code: `export const collection = new Map()`,
},
{
code: `
const a = {foo: false};
const xs = [a];
xs[0].foo = true;
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -197,11 +204,6 @@ ruleTester.run('Primitive return types should be used.', rule, {
arrayConstructor[1] = 42;
}
`),
invalidTest(`function nok2_1() {
let arrayConstructor = new Array(); // Noncompliant
arrayConstructor[1][2] = 42;
}
`),
invalidTest(`function nok3() {
let arrayWithoutNew = Array(); // Noncompliant
arrayWithoutNew[1] = 42;
Expand Down

0 comments on commit e8d2d08

Please sign in to comment.