Skip to content

Commit a8f3151

Browse files
committed
fix(compiler): track hook setters through member bindings and flatten destructured globals
Fix two Strong mode analysis bugs: 1. Hook setters missed on member bindings: Custom-hook return provenance was not rebound when the initializer was a member expression on the call (e.g. useThing()[1]), and stored returned-array/returned-object shapes were never bound to identifiers. Add returnedShapeElement helper to resolve member accesses on call shapes and returned-shape bindings, extend bindDeclaration to re-trigger binding for member expressions on calls, remove the identifier exclusion in bindReturnedShape, and use the helper in callableValue for callee resolution. 2. Destructured globals skip zero-delay checks: keyOf flattened window/globalThis members to g:name for MemberExpression but produced g:window.name for object-pattern aliases, which globalFunction rejected due to the dot. Apply the same g:window flattening in the declarator path that the MemberExpression path already uses.
1 parent 388bbb7 commit a8f3151

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎packages/octane/src/compiler/strong-effects.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
123123
const init = info === null ? null : unwrap(info.decl.init);
124124
if (init?.type === 'Identifier' || init?.type === 'MemberExpression') {
125125
const base = keyOf(init, depth + 1);
126-
if (base !== null) return info.path.length === 0 ? base : `${base}.${info.path[0]}`;
126+
if (base !== null) {
127+
if (info.path.length === 0) return base;
128+
if (base === 'g:window')
129+
return GLOBAL_OBJECTS.has(info.path[0]) ? base : `g:${info.path[0]}`;
130+
return `${base}.${info.path[0]}`;
131+
}
127132
}
128133
}
129134
return `b${binding.id}`;

‎packages/octane/src/compiler/strong-mode.js‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,8 +2745,11 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
27452745
} else if (prop) bindPropPattern(declaration.id, bind);
27462746
}
27472747
}
2748-
const shape = initial?.type === 'CallExpression' ? callShapes.get(initial) : undefined;
2749-
if (shape !== undefined && stateTuple === null) {
2748+
let shape = initial?.type === 'CallExpression' ? callShapes.get(initial) : undefined;
2749+
if (shape === undefined && stateTuple === null && initial?.type === 'MemberExpression') {
2750+
shape = returnedShapeElement(initial, scope);
2751+
}
2752+
if (shape != null && stateTuple === null) {
27502753
bindReturnedShape(declaration.id, shape, scope, bind);
27512754
}
27522755
}
@@ -2895,11 +2898,37 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
28952898
return null;
28962899
}
28972900

2901+
function returnedShapeElement(node, scope) {
2902+
if (node?.type !== 'MemberExpression') return undefined;
2903+
const object = unwrap(node.object);
2904+
const shape =
2905+
object?.type === 'CallExpression'
2906+
? callShapes.get(object)
2907+
: object?.type === 'Identifier'
2908+
? resolve(scope, object.name)
2909+
: null;
2910+
if (shape?.kind === 'returned-array') {
2911+
if (!node.computed) return undefined;
2912+
const key = staticPrimitiveValue(node.property, scope);
2913+
const index = typeof key === 'number' ? key : typeof key === 'string' ? Number(key) : NaN;
2914+
return Number.isInteger(index) && index >= 0 ? shape.elements[index] : undefined;
2915+
}
2916+
if (shape?.kind === 'returned-object') {
2917+
const key = node.computed
2918+
? staticPrimitiveValue(node.property, scope)
2919+
: node.property?.type === 'Identifier'
2920+
? node.property.name
2921+
: null;
2922+
return typeof key === 'string' ? shape.properties.get(key) : undefined;
2923+
}
2924+
return undefined;
2925+
}
2926+
28982927
function bindReturnedShape(pattern, shape, scope, bind) {
28992928
if (shape === TRANSITION_TUPLE_BINDING) {
29002929
bindTransitionPattern(pattern, scope, bind);
29012930
} else if (pattern?.type === 'Identifier') {
2902-
if (shape.kind !== 'returned-array' && shape.kind !== 'returned-object') bind(pattern, shape);
2931+
bind(pattern, shape);
29032932
} else if (pattern?.type === 'ArrayPattern' && shape.kind === 'returned-array') {
29042933
pattern.elements?.forEach((element, index) => {
29052934
const value = shape.elements[index];
@@ -3105,7 +3134,11 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
31053134
bindDeclarationValue(declaration, declarationKind, scope);
31063135
visit(declaration.init, scope, phase);
31073136
// Visiting a local call records what it returned; bind that provenance.
3108-
if (callShapes.has(unwrap(declaration.init))) {
3137+
const initNode = unwrap(declaration.init);
3138+
if (
3139+
callShapes.has(initNode) ||
3140+
(initNode?.type === 'MemberExpression' && callShapes.has(unwrap(initNode.object)))
3141+
) {
31093142
bindDeclarationValue(declaration, declarationKind, scope);
31103143
}
31113144
return visitPatternExpressions(
@@ -3489,6 +3522,8 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
34893522
const setter = stateTupleUpdater(node, scope);
34903523
if (setter !== null) return setter;
34913524
if (stateTupleGetter(node, scope)) return STATE_GETTER_BINDING;
3525+
const member = returnedShapeElement(node, scope);
3526+
if (isCallableValue(member)) return member;
34923527
if (node?.type === 'SequenceExpression') {
34933528
return callableValue(node.expressions?.[node.expressions.length - 1], scope);
34943529
}

0 commit comments

Comments
 (0)