@@ -443,33 +443,29 @@ extension IfConfigDeclSyntax {
443443 }
444444}
445445
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+
446460extension SyntaxProtocol {
447461 // Produce the source code for this syntax node with all of the comments
448462 // and #sourceLocations removed. Each comment will be replaced with either
449463 // a newline or a space, depending on whether the comment involved a newline.
450464 @_spi ( Compiler)
451465 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
473469 }
474470}
475471
0 commit comments