@@ -443,33 +443,29 @@ extension IfConfigDeclSyntax {
443
443
}
444
444
}
445
445
446
+ /// Syntax visitor that writes out a syntax tree without comments or #sourceLocations.
447
+ fileprivate class DescriptionWithoutCommentsAndSourceLocationsVisitor : SyntaxVisitor {
448
+ var result : String = " "
449
+ override func visit( _ token: TokenSyntax ) -> SyntaxVisitorContinueKind {
450
+ token. leadingTrivia. writeWithoutComments ( to: & result)
451
+ token. text. write ( to: & result)
452
+ token. trailingTrivia. writeWithoutComments ( to: & result)
453
+ return . skipChildren
454
+ }
455
+ override func visit( _ node: PoundSourceLocationSyntax ) -> SyntaxVisitorContinueKind {
456
+ return . skipChildren
457
+ }
458
+ }
459
+
446
460
extension SyntaxProtocol {
447
461
// Produce the source code for this syntax node with all of the comments
448
462
// and #sourceLocations removed. Each comment will be replaced with either
449
463
// a newline or a space, depending on whether the comment involved a newline.
450
464
@_spi ( Compiler)
451
465
public var descriptionWithoutCommentsAndSourceLocations : String {
452
- var result = " "
453
- var skipUntilRParen = false
454
- for token in tokens ( viewMode: . sourceAccurate) {
455
- // Skip #sourceLocation(...).
456
- if token. tokenKind == . poundSourceLocation {
457
- skipUntilRParen = true
458
- continue
459
- }
460
-
461
- if skipUntilRParen {
462
- if token. tokenKind == . rightParen {
463
- skipUntilRParen = false
464
- }
465
- continue
466
- }
467
-
468
- token. leadingTrivia. writeWithoutComments ( to: & result)
469
- token. text. write ( to: & result)
470
- token. trailingTrivia. writeWithoutComments ( to: & result)
471
- }
472
- return result
466
+ let visitor = DescriptionWithoutCommentsAndSourceLocationsVisitor ( viewMode: . sourceAccurate)
467
+ visitor. walk ( self )
468
+ return visitor. result
473
469
}
474
470
}
475
471
0 commit comments