fix(javascript): decode literal file URLs at the path boundary

Decode known literal file URLs with explicit path semantics before assigning filesystem-expression context. Preserve unknown drive, UNC, dynamic and malformed inputs and existing source-relative projection.

Validation: 64 path-owner and 26 filesystem application tests pass. Fixes #2561.

Based on #3147.

Co-authored-by: João Pedro de Faria <jpdefo7@hotmail.com>
This commit is contained in:
morluto
2026-10-14 13:58:13 +08:00
parent 66dcd94d09
commit 7e1c560dd6
3 changed files with 98 additions and 10 deletions
@@ -1,4 +1,5 @@
import { posix } from "node:path";
import { fileURLToPath } from "node:url";
import { parseExpression } from "@babel/parser";
import { describe, expect, it } from "vitest";
@@ -207,6 +208,32 @@ describe("inert JavaScript path resolution", () => {
);
});
it.each([
"file:///app/preload%20file.js",
"file:///app/%E2%9C%93.js",
"file:///app/preload.js?query=1#fragment",
])("decodes literal file URL %s as a filesystem expression", (url) => {
assertPathProjection(
`fileURLToPath(${JSON.stringify(url)})`,
fileURLToPath(url, { windows: false }),
"filesystem-expression",
);
});
it.each([
'fileURLToPath("file:///app/%2Fpreload.js")',
'fileURLToPath("file:///app/%5Cpreload.js")',
'fileURLToPath("file:///app/%")',
'fileURLToPath("file:///C:/app/preload.js")',
'fileURLToPath("file://remote/share/preload.js")',
'fileURLToPath("file:///app/preload.js", options)',
"fileURLToPath(selectedUrl)",
])("leaves platform-dependent or invalid %s unknown", (expression) =>
expect(
staticPath(parseExpression(expression, { sourceType: "module" })),
).toBeUndefined(),
);
const analyzePathConsumers = (expression: string) => {
const property = `preload: ${expression}`;
const genericPrefix = "const options = { ";