Skip to content

Commit 04f3baf

Browse files
fix(no-floating-observables): handle chain expressions
1 parent d021217 commit 04f3baf

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/etc/is.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export function isCallExpression(node: TSESTree.Node): node is TSESTree.CallExpr
3434
return node.type === AST_NODE_TYPES.CallExpression;
3535
}
3636

37+
export function isChainExpression(node: TSESTree.Node): node is TSESTree.ChainExpression {
38+
return node.type === AST_NODE_TYPES.ChainExpression;
39+
}
40+
3741
export function isExportNamedDeclaration(
3842
node: TSESTree.Node,
3943
): node is TSESTree.ExportNamedDeclaration {

src/rules/no-floating-observables.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TSESTree as es } from '@typescript-eslint/utils';
2-
import { getTypeServices, isCallExpression } from '../etc';
2+
import { getTypeServices, isCallExpression, isChainExpression } from '../etc';
33
import { ruleCreator } from '../utils';
44

55
const defaultOptions: readonly {
@@ -57,9 +57,19 @@ export const noFloatingObservablesRule = ruleCreator({
5757
'ExpressionStatement > UnaryExpression': (node: es.UnaryExpression) => {
5858
if (ignoreVoid) return;
5959
if (node.operator !== 'void') return;
60-
if (!isCallExpression(node.argument)) return;
6160

62-
checkNode(node.argument);
61+
let expression = node.argument;
62+
if (isChainExpression(expression)) {
63+
expression = expression.expression;
64+
}
65+
66+
if (!isCallExpression(expression)) return;
67+
checkNode(expression);
68+
},
69+
'ExpressionStatement > ChainExpression': (node: es.ChainExpression) => {
70+
if (!isCallExpression(node.expression)) return;
71+
72+
checkNode(node.expression);
6373
},
6474
};
6575
},

tests/rules/no-floating-observables.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ ruleTester({ types: true }).run('no-floating-observables', noFloatingObservables
6161
~~~~~~~~~~~~~ [forbidden]
6262
`,
6363
),
64+
fromFixture(
65+
stripIndent`
66+
// chain expression
67+
import { Observable, of } from "rxjs";
68+
69+
const arrowSource: null | (() => Observable<number>) = () => of(42);
70+
71+
arrowSource?.();
72+
~~~~~~~~~~~~~~~ [forbidden]
73+
`,
74+
),
6475
fromFixture(
6576
stripIndent`
6677
// ignoreVoid false
@@ -72,6 +83,8 @@ ruleTester({ types: true }).run('no-floating-observables', noFloatingObservables
7283
7384
void functionSource();
7485
~~~~~~~~~~~~~~~~ [forbiddenNoVoid]
86+
void functionSource?.();
87+
~~~~~~~~~~~~~~~~~~ [forbiddenNoVoid]
7588
`,
7689
{
7790
options: [{ ignoreVoid: false }],

0 commit comments

Comments
 (0)