Skip to content

Commit

Permalink
Fix top-level yield in iife mode
Browse files Browse the repository at this point in the history
Fixes #1659
  • Loading branch information
edemaine committed Dec 26, 2024
1 parent 0ebaf19 commit 713a6da
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Program
bare: true,
root: true,
topLevelAwait: hasAwait(statements),
topLevelYield: hasYield(statements),
}
processProgram(program)
return program
Expand Down
4 changes: 3 additions & 1 deletion source/parser/lib.civet
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,10 @@ function processProgram(root: BlockStatement): void
let rootIIFE: ASTNode
if config.iife or config.repl
// Avoid top-level await if code is async (same as CoffeeScript),
// and avoid top-level yield* if code is a generator,
// so that code works in CommonJS environment and so REPL can eval it
rootIIFE = wrapIIFE root.expressions, root.topLevelAwait
rootIIFE = wrapIIFE root.expressions, root.topLevelAwait,
if root.topLevelYield then "*"
newExpressions: StatementTuple[] := [['', rootIIFE]]
root.children = root.children.map & is root.expressions ? newExpressions : &
root.expressions = newExpressions
Expand Down
1 change: 1 addition & 0 deletions source/parser/types.civet
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ export type BlockStatement =
implicitlyReturned?: boolean // fat arrow function with no braces
root?: boolean // is this the global root block for the program?
topLevelAwait?: boolean // for root block, is there top-level `await`? (before any IIFE wrapping)
topLevelYield?: boolean // for root block, is there top-level `yield`? (before any IIFE wrapping)
parent?: Parent

export type ImportDeclaration
Expand Down
9 changes: 9 additions & 0 deletions test/iife.civet
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ describe "iife directive", ->
(async ()=>{return console.log((await fetch('https://civet.dev')).status)})()
"""

testCase """
generator
---
"civet iife"
yield 5
---
(function*(){yield 5})()
"""

describe "repl directive", ->
testCase """
top-level declarations
Expand Down

0 comments on commit 713a6da

Please sign in to comment.