mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 21:53:51 +00:00
fix(javascript): preserve uncertainty after immutable pattern targets (#12673)
* fix(javascript): preserve uncertainty after immutable pattern targets * fix(javascript): retain self-name assignment rules in patterns
This commit is contained in:
1 parent
2c7c858c5a
commit
a5f19cbaf6
6 files changed
+268
-31
No files matched your search
@@ -1,5 +1,9 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { analyzeJavaScriptSemantics } from "../../domain/javascript/javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
analyzeJavaScriptSemantics,
|
||||
analyzeParsedJavaScriptSemantics,
|
||||
} from "../../domain/javascript/javascriptSemanticAnalysis.js";
|
||||
import { parseJavaScriptSource } from "../../domain/javascript/javascriptSourceParser.js";
|
||||
import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js";
|
||||
|
||||
it.each([
|
||||
@@ -2581,3 +2585,157 @@ it.each([
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"immutable array assignment target",
|
||||
'const fixed=0;let selected;const xs=[1,"one"];[fixed,selected]=xs;xs[1]="later";',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"immutable target with a known merged source slot",
|
||||
'const fixed=0;let selected;const xs=[1,"one"];[fixed,selected]=xs;',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"immutable object assignment target",
|
||||
'const fixed=0;let selected;const obj={fixed:1,mode:"one"};({fixed,mode:selected}=obj);obj.mode="later";',
|
||||
["mode"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"immutable target nested inside an array",
|
||||
'const fixed=0;let selected;const xs=[{fixed:1,mode:"one"}];[{fixed,mode:selected}]=xs;xs[0].mode="later";',
|
||||
["0", "mode"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"runtime import assignment target",
|
||||
'import {fixed} from "./hypothetical.js";let selected;const xs=[1,"one"];[fixed,selected]=xs;xs[1]="later";',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"resource assignment target",
|
||||
'using fixed={ [Symbol.dispose]() {} };let selected;const xs=[1,"one"];[fixed,selected]=xs;',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"unknown property key with an immutable target",
|
||||
'const fixed=0;let selected;const obj={fixed:1,mode:"one"};({[key]:fixed,mode:selected}=obj);',
|
||||
["mode"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"nested rest with an immutable target",
|
||||
'const fixed=0;let selected;const xs=[[1],"one"];[[...fixed],selected]=xs;',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"inner class declaration name",
|
||||
'class Fixed { static { let selected; const xs=[1,"one"]; [Fixed,selected]=xs; } }',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"named class expression's inner name",
|
||||
'const Outer=class Fixed { method(){let selected; const xs=[1,"one"]; [Fixed,selected]=xs;} };',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"mutable outer class declaration name",
|
||||
'class Fixed {} let selected; const xs=[1,"one"];[Fixed,selected]=xs;',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"mutable parameter shadowing a class name",
|
||||
'class Fixed { method(Fixed){let selected; const xs=[1,"one"]; [Fixed,selected]=xs;} }',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"strict write to a named function expression's self-binding",
|
||||
'const run=function Fixed(){"use strict";let selected;const xs=[1,"one"];[Fixed,selected]=xs;};',
|
||||
["1"],
|
||||
"candidate",
|
||||
"app.cjs",
|
||||
],
|
||||
[
|
||||
"sloppy write to a named function expression's self-binding",
|
||||
'const run=function Fixed(){let selected;const xs=[1,"one"];[Fixed,selected]=xs;};',
|
||||
["1"],
|
||||
"resolved",
|
||||
"app.cjs",
|
||||
],
|
||||
[
|
||||
"write to a named function expression's self-binding in unknown mode",
|
||||
'const run=function Fixed(){let selected;const xs=[1,"one"];[Fixed,selected]=xs;};',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"strict nested write to a sloppy named function expression's self-binding",
|
||||
'const run=function Fixed(){function nested(){"use strict";let selected;const xs=[1,"one"];[Fixed,selected]=xs;} nested();};',
|
||||
["1"],
|
||||
"candidate",
|
||||
"app.cjs",
|
||||
],
|
||||
[
|
||||
"strict write to a mutable parameter shadowing a named function expression",
|
||||
'const run=function Fixed(Fixed){"use strict";let selected;const xs=[1,"one"];[Fixed,selected]=xs;};',
|
||||
["1"],
|
||||
"resolved",
|
||||
"app.cjs",
|
||||
],
|
||||
[
|
||||
"read before an immutable target",
|
||||
"let selected;const fixed=0;let later;const xs=[1,2,3];[selected,fixed,later]=xs;xs[0]=2;",
|
||||
["0"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"initialized mutable target",
|
||||
'let fixed,selected;const xs=[1,"one"];[fixed,selected]=xs;xs[1]="later";',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"immutable lexical declaration",
|
||||
'const xs=[1,"one"];const [fixed,selected]=xs;xs[1]="later";',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
] as const)(
|
||||
"keeps destructuring evidence ordered for %s",
|
||||
(_name, source, path, resolution, sourcePath?: string) => {
|
||||
const file =
|
||||
sourcePath === undefined
|
||||
? undefined
|
||||
: parseJavaScriptSource(source, sourcePath);
|
||||
expect(file).not.toBeNull();
|
||||
const graph = graphForJavaScript(
|
||||
source,
|
||||
file == null ? undefined : analyzeParsedJavaScriptSemantics(file),
|
||||
);
|
||||
const selected = graph.nodes.find(
|
||||
({ kind, label }) => kind === "binding" && label === "selected",
|
||||
);
|
||||
expect(selected).toBeDefined();
|
||||
const edge = graph.relations.find(
|
||||
(relation) =>
|
||||
relation.relation === "destructures" &&
|
||||
relation.target_node_id === selected?.node_id,
|
||||
);
|
||||
expect(edge).toMatchObject({ resolution });
|
||||
expect(
|
||||
graph.nodes.find(({ node_id }) => node_id === edge?.source_node_id)
|
||||
?.properties.property_path,
|
||||
).toEqual(path);
|
||||
},
|
||||
);
|
||||
@@ -189,7 +189,7 @@ const addObjectUnknown = (
|
||||
family: "object-flow",
|
||||
relation_kinds: [relation],
|
||||
reason: "ambiguous-target",
|
||||
detail: `Static ${operation.kind} property ${operation.propertyPath === null ? "path is unresolved" : semanticPropertyPointer(operation.propertyPath)} has ${target.presence} presence; its receiver is ${target.receiverResolved ? "a retained container" : "unresolved or not a retained container"}; object identity is ${operation.resolution}.`,
|
||||
detail: `Static ${operation.kind} property ${operation.propertyPath === null ? "path is unresolved" : semanticPropertyPointer(operation.propertyPath)} has ${target.presence} presence; its receiver is ${target.receiverResolved ? "a retained container" : "unresolved or not a retained container"}; operation coverage is ${operation.resolution}.`,
|
||||
candidate_node_ids: [],
|
||||
evidence: unknownSemanticEvidence(context.file, operation.location),
|
||||
});
|
||||
|
||||
@@ -191,6 +191,7 @@ interface AddBindingInput {
|
||||
readonly initializer: t.Node | null;
|
||||
readonly directOrigin?: JavaScriptModuleOrigin;
|
||||
readonly runtimeImport?: boolean;
|
||||
readonly selfNameAssignment?: JavaScriptSemanticBindingState["selfNameAssignment"];
|
||||
readonly projection?: readonly (string | number | null)[];
|
||||
}
|
||||
|
||||
@@ -1775,6 +1776,7 @@ const bindInnerDeclaration = (
|
||||
name: node.id.name,
|
||||
kind: "class",
|
||||
mutable: false,
|
||||
selfNameAssignment: "always-throws",
|
||||
definitionNode: node.id,
|
||||
initializer: node,
|
||||
});
|
||||
@@ -2090,6 +2092,7 @@ const functionNameScope = (
|
||||
name: node.id.name,
|
||||
kind: "function",
|
||||
mutable: false,
|
||||
selfNameAssignment: "throws-in-strict-mode",
|
||||
definitionNode: node.id,
|
||||
initializer: node,
|
||||
});
|
||||
@@ -2425,6 +2428,8 @@ const addBinding = (input: AddBindingInput): void => {
|
||||
: directOrigin,
|
||||
);
|
||||
binding.runtimeImport ||= runtimeImport === true;
|
||||
if (input.selfNameAssignment !== undefined)
|
||||
binding.selfNameAssignment = input.selfNameAssignment;
|
||||
};
|
||||
|
||||
const createBinding = (
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
} from "./javascriptAstValues.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
semanticBindingMayRejectAssignment,
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
} from "./javascriptSemanticState.js";
|
||||
import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js";
|
||||
@@ -153,6 +154,8 @@ const collectDestructuring = (
|
||||
): void => {
|
||||
const source = expressionObjectIdentity(initializer, state);
|
||||
const rootArray = t.isArrayPattern(root);
|
||||
const owner = state.parentsByNode.get(root);
|
||||
const assignment = t.isAssignmentExpression(owner);
|
||||
const sourcePresence =
|
||||
initializer != null && source.path !== null
|
||||
? readPresence(source.bindingId, initializer, {
|
||||
@@ -161,12 +164,13 @@ const collectDestructuring = (
|
||||
})
|
||||
: undefined;
|
||||
let ownSlot = sourcePresence?.read;
|
||||
// An unsupported property still occupies its place in the pattern's
|
||||
// evaluation order, even when it cannot produce a graph operation.
|
||||
let assignmentBlocked = false;
|
||||
// A null path keeps a target in evaluation order without emitting an
|
||||
// operation for a source slot we cannot identify.
|
||||
const pending: (
|
||||
| {
|
||||
readonly pattern: t.Node | undefined;
|
||||
readonly path: readonly string[];
|
||||
readonly path: readonly string[] | null;
|
||||
readonly plainObjectPath?: boolean;
|
||||
readonly lookup?: "own" | "unproven";
|
||||
}
|
||||
@@ -186,7 +190,7 @@ const collectDestructuring = (
|
||||
}
|
||||
const { path } = next;
|
||||
const propertyPath =
|
||||
source.path === null ? null : [...source.path, ...path];
|
||||
source.path === null || path === null ? null : [...source.path, ...path];
|
||||
const selected =
|
||||
next.lookup === "own" && propertyPath !== null
|
||||
? ownSlot?.(propertyPath)
|
||||
@@ -205,12 +209,17 @@ const collectDestructuring = (
|
||||
if (
|
||||
hasDefault &&
|
||||
(!isKnownDefinedSource(selected) ||
|
||||
!isLexicalPatternDeclaration(state.parentsByNode.get(root), state))
|
||||
!isLexicalPatternDeclaration(owner, state))
|
||||
)
|
||||
ownSlot = undefined;
|
||||
const pattern = hasDefault
|
||||
? unwrapJavaScriptExpression(occurrence.left).node
|
||||
: occurrence;
|
||||
if (t.isRestElement(pattern)) {
|
||||
ownSlot = undefined;
|
||||
if (assignment) pending.push({ pattern: pattern.argument, path: null });
|
||||
continue;
|
||||
}
|
||||
if (t.isObjectPattern(pattern)) {
|
||||
// Later siblings require the nested receiver check to complete.
|
||||
if (
|
||||
@@ -224,20 +233,28 @@ const collectDestructuring = (
|
||||
)
|
||||
ownSlot = undefined;
|
||||
for (const property of pattern.properties.toReversed()) {
|
||||
if (!t.isObjectProperty(property)) {
|
||||
pending.push(null);
|
||||
continue;
|
||||
}
|
||||
const name = semanticStaticPropertyKey(property.key, property.computed);
|
||||
if (name === null) {
|
||||
pending.push(null);
|
||||
const prop = t.isObjectProperty(property) ? property : undefined;
|
||||
const name =
|
||||
prop === undefined
|
||||
? null
|
||||
: semanticStaticPropertyKey(prop.key, prop.computed);
|
||||
if (path === null || prop === undefined || name === null) {
|
||||
pending.push(
|
||||
assignment
|
||||
? {
|
||||
pattern: prop?.value ?? property,
|
||||
path: null,
|
||||
lookup: "unproven",
|
||||
}
|
||||
: null,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
pending.push({
|
||||
pattern: property.value,
|
||||
pattern: prop.value,
|
||||
path: [...path, name],
|
||||
plainObjectPath: plainObjectPath && !property.computed,
|
||||
lookup: property.computed ? "unproven" : "own",
|
||||
plainObjectPath: plainObjectPath && !prop.computed,
|
||||
lookup: prop.computed ? "unproven" : "own",
|
||||
});
|
||||
}
|
||||
continue;
|
||||
@@ -248,8 +265,9 @@ const collectDestructuring = (
|
||||
ownSlot =
|
||||
parentRead !== undefined &&
|
||||
parentRead === sourcePresence?.read &&
|
||||
(!rootArray || path.length === 1) &&
|
||||
(!rootArray || path?.length === 1) &&
|
||||
t.isArrayExpression(selected) &&
|
||||
path !== null &&
|
||||
propertyPath !== null
|
||||
? sourcePresence?.readNestedArray(
|
||||
selected,
|
||||
@@ -263,23 +281,19 @@ const collectDestructuring = (
|
||||
if (ownSlot !== undefined)
|
||||
pending.push(
|
||||
parentRead !== undefined &&
|
||||
semanticArrayPatternCanResumeAfterClose(
|
||||
pattern,
|
||||
state.parentsByNode.get(root),
|
||||
state,
|
||||
)
|
||||
semanticArrayPatternCanResumeAfterClose(pattern, owner, state)
|
||||
? { resume: parentRead }
|
||||
: null,
|
||||
);
|
||||
}
|
||||
for (let index = pattern.elements.length - 1; index >= 0; index -= 1) {
|
||||
const element = pattern.elements[index];
|
||||
if (element === undefined || t.isRestElement(element))
|
||||
if (element === undefined || (!assignment && t.isRestElement(element)))
|
||||
pending.push(null);
|
||||
else
|
||||
pending.push({
|
||||
pattern: element ?? undefined,
|
||||
path: [...path, String(index)],
|
||||
path: path === null ? null : [...path, String(index)],
|
||||
lookup: "own",
|
||||
});
|
||||
}
|
||||
@@ -294,7 +308,12 @@ const collectDestructuring = (
|
||||
pattern,
|
||||
pattern.name,
|
||||
);
|
||||
if (targetBinding === undefined) ownSlot = undefined;
|
||||
assignmentBlocked ||=
|
||||
assignment &&
|
||||
targetBinding !== undefined &&
|
||||
semanticBindingMayRejectAssignment(targetBinding, owner, state);
|
||||
if (targetBinding === undefined || assignmentBlocked) ownSlot = undefined;
|
||||
if (path === null) continue;
|
||||
addObjectOperation(
|
||||
{
|
||||
node: occurrence,
|
||||
@@ -303,12 +322,14 @@ const collectDestructuring = (
|
||||
objectBindingId: source.bindingId,
|
||||
targetBindingId: targetBinding?.bindingId ?? null,
|
||||
propertyPath,
|
||||
partial: assignmentBlocked,
|
||||
provenOwnSlot:
|
||||
ownSlot !== undefined ||
|
||||
(plainObjectPath &&
|
||||
targetBinding !== undefined &&
|
||||
propertyPath !== null &&
|
||||
sourcePresence?.read(propertyPath, occurrence) !== undefined),
|
||||
!assignmentBlocked &&
|
||||
(ownSlot !== undefined ||
|
||||
(plainObjectPath &&
|
||||
targetBinding !== undefined &&
|
||||
propertyPath !== null &&
|
||||
sourcePresence?.read(propertyPath, occurrence) !== undefined)),
|
||||
},
|
||||
state,
|
||||
output,
|
||||
@@ -324,6 +345,7 @@ interface AddObjectOperationInput {
|
||||
readonly targetBindingId: string | null;
|
||||
readonly propertyPath: readonly string[] | null;
|
||||
readonly provenOwnSlot?: boolean;
|
||||
readonly partial?: boolean;
|
||||
}
|
||||
|
||||
const addObjectOperation = (
|
||||
@@ -349,6 +371,7 @@ const addObjectOperation = (
|
||||
targetBindingId: input.targetBindingId,
|
||||
propertyPath: input.propertyPath,
|
||||
resolution:
|
||||
input.partial === true ||
|
||||
input.objectBindingId === null ||
|
||||
input.propertyPath === null ||
|
||||
(input.kind === "destructure" && input.targetBindingId === null)
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
unwrapJavaScriptExpression,
|
||||
} from "./javascriptAstValues.js";
|
||||
import type { SemanticIterationSource } from "./javascriptSemanticIterationFacts.js";
|
||||
import { javaScriptStrictnessAt } from "./javascriptSourceParser.js";
|
||||
|
||||
import type {
|
||||
JavaScriptSemanticPropertyPath,
|
||||
@@ -104,8 +105,24 @@ export interface JavaScriptSemanticBindingState {
|
||||
readonly directOrigins: JavaScriptModuleOrigin[];
|
||||
/** An ECMAScript import that exists at runtime, excluding type-only and import-equals syntax. */
|
||||
runtimeImport: boolean;
|
||||
/** Immutable self-name binding created by evaluating a class or named function expression. */
|
||||
selfNameAssignment?: "always-throws" | "throws-in-strict-mode";
|
||||
}
|
||||
|
||||
/** Classify rejection independently from whether a syntactic write was recorded. */
|
||||
export const semanticBindingMayRejectAssignment = (
|
||||
binding: JavaScriptSemanticBindingState,
|
||||
site: t.Node,
|
||||
{ parentsByNode }: JavaScriptSemanticAnalysisState,
|
||||
): boolean =>
|
||||
binding.runtimeImport ||
|
||||
binding.declarationKinds.has("const") ||
|
||||
binding.declarationKinds.has("using") ||
|
||||
binding.declarationKinds.has("await using") ||
|
||||
binding.selfNameAssignment === "always-throws" ||
|
||||
(binding.selfNameAssignment === "throws-in-strict-mode" &&
|
||||
javaScriptStrictnessAt(site, parentsByNode) !== "sloppy");
|
||||
|
||||
/** Mutable lexical-scope state used only during AST recovery. */
|
||||
export interface JavaScriptSemanticScopeState {
|
||||
readonly scopeId: string;
|
||||
|
||||
Reference in new issue
Block a user