diff --git a/src/etc/is.ts b/src/etc/is.ts index d712acca..5b52b602 100644 --- a/src/etc/is.ts +++ b/src/etc/is.ts @@ -134,6 +134,10 @@ export function isRestElement(node: TSESTree.Node): node is TSESTree.RestElement return node.type === AST_NODE_TYPES.RestElement; } +export function isSpreadElement(node: TSESTree.Node): node is TSESTree.SpreadElement { + return node.type === AST_NODE_TYPES.SpreadElement; +} + export function isThisExpression(node: TSESTree.Node): node is TSESTree.ThisExpression { return node.type === AST_NODE_TYPES.ThisExpression; } diff --git a/src/rules/no-unnecessary-collection.ts b/src/rules/no-unnecessary-collection.ts index 1b6c7e80..e76e9a71 100644 --- a/src/rules/no-unnecessary-collection.ts +++ b/src/rules/no-unnecessary-collection.ts @@ -2,7 +2,7 @@ import { TSESTree as es, } from '@typescript-eslint/utils'; import { MULTIPLE_OBSERVABLE_ACCEPTING_STATIC_OBSERVABLE_CREATORS, SOURCES_OBJECT_ACCEPTING_STATIC_OBSERVABLE_CREATORS } from '../constants'; -import { getTypeServices, isArrayExpression, isObjectExpression, isProperty } from '../etc'; +import { getTypeServices, isArrayExpression, isObjectExpression, isProperty, isSpreadElement } from '../etc'; import { ruleCreator } from '../utils'; export const noUnnecessaryCollectionRule = ruleCreator({ @@ -71,7 +71,7 @@ export const noUnnecessaryCollectionRule = ruleCreator({ } // Single rest parameter argument. - if (args.length === 1 && couldBeObservable(firstArg)) { + if (args.length === 1 && !isSpreadElement(firstArg) && couldBeObservable(firstArg)) { context.report({ messageId: 'forbidden', node: node.callee, diff --git a/tests/rules/no-unnecessary-collection.test.ts b/tests/rules/no-unnecessary-collection.test.ts index 6fe5ca66..3dd93518 100644 --- a/tests/rules/no-unnecessary-collection.test.ts +++ b/tests/rules/no-unnecessary-collection.test.ts @@ -38,6 +38,13 @@ ruleTester({ types: true }).run('no-unnecessary-collection', noUnnecessaryCollec const b$ = of(2); const merged$ = merge(a$, b$); `, + stripIndent` + // merge with spread + import { merge, of } from "rxjs"; + + const arr = [of(1), of(2)]; + const merged$ = merge(...arr); + `, stripIndent` // zip with multiple observables import { zip, of } from "rxjs";