fix(javascript): preserve capture and property-key effect ownership

This commit is contained in:
morluto
2026-10-24 04:14:34 +08:00
parent b3433d0187
commit c6ac60acfe
7 changed files with 129 additions and 7 deletions
@@ -0,0 +1,42 @@
import * as t from "@babel/types";
import {
semanticStaticPropertyKey,
unwrapJavaScriptExpression,
} from "./javascriptAstValues.js";
import { isUnmodifiedGlobalReference } from "./javascriptSemanticAmbientWrites.js";
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
// These intrinsic properties hold symbols, so ToPropertyKey cannot produce a
// string slot. Visible replacement of Symbol/the selected property voids proof.
const wellKnownSymbols = new Set([
"asyncIterator",
"hasInstance",
"isConcatSpreadable",
"iterator",
"match",
"matchAll",
"replace",
"search",
"species",
"split",
"toPrimitive",
"toStringTag",
"unscopables",
"dispose",
"asyncDispose",
]);
/** A proved symbol key cannot shadow a selected string property. */
export const semanticKnownSymbolKey = (
node: t.Node,
state: JavaScriptSemanticAnalysisState,
): boolean => {
const key = unwrapJavaScriptExpression(node).node;
if (!t.isMemberExpression(key)) return false;
const name = semanticStaticPropertyKey(key.property, key.computed);
return (
name !== null &&
wellKnownSymbols.has(name) &&
isUnmodifiedGlobalReference(key, state, ["Symbol", name])
);
};