-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler] Implement support for non-declaration for initializers #31712
base: gh/mvitousek/40/base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1354,6 +1354,8 @@ function codegenForInit( | |
init: ReactiveValue, | ||
): t.Expression | t.VariableDeclaration | null { | ||
if (init.kind === 'SequenceExpression') { | ||
// We may end up emitti | ||
const temp = cx.temp; | ||
const body = codegenBlock( | ||
cx, | ||
init.instructions.map(instruction => ({ | ||
|
@@ -1363,7 +1365,7 @@ function codegenForInit( | |
).body; | ||
const declarators: Array<t.VariableDeclarator> = []; | ||
let kind: 'let' | 'const' = 'const'; | ||
body.forEach(instr => { | ||
for (const instr of body) { | ||
let top: undefined | t.VariableDeclarator = undefined; | ||
if ( | ||
instr.type === 'ExpressionStatement' && | ||
|
@@ -1375,30 +1377,29 @@ function codegenForInit( | |
top?.init == null | ||
) { | ||
top.init = instr.expression.right; | ||
} else { | ||
CompilerError.invariant( | ||
instr.type === 'VariableDeclaration' && | ||
(instr.kind === 'let' || instr.kind === 'const'), | ||
{ | ||
reason: 'Expected a variable declaration', | ||
loc: init.loc, | ||
description: `Got ${instr.type}`, | ||
suggestions: null, | ||
}, | ||
); | ||
} else if ( | ||
instr.type === 'VariableDeclaration' && | ||
(instr.kind === 'let' || instr.kind === 'const') | ||
) { | ||
if (instr.kind === 'let') { | ||
kind = 'let'; | ||
} | ||
declarators.push(...instr.declarations); | ||
} else { | ||
/* | ||
* We found instructions in the initializer that don't correspond to a variable declarator: | ||
* emit as an expression instead | ||
*/ | ||
cx.temp = temp; | ||
return codegenInstructionValueToExpression(cx, init); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this skip over remaining instructions? since it's exiting the loop and function |
||
} | ||
}); | ||
CompilerError.invariant(declarators.length > 0, { | ||
reason: 'Expected a variable declaration', | ||
loc: init.loc, | ||
description: null, | ||
suggestions: null, | ||
}); | ||
return t.variableDeclaration(kind, declarators); | ||
} | ||
if (declarators.length > 0) { | ||
return t.variableDeclaration(kind, declarators); | ||
} else { | ||
cx.temp = temp; | ||
return codegenInstructionValueToExpression(cx, init); | ||
} | ||
} else { | ||
return codegenInstructionValueToExpression(cx, init); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Foo() { | ||
let i = 0; | ||
for (42; i < 1; i += 1) {} | ||
for (bar(); i < 1; i += 1) {} | ||
for (; i < 1; i += 1) {} | ||
for (i = 0; i < 1; i += 1) {} | ||
} | ||
|
||
function bar() {} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
function Foo() { | ||
let i = 0; | ||
for (42; i < 1; i = i + 1, i) {} | ||
for (bar(); i < 1; i = i + 1, i) {} | ||
for (undefined; i < 1; i = i + 1, i) {} | ||
for (i = 0; i < 1; i = i + 1, i) {} | ||
} | ||
|
||
function bar() {} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function Foo() { | ||
let i = 0; | ||
for (42; i < 1; i += 1) {} | ||
for (bar(); i < 1; i += 1) {} | ||
for (; i < 1; i += 1) {} | ||
for (i = 0; i < 1; i += 1) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's test a more complex initializer value, like a logical expression, ternary, sequence expression, or optional. |
||
} | ||
|
||
function bar() {} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: looks cut off