dillon1000/react

Commit

Temporary workaround for emitting temporaries multiple times

This is a temporary fix for the issue we discovered on our first integration, 
where destructuring of a function return value is emitting the function call 
multiple times: 

```javascript 

// Input 

const [x, setX] = useState(null); 

// Output 

const x = useState(null)[0]; 

const setX = useState(null)[1]; 

``` 

The reason this happens is that we lower `useState(null)` to a temporary, and 
then generate a ComputedLoad for each of x and setX. Codegen doesn't emit 
temporaries eagerly - it assumes they are going to be used exactly once and it 
re-emits the value each time the temporary is used. Hence why the 
`useState(null)` part gets duplicated in the output. 

Right now destructuring is the only place i'm aware of where we reuse 
temporaries this way. And we do want to change codegen to preserve destructuring 
in the output to correctly handle array patterns. However, that's a more 
involved change. For now, this PR is a stopgap. During the pass where we promote 
temporaries used in scopes to named variables, we now check to see if those 
temporaries are used multiple times and promote them. 

The above example would then generate something like 

```javascript 

const t0 = useState(null); 

const x = t0[0]; 

const setX = t0[1]; 

``` 

This is still incorrect (it assumes t0 is an array), but it's more likely to 
work in practice. I'll revert this change once we correctly handle 
destructuring.
Browse files
Changed paths5 files
First-parent comparison
M compiler/forget/src/HIR/BuildHIR.ts ModifiedM compiler/forget/src/ReactiveScopes/PromoteUsedTemporaries.ts ModifiedM compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/assignment-variations-complex-lvalue.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/controlled-input.expect.md Modified
Patch

Files changed

Rendering syntax-highlighted changes…