feat(electron): resolve stable local constant window options

Resolve stable local constant BrowserWindow options with retained provenance and shared early-exit mutation queries. Preserve source ordering and unknown results for relevant mutations or escapes.

Includes real Joplin correctness/cost receipts and regression coverage.

Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
YsopwnableandKaoru0822 authored and GitHub committed 2026-10-16 14:52:33 +08:00
1 parent 5a77acfbcd
commit d72d856f58
20 files changed
+4544 -64

No files matched your search

@@ -2,6 +2,128 @@ import { describe, expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
describe("Electron constant option evidence", () => {
it("keeps mixed constant, inline and unresolved windows in construction order", () => {
const analysis = analyzeJavaScriptStaticSource(
[
'const { BrowserWindow } = require("electron");',
"const options = { webPreferences: { sandbox: true } };",
"new BrowserWindow(options);",
"new BrowserWindow({ webPreferences: { sandbox: false } });",
"new BrowserWindow(dynamic);",
"new BrowserWindow(options);",
].join("\n"),
);
expect(
analysis.electron.browser_windows.map(
({ location }) => location.start.line,
),
).toEqual([3, 4, 5, 6]);
expect(
analysis.electron.browser_windows.map(
({ options_status }) => options_status,
),
).toEqual([
"object-literal",
"object-literal",
"dynamic",
// The first constructor already received this object; later contents
// remain unknown even though the declarations retain their source order.
"dynamic",
]);
});
it("resolves local constant options despite an unrelated option write", () => {
const analysis = analyzeJavaScriptStaticSource(`
const { BrowserWindow } = require("electron");
function open() {
const preferences = { nodeIntegration: true, contextIsolation: false };
const options = { webPreferences: preferences };
if (linux) options.icon = "icon.png";
new BrowserWindow(options);
}
`);
expect(analysis.electron.browser_windows).toMatchObject([
{
options_status: "object-literal",
web_preferences_status: "object-literal",
web_preferences: [
{
name: "contextIsolation",
value: { status: "literal", value: false },
},
{
name: "nodeIntegration",
value: { status: "literal", value: true },
},
],
resolution_sources: [expect.any(Object), expect.any(Object)],
},
]);
});
it.each([
"options.webPreferences.sandbox = false;",
"const alias = options.webPreferences; alias.sandbox = false;",
"mutate(options);",
"const { webPreferences: alias } = options; mutate(alias);",
"Object.assign(options, unknown);",
"const box = {}; box.options = options; box.options.webPreferences.sandbox = false;",
"options = replacement;",
])("keeps changed or escaped constant options unknown: %s", (mutation) => {
const analysis = analyzeJavaScriptStaticSource(`
const { BrowserWindow } = require("electron");
const options = { webPreferences: { sandbox: true } };
${mutation}
new BrowserWindow(options);
`);
expect(analysis.electron.browser_windows).toMatchObject([
{
options_status: "dynamic",
web_preferences_status: "dynamic",
web_preferences: [],
preload_path: null,
},
]);
});
it.each([
"new BrowserWindow(options); const options = { webPreferences: { sandbox: true } };",
"const options = alias; const alias = { webPreferences: { sandbox: true } }; new BrowserWindow(options);",
"const options = alias; const alias = options; new BrowserWindow(options);",
"const options = { webPreferences: { sandbox: true } }; { const options = unknown; new BrowserWindow(options); }",
"let options = { webPreferences: { sandbox: true } }; new BrowserWindow(options);",
])("does not invent a constant initializer: %s", (source) => {
const analysis = analyzeJavaScriptStaticSource(`
const { BrowserWindow } = require("electron"); ${source}
`);
expect(analysis.electron.browser_windows).toMatchObject([
{
options_status: "dynamic",
web_preferences_status: "dynamic",
web_preferences: [],
},
]);
});
it("preserves constants selected before later mutation and resolves preload context", () => {
const analysis = analyzeJavaScriptStaticSource(`
const { BrowserWindow } = require("electron");
const original = { webPreferences: { preload: require("path").join(__dirname, "preload.js"), sandbox: true } };
const options = original;
new BrowserWindow(options);
original.webPreferences.sandbox = false;
`);
expect(analysis.electron.browser_windows).toMatchObject([
{
options_status: "object-literal",
preload_path: "preload.js",
preload_resolution_context: "filesystem-expression",
},
]);
});
});
describe("Electron option evidence", () => {
it.each([
'({ webPreferences: { preload: "./preload.js", sandbox: true } } as const)',