mirror of
https://github.com/morluto/rea
synced 2026-10-28 06:46:47 -04:00
fix(javascript): retain static paths through TypeScript wrappers (#3998)
Normalize erased TypeScript wrappers through existing expression/string owners for static paths, call targets, collection/configuration receivers and global aliases. Preserve binding/mutation guards and explicit unknown targets, with CLI and application-boundary regressions. Original contribution by @ShuaixinHuang. Co-authored-by: ShuaixinHuang <1836656514@qq.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import { parseExpression } from "@babel/parser";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
|
||||
import { parserPluginsForPath } from "./javascriptSourceParser.js";
|
||||
import {
|
||||
staticPath,
|
||||
staticPathResolutionContext,
|
||||
@@ -46,8 +47,13 @@ const anchoredExpressions = [
|
||||
] as const;
|
||||
|
||||
const unknownExpressions = [
|
||||
"runtimePath as string",
|
||||
"path.join(__dirname as string, runtimeFile as string) as string",
|
||||
'path[method](__dirname, "preload.js")',
|
||||
'path[`join${suffix}`](__dirname, "preload.js")',
|
||||
'(path[method] as typeof path.join)(__dirname, "preload.js")',
|
||||
'((useJoin ? path.join : path.resolve) as typeof path.join)(__dirname, "preload.js")',
|
||||
'fileURLToPath(new (runtimeURL as typeof URL)("./preload.js", import.meta.url))',
|
||||
"path.resolve()",
|
||||
"path.posix.resolve()",
|
||||
'path.resolve("sub", "../preload.js")',
|
||||
@@ -69,7 +75,101 @@ const unknownExpressions = [
|
||||
'path.dirname(__filename) + "preload.js"',
|
||||
];
|
||||
|
||||
const pathValueExpressions = [
|
||||
['"./preload.js" as const', "./preload.js", "module-specifier"],
|
||||
[
|
||||
'path.join(__dirname, "preload.js") as string',
|
||||
"preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
[
|
||||
'path.join(__dirname as string, "preload.js" as const)',
|
||||
"preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
[
|
||||
'path.resolve(__dirname as string, "preload.js" as const)',
|
||||
"preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
[
|
||||
'fileURLToPath(new URL("./preload.js", import.meta.url as string) as URL) as string',
|
||||
"./preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
[
|
||||
'path.join(path.dirname(__filename as string), "preload.js")',
|
||||
"preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
[
|
||||
'path.join(path.dirname(__filename) as string, "preload.js")',
|
||||
"preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
[
|
||||
'((__dirname + "/lib") as string) + "/preload.js"',
|
||||
"lib/preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
['"/app/preload.js"', "/app/preload.js", "filesystem-expression"],
|
||||
["`/app/preload.js`", "/app/preload.js", "filesystem-expression"],
|
||||
[
|
||||
'path.join(`/app`, "preload.js")',
|
||||
"/app/preload.js",
|
||||
"filesystem-expression",
|
||||
],
|
||||
["`./preload.js`", "./preload.js", "module-specifier"],
|
||||
] as const;
|
||||
|
||||
const pathCallTargetExpressions = [
|
||||
['(path.join as typeof path.join)(__dirname, "preload.js")', "preload.js"],
|
||||
['path.join!(__dirname, "preload.js")', "preload.js"],
|
||||
[
|
||||
'(path.resolve satisfies typeof path.resolve)(__dirname, "preload.js")',
|
||||
"preload.js",
|
||||
],
|
||||
[
|
||||
'((path as typeof path).posix as typeof path.posix).resolve("/app", "preload.js")',
|
||||
"/app/preload.js",
|
||||
],
|
||||
[
|
||||
'(fileURLToPath as typeof fileURLToPath)(new URL("./preload.js", import.meta.url))',
|
||||
"./preload.js",
|
||||
],
|
||||
[
|
||||
'fileURLToPath(new (URL as typeof URL)("./preload.js", import.meta.url))',
|
||||
"./preload.js",
|
||||
],
|
||||
[
|
||||
'fileURLToPath(new (url.URL as typeof URL)("./preload.js", import.meta.url))',
|
||||
"./preload.js",
|
||||
],
|
||||
[
|
||||
'path.join((path.dirname as typeof path.dirname)(__filename), "preload.js")',
|
||||
"preload.js",
|
||||
],
|
||||
[
|
||||
'path.join(dirname((fileURLToPath as typeof fileURLToPath)(import.meta.url)), "preload.js")',
|
||||
"preload.js",
|
||||
],
|
||||
] as const;
|
||||
|
||||
describe("inert JavaScript path resolution", () => {
|
||||
it.each(pathCallTargetExpressions)(
|
||||
"preserves exact path values and context through call target %s",
|
||||
(expression, path) => {
|
||||
assertPathProjection(expression, path, "filesystem-expression");
|
||||
},
|
||||
);
|
||||
|
||||
it.each(pathValueExpressions)(
|
||||
"preserves exact path values and context through %s",
|
||||
(expression, path, context) => {
|
||||
assertPathProjection(expression, path, context);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(absoluteParts)(
|
||||
"matches the POSIX resolve oracle for %j",
|
||||
(...parts) => {
|
||||
@@ -141,9 +241,7 @@ describe("inert JavaScript path resolution", () => {
|
||||
});
|
||||
|
||||
it.each(unknownExpressions)("keeps unsupported %s unknown", (expression) => {
|
||||
expect(
|
||||
staticPath(parseExpression(expression, { sourceType: "module" })),
|
||||
).toBeUndefined();
|
||||
expect(staticPath(parsePathExpression(expression))).toBeUndefined();
|
||||
const { analysis, windowExpression, utilityExpression } =
|
||||
analyzePathConsumers(expression);
|
||||
expect(analysis.parse_status).toBe("partial");
|
||||
@@ -210,9 +308,7 @@ describe("inert JavaScript path resolution", () => {
|
||||
'path.join("/app", runtimeFile)',
|
||||
"path.join(...parts)",
|
||||
])("preserves unresolved join syntax in %s", (expression) =>
|
||||
expect(
|
||||
staticPath(parseExpression(expression, { sourceType: "module" })),
|
||||
).toBeUndefined(),
|
||||
expect(staticPath(parsePathExpression(expression))).toBeUndefined(),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -237,11 +333,15 @@ it.each([
|
||||
'fileURLToPath("file:///app/preload.js", options)',
|
||||
"fileURLToPath(selectedUrl)",
|
||||
])("leaves platform-dependent or invalid %s unknown", (expression) =>
|
||||
expect(
|
||||
staticPath(parseExpression(expression, { sourceType: "module" })),
|
||||
).toBeUndefined(),
|
||||
expect(staticPath(parsePathExpression(expression))).toBeUndefined(),
|
||||
);
|
||||
|
||||
const parsePathExpression = (expression: string) =>
|
||||
parseExpression(expression, {
|
||||
sourceType: "module",
|
||||
plugins: parserPluginsForPath("preload.ts"),
|
||||
});
|
||||
|
||||
const analyzePathConsumers = (expression: string) => {
|
||||
const property = `preload: ${expression}`;
|
||||
const genericPrefix = "const options = { ";
|
||||
@@ -265,7 +365,7 @@ const assertPathProjection = (
|
||||
path: string,
|
||||
context: JavaScriptStaticPathContext,
|
||||
) => {
|
||||
const node = parseExpression(expression, { sourceType: "module" });
|
||||
const node = parsePathExpression(expression);
|
||||
expect(staticPath(node)).toBe(path);
|
||||
expect(staticPathResolutionContext(node)).toBe(context);
|
||||
const {
|
||||
|
||||
Reference in New Issue
Block a user