Files
rea/src/domain/javascript/javascriptStaticAnalysisCalleeBoundary.test.ts
T
Kaoru0822andClaude Opus 5.5 af6a998066 fix(javascript): keep string Worker paths unresolved without a document base (#3797)
A plain new Worker("./w.js") or navigator.serviceWorker.register("./sw.js")
resolves against the document base URL in a browser, and a Node
worker_threads string resolves against the working directory. Static
analysis was resolving browser strings against the calling script, which
recorded a resolved mapping to the wrong file.

Only new Worker(new URL(path, import.meta.url)) now produces a specifier.
Other worker and service-worker strings keep their expression and stay
unresolved, matching the existing worker_threads string handling.

Fixes #3309

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 19:13:17 -07:00

43 lines
1.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
describe("static callee boundary matching", () => {
it("matches complete callee members without suffix collisions", () => {
const analysis = analyzeJavaScriptStaticSource(`
win.loadFile("./window.html");
BrowserWindow.loadURL("https://app.example.test");
navigator.serviceWorker.register("./worker.js");
loadFile("./bare.html");
win?.loadURL("https://optional.example.test");
uploadFile("./upload.html");
preloadFile("./preload.html");
downloadURL("https://download.example.test");
reloadURL("https://reload.example.test");
notloadURL("https://other.example.test");
myserviceWorker.register("./not-worker.js");
`);
expect(
analysis.role_paths.map(({ path, mechanism }) => [path, mechanism]),
).toEqual([
["./window.html", "call:win.loadFile"],
["https://app.example.test", "call:BrowserWindow.loadURL"],
["./bare.html", "call:loadFile"],
["https://optional.example.test", "call:win.loadURL"],
]);
// The document base URL is unknown, so the script path stays unresolved.
expect(
analysis.references
.filter(({ kind }) => kind === "service-worker")
.map(({ specifier, expression }) => [specifier, expression]),
).toEqual([[null, 'navigator.serviceWorker.register("./worker.js")']]);
expect(
analysis.endpoints
.filter(({ mechanism }) => mechanism.endsWith("loadURL"))
.map(({ value }) => value),
).toEqual(["https://app.example.test", "https://optional.example.test"]);
});
});