Skip to content

Commit 7036a41

Browse files
committed
Expand descriptionWithoutComments to also remove #sourceLocation
1 parent 392167b commit 7036a41

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Sources/SwiftIfConfig/ActiveSyntaxRewriter.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,26 @@ extension IfConfigDeclSyntax {
382382

383383
extension SyntaxProtocol {
384384
// Produce the source code for this syntax node with all of the comments
385-
// removed. Each comment will be replaced with either a newline or a space,
386-
// depending on whether the comment involved a newline.
385+
// and #sourceLocations removed. Each comment will be replaced with either
386+
// a newline or a space, depending on whether the comment involved a newline.
387387
@_spi(Compiler)
388-
public var descriptionWithoutComments: String {
388+
public var descriptionWithoutCommentsAndSourceLocations: String {
389389
var result = ""
390+
var skipUntilRParen = false
390391
for token in tokens(viewMode: .sourceAccurate) {
392+
// Skip #sourceLocation(...).
393+
if token.tokenKind == .poundSourceLocation {
394+
skipUntilRParen = true
395+
continue
396+
}
397+
398+
if skipUntilRParen {
399+
if token.tokenKind == .rightParen {
400+
skipUntilRParen = false
401+
}
402+
continue
403+
}
404+
391405
token.leadingTrivia.writeWithoutComments(to: &result)
392406
token.text.write(to: &result)
393407
token.trailingTrivia.writeWithoutComments(to: &result)

Tests/SwiftIfConfigTest/VisitorTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,13 @@ public class VisitorTests: XCTestCase {
300300
)
301301
}
302302

303-
func testRemoveComments() {
303+
func testRemoveCommentsAndSourceLocations() {
304304
let original: SourceFileSyntax = """
305305
306306
/// This is a documentation comment
307307
func f() { }
308308
309+
#sourceLocation(file: "if-configs.swift", line: 200)
309310
/** Another documentation comment
310311
that is split across
311312
multiple lines */
@@ -318,14 +319,13 @@ public class VisitorTests: XCTestCase {
318319
"""
319320

320321
assertStringsEqualWithDiff(
321-
original.descriptionWithoutComments,
322+
original.descriptionWithoutCommentsAndSourceLocations,
322323
"""
323324
324325
325326
func f() { }
326327
327328
328-
329329
func g() { }
330330
331331
func h() {

0 commit comments

Comments
 (0)