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

Commit e8d2d08

Browse files
authored
Make no-unused-collection not trigger if writing to elements of said collection (#452)
1 parent 710b5f1 commit e8d2d08

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/rules/no-unused-collection.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,7 @@ function isElementWrite(statement: TSESTree.ExpressionStatement, ref: TSESLint.S
184184
}
185185

186186
function isMemberExpressionReference(lhs: TSESTree.Node, ref: TSESLint.Scope.Reference): boolean {
187-
return (
188-
lhs.type === 'MemberExpression' &&
189-
(isReferenceTo(ref, lhs.object) || isMemberExpressionReference(lhs.object, ref))
190-
);
187+
return lhs.type === 'MemberExpression' && isReferenceTo(ref, lhs.object);
191188
}
192189

193190
function isIdentifier(node: TSESTree.Node, ...values: string[]): node is TSESTree.Identifier {

tests/rules/no-unused-collection.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function invalidTest(code: string) {
3434
};
3535
}
3636

37-
ruleTester.run('Primitive return types should be used.', rule, {
37+
ruleTester.run('Collection contents should be used', rule, {
3838
valid: [
3939
{
4040
code: `
@@ -169,6 +169,13 @@ ruleTester.run('Primitive return types should be used.', rule, {
169169
{
170170
code: `export const collection = new Map()`,
171171
},
172+
{
173+
code: `
174+
const a = {foo: false};
175+
const xs = [a];
176+
xs[0].foo = true;
177+
`,
178+
},
172179
],
173180
invalid: [
174181
{
@@ -197,11 +204,6 @@ ruleTester.run('Primitive return types should be used.', rule, {
197204
arrayConstructor[1] = 42;
198205
}
199206
`),
200-
invalidTest(`function nok2_1() {
201-
let arrayConstructor = new Array(); // Noncompliant
202-
arrayConstructor[1][2] = 42;
203-
}
204-
`),
205207
invalidTest(`function nok3() {
206208
let arrayWithoutNew = Array(); // Noncompliant
207209
arrayWithoutNew[1] = 42;

0 commit comments

Comments
 (0)