From f81cbd0c43eaced30566034e901409534759635b Mon Sep 17 00:00:00 2001 From: SahinurDEV <68515168+SahinurDEV@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:09:54 +0600 Subject: [PATCH] fix(javascript): keep computed definition key conversion effects Defining a computed class or object member converts its key to a property name, so preserve that coercion escape alongside the key expression walk. Reuse javaScriptComputedDefinitionKey for computed-key ownership in the getter result walker instead of repeating the check. Refs #9229 Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- ...tSemanticAnalysis.getter_receivers.test.ts | 12 +++++++ .../javascriptSemanticGetterResultWalker.ts | 8 ++--- .../javascriptSemanticMemberMutations.ts | 35 +++++++++++-------- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/domain/javascript/javascriptSemanticAnalysis.getter_receivers.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.getter_receivers.test.ts index aed957fe2b..479639fe1c 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.getter_receivers.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.getter_receivers.test.ts @@ -203,6 +203,18 @@ describe("getter receiver mutation aliases (#1665, #2654)", () => { ).toMatchObject({ status: "unknown" }); }); + it.each([ + ["class computed method key", "class { [this]() {} }"], + ["class computed field key", "class { [this] = 0; }"], + ["object method computed key", "({ [this]() {} })"], + ])("invalidates the receiver when a %s converts it", (_label, result) => { + expect( + resultValue( + `const box = { x: 1, toString() { this.x = 2; return 'k'; }, get trigger() { return ${result}; } }; box.trigger; return box.x;`, + ), + ).toMatchObject({ status: "unknown" }); + }); + it.each([ ["uninvoked class method", "class { method() { this.x = 2; } }"], [ diff --git a/src/domain/javascript/javascriptSemanticGetterResultWalker.ts b/src/domain/javascript/javascriptSemanticGetterResultWalker.ts index e0193ccbef..d301c90860 100644 --- a/src/domain/javascript/javascriptSemanticGetterResultWalker.ts +++ b/src/domain/javascript/javascriptSemanticGetterResultWalker.ts @@ -1,5 +1,6 @@ import * as t from "@babel/types"; import { + javaScriptComputedDefinitionKey, unwrapJavaScriptExpression, semanticStaticPropertyKey, } from "./javascriptAstValues.js"; @@ -81,12 +82,7 @@ const resultBranches = (node: t.Node): readonly t.Node[] | undefined => { const evaluatesInOuterThis = (owner: t.Node, child: t.Node): boolean => t.isClass(owner) ? child === owner.superClass - : (t.isObjectMethod(owner) || - t.isClassMethod(owner) || - t.isClassProperty(owner) || - t.isClassAccessorProperty(owner)) && - owner.computed === true && - child === owner.key; + : javaScriptComputedDefinitionKey(owner, child); /** Each walk owns local aliases; getter-to-getter traversal belongs to the query worklist. */ export class GetterResultWalker { diff --git a/src/domain/javascript/javascriptSemanticMemberMutations.ts b/src/domain/javascript/javascriptSemanticMemberMutations.ts index 2ef7c16687..cc6a2d65a2 100644 --- a/src/domain/javascript/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascript/javascriptSemanticMemberMutations.ts @@ -89,6 +89,7 @@ import { childNodes, } from "./javascriptSemanticTraversal.js"; import { + javaScriptComputedDefinitionKey, semanticStaticPropertyKey, semanticWriteSiteTarget, semanticWriteSiteReadsFirst, @@ -393,23 +394,25 @@ const accessorOperands = new WeakMap< readonly AccessorEffectOperand[] >(); -/** Expressions a definition runs immediately; deferred bodies and class-receiver initializers stay out. */ -const definitionTimeExpressions = (node: t.Function | t.Class): t.Node[] => { - if (t.isObjectMethod(node)) return node.computed ? [node.key] : []; +/** Members whose computed keys a definition evaluates and converts immediately. */ +const computedDefinitionMembers = (node: t.Function | t.Class): t.Node[] => { + if (t.isObjectMethod(node)) + return javaScriptComputedDefinitionKey(node, node.key) ? [node] : []; if (!t.isClass(node)) return []; - return [ - ...(node.superClass == null ? [] : [node.superClass]), - ...node.body.body.flatMap((member) => - (t.isClassMethod(member) || - t.isClassProperty(member) || - t.isClassAccessorProperty(member)) && - member.computed - ? [member.key] - : [], - ), - ]; + return node.body.body.filter( + (member) => + "key" in member && javaScriptComputedDefinitionKey(member, member.key), + ); }; +/** Expressions a definition runs immediately; deferred bodies and class-receiver initializers stay out. */ +const definitionTimeExpressions = (node: t.Function | t.Class): t.Node[] => [ + ...(t.isClass(node) && node.superClass != null ? [node.superClass] : []), + ...computedDefinitionMembers(node).flatMap((member) => + "key" in member ? [member.key] : [], + ), +]; + /** Collect invocation operands once; receiver interpretation belongs to the getter owner. */ const accessorEffectOperands = ( method: t.Function, @@ -426,6 +429,10 @@ const accessorEffectOperands = ( // Defining a nested callable does not invoke it, but the definition itself // evaluates heritage and computed keys against the enclosing receiver. if (t.isFunction(node) || t.isClass(node)) { + // Defining a computed member also converts its key to a property name. + for (const member of computedDefinitionMembers(node)) + for (const operand of semanticCoercionOperands(member)) + operands.push({ kind: "escape", node: operand }); for (const child of definitionTimeExpressions(node).reverse()) pending.push({ node: child, parent: node }); continue;