Skip to content

Commit 31913f3

Browse files
test(lint): a computed member with a variable key is not a wall-clock read
Macroscope review on #22562: `Date[now](...)` resolved the identifier key to "now" and was reported. Only a literal key names a member. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent a805499 commit 31913f3

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎oxlint-plugin-t3code/rules/no-wall-clock-in-tests.test.ts‎

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

99
describe("t3code/no-wall-clock-in-tests", () => {
10+
rule.valid(
11+
"ignores a computed member with a variable key",
12+
`
13+
import { it } from "vitest";
14+
it("parses", () => {
15+
const now = "parse";
16+
const parsed = Date[now]("2026-01-01");
17+
const literal = Date["parse"]("2026-01-01");
18+
});
19+
`,
20+
);
21+
1022
rule.valid(
1123
"allows fixed dates and readings that seed mocks",
1224
`

‎oxlint-plugin-t3code/rules/no-wall-clock-in-tests.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ const MOCK_METHODS = new Set([
2323
const MESSAGE =
2424
"Wall-clock reading in a test; elapsed-time assertions pass or fail with runner load. Use TestClock/`Clock` under `it.effect`, `vi.setSystemTime`, or a fixed epoch. Benchmarks live behind an env gate.";
2525

26+
const isStringLiteral = (node: unknown) =>
27+
Option.exists(
28+
unwrapExpression(node),
29+
(expression) => expression.type === "Literal" && typeof expression.value === "string",
30+
);
31+
2632
const memberPath = (node: unknown): Option.Option<ReadonlyArray<string>> => {
2733
const expression = unwrapExpression(node);
2834
if (Option.isNone(expression)) return Option.none();
2935
if (expression.value.type === "Identifier") return Option.some([expression.value.name]);
3036
if (expression.value.type !== "MemberExpression") return Option.none();
37+
// `Date[now]` with a variable key is unknowable; only a literal key names a member.
38+
if (expression.value.computed && !isStringLiteral(expression.value.property))
39+
return Option.none();
3140
const property = getPropertyName(expression.value.property);
3241
if (Option.isNone(property)) return Option.none();
3342
return Option.map(memberPath(expression.value.object), (path) => [...path, property.value]);

0 commit comments

Comments
 (0)