Skip to content

Commit 9339ec3

Browse files
test(lint): what follows a loop no longer decides whether it polls
Macroscope review on #22565: a fixed-count flush followed by unrelated error handling was reported because any failure in the next statement counted as budget exhaustion. A conditional exit in the body or a non-counter test already identifies every polling loop. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 8ab4d34 commit 9339ec3

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

‎oxlint-plugin-t3code/rules/no-polling-loop-in-tests.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ const rule = createOxlintRuleHarness("t3code/no-polling-loop-in-tests", {
77
});
88

99
describe("t3code/no-polling-loop-in-tests", () => {
10+
rule.valid(
11+
"allows a fixed-count flush followed by unrelated error handling",
12+
`
13+
import { it } from "@effect/vitest";
14+
import * as Effect from "effect/Effect";
15+
it.effect("flushes", () =>
16+
Effect.gen(function* () {
17+
for (let i = 0; i < 3; i++) yield* Effect.yieldNow;
18+
if (!ready()) return yield* Effect.die("not ready");
19+
}),
20+
);
21+
`,
22+
);
23+
1024
rule.valid(
1125
"allows a fixed-N flush with a literal bound",
1226
`

‎oxlint-plugin-t3code/rules/no-polling-loop-in-tests.ts‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ const isWait = (node: ESTree.Node): boolean => {
9494
return false;
9595
};
9696

97-
const isFailure = (node: ESTree.Node): boolean =>
98-
node.type === "ThrowStatement" ||
99-
(node.type === "CallExpression" &&
100-
(isMember(node.callee, "Effect", "die") || isMember(node.callee, "Effect", "fail")));
101-
10297
const hasConditionalExit = (body: ESTree.Node): boolean =>
10398
someDescendant(
10499
body,
@@ -167,19 +162,12 @@ const isTrueLiteral = (test: ESTree.Expression): boolean => {
167162

168163
// `for (...) { yield* Effect.yieldNow } return yield* Effect.die("timed out")`
169164
// keys a failure on the loop running out, which makes the bound a budget.
170-
const failsAfterLoop = (loop: LoopNode): boolean => {
171-
const parent = loop.parent;
172-
if (parent?.type !== "BlockStatement" && parent?.type !== "Program") return false;
173-
const statements: ReadonlyArray<ESTree.Node> = parent.body;
174-
const next = statements[statements.indexOf(loop) + 1];
175-
return next !== undefined && someDescendant(next, isFailure);
176-
};
177-
178-
const hasPredicateExit = (loop: LoopNode): boolean => {
179-
if (hasConditionalExit(loop.body)) return true;
180-
if (loop.test != null && !isTrueLiteral(loop.test) && !isCounterTest(loop)) return true;
181-
return failsAfterLoop(loop);
182-
};
165+
// A loop is polling when a predicate decides whether it keeps going: a conditional exit in the
166+
// body, or a test that is neither `true` nor a counter the loop advances. A fixed-count flush
167+
// followed by unrelated error handling is not polling, so what comes after the loop is ignored.
168+
const hasPredicateExit = (loop: LoopNode): boolean =>
169+
hasConditionalExit(loop.body) ||
170+
(loop.test != null && !isTrueLiteral(loop.test) && !isCounterTest(loop));
183171

184172
export default defineRule({
185173
meta: {

0 commit comments

Comments
 (0)