File tree Expand file tree Collapse file tree
crates/syntax/src/ast/syntax_factory Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1496,7 +1496,20 @@ impl SyntaxFactory {
14961496 if let Some ( mut mapping) = self . mappings ( ) {
14971497 let mut builder = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
14981498
1499- builder. map_node ( name. syntax ( ) . clone ( ) , ast. name_ref ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
1499+ if let Some ( ast_name_ref) = ast. name_ref ( ) {
1500+ // NameRef is a direct child
1501+ builder. map_node ( name. syntax ( ) . clone ( ) , ast_name_ref. syntax ( ) . clone ( ) ) ;
1502+ } else {
1503+ // NameRef is nested inside PathExpr > Path > PathSegment.
1504+ // map_node requires the output to be a direct child of the builder's parent, so
1505+ // we need a separate builder scoped to PathSegment.
1506+ let ast:: Expr :: PathExpr ( path_expr) = ast. expr ( ) . unwrap ( ) else { unreachable ! ( ) } ;
1507+ let path_segment = path_expr. path ( ) . unwrap ( ) . segment ( ) . unwrap ( ) ;
1508+ let inner_name_ref = path_segment. name_ref ( ) . unwrap ( ) ;
1509+ let mut inner_builder = SyntaxMappingBuilder :: new ( path_segment. syntax ( ) . clone ( ) ) ;
1510+ inner_builder. map_node ( name. syntax ( ) . clone ( ) , inner_name_ref. syntax ( ) . clone ( ) ) ;
1511+ inner_builder. finish ( & mut mapping) ;
1512+ }
15001513 if let Some ( expr) = expr {
15011514 builder. map_node ( expr. syntax ( ) . clone ( ) , ast. expr ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
15021515 }
You can’t perform that action at this time.
0 commit comments