Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 7 additions & 1 deletion src/ast/s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ pub struct ExportFrom {
}

pub struct ExportDefault {
pub default_name: LocRef, // value may be a SFunction or SClass
/// The module-scope binding that holds the default export.
///
/// For a function or class declaration with a name: the symbol of that name.
/// Otherwise: a generated symbol. It is never the name of a function or class
/// expression, because the expression binds that name in its own scope.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub default_name: LocRef,
/// A function or class declaration, or an expression.
pub value: StmtOrExpr,
}

Expand Down
38 changes: 0 additions & 38 deletions src/js_parser/p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4567,44 +4567,6 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
)
}

pub(crate) fn default_name_for_expr(&mut self, expr: Expr, loc: bun_ast::Loc) -> LocRef {
match &expr.data {
js_ast::ExprData::EFunction(func_container) => {
if let Some(_name) = &func_container.func.name {
if let Some(r#ref) = _name.ref_.to_nullable() {
return LocRef { loc, ref_: r#ref };
}
}
}
js_ast::ExprData::EIdentifier(ident) => {
return LocRef {
loc,
ref_: ident.ref_,
};
}
js_ast::ExprData::EImportIdentifier(ident) => {
if !Self::ALLOW_MACROS
|| (Self::ALLOW_MACROS && !self.macro_.refs.contains(&ident.ref_))
{
return LocRef {
loc,
ref_: ident.ref_,
};
}
}
js_ast::ExprData::EClass(class) => {
if let Some(_name) = &class.class_name {
if let Some(r#ref) = _name.ref_.to_nullable() {
return LocRef { loc, ref_: r#ref };
}
}
}
_ => {}
}

self.create_default_name(loc)
}

pub(crate) fn discard_scopes_up_to(&mut self, scope_index: usize) {
// Remove any direct children from their parent. `StoreRef` is `Copy` and
// does not borrow `self`, so the immutable iter over `scopes_in_order`
Expand Down
12 changes: 10 additions & 2 deletions src/js_parser/parse/parse_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,8 +1187,16 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O

p.lexer.expect_or_insert_semicolon()?;

// Use the expression name if present, since it's a better name
let default_name = p.default_name_for_expr(expr, default_loc);
// A function or class expression binds its name in its own scope, so unlike
// the statement forms above it cannot lend that name to the module.
Comment thread
robobun marked this conversation as resolved.
Resolved
let default_name = match expr.data {
// Not a symbol yet: the visit pass makes the default name for an identifier.
js_ast::ExprData::EIdentifier(ident) => LocRef {
loc: default_loc,
ref_: ident.ref_,
},
_ => p.create_default_name(default_loc),
Comment thread
robobun marked this conversation as resolved.
Resolved
};
Ok(p.s(
S::ExportDefault {
default_name,
Expand Down
7 changes: 7 additions & 0 deletions src/js_parser/visit/visit_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
if !data.default_name.ref_.is_symbol() {
data.default_name = p.create_default_name(expr.loc);
}
debug_assert!(
p.module_scope()
.generated
.slice()
.contains(&data.default_name.ref_),
"the default name of an expression must be a generated module-scope symbol"
);

let should_emit_temp_var = p.options.features.react_fast_refresh
&& match expr.data {
Expand Down
4 changes: 3 additions & 1 deletion src/jsc/RuntimeTranspilerCache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ bun_core::declare_scope!(cache, visible);
/// the cache-HIT path reinstates #33904 for them.
/// Version 34: An import that a plugin `onResolve` answers is printed as it is written.
/// Version 35: Long hexadecimal, octal and binary literals are rounded to the nearest double.
const EXPECTED_VERSION: u32 = 35;
/// Version 36: `export default (function f() {})` and `export default (class f {})` have their
/// own default symbol. With `minify_identifiers`, older entries give the name `f` to a local.
Comment thread
robobun marked this conversation as resolved.
Resolved
const EXPECTED_VERSION: u32 = 36;

/// Source files smaller than this are not written to / read from the on-disk
/// transpiler cache. Originally 50 KiB, which excluded almost every file in a
Expand Down
Loading
Loading