File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -188,8 +188,10 @@ struct AbsoluteRawSyntax {
188
188
return nil
189
189
}
190
190
191
- func replacingSelf( _ newRaw: RawSyntax ) -> AbsoluteRawSyntax {
192
- return . init( raw: newRaw, info: info)
191
+ func replacingSelf( _ newRaw: RawSyntax , newRootId: UInt32 ) -> AbsoluteRawSyntax {
192
+ let nodeId = SyntaxIdentifier ( rootId: newRootId, indexInTree: info. nodeId. indexInTree)
193
+ let newInfo = AbsoluteSyntaxInfo ( position: info. position, nodeId: nodeId)
194
+ return . init( raw: newRaw, info: newInfo)
193
195
}
194
196
195
197
static func forRoot( _ raw: RawSyntax ) -> AbsoluteRawSyntax {
@@ -334,7 +336,7 @@ struct SyntaxData {
334
336
if let parent = parent {
335
337
let parentData = parent. data. replacingChild ( newRaw, at: indexInParent)
336
338
let newParent = Syntax ( parentData)
337
- return SyntaxData ( absoluteRaw. replacingSelf ( newRaw) , parent: newParent)
339
+ return SyntaxData ( absoluteRaw. replacingSelf ( newRaw, newRootId : parentData . nodeId . rootId ) , parent: newParent)
338
340
} else {
339
341
// Otherwise, we're already the root, so return the new root data.
340
342
return . forRoot( newRaw)
Original file line number Diff line number Diff line change @@ -94,4 +94,19 @@ public class SyntaxVisitorTests: XCTestCase {
94
94
XCTAssertEqual ( hashBefore, parsed. hashValue)
95
95
} ( ) )
96
96
}
97
+
98
+ public func testRewriteTrivia( ) {
99
+ class TriviaRemover : SyntaxRewriter {
100
+ override func visit( _ token: TokenSyntax ) -> Syntax {
101
+ return Syntax ( token. withTrailingTrivia ( . zero) )
102
+ }
103
+ }
104
+
105
+ XCTAssertNoThrow ( try {
106
+ let parsed = try SyntaxParser . parse ( source: " let a = 5 " )
107
+ let visitor = TriviaRemover ( )
108
+ let rewritten = visitor. visit ( parsed)
109
+ XCTAssertEqual ( rewritten. description, " leta=5 " )
110
+ } ( ) )
111
+ }
97
112
}
You can’t perform that action at this time.
0 commit comments