test(javascript): preserve exact empty property keys in declarations

This commit is contained in:
morluto
2026-10-24 03:05:35 +08:00
parent e8374f4e58
commit b4502be51a
+22
View File
@@ -4,6 +4,28 @@ import { expect, it } from "vitest";
import { parseJavaScriptSource } from "./javascript/javascriptSourceParser.js";
import { objectString } from "./webBundleAnalyzerAst.js";
it.each([
{ property: "[``]: 0", expected: "catalog" },
{ property: '"": 0', expected: "catalog" },
{ property: '[""]: 0', expected: "catalog" },
{ property: "[`other`]: 0", expected: "catalog" },
{ property: "[unknownKey]: 0", expected: undefined },
])(
"preserves WebMCP identity beside exact unrelated keys: $property",
({ property, expected }) => {
const file = parseJavaScriptSource(
`const declaration = {name:"catalog", ${property}};`,
);
const declaration = file?.program.body[0];
if (!t.isVariableDeclaration(declaration))
throw new Error("Expected variable declaration");
const object = declaration.declarations[0]?.init;
if (!t.isObjectExpression(object))
throw new Error("Expected object expression");
expect(objectString(object, "name")).toBe(expected);
},
);
it("reads WebMCP declaration strings through the exact-literal owner", () => {
const file = parseJavaScriptSource(
"const declaration = { name: `catalog`, description: `Browse catalog` }; const dynamic = { name: `catalog ${scope}` };",