fix(semantics): preserve empty string property keys (#764)
Signed-off-by: Rudy Celekli <rudy@gradiahq.com>
This commit is contained in:
@@ -310,3 +310,42 @@ it.each([
|
||||
).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
"const routes = {'': 'HOME'}; const root = routes[''];",
|
||||
"const routes = {'': 'HOME'}; const {'': root} = routes;",
|
||||
])(
|
||||
"preserves empty property identities with valid display labels: %s",
|
||||
(source) => {
|
||||
const graph = graphFor(source);
|
||||
expect(graph.nodes).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
kind: "property-slot",
|
||||
label: '""',
|
||||
properties: expect.objectContaining({ name: "" }),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const root = graph.nodes.find(
|
||||
({ kind, label }) => kind === "binding" && label === "root",
|
||||
);
|
||||
const literal = graph.nodes.find(
|
||||
({ kind, identity, properties }) =>
|
||||
kind === "literal" &&
|
||||
properties.value === "HOME" &&
|
||||
identity.role_key.includes(":binding:root:"),
|
||||
);
|
||||
expect(literal).toBeDefined();
|
||||
expect(graph.relations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
source_node_id: literal?.node_id,
|
||||
target_node_id: root?.node_id,
|
||||
relation: "defines",
|
||||
resolution: "resolved",
|
||||
}),
|
||||
]),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -116,7 +116,7 @@ export const semanticPropertySlot = (
|
||||
kind: "property-slot",
|
||||
roleKey: `property:${objectBindingId}:${name}`,
|
||||
location: null,
|
||||
label: name,
|
||||
label: name === "" ? '""' : name,
|
||||
functionNodeId:
|
||||
context.bindingNodes.get(objectBindingId)?.function_node_id ?? null,
|
||||
properties: {
|
||||
|
||||
@@ -12,7 +12,14 @@ export const propertyName = (node: t.Node): string => {
|
||||
export const semanticStaticPropertyName = (
|
||||
property: t.Node,
|
||||
computed: boolean,
|
||||
): string =>
|
||||
computed && !t.isStringLiteral(property) && !t.isNumericLiteral(property)
|
||||
? ""
|
||||
: propertyName(property);
|
||||
): string => semanticStaticPropertyKey(property, computed) ?? "";
|
||||
|
||||
/** Read an exact property key, preserving the legal empty-string key. */
|
||||
export const semanticStaticPropertyKey = (
|
||||
property: t.Node,
|
||||
computed: boolean,
|
||||
): string | null => {
|
||||
if (t.isStringLiteral(property) || t.isNumericLiteral(property))
|
||||
return String(property.value);
|
||||
return !computed && t.isIdentifier(property) ? property.name : null;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
it.each([
|
||||
"const routes = {'': 'HOME'}; const root = routes[''];",
|
||||
"const routes = {['']: 'HOME'}; const root = routes[''];",
|
||||
"const routes = {'': 'HOME'}; const {'': root} = routes;",
|
||||
"const routes = {'': 'HOME'}; const {['']: root} = routes;",
|
||||
"const routes = {home: 'HOME'}; const root = routes.home;",
|
||||
])("preserves exact string keys in %s", (source) => {
|
||||
const ir = analyzeJavaScriptSemantics(source);
|
||||
expect(ir.bindings.find(({ name }) => name === "root")?.value).toEqual({
|
||||
status: "literal",
|
||||
value: "HOME",
|
||||
});
|
||||
expect(
|
||||
ir.frontiers.filter(({ kind }) => kind === "dynamic-property"),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps a computed identifier key unresolved", () => {
|
||||
const ir = analyzeJavaScriptSemantics(
|
||||
"const routes = {'': 'HOME'}; const root = routes[key];",
|
||||
);
|
||||
expect(ir.bindings.find(({ name }) => name === "root")?.value.status).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(ir.frontiers).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ kind: "dynamic-property" }),
|
||||
]),
|
||||
);
|
||||
});
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
collectSemanticReferences,
|
||||
immutableSemanticBindings,
|
||||
immutableSemanticScopes,
|
||||
semanticStaticPropertyName,
|
||||
semanticStaticPropertyKey,
|
||||
} from "./javascriptSemanticProjection.js";
|
||||
import type {
|
||||
JavaScriptSemanticAnalysisState,
|
||||
@@ -481,14 +481,11 @@ const bindPattern = (input: BindPatternInput): void => {
|
||||
projection: [],
|
||||
});
|
||||
else {
|
||||
const name = semanticStaticPropertyName(
|
||||
property.key,
|
||||
property.computed,
|
||||
);
|
||||
const name = semanticStaticPropertyKey(property.key, property.computed);
|
||||
bindPattern({
|
||||
...input,
|
||||
pattern: property.value,
|
||||
projection: [...projection, name === "" ? null : name],
|
||||
projection: [...projection, name],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import type {
|
||||
import {
|
||||
semanticCallableIdForNode,
|
||||
semanticReferenceRole,
|
||||
semanticStaticPropertyName,
|
||||
semanticStaticPropertyKey,
|
||||
} from "./javascriptSemanticProjection.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
@@ -336,7 +336,7 @@ const collectDynamicProperty = (
|
||||
): void => {
|
||||
if (
|
||||
(t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) &&
|
||||
semanticStaticPropertyName(node.property, node.computed) === ""
|
||||
semanticStaticPropertyKey(node.property, node.computed) === null
|
||||
)
|
||||
addFrontier(
|
||||
{
|
||||
@@ -351,7 +351,7 @@ const collectDynamicProperty = (
|
||||
else if (
|
||||
(t.isObjectProperty(node) || t.isObjectMethod(node)) &&
|
||||
node.computed &&
|
||||
semanticStaticPropertyName(node.key, true) === ""
|
||||
semanticStaticPropertyKey(node.key, true) === null
|
||||
)
|
||||
addFrontier(
|
||||
{
|
||||
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
type JavaScriptSemanticBindingState,
|
||||
} from "./javascriptSemanticState.js";
|
||||
import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js";
|
||||
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
|
||||
import {
|
||||
semanticStaticPropertyKey,
|
||||
semanticStaticPropertyName,
|
||||
} from "./javascriptAstValues.js";
|
||||
|
||||
export interface SemanticProjectedBinding {
|
||||
readonly bindingId: string;
|
||||
@@ -96,12 +99,11 @@ const projectedPatternBindings = (
|
||||
property.value,
|
||||
[
|
||||
...path,
|
||||
semanticStaticPropertyName(property.key, property.computed) ||
|
||||
null,
|
||||
semanticStaticPropertyKey(property.key, property.computed),
|
||||
],
|
||||
partial ||
|
||||
semanticStaticPropertyName(property.key, property.computed) ===
|
||||
"",
|
||||
semanticStaticPropertyKey(property.key, property.computed) ===
|
||||
null,
|
||||
);
|
||||
}
|
||||
} else if (t.isArrayPattern(current)) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import type {
|
||||
import {
|
||||
semanticCallableIdForNode,
|
||||
semanticReadsBeforeWrite,
|
||||
semanticStaticPropertyName,
|
||||
semanticStaticPropertyKey,
|
||||
} from "./javascriptSemanticProjection.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
@@ -75,8 +75,8 @@ const collectMember = (
|
||||
context: ObjectCollectionContext,
|
||||
): void => {
|
||||
const { state, output } = context;
|
||||
const name = semanticStaticPropertyName(node.property, node.computed);
|
||||
if (name.length === 0) return;
|
||||
const name = semanticStaticPropertyKey(node.property, node.computed);
|
||||
if (name === null) return;
|
||||
const write =
|
||||
(t.isAssignmentExpression(parent) && parent.left === node) ||
|
||||
(t.isUpdateExpression(parent) && parent.argument === node);
|
||||
@@ -118,8 +118,8 @@ const collectDestructuring = (
|
||||
const objectBindingId = expressionBindingId(node.init, state);
|
||||
for (const property of node.id.properties) {
|
||||
if (!t.isObjectProperty(property)) continue;
|
||||
const name = semanticStaticPropertyName(property.key, property.computed);
|
||||
if (name.length === 0) continue;
|
||||
const name = semanticStaticPropertyKey(property.key, property.computed);
|
||||
if (name === null) continue;
|
||||
const target = bindingIdentifier(property.value);
|
||||
addObjectOperation(
|
||||
{
|
||||
|
||||
@@ -25,7 +25,10 @@ import {
|
||||
range,
|
||||
stringValue,
|
||||
} from "./javascriptStaticAnalysisHelpers.js";
|
||||
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
|
||||
import {
|
||||
semanticStaticPropertyKey,
|
||||
semanticStaticPropertyName,
|
||||
} from "./javascriptAstValues.js";
|
||||
|
||||
interface CollectCallableInput {
|
||||
readonly node: t.Node;
|
||||
@@ -202,7 +205,10 @@ const semanticModuleOrigin = (
|
||||
};
|
||||
};
|
||||
|
||||
export { semanticStaticPropertyName } from "./javascriptAstValues.js";
|
||||
export {
|
||||
semanticStaticPropertyKey,
|
||||
semanticStaticPropertyName,
|
||||
} from "./javascriptAstValues.js";
|
||||
|
||||
const callableKind = (
|
||||
node: t.Node,
|
||||
@@ -635,5 +641,4 @@ const commonJsExportName = (
|
||||
/** Exact member key, or null when a computed key commits no name. */
|
||||
const memberKey = (
|
||||
node: t.MemberExpression | t.OptionalMemberExpression,
|
||||
): string | null =>
|
||||
semanticStaticPropertyName(node.property, node.computed) || null;
|
||||
): string | null => semanticStaticPropertyKey(node.property, node.computed);
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from "./javascriptSemanticState.js";
|
||||
import { stringValue } from "./javascriptStaticAnalysisHelpers.js";
|
||||
import { compareCodePoints } from "./canonicalOrdering.js";
|
||||
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
|
||||
import { semanticStaticPropertyKey } from "./javascriptAstValues.js";
|
||||
import {
|
||||
semanticAmbiguousProvenance,
|
||||
semanticLocalProvenance,
|
||||
@@ -170,8 +170,8 @@ const evaluateObject = (
|
||||
invalidateEarlierProperties();
|
||||
continue;
|
||||
}
|
||||
const name = semanticStaticPropertyName(property.key, property.computed);
|
||||
if (name === "" && !t.isStringLiteral(property.key, { value: "" })) {
|
||||
const name = semanticStaticPropertyKey(property.key, property.computed);
|
||||
if (name === null) {
|
||||
unknownProperties = true;
|
||||
if (omittedProperties !== null) omittedProperties += 1;
|
||||
invalidateEarlierProperties();
|
||||
@@ -491,8 +491,8 @@ const mergeValues = (
|
||||
const memberKey = (
|
||||
node: t.MemberExpression | t.OptionalMemberExpression,
|
||||
): string | number | undefined => {
|
||||
const name = semanticStaticPropertyName(node.property, node.computed);
|
||||
return name === "" ? undefined : name;
|
||||
const name = semanticStaticPropertyKey(node.property, node.computed);
|
||||
return name ?? undefined;
|
||||
};
|
||||
|
||||
const nestedContext = (
|
||||
|
||||
@@ -239,6 +239,39 @@ describe("rest parameter semantic trace CLI", () => {
|
||||
}, 20_000);
|
||||
});
|
||||
|
||||
describe("empty property key application CLI", () => {
|
||||
it.each(["const root = routes[''];", "const {'': root} = routes;"])(
|
||||
"analyzes a root-route dictionary with %s",
|
||||
async (read) => {
|
||||
const root = await createTestTempDirectory("rea-empty-key-cli-");
|
||||
await writeFile(
|
||||
join(root, "app.js"),
|
||||
`const routes = {'': 'HOME'}; ${read}`,
|
||||
);
|
||||
const evidence = await runCli([
|
||||
"analyze-javascript-application",
|
||||
root,
|
||||
"--json",
|
||||
]);
|
||||
expect(evidence).toMatchObject({
|
||||
operation: "analyze_javascript_application",
|
||||
normalized_result: {
|
||||
semantic_graph: {
|
||||
nodes: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
kind: "property-slot",
|
||||
label: '""',
|
||||
properties: expect.objectContaining({ name: "" }),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
20_000,
|
||||
);
|
||||
});
|
||||
|
||||
describe("application workflow CLI input", () => {
|
||||
it("rejects Evidence ID-only workflow inputs", async () => {
|
||||
const result = await runCli([
|
||||
|
||||
Reference in New Issue
Block a user