Skip to content

Commit f1b79fe

Browse files
fix(no-floating-observables): reduce selectors for perf
Go back to more limited selectors for performance. Still checking the expression's type (call or unary) so we don't type-check too early.
1 parent d099698 commit f1b79fe

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/rules/no-floating-observables.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,25 @@ export const noFloatingObservablesRule = ruleCreator({
4141
const [config = {}] = context.options;
4242
const { ignoreVoid = true } = config;
4343

44-
return {
45-
ExpressionStatement: (node: es.ExpressionStatement) => {
46-
const { expression } = node;
44+
function checkNode(node: es.Expression) {
45+
if (!ignoreVoid && isUnaryExpression(node) && node.operator === 'void') {
46+
node = node.argument;
47+
}
4748

48-
if (isCallExpression(expression) && couldBeObservable(expression)) {
49-
context.report({
50-
messageId: ignoreVoid ? 'forbidden' : 'forbiddenNoVoid',
51-
node,
52-
});
53-
return;
54-
}
49+
if (isCallExpression(node) && couldBeObservable(node)) {
50+
context.report({
51+
messageId: ignoreVoid ? 'forbidden' : 'forbiddenNoVoid',
52+
node,
53+
});
54+
}
55+
}
5556

56-
if (!ignoreVoid && isUnaryExpression(expression)) {
57-
const { operator, argument } = expression;
58-
if (operator === 'void' && isCallExpression(argument) && couldBeObservable(argument)) {
59-
context.report({
60-
messageId: 'forbiddenNoVoid',
61-
node: argument,
62-
});
63-
return;
64-
}
65-
}
57+
return {
58+
'ExpressionStatement > CallExpression': (node: es.CallExpression) => {
59+
checkNode(node);
60+
},
61+
'ExpressionStatement > UnaryExpression': (node: es.UnaryExpression) => {
62+
checkNode(node);
6663
},
6764
};
6865
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ruleTester({ types: true }).run('no-floating-observables', noFloatingObservables
4747
}
4848
4949
functionSource();
50-
~~~~~~~~~~~~~~~~~ [forbidden]
50+
~~~~~~~~~~~~~~~~ [forbidden]
5151
`,
5252
),
5353
fromFixture(
@@ -58,7 +58,7 @@ ruleTester({ types: true }).run('no-floating-observables', noFloatingObservables
5858
const arrowSource = () => of(42);
5959
6060
arrowSource();
61-
~~~~~~~~~~~~~~ [forbidden]
61+
~~~~~~~~~~~~~ [forbidden]
6262
`,
6363
),
6464
fromFixture(

0 commit comments

Comments
 (0)