Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/js_parser/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
) -> RelocateVars {
let p = self;
// Only do this when the scope is not already top-level and when we're not inside a function.
if p.current_scope == p.module_scope {
// A brace-less body opens no scope, so there the scope does not say "top-level".
if p.current_scope == p.module_scope && !p.relocates_single_stmt_body_decls() {
Comment thread
robobun marked this conversation as resolved.
Resolved
return RelocateVars {
ok: false,
..Default::default()
Expand Down
25 changes: 24 additions & 1 deletion src/js_parser/p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ pub struct P<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> {
// The old "var" statements are turned into regular assignments instead. This
// makes it easier to quickly scan the top-level statements for "var" locals
// with the guarantee that all will be found.
// Not all yet: see `relocates_single_stmt_body_decls`, and a lowered top-level `using` keeps them in its `try` (#38284).
pub(crate) relocated_top_level_vars: List<'a, js_ast::LocRef>,

// ArrowFunction is a special case in the grammar. Although it appears to be
Expand Down Expand Up @@ -6890,7 +6891,13 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
value: None,
}]);

if self.current_scope == self.module_scope {
if self.current_scope == self.module_scope && self.relocates_single_stmt_body_decls() {
// `if (c) enum E {}`: `append_part` declares the "var" at the top level.
self.relocated_top_level_vars.push(LocRef {
loc: name_loc,
ref_: name_ref,
});
} else if self.current_scope == self.module_scope {
// Top-level namespace: "var"
stmts.push(self.s(
S::Local {
Expand Down Expand Up @@ -8025,6 +8032,22 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
Expr::init_identifier(r#ref, loc)
}

/// The linker's `__esm` wrapper and the REPL transform move top-level declarations out of their closure, and only those.
#[inline]
pub(crate) fn lifts_top_level_decls(&self) -> bool {
self.options.repl_mode
|| (self.options.bundle && !self.options.features.hot_module_reloading)
}

/// A brace-less `if`/`else`/`while`/`do` body opens no scope, so `lifts_top_level_decls` needs its declarations relocated too.
#[inline]
pub(crate) fn relocates_single_stmt_body_decls(&self) -> bool {
self.is_inside_single_stmt_body
&& self.lifts_top_level_decls()
// A bundle is module code, which rejects a top-level "var f" beside "function f" (#44001).
&& !(self.options.bundle && self.has_top_level_function_merged_with_var)
Comment thread
robobun marked this conversation as resolved.
Resolved
}

// One statement could potentially expand to several statements
pub(crate) fn stmts_to_single_stmt(
&mut self,
Expand Down
3 changes: 3 additions & 0 deletions src/js_parser/visit/visit_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
);
if let Some(relocated) = relocate.stmt {
data.init = Some(relocated);
} else if relocate.ok && p.lifts_top_level_decls() {
// `for (var i; ...)`: a "var" left here declares a second `i` inside the wrapper.
data.init = None;
}
}
}
Expand Down
Loading
Loading