Repository navigation
bundler: implement --keep-names with __name helper #40503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5254,8 +5254,82 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O | |
| } | ||
| } | ||
|
|
||
| pub(crate) fn keep_expr_symbol_name(&mut self, _value: Expr, _name: &[u8]) -> Expr { | ||
| _value | ||
| pub(crate) fn keep_expr_symbol_name(&mut self, value: Expr, name: &'a [u8]) -> Expr { | ||
| if !self.options.features.minify_keep_names { | ||
| return value; | ||
| } | ||
| if let Some(mut class) = value.data.e_class() { | ||
| self.keep_class_symbol_name(&mut class, name, value.loc); | ||
| return value; | ||
| } | ||
| let loc = value.loc; | ||
| let target = self.runtime_identifier(loc, b"__name"); | ||
| let name_expr = self.new_expr(E::String::init(name), loc); | ||
| let args = self.arena.alloc_slice_copy(&[value, name_expr]); | ||
| self.new_expr( | ||
| E::Call { | ||
| target, | ||
| args: ExprNodeList::from_arena_slice(args), | ||
| can_be_unwrapped_if_unused: E::CallUnwrap::IfUnused, | ||
| ..Default::default() | ||
| }, | ||
| loc, | ||
| ) | ||
| } | ||
|
|
||
| /// `__name(foo, "foo");` emitted after a function declaration. | ||
| pub(crate) fn keep_stmt_symbol_name( | ||
| &mut self, | ||
| loc: bun_ast::Loc, | ||
| ref_: Ref, | ||
| name: &'a [u8], | ||
| ) -> Stmt { | ||
| let name_expr = self.new_expr(E::String::init(name), loc); | ||
| let args = self | ||
| .arena | ||
| .alloc_slice_copy(&[Expr::init_identifier(ref_, loc), name_expr]); | ||
| let value = self.call_runtime(loc, b"__name", ExprNodeList::from_arena_slice(args)); | ||
| self.s( | ||
| S::SExpr { | ||
| value, | ||
| does_not_affect_tree_shaking: true, | ||
| }, | ||
| loc, | ||
| ) | ||
| } | ||
|
|
||
| /// Prepends `static { __name(this, "Foo"); }` so `.name` survives identifier renaming. | ||
| pub(crate) fn keep_class_symbol_name( | ||
| &mut self, | ||
| class: &mut G::Class, | ||
| name: &'a [u8], | ||
| loc: bun_ast::Loc, | ||
| ) { | ||
| let this = self.new_expr(E::This, loc); | ||
| let name_expr = self.new_expr(E::String::init(name), loc); | ||
| let args = self.arena.alloc_slice_copy(&[this, name_expr]); | ||
| let call = self.call_runtime(loc, b"__name", ExprNodeList::from_arena_slice(args)); | ||
| let stmt = self.s( | ||
| S::SExpr { | ||
| value: call, | ||
| does_not_affect_tree_shaking: true, | ||
| }, | ||
| loc, | ||
| ); | ||
| let stmts = | ||
| bun_alloc::AstVec::<Stmt>::from_arena_slice(self.arena.alloc_slice_copy(&[stmt])); | ||
| let block = self.arena.alloc(G::ClassStaticBlock { loc, stmts }); | ||
| let mut properties = BumpVec::with_capacity_in(class.properties.len() + 1, self.arena); | ||
| properties.push(G::Property { | ||
| kind: js_ast::g::PropertyKind::ClassStaticBlock, | ||
| class_static_block: Some(js_ast::StoreRef::from_bump(block)), | ||
| ..Default::default() | ||
| }); | ||
| for prop in class.properties.slice() { | ||
| // SAFETY: arena-resident AST nodes never run `Drop`, so the shallow copy cannot double free. | ||
| properties.push(unsafe { core::ptr::read(prop) }); | ||
| } | ||
| class.properties = bun_ast::StoreSlice::new_mut(properties.into_bump_slice_mut()); | ||
| } | ||
|
Comment on lines
+5302
to
5333
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Extended reasoning...The injected Verification: normal — The failure is real and reachable, and regresses behavior that works on the base branch. |
||
|
|
||
| pub(crate) fn is_simple_parameter_list(args: &[G::Arg], has_rest_arg: bool) -> bool { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Add
record_usagefor the identifier reference inkeep_stmt_symbol_name.keep_stmt_symbol_namebuildsExpr::init_identifier(ref_, loc)as the call argument but never callsself.record_usage(ref_)for it. Compare withemit_react_refresh_register, which creates the same kind of synthetic identifier reference and explicitly callsself.record_usage(r#ref)afterward.Without
record_usage,symbols[ref_].use_count_estimateand the current part'ssymbol_usesmap understate this reference. In this codebase, synthesized post-visit identifier references consistently callrecord_usageto keep that bookkeeping accurate (it feeds the character-frequency renamer's minified-name ordering). Add the same call here for consistency.🔧 Proposed fix
pub(crate) fn keep_stmt_symbol_name( &mut self, loc: bun_ast::Loc, ref_: Ref, name: &'a [u8], ) -> Stmt { let name_expr = self.new_expr(E::String::init(name), loc); let args = self .arena .alloc_slice_copy(&[Expr::init_identifier(ref_, loc), name_expr]); + self.record_usage(ref_); let value = self.call_runtime(loc, b"__name", ExprNodeList::from_arena_slice(args));📝 Committable suggestion
🤖 Prompt for AI Agents