Skip to content

Commit

Permalink
Merge pull request #1448 from DanielXMoore/cli-var-fix
Browse files Browse the repository at this point in the history
CLI exposes top-level declarations with top-level `await`
  • Loading branch information
edemaine authored Oct 10, 2024
2 parents 3a367fc + 3453858 commit 71bd48b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions source/cli.civet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ compile, generate, parse, lib, isCompileError, SourceMap, type ParseError, type ParseErrors } from ./main.civet
{ compile, generate, parse, lib, isCompileError, SourceMap, type BlockStatement, type ParseError, type ParseErrors } from ./main.civet
{ findConfig, loadConfig } from ./config.civet

// unplugin ends up getting installed in the same dist directory
Expand Down Expand Up @@ -344,13 +344,16 @@ export function repl(args: string[], options: Options)
(coffee ? ')' : ''),
{...options, filename, ast: true}
// Hoist top-level declarations outside the IIFE wrapper
lib.gatherRecursive ast, .type is 'BlockStatement'
.forEach (topBlock) =>
lib.gatherRecursiveWithinFunction topBlock, .type is 'Declaration'
.forEach (decl) =>
type := decl.children.shift() // const/let/var
topBlock :=
if coffee
lib.gatherRecursive(ast.children, &.type is 'BlockStatement')[0]
else
lib.gatherRecursive(ast.children, &.type is 'DoStatement')[0].block
for each [, statement] of topBlock.expressions
if statement is like {type: 'Declaration'}
statement.children.shift() // const/let/var
ast = [ast] unless Array.isArray ast
ast.unshift `var ${decl.names.join ','};`
ast.unshift `var ${statement.names.join ','};`

errors: unknown[] .= []
try
Expand Down

0 comments on commit 71bd48b

Please sign in to comment.