@@ -40,7 +40,7 @@ fn remove_derives_up_to(item: ast::Item, attr_index: usize) -> ast::Item {
4040 attr. path ( ) . and_then ( |path| path. as_single_segment ( ) ) . and_then ( |seg| seg. name_ref ( ) )
4141 {
4242 if name. as_name ( ) == name ! [ derive] {
43- attr . syntax ( ) . detach ( ) ;
43+ replace_attr ( & item , & attr ) ;
4444 }
4545 }
4646 }
@@ -54,10 +54,14 @@ fn remove_attr_invoc(item: ast::Item, attr_index: usize) -> ast::Item {
5454 . attrs ( )
5555 . nth ( attr_index)
5656 . unwrap_or_else ( || panic ! ( "cannot find attribute #{}" , attr_index) ) ;
57+ replace_attr ( & item, & attr) ;
58+ item
59+ }
60+
61+ fn replace_attr ( item : & ast:: Item , attr : & ast:: Attr ) {
5762 let syntax_index = attr. syntax ( ) . index ( ) ;
5863 let ws = make:: tokens:: whitespace ( & " " . repeat ( u32:: from ( attr. syntax ( ) . text ( ) . len ( ) ) as usize ) ) ;
5964 item. syntax ( ) . splice_children ( syntax_index..syntax_index + 1 , vec ! [ ws. into( ) ] ) ;
60- item
6165}
6266
6367#[ cfg( test) ]
@@ -78,7 +82,9 @@ mod tests {
7882 assert_eq ! ( items. len( ) , 1 ) ;
7983
8084 let item = remove_derives_up_to ( items. pop ( ) . unwrap ( ) , attr) ;
81- expect. assert_eq ( & item. to_string ( ) ) ;
85+ let res: String =
86+ item. syntax ( ) . children_with_tokens ( ) . map ( |e| format ! ( "{:?}\n " , e) ) . collect ( ) ;
87+ expect. assert_eq ( & res) ;
8288 }
8389
8490 #[ test]
@@ -95,13 +101,20 @@ struct A {
95101}
96102 "# ,
97103 expect ! [ [ r#"
98- #[allow(unused)]
99-
100-
101- #[derive(Clone)]
102- struct A {
103- bar: u32
104- }"# ] ] ,
104+ Node(ATTR@0..16)
105+ Token(WHITESPACE@16..17 "\n")
106+ Token(WHITESPACE@17..32 " ")
107+ Token(WHITESPACE@32..33 "\n")
108+ Token(WHITESPACE@33..49 " ")
109+ Token(WHITESPACE@49..50 "\n")
110+ Node(ATTR@50..66)
111+ Token(WHITESPACE@66..67 "\n")
112+ Token(STRUCT_KW@67..73 "struct")
113+ Token(WHITESPACE@73..74 " ")
114+ Node(NAME@74..75)
115+ Token(WHITESPACE@75..76 " ")
116+ Node(RECORD_FIELD_LIST@76..92)
117+ "# ] ] ,
105118 ) ;
106119 }
107120}
0 commit comments