@@ -1626,90 +1626,92 @@ impl<C: Comments> VisitMut for ServerActions<C> {
16261626 let in_cache_file = matches ! ( self . file_directive, Some ( Directive :: UseCache { .. } ) ) ;
16271627 let in_action_file = matches ! ( self . file_directive, Some ( Directive :: UseServer ) ) ;
16281628
1629+ // Only track exported identifiers in server action files or cache files.
1630+ let should_track_exports = in_action_file || in_cache_file;
1631+
16291632 // Pre-pass: Collect a mapping from local identifiers to export names for all exports
16301633 // in server boundary files ('use server' or 'use cache'). This mapping is used to:
16311634 // 1. Set current_export_name when visiting exported functions/variables during the main
1632- // pass
1633- // 2. Register any remaining exports in the post-pass that weren't handled by the visitor
1634- for stmt in stmts. iter ( ) {
1635- match stmt {
1636- ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDefaultExpr ( export_default_expr) ) => {
1637- if let Expr :: Ident ( ident) = & * export_default_expr. expr {
1638- self . export_name_by_local_id
1639- . insert ( ident. to_id ( ) , atom ! ( "default" ) ) ;
1640- }
1641- }
1642- ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDefaultDecl ( export_default_decl) ) => {
1643- // export default function foo() {}
1644- if let DefaultDecl :: Fn ( f) = & export_default_decl. decl {
1645- if let Some ( ident) = & f. ident {
1635+ // pass.
1636+ // 2. Register any remaining exports in the post-pass that weren't handled by the visitor.
1637+ if should_track_exports {
1638+ for stmt in stmts. iter ( ) {
1639+ match stmt {
1640+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDefaultExpr ( export_default_expr) ) => {
1641+ if let Expr :: Ident ( ident) = & * export_default_expr. expr {
16461642 self . export_name_by_local_id
16471643 . insert ( ident. to_id ( ) , atom ! ( "default" ) ) ;
16481644 }
16491645 }
1650- }
1651- ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( export_decl ) ) => {
1652- // export function foo() {} or export const bar = ...
1653- match & export_decl . decl {
1654- Decl :: Fn ( f ) => {
1655- self . export_name_by_local_id
1656- . insert ( f . ident . to_id ( ) , f . ident . sym . clone ( ) ) ;
1646+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDefaultDecl ( export_default_decl ) ) => {
1647+ // export default function foo() {}
1648+ if let DefaultDecl :: Fn ( f ) = & export_default_decl . decl {
1649+ if let Some ( ident ) = & f . ident {
1650+ self . export_name_by_local_id
1651+ . insert ( ident . to_id ( ) , atom ! ( "default" ) ) ;
1652+ }
16571653 }
1658- Decl :: Var ( var) => {
1659- for decl in & var. decls {
1660- // Collect all identifiers from the pattern (handles destructuring)
1661- let mut idents = vec ! [ ] ;
1662- collect_idents_in_pat ( & decl. name , & mut idents) ;
1663- for ident in idents {
1664- self . export_name_by_local_id
1665- . insert ( ident. to_id ( ) , ident. sym . clone ( ) ) ;
1654+ }
1655+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( export_decl) ) => {
1656+ // export function foo() {} or export const bar = ...
1657+ match & export_decl. decl {
1658+ Decl :: Fn ( f) => {
1659+ self . export_name_by_local_id
1660+ . insert ( f. ident . to_id ( ) , f. ident . sym . clone ( ) ) ;
1661+ }
1662+ Decl :: Var ( var) => {
1663+ for decl in & var. decls {
1664+ // Collect all identifiers from the pattern (handles
1665+ // destructuring)
1666+ let mut idents = vec ! [ ] ;
1667+ collect_idents_in_pat ( & decl. name , & mut idents) ;
1668+ for ident in idents {
1669+ self . export_name_by_local_id
1670+ . insert ( ident. to_id ( ) , ident. sym . clone ( ) ) ;
1671+ }
16661672 }
16671673 }
1674+ _ => { }
16681675 }
1669- _ => { }
16701676 }
1671- }
1672- ModuleItem :: ModuleDecl ( ModuleDecl :: ExportNamed ( named_export) ) => {
1673- if named_export. src . is_none ( ) {
1674- for spec in & named_export. specifiers {
1675- match spec {
1676- ExportSpecifier :: Named ( ExportNamedSpecifier {
1677- orig : ModuleExportName :: Ident ( orig) ,
1678- exported : Some ( ModuleExportName :: Ident ( exported) ) ,
1679- ..
1680- } ) => {
1681- // export { foo as bar }
1682- self . export_name_by_local_id
1683- . insert ( orig. to_id ( ) , exported. sym . clone ( ) ) ;
1684- }
1685- ExportSpecifier :: Named ( ExportNamedSpecifier {
1686- orig : ModuleExportName :: Ident ( orig) ,
1687- exported : None ,
1688- ..
1689- } ) => {
1690- // export { foo }
1691- self . export_name_by_local_id
1692- . insert ( orig. to_id ( ) , orig. sym . clone ( ) ) ;
1677+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportNamed ( named_export) ) => {
1678+ if named_export. src . is_none ( ) {
1679+ for spec in & named_export. specifiers {
1680+ match spec {
1681+ ExportSpecifier :: Named ( ExportNamedSpecifier {
1682+ orig : ModuleExportName :: Ident ( orig) ,
1683+ exported : Some ( ModuleExportName :: Ident ( exported) ) ,
1684+ ..
1685+ } ) => {
1686+ // export { foo as bar }
1687+ self . export_name_by_local_id
1688+ . insert ( orig. to_id ( ) , exported. sym . clone ( ) ) ;
1689+ }
1690+ ExportSpecifier :: Named ( ExportNamedSpecifier {
1691+ orig : ModuleExportName :: Ident ( orig) ,
1692+ exported : None ,
1693+ ..
1694+ } ) => {
1695+ // export { foo }
1696+ self . export_name_by_local_id
1697+ . insert ( orig. to_id ( ) , orig. sym . clone ( ) ) ;
1698+ }
1699+ _ => { }
16931700 }
1694- _ => { }
16951701 }
16961702 }
16971703 }
1704+ _ => { }
16981705 }
1699- _ => { }
17001706 }
17011707 }
17021708
1703- // Only track exported identifiers in action files or cache files.
1704- let should_track_exports = self . file_directive . is_some ( ) ;
1705-
17061709 let old_annotations = self . annotations . take ( ) ;
17071710 let mut new = Vec :: with_capacity ( stmts. len ( ) ) ;
17081711
17091712 // Main pass: For each statement, validate exports in server boundary files,
17101713 // visit and transform it, and add it to the output along with any hoisted items.
17111714 for mut stmt in stmts. take ( ) {
1712- // For server boundary files: validate exports and do pre-visit transformations.
17131715 if should_track_exports {
17141716 let mut disallowed_export_span = DUMMY_SP ;
17151717
0 commit comments