Skip to content

Commit c217d98

Browse files
authored
Merge pull request #1813 from kishikawakatsumi/literal
Fixes wrong escape when creating a string literal where a pound sign follows a sequence of double quotes or backslashes
2 parents 6507f71 + 60ac579 commit c217d98

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ extension StringLiteralExprSyntax {
287287
case (true, _) where c.unicodeScalars.contains("#"):
288288
consecutivePounds += 1
289289
maxPounds = max(maxPounds, consecutivePounds)
290+
case (true, "\""), (true, "\\"):
291+
continue
290292
case (true, _):
291293
countingPounds = false
292294
consecutivePounds = 0

Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
7171
)
7272
}
7373
74+
func testEscapePoundsAfterConsecutiveQuotes() {
75+
assertBuildResult(
76+
StringLiteralExprSyntax(content: ##"foobar""#"##),
77+
"""
78+
##"foobar""#"##
79+
"""
80+
)
81+
}
82+
83+
func testEscapePoundsAfterConsecutiveBackslashes() {
84+
assertBuildResult(
85+
StringLiteralExprSyntax(content: ##"foobar\\#"##),
86+
##"""
87+
##"foobar\\#"##
88+
"""##
89+
)
90+
}
91+
7492
func testEscapePoundEmojis() {
7593
assertBuildResult(
7694
StringLiteralExprSyntax(content: ##"foo"#️⃣"bar"##),

0 commit comments

Comments
 (0)