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

Make no-unused-collection not trigger if writing to elements of said collection #452

Merged
merged 2 commits into from
Mar 18, 2024
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
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, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems to be some copypasta from some other test.

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;
}
Comment on lines -200 to -203
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this would now be considered compliant code, as the arrayConstructor array is being read to access arrayConstructor[1], hence should be not considered unused.

`),
invalidTest(`function nok3() {
let arrayWithoutNew = Array(); // Noncompliant
arrayWithoutNew[1] = 42;
Expand Down