From 3af4ba3fef57f166e157c489f6cf463f2faa0912 Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Sun, 25 Oct 2026 15:02:24 +0000 Subject: [PATCH 1/6] tests: close the 4 clock lint gaps from #7272 (#7282) --- .oxlintrc.json | 5 ++- eslint-local-rules.mjs | 73 +++++++++++++++++++++++++++++-- eslint.config.mjs | 7 +++ scripts/no-raw-clock-lint.test.ts | 52 ++++++++++++++++++++++ scripts/noHostImports.test.ts | 20 +++++++++ 5 files changed, 153 insertions(+), 4 deletions(-) diff --git a/.oxlintrc.json b/.oxlintrc.json index 95e2e37f916..91701485e74 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -1242,6 +1242,8 @@ "net", "child_process", "electron", + "timers", + "timers/promises", "@/node/utils/writeFileAtomic", { "module": "@/node/utils/fs", @@ -1310,7 +1312,8 @@ "error", { "allow": { - "src/node/services/tools/bash.ts": 1 + "src/node/services/tools/bash.ts": 1, + "src/node/services/tools/skillsCatalogFetch.ts": 1 } } ] diff --git a/eslint-local-rules.mjs b/eslint-local-rules.mjs index eae7077cedb..b08a8d1fbc5 100644 --- a/eslint-local-rules.mjs +++ b/eslint-local-rules.mjs @@ -764,7 +764,7 @@ const localPlugin = { type: "problem", docs: { description: - "Disallow raw clock reads (Date.now(), new Date(), setTimeout(), setInterval()) in agent-loop code", + "Disallow raw clock reads (Date.now(), Date(), new Date(), setTimeout(), setInterval(), AbortSignal.timeout()) in agent-loop code", }, schema: [ { @@ -789,8 +789,14 @@ const localPlugin = { const relFile = path.relative(context.cwd, context.filename).split(path.sep).join("/"); const allowed = context.options[0]?.allow?.[relFile] ?? 0; const found = []; + const TIMERS = new Set(["setTimeout", "setInterval"]); + // Local names bound to a timer by an import from "timers" or "timers/promises" + // (`import { setTimeout as delay } from "node:timers/promises"`), and the namespace or + // default bindings of those modules (`timers.setTimeout`). + const timerAliases = new Set(); + const timerModules = new Set(); const isTimer = (node) => - node.type === "Identifier" && (node.name === "setTimeout" || node.name === "setInterval"); + node.type === "Identifier" && (TIMERS.has(node.name) || timerAliases.has(node.name)); const memberOf = (node, object) => node.type === "MemberExpression" && !node.computed && @@ -801,6 +807,26 @@ const localPlugin = { (node.type === "Identifier" && node.name === "Date") || (memberOf(node, "globalThis") && node.property.name === "Date"); return { + // Imports are scanned first, so a call above a (hoisted) import still counts. + Program(program) { + for (const statement of program.body) { + if (statement.type !== "ImportDeclaration" || statement.importKind === "type") { + continue; + } + const module = String(statement.source.value).replace(/^node:/, ""); + if (module !== "timers" && module !== "timers/promises") continue; + for (const specifier of statement.specifiers) { + if (specifier.type !== "ImportSpecifier") { + timerModules.add(specifier.local.name); + } else if ( + specifier.importKind !== "type" && + TIMERS.has(specifier.imported.name ?? specifier.imported.value) + ) { + timerAliases.add(specifier.local.name); + } + } + } + }, CallExpression(node) { const callee = node.callee; if ( @@ -810,10 +836,31 @@ const localPlugin = { callee.property.name === "now" ) { found.push({ node, what: "Date.now()" }); + } else if (isDate(callee)) { + // `Date()` without `new` returns the current time as a string. + found.push({ node, what: "Date()" }); } else if (isTimer(callee)) { found.push({ node, what: `${callee.name}()` }); - } else if (memberOf(callee, "globalThis") && isTimer(callee.property)) { + } else if (memberOf(callee, "globalThis") && TIMERS.has(callee.property.name)) { found.push({ node, what: `globalThis.${callee.property.name}()` }); + } else if ( + callee.type === "MemberExpression" && + !callee.computed && + callee.object.type === "Identifier" && + timerModules.has(callee.object.name) && + TIMERS.has(callee.property.name) + ) { + found.push({ node, what: `${callee.object.name}.${callee.property.name}()` }); + } else if ( + callee.type === "MemberExpression" && + !callee.computed && + callee.property.name === "timeout" && + ((callee.object.type === "Identifier" && callee.object.name === "AbortSignal") || + (memberOf(callee.object, "globalThis") && + callee.object.property.name === "AbortSignal")) + ) { + // A host timer that aborts the signal: the TestClock cannot reach it. + found.push({ node, what: "AbortSignal.timeout()" }); } }, NewExpression(node) { @@ -879,6 +926,26 @@ const localPlugin = { context.report({ node, messageId: "patch", data: { what: 'spyOn(Date, "now")' } }); } }, + // `Date.now = () => t` (or `globalThis.Date.now = ...`) replaces the clock directly. + AssignmentExpression(node) { + const left = node.left; + const isDateObject = (object) => + (object.type === "Identifier" && object.name === "Date") || + (object.type === "MemberExpression" && + !object.computed && + object.object.type === "Identifier" && + object.object.name === "globalThis" && + object.property.name === "Date"); + if ( + left.type === "MemberExpression" && + isDateObject(left.object) && + (left.computed + ? left.property.type === "Literal" && left.property.value === "now" + : left.property.name === "now") + ) { + context.report({ node, messageId: "patch", data: { what: "Date.now = ..." } }); + } + }, }; }, }, diff --git a/eslint.config.mjs b/eslint.config.mjs index f8c018ebcc2..49e7b1f7892 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -127,6 +127,10 @@ const CLOCK_PATCH_FREE_TESTS = [ // moves the file onto the HostClock service, and the rule fails an entry that is too high. const RAW_CLOCK_ALLOW = { "src/node/services/tools/bash.ts": 1, + // AbortSignal.timeout() on the skills.sh search request (#7282). The file calls the + // module-level node host services, not injected ones, so it has no HostClock to arm the + // deadline on. It bounds a network request, not loop time. + "src/node/services/tools/skillsCatalogFetch.ts": 1, }; // The rule strips "node:" and resolves relative specifiers to "@/..." forms. const HOST_MODULES = [ @@ -136,6 +140,9 @@ const HOST_MODULES = [ "net", "child_process", "electron", + // Host timers. Loop code arms timers through the HostClock service (phase 3). + "timers", + "timers/promises", "@/node/utils/writeFileAtomic", { module: "@/node/utils/fs", names: ["ensurePrivateDir", "ensurePrivateDirSync"] }, // Concrete runtimes. The Runtime interface (runtime/Runtime.ts) stays allowed. diff --git a/scripts/no-raw-clock-lint.test.ts b/scripts/no-raw-clock-lint.test.ts index ebe8d3fd918..0791cb5b3ee 100644 --- a/scripts/no-raw-clock-lint.test.ts +++ b/scripts/no-raw-clock-lint.test.ts @@ -55,6 +55,45 @@ test("reports raw clock reads and global timers", () => { expect(rawClock(`const d = new globalThis.Date();`)).toEqual(["raw"]); }); +// #7282: forms the first version of the rule missed, one test per gap. +test("reports a Date() call", () => { + expect(rawClock(`const s = Date();`)).toEqual(["raw"]); + expect(rawClock(`const s = globalThis.Date();`)).toEqual(["raw"]); +}); + +test("reports AbortSignal.timeout()", () => { + expect(rawClock(`const signal = AbortSignal.timeout(10);`)).toEqual(["raw"]); + expect(rawClock(`const signal = globalThis.AbortSignal.timeout(10);`)).toEqual(["raw"]); +}); + +test("reports timers imported from the timers modules under another name", () => { + expect( + rawClock(`import { setTimeout as delay } from "node:timers/promises";\nawait delay(10);`) + ).toEqual(["raw"]); + expect(rawClock(`import { setInterval as every } from "timers";\nevery(tick, 10);`)).toEqual([ + "raw", + ]); + expect(rawClock(`import * as timers from "node:timers";\ntimers.setTimeout(fire, 10);`)).toEqual([ + "raw", + ]); + // A call above the import still counts, and an unaliased import counts once. + expect( + rawClock(`await delay(10);\nimport { setTimeout as delay } from "timers/promises";`) + ).toEqual(["raw"]); + expect(rawClock(`import { setTimeout } from "timers/promises";\nawait setTimeout(10);`)).toEqual([ + "raw", + ]); +}); + +test("passes non-timer imports from the timers modules and other abort signals", () => { + expect( + rawClock(`import { setImmediate as yieldNow } from "node:timers/promises";\nawait yieldNow();`) + ).toEqual([]); + expect(rawClock(`import { setTimeout as delay } from "./delay";\nawait delay(10);`)).toEqual([]); + expect(rawClock(`const signal = AbortSignal.any([a, b]);`)).toEqual([]); + expect(rawClock(`const signal = clock.timeout(10);`)).toEqual([]); +}); + test("passes HostClock reads, monotonic durations and date parsing", () => { expect(rawClock(`const t = clock.nowMs();`)).toEqual([]); expect(rawClock(`const cancel = clock.setTimer(fire, 10);`)).toEqual([]); @@ -88,6 +127,16 @@ test("reports global time patches in tests that moved to the TestClock", () => { expect(patch(`await clock.adjust(1000);`)).toEqual([]); }); +// #7282: direct assignment replaces the clock too. +test("reports a direct Date.now assignment in tests that moved to the TestClock", () => { + const patch = (code: string) => ids("no-global-clock-patch", code); + expect(patch(`Date.now = () => 1;`)).toEqual(["patch"]); + expect(patch(`globalThis.Date.now = () => 1;`)).toEqual(["patch"]); + expect(patch(`Date["now"] = () => 1;`)).toEqual(["patch"]); + expect(patch(`const now = Date.now;`)).toEqual([]); + expect(patch(`clock.now = () => 1;`)).toEqual([]); +}); + // The raw reads of each loop file when the ratchet landed. Entries only go down: a later PR // lowers or removes a file's entry when it moves that file onto the HostClock service. const RAW_CLOCK_CEILING: Record = { @@ -102,6 +151,9 @@ const RAW_CLOCK_CEILING: Record = { "src/node/services/tools/bash_background_list.ts": 1, "src/node/services/tools/code_execution.ts": 3, "src/node/services/tools/skills_catalog_read.ts": 1, + // Its AbortSignal.timeout() was there when the ratchet landed, but the rule counted it only + // from #7282 on. + "src/node/services/tools/skillsCatalogFetch.ts": 1, "src/node/services/tools/task.ts": 1, "src/node/services/tools/task_await.ts": 7, "src/node/services/tools/toolUtils.ts": 1, diff --git a/scripts/noHostImports.test.ts b/scripts/noHostImports.test.ts index 1db4c679035..3118d2f981c 100644 --- a/scripts/noHostImports.test.ts +++ b/scripts/noHostImports.test.ts @@ -149,3 +149,23 @@ test("each allow key is an agent-loop file", () => { }); } }); + +// #7282: host timers count as host access, so loop code arms timers through the HostClock. +test("the configured module list covers the timers modules", () => { + const [, options] = ruleEntry().rules?.["local/no-host-imports"] as [string, unknown]; + const lintConfig = [ + { + files: ["**/*.ts"], + languageOptions: { parser: tseslint.parser }, + plugins: { local: { rules: { "no-host-imports": findRule() } } }, + rules: { "local/no-host-imports": ["error", options] as ["error", unknown] }, + }, + ]; + const report = (code: string) => + linter + .verify(code, lintConfig, "src/node/services/tools/x.ts") + .map((message) => message.messageId ?? message.message); + expect(report(`import { setTimeout as delay } from "node:timers/promises";`)).toEqual(["host"]); + expect(report(`import { setInterval } from "timers";`)).toEqual(["host"]); + expect(report(`import type { TimerOptions } from "node:timers";`)).toEqual([]); +}); From b71e0105ae525d7059a80e277fbc844c63f2f341 Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Sun, 25 Oct 2026 15:49:24 +0000 Subject: [PATCH 2/6] tests: resolve timer aliases by binding, track CommonJS timer loads, and treat global like globalThis --- eslint-local-rules.mjs | 134 +++++++++++++++++++++--------- scripts/no-raw-clock-lint.test.ts | 29 +++++++ 2 files changed, 122 insertions(+), 41 deletions(-) diff --git a/eslint-local-rules.mjs b/eslint-local-rules.mjs index b08a8d1fbc5..4cc7ba96414 100644 --- a/eslint-local-rules.mjs +++ b/eslint-local-rules.mjs @@ -790,40 +790,79 @@ const localPlugin = { const allowed = context.options[0]?.allow?.[relFile] ?? 0; const found = []; const TIMERS = new Set(["setTimeout", "setInterval"]); - // Local names bound to a timer by an import from "timers" or "timers/promises" - // (`import { setTimeout as delay } from "node:timers/promises"`), and the namespace or - // default bindings of those modules (`timers.setTimeout`). - const timerAliases = new Set(); - const timerModules = new Set(); - const isTimer = (node) => - node.type === "Identifier" && (TIMERS.has(node.name) || timerAliases.has(node.name)); - const memberOf = (node, object) => + const TIMER_MODULES = new Set(["timers", "timers/promises"]); + // Declaring identifiers of the bindings that the "timers" and "timers/promises" modules + // give: a timer under any name (`import { setTimeout as delay }`, + // `const { setTimeout: delay } = require(...)`), or the module itself (`import * as + // timers`, `import timers = require(...)`, `const timers = require(...)`). + const timerBindings = new Set(); + const moduleBindings = new Set(); + const isTimerModule = (source) => + source?.type === "Literal" && + typeof source.value === "string" && + TIMER_MODULES.has(source.value.replace(/^node:/, "")); + const isRequireOfTimers = (node) => + node?.type === "CallExpression" && + node.callee.type === "Identifier" && + node.callee.name === "require" && + isTimerModule(node.arguments[0]); + // `globalThis` or Node's `global`: both name the global object. + const isGlobalObject = (node) => + node.type === "Identifier" && (node.name === "globalThis" || node.name === "global"); + const globalMember = (node, name) => node.type === "MemberExpression" && !node.computed && - node.object.type === "Identifier" && - node.object.name === object; - // `Date` or `globalThis.Date`: both name the host clock. + isGlobalObject(node.object) && + node.property.name === name; + // `Date`, `globalThis.Date` or `global.Date`: each names the host clock. const isDate = (node) => - (node.type === "Identifier" && node.name === "Date") || - (memberOf(node, "globalThis") && node.property.name === "Date"); + (node.type === "Identifier" && node.name === "Date") || globalMember(node, "Date"); + // A call whose callee needs its binding checked at the end: `id` must resolve to one of + // `bindings`, so a parameter or local that shadows the import is not a timer. + const viaBinding = (node, what, id, bindings) => + found.push({ node, what, check: { id, bindings } }); + const resolvesTo = ({ id, bindings }) => + resolveVariable(context.sourceCode, id)?.identifiers.some((def) => bindings.has(def)) ?? + false; return { - // Imports are scanned first, so a call above a (hoisted) import still counts. - Program(program) { - for (const statement of program.body) { - if (statement.type !== "ImportDeclaration" || statement.importKind === "type") { - continue; + ImportDeclaration(node) { + if (node.importKind === "type" || !isTimerModule(node.source)) return; + for (const specifier of node.specifiers) { + if (specifier.type !== "ImportSpecifier") { + moduleBindings.add(specifier.local); + } else if ( + specifier.importKind !== "type" && + TIMERS.has(specifier.imported.name ?? specifier.imported.value) + ) { + timerBindings.add(specifier.local); } - const module = String(statement.source.value).replace(/^node:/, ""); - if (module !== "timers" && module !== "timers/promises") continue; - for (const specifier of statement.specifiers) { - if (specifier.type !== "ImportSpecifier") { - timerModules.add(specifier.local.name); - } else if ( - specifier.importKind !== "type" && - TIMERS.has(specifier.imported.name ?? specifier.imported.value) - ) { - timerAliases.add(specifier.local.name); - } + } + }, + // `import timers = require("node:timers")` (TypeScript import assignment). + TSImportEqualsDeclaration(node) { + if ( + node.importKind !== "type" && + node.moduleReference.type === "TSExternalModuleReference" && + isTimerModule(node.moduleReference.expression) + ) { + moduleBindings.add(node.id); + } + }, + VariableDeclarator(node) { + if (!isRequireOfTimers(node.init)) return; + if (node.id.type === "Identifier") { + moduleBindings.add(node.id); + return; + } + if (node.id.type !== "ObjectPattern") return; + for (const property of node.id.properties) { + if ( + property.type === "Property" && + !property.computed && + TIMERS.has(property.key.name ?? property.key.value) && + property.value.type === "Identifier" + ) { + timerBindings.add(property.value); } } }, @@ -839,25 +878,35 @@ const localPlugin = { } else if (isDate(callee)) { // `Date()` without `new` returns the current time as a string. found.push({ node, what: "Date()" }); - } else if (isTimer(callee)) { + } else if (callee.type === "Identifier" && TIMERS.has(callee.name)) { found.push({ node, what: `${callee.name}()` }); - } else if (memberOf(callee, "globalThis") && TIMERS.has(callee.property.name)) { - found.push({ node, what: `globalThis.${callee.property.name}()` }); + } else if (callee.type === "Identifier") { + viaBinding(node, `${callee.name}()`, callee, timerBindings); } else if ( callee.type === "MemberExpression" && !callee.computed && - callee.object.type === "Identifier" && - timerModules.has(callee.object.name) && - TIMERS.has(callee.property.name) + TIMERS.has(callee.property.name) && + isGlobalObject(callee.object) ) { found.push({ node, what: `${callee.object.name}.${callee.property.name}()` }); + } else if ( + callee.type === "MemberExpression" && + !callee.computed && + TIMERS.has(callee.property.name) && + callee.object.type === "Identifier" + ) { + viaBinding( + node, + `${callee.object.name}.${callee.property.name}()`, + callee.object, + moduleBindings + ); } else if ( callee.type === "MemberExpression" && !callee.computed && callee.property.name === "timeout" && ((callee.object.type === "Identifier" && callee.object.name === "AbortSignal") || - (memberOf(callee.object, "globalThis") && - callee.object.property.name === "AbortSignal")) + globalMember(callee.object, "AbortSignal")) ) { // A host timer that aborts the signal: the TestClock cannot reach it. found.push({ node, what: "AbortSignal.timeout()" }); @@ -871,14 +920,16 @@ const localPlugin = { "Program:exit"(program) { // The allow entry is an exact count, so it can only go down: a move PR that // removes reads must lower its entry, and a new read fails. - for (const { node, what } of found.slice(allowed)) { + // Bindings are known only now: an import or require can follow the call. + const reads = found.filter(({ check }) => check === undefined || resolvesTo(check)); + for (const { node, what } of reads.slice(allowed)) { context.report({ node, messageId: "raw", data: { what } }); } - if (found.length < allowed) { + if (reads.length < allowed) { context.report({ node: program, messageId: "lowerAllow", - data: { found: String(found.length), allowed: String(allowed) }, + data: { found: String(reads.length), allowed: String(allowed) }, }); } }, @@ -929,12 +980,13 @@ const localPlugin = { // `Date.now = () => t` (or `globalThis.Date.now = ...`) replaces the clock directly. AssignmentExpression(node) { const left = node.left; + // `Date`, `globalThis.Date` or Node's `global.Date`: one process-wide clock. const isDateObject = (object) => (object.type === "Identifier" && object.name === "Date") || (object.type === "MemberExpression" && !object.computed && object.object.type === "Identifier" && - object.object.name === "globalThis" && + (object.object.name === "globalThis" || object.object.name === "global") && object.property.name === "Date"); if ( left.type === "MemberExpression" && diff --git a/scripts/no-raw-clock-lint.test.ts b/scripts/no-raw-clock-lint.test.ts index 0791cb5b3ee..a1660e06643 100644 --- a/scripts/no-raw-clock-lint.test.ts +++ b/scripts/no-raw-clock-lint.test.ts @@ -85,6 +85,34 @@ test("reports timers imported from the timers modules under another name", () => ]); }); +test("reports timers that CommonJS forms load from the timers modules", () => { + expect(rawClock(`import timers = require("node:timers");\ntimers.setTimeout(fire, 10);`)).toEqual( + ["raw"] + ); + expect(rawClock(`const timers = require("timers");\ntimers.setInterval(tick, 10);`)).toEqual([ + "raw", + ]); + expect( + rawClock(`const { setTimeout: delay } = require("node:timers/promises");\nawait delay(10);`) + ).toEqual(["raw"]); +}); + +test("passes a local name that shadows an imported timer", () => { + const imported = `import { setTimeout as delay } from "node:timers/promises";\n`; + expect(rawClock(`${imported}function run(delay: () => void) {\n delay();\n}`)).toEqual([]); + expect(rawClock(`${imported}{\n const delay = () => 1;\n delay();\n}`)).toEqual([]); + const namespace = `import * as timers from "node:timers";\n`; + expect(rawClock(`${namespace}function run(timers: T) {\n timers.setTimeout(f, 1);\n}`)).toEqual( + [] + ); +}); + +test("reports Node's global object like globalThis", () => { + expect(rawClock(`const t = global.Date.now();`)).toEqual(["raw"]); + expect(rawClock(`global.setTimeout(() => undefined, 10);`)).toEqual(["raw"]); + expect(rawClock(`const signal = global.AbortSignal.timeout(10);`)).toEqual(["raw"]); +}); + test("passes non-timer imports from the timers modules and other abort signals", () => { expect( rawClock(`import { setImmediate as yieldNow } from "node:timers/promises";\nawait yieldNow();`) @@ -132,6 +160,7 @@ test("reports a direct Date.now assignment in tests that moved to the TestClock" const patch = (code: string) => ids("no-global-clock-patch", code); expect(patch(`Date.now = () => 1;`)).toEqual(["patch"]); expect(patch(`globalThis.Date.now = () => 1;`)).toEqual(["patch"]); + expect(patch(`global.Date.now = () => 1;`)).toEqual(["patch"]); expect(patch(`Date["now"] = () => 1;`)).toEqual(["patch"]); expect(patch(`const now = Date.now;`)).toEqual([]); expect(patch(`clock.now = () => 1;`)).toEqual([]); From 42567a0ee1eb876f0e180fd222e558efb1ee9a64 Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Sun, 25 Oct 2026 16:31:24 +0000 Subject: [PATCH 3/6] tests: skip locals named global or globalThis, and count { default as timers } imports --- eslint-local-rules.mjs | 27 +++++++++++++++++++-------- scripts/no-raw-clock-lint.test.ts | 13 +++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/eslint-local-rules.mjs b/eslint-local-rules.mjs index 4cc7ba96414..e5868ab7082 100644 --- a/eslint-local-rules.mjs +++ b/eslint-local-rules.mjs @@ -81,6 +81,11 @@ function resolveVariable(sourceCode, identifier) { return null; } +/** True when a parameter, import or local declares the identifier's name in its scope chain. */ +function isDeclaredLocally(sourceCode, identifier) { + return (resolveVariable(sourceCode, identifier)?.defs.length ?? 0) > 0; +} + function variableDeclarator(variable) { if (variable.defs.length !== 1) { return null; @@ -806,9 +811,12 @@ const localPlugin = { node.callee.type === "Identifier" && node.callee.name === "require" && isTimerModule(node.arguments[0]); - // `globalThis` or Node's `global`: both name the global object. + // `globalThis` or Node's `global`: both name the global object, unless a parameter or + // local declares that name. const isGlobalObject = (node) => - node.type === "Identifier" && (node.name === "globalThis" || node.name === "global"); + node.type === "Identifier" && + (node.name === "globalThis" || node.name === "global") && + !isDeclaredLocally(context.sourceCode, node); const globalMember = (node, name) => node.type === "MemberExpression" && !node.computed && @@ -828,12 +836,14 @@ const localPlugin = { ImportDeclaration(node) { if (node.importKind === "type" || !isTimerModule(node.source)) return; for (const specifier of node.specifiers) { - if (specifier.type !== "ImportSpecifier") { - moduleBindings.add(specifier.local); - } else if ( - specifier.importKind !== "type" && - TIMERS.has(specifier.imported.name ?? specifier.imported.value) - ) { + const imported = + specifier.type === "ImportSpecifier" + ? (specifier.imported.name ?? specifier.imported.value) + : null; + // A namespace or default import, or `import { default as timers }`. + if (imported === null || imported === "default") { + if (specifier.importKind !== "type") moduleBindings.add(specifier.local); + } else if (specifier.importKind !== "type" && TIMERS.has(imported)) { timerBindings.add(specifier.local); } } @@ -987,6 +997,7 @@ const localPlugin = { !object.computed && object.object.type === "Identifier" && (object.object.name === "globalThis" || object.object.name === "global") && + !isDeclaredLocally(context.sourceCode, object.object) && object.property.name === "Date"); if ( left.type === "MemberExpression" && diff --git a/scripts/no-raw-clock-lint.test.ts b/scripts/no-raw-clock-lint.test.ts index a1660e06643..088e96fc2fe 100644 --- a/scripts/no-raw-clock-lint.test.ts +++ b/scripts/no-raw-clock-lint.test.ts @@ -107,6 +107,19 @@ test("passes a local name that shadows an imported timer", () => { ); }); +test("passes a local named global or globalThis", () => { + expect(rawClock(`function read(global: G) {\n return global.Date.now();\n}`)).toEqual([]); + expect(rawClock(`function arm(globalThis: G) {\n globalThis.setTimeout(f, 1);\n}`)).toEqual([]); + const patch = (code: string) => ids("no-global-clock-patch", code); + expect(patch(`function fake(global: G) {\n global.Date.now = () => 1;\n}`)).toEqual([]); +}); + +test("reports timers from a named default import of a timers module", () => { + expect( + rawClock(`import { default as timers } from "node:timers";\ntimers.setTimeout(fire, 10);`) + ).toEqual(["raw"]); +}); + test("reports Node's global object like globalThis", () => { expect(rawClock(`const t = global.Date.now();`)).toEqual(["raw"]); expect(rawClock(`global.setTimeout(() => undefined, 10);`)).toEqual(["raw"]); From 8d1c71052e6c5648a25bbedb1855ce98c146a063 Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Sun, 25 Oct 2026 17:37:34 +0000 Subject: [PATCH 4/6] tests: resolve Date and AbortSignal by binding, and count await import() and static computed timer members --- eslint-local-rules.mjs | 115 ++++++++++++------------------ scripts/no-raw-clock-lint.test.ts | 26 +++++++ 2 files changed, 72 insertions(+), 69 deletions(-) diff --git a/eslint-local-rules.mjs b/eslint-local-rules.mjs index e5868ab7082..6ef3b9a6674 100644 --- a/eslint-local-rules.mjs +++ b/eslint-local-rules.mjs @@ -86,6 +86,29 @@ function isDeclaredLocally(sourceCode, identifier) { return (resolveVariable(sourceCode, identifier)?.defs.length ?? 0) > 0; } +/** The property name of `a.b` or of the static `a["b"]`; null for any other member. */ +function memberName(node) { + if (node.type !== "MemberExpression") return null; + if (!node.computed) return node.property.name ?? null; + return node.property.type === "Literal" && typeof node.property.value === "string" + ? node.property.value + : null; +} + +/** + * The built-in global `name`: the bare name when no parameter, import or local declares it, or + * a member of the global object (`globalThis.name`, Node's `global.name`, or `global["name"]`). + */ +function isHostGlobal(sourceCode, node, name) { + if (node.type === "Identifier") return node.name === name && !isDeclaredLocally(sourceCode, node); + return ( + memberName(node) === name && + node.object.type === "Identifier" && + (node.object.name === "globalThis" || node.object.name === "global") && + !isDeclaredLocally(sourceCode, node.object) + ); +} + function variableDeclarator(variable) { if (variable.defs.length !== 1) { return null; @@ -806,25 +829,16 @@ const localPlugin = { source?.type === "Literal" && typeof source.value === "string" && TIMER_MODULES.has(source.value.replace(/^node:/, "")); - const isRequireOfTimers = (node) => - node?.type === "CallExpression" && - node.callee.type === "Identifier" && - node.callee.name === "require" && - isTimerModule(node.arguments[0]); - // `globalThis` or Node's `global`: both name the global object, unless a parameter or - // local declares that name. - const isGlobalObject = (node) => - node.type === "Identifier" && - (node.name === "globalThis" || node.name === "global") && - !isDeclaredLocally(context.sourceCode, node); - const globalMember = (node, name) => - node.type === "MemberExpression" && - !node.computed && - isGlobalObject(node.object) && - node.property.name === name; - // `Date`, `globalThis.Date` or `global.Date`: each names the host clock. - const isDate = (node) => - (node.type === "Identifier" && node.name === "Date") || globalMember(node, "Date"); + // `require("node:timers")` or `await import("node:timers")`. + const loadsTimers = (node) => + (node?.type === "CallExpression" && + node.callee.type === "Identifier" && + node.callee.name === "require" && + isTimerModule(node.arguments[0])) || + (node?.type === "AwaitExpression" && + node.argument.type === "ImportExpression" && + isTimerModule(node.argument.source)); + const hostGlobal = (node, name) => isHostGlobal(context.sourceCode, node, name); // A call whose callee needs its binding checked at the end: `id` must resolve to one of // `bindings`, so a parameter or local that shadows the import is not a timer. const viaBinding = (node, what, id, bindings) => @@ -859,7 +873,7 @@ const localPlugin = { } }, VariableDeclarator(node) { - if (!isRequireOfTimers(node.init)) return; + if (!loadsTimers(node.init)) return; if (node.id.type === "Identifier") { moduleBindings.add(node.id); return; @@ -878,52 +892,26 @@ const localPlugin = { }, CallExpression(node) { const callee = node.callee; - if ( - callee.type === "MemberExpression" && - !callee.computed && - isDate(callee.object) && - callee.property.name === "now" - ) { + const member = memberName(callee); + const hostTimer = [...TIMERS].find((timer) => hostGlobal(callee, timer)); + if (member === "now" && hostGlobal(callee.object, "Date")) { found.push({ node, what: "Date.now()" }); - } else if (isDate(callee)) { + } else if (hostGlobal(callee, "Date")) { // `Date()` without `new` returns the current time as a string. found.push({ node, what: "Date()" }); - } else if (callee.type === "Identifier" && TIMERS.has(callee.name)) { - found.push({ node, what: `${callee.name}()` }); + } else if (hostTimer !== undefined) { + found.push({ node, what: `${hostTimer}()` }); } else if (callee.type === "Identifier") { viaBinding(node, `${callee.name}()`, callee, timerBindings); - } else if ( - callee.type === "MemberExpression" && - !callee.computed && - TIMERS.has(callee.property.name) && - isGlobalObject(callee.object) - ) { - found.push({ node, what: `${callee.object.name}.${callee.property.name}()` }); - } else if ( - callee.type === "MemberExpression" && - !callee.computed && - TIMERS.has(callee.property.name) && - callee.object.type === "Identifier" - ) { - viaBinding( - node, - `${callee.object.name}.${callee.property.name}()`, - callee.object, - moduleBindings - ); - } else if ( - callee.type === "MemberExpression" && - !callee.computed && - callee.property.name === "timeout" && - ((callee.object.type === "Identifier" && callee.object.name === "AbortSignal") || - globalMember(callee.object, "AbortSignal")) - ) { + } else if (TIMERS.has(member) && callee.object.type === "Identifier") { + viaBinding(node, `${callee.object.name}.${member}()`, callee.object, moduleBindings); + } else if (member === "timeout" && hostGlobal(callee.object, "AbortSignal")) { // A host timer that aborts the signal: the TestClock cannot reach it. found.push({ node, what: "AbortSignal.timeout()" }); } }, NewExpression(node) { - if (isDate(node.callee) && node.arguments.length === 0) { + if (hostGlobal(node.callee, "Date") && node.arguments.length === 0) { found.push({ node, what: "new Date()" }); } }, @@ -991,20 +979,9 @@ const localPlugin = { AssignmentExpression(node) { const left = node.left; // `Date`, `globalThis.Date` or Node's `global.Date`: one process-wide clock. - const isDateObject = (object) => - (object.type === "Identifier" && object.name === "Date") || - (object.type === "MemberExpression" && - !object.computed && - object.object.type === "Identifier" && - (object.object.name === "globalThis" || object.object.name === "global") && - !isDeclaredLocally(context.sourceCode, object.object) && - object.property.name === "Date"); if ( - left.type === "MemberExpression" && - isDateObject(left.object) && - (left.computed - ? left.property.type === "Literal" && left.property.value === "now" - : left.property.name === "now") + memberName(left) === "now" && + isHostGlobal(context.sourceCode, left.object, "Date") ) { context.report({ node, messageId: "patch", data: { what: "Date.now = ..." } }); } diff --git a/scripts/no-raw-clock-lint.test.ts b/scripts/no-raw-clock-lint.test.ts index 088e96fc2fe..07a0f14c5f4 100644 --- a/scripts/no-raw-clock-lint.test.ts +++ b/scripts/no-raw-clock-lint.test.ts @@ -114,6 +114,32 @@ test("passes a local named global or globalThis", () => { expect(patch(`function fake(global: G) {\n global.Date.now = () => 1;\n}`)).toEqual([]); }); +test("passes locals named Date or AbortSignal", () => { + expect(rawClock(`function render(Date: () => string) {\n return Date();\n}`)).toEqual([]); + expect(rawClock(`function f(Date: D) {\n return Date.now() + new Date().getTime();\n}`)).toEqual( + [] + ); + expect(rawClock(`function f(AbortSignal: S) {\n return AbortSignal.timeout(10);\n}`)).toEqual( + [] + ); + const patch = (code: string) => ids("no-global-clock-patch", code); + expect(patch(`function fake(Date: D) {\n Date.now = () => 1;\n}`)).toEqual([]); +}); + +test("reports timers loaded by await import() and static computed members", () => { + expect( + rawClock(`const timers = await import("node:timers");\ntimers.setTimeout(fire, 10);`) + ).toEqual(["raw"]); + expect( + rawClock(`const { setTimeout: delay } = await import("timers/promises");\nawait delay(10);`) + ).toEqual(["raw"]); + expect( + rawClock(`import * as timers from "node:timers";\ntimers["setTimeout"](fire, 10);`) + ).toEqual(["raw"]); + expect(rawClock(`globalThis["setTimeout"](fire, 10);`)).toEqual(["raw"]); + expect(rawClock(`const t = global["Date"].now();`)).toEqual(["raw"]); +}); + test("reports timers from a named default import of a timers module", () => { expect( rawClock(`import { default as timers } from "node:timers";\ntimers.setTimeout(fire, 10);`) From 6a0dfcc45db6ab0f5f5d5cea7d58e263cccb962a Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Sun, 25 Oct 2026 17:40:24 +0000 Subject: [PATCH 5/6] tests: document the limits of local/no-raw-clock --- eslint-local-rules.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/eslint-local-rules.mjs b/eslint-local-rules.mjs index 6ef3b9a6674..f0082eeb8e0 100644 --- a/eslint-local-rules.mjs +++ b/eslint-local-rules.mjs @@ -787,6 +787,10 @@ const localPlugin = { }; }, }, + // Limits: this rule catches direct spellings (Date.now(), globalThis.setTimeout, + // timers imports, require("timers")) and skips locals that shadow those names. + // It does not follow aliases through variables, destructuring, Reflect, eval or + // other dynamic access, so `const d = Date; d.now()` passes. Review catches those. "no-raw-clock": { meta: { type: "problem", From 25dd4e69cc66746ad95700b250f5ba3191f80394 Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Sun, 25 Oct 2026 18:23:21 +0000 Subject: [PATCH 6/6] tests: let type-only declarations keep the host clock global, and resolve require by binding --- eslint-local-rules.mjs | 10 +++++++--- scripts/no-raw-clock-lint.test.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/eslint-local-rules.mjs b/eslint-local-rules.mjs index f0082eeb8e0..b93c30dbdaf 100644 --- a/eslint-local-rules.mjs +++ b/eslint-local-rules.mjs @@ -81,9 +81,13 @@ function resolveVariable(sourceCode, identifier) { return null; } -/** True when a parameter, import or local declares the identifier's name in its scope chain. */ +/** + * True when a parameter, import or local declares the identifier's name as a value in its scope + * chain. A type-only declaration (`interface Date`, `type AbortSignal = ...`) does not count: + * TypeScript keeps types and values apart, so `Date.now()` still reads the host global. + */ function isDeclaredLocally(sourceCode, identifier) { - return (resolveVariable(sourceCode, identifier)?.defs.length ?? 0) > 0; + return resolveVariable(sourceCode, identifier)?.defs.some((def) => def.type !== "Type") ?? false; } /** The property name of `a.b` or of the static `a["b"]`; null for any other member. */ @@ -836,8 +840,8 @@ const localPlugin = { // `require("node:timers")` or `await import("node:timers")`. const loadsTimers = (node) => (node?.type === "CallExpression" && + isHostGlobal(context.sourceCode, node.callee, "require") && node.callee.type === "Identifier" && - node.callee.name === "require" && isTimerModule(node.arguments[0])) || (node?.type === "AwaitExpression" && node.argument.type === "ImportExpression" && diff --git a/scripts/no-raw-clock-lint.test.ts b/scripts/no-raw-clock-lint.test.ts index 07a0f14c5f4..f588e6699fc 100644 --- a/scripts/no-raw-clock-lint.test.ts +++ b/scripts/no-raw-clock-lint.test.ts @@ -126,6 +126,24 @@ test("passes locals named Date or AbortSignal", () => { expect(patch(`function fake(Date: D) {\n Date.now = () => 1;\n}`)).toEqual([]); }); +test("reports host clock reads next to a type-only declaration of the same name", () => { + // TypeScript keeps types and values apart: `interface Date` does not shadow the value `Date`. + expect(rawClock(`interface Date {\n x: number;\n}\nconst t = Date.now();`)).toEqual(["raw"]); + expect(rawClock(`type AbortSignal = number;\nconst s = AbortSignal.timeout(10);`)).toEqual([ + "raw", + ]); + const patch = (code: string) => ids("no-global-clock-patch", code); + expect(patch(`interface Date {\n x: number;\n}\nDate.now = () => 1;`)).toEqual(["patch"]); +}); + +test("passes a local named require", () => { + expect( + rawClock( + `function f(require: (id: string) => T) {\n const timers = require("node:timers");\n return timers.setTimeout(fire, 10);\n}` + ) + ).toEqual([]); +}); + test("reports timers loaded by await import() and static computed members", () => { expect( rawClock(`const timers = await import("node:timers");\ntimers.setTimeout(fire, 10);`)