dillon1000/react
Commit
Browse files Support OptionalCallExpression as LHS of LogicalExpression
A common idiom is to map over some possibly-missing list of items from a data payload and fall back to an empty array: ```javascript const renderedItems = data?.items?.map(renderItem) ?? []; ``` The way we were lowering OptionalCallExpression meant that in this case, we'd end up with an OptionalCallTerminal as the terminal of the logical expression's test block, which violates our internal invariant. Logical test blocks must end in a Branch! This PR fixes the immediate issue, which is that the callee - in this case `data?.items?.map` — was being lowered prior to the OptionalCallTerminal instead of inside its test block. Changing that fixes the shape of the IR and makes this example work. As part of investigating this I realized that the way I originally handled lowering of optional call isn't quite right. The difference isn't observable unless we did more sophisticated DCE but we don't correctly model the fact that if `data.items` is null that the `map()` call won't occur. That is technically fine bc we do model the fact that the `map()` call is conditional, and notably its arguments are only conditional dependencies. So it's good enough. But in a follow-up I'll change to model the fact that `data.items` is null, that the map call isn't reachable at all.
Changed paths11 files
First-parent comparisoncompiler/forget/src/HIR/BuildHIR.ts ModifiedM compiler/forget/src/HIR/HIRBuilder.ts ModifiedM compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts ModifiedM compiler/forget/src/ReactiveScopes/PrintReactiveFunction.ts ModifiedM compiler/forget/src/ReactiveScopes/PruneNonEscapingScopes.ts ModifiedA compiler/forget/src/__tests__/fixtures/compiler/optional-call-chained.expect.md AddedA compiler/forget/src/__tests__/fixtures/compiler/optional-call-chained.js AddedA compiler/forget/src/__tests__/fixtures/compiler/optional-call-logical.expect.md AddedA compiler/forget/src/__tests__/fixtures/compiler/optional-call-logical.js AddedA compiler/forget/src/__tests__/fixtures/compiler/optional-call-simple.expect.md AddedA compiler/forget/src/__tests__/fixtures/compiler/optional-call-simple.js AddedPatch
Files changed
Rendering syntax-highlighted changes…