From 3453858ea4f729ac1aadb92c415d145c88e8ceaa Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Thu, 10 Oct 2024 12:11:12 -0400 Subject: [PATCH] CLI exposes top-level declarations with top-level `await` --- source/cli.civet | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/source/cli.civet b/source/cli.civet index 166ee970..d9f3dd9f 100644 --- a/source/cli.civet +++ b/source/cli.civet @@ -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 @@ -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