Skip to content

Commit 0686dc7

Browse files
committed
Don't allow escaping newlines in single-line literals
We were missing a check for multi-line literals here.
1 parent ab63863 commit 0686dc7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@ extension Lexer.Cursor {
17771777
error: error,
17781778
stateTransition: .push(newState: .inStringInterpolationStart(stringLiteralKind: stringLiteralKind))
17791779
)
1780-
} else if self.isAtEscapedNewline(delimiterLength: delimiterLength) {
1780+
} else if stringLiteralKind == .multiLine && self.isAtEscapedNewline(delimiterLength: delimiterLength) {
17811781
return Lexer.Result(
17821782
.stringSegment,
17831783
trailingTriviaLexingMode: .escapedNewlineInMultiLineStringLiteral

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,21 @@ final class StatementExpressionTests: XCTestCase {
16661666
)
16671667
}
16681668

1669+
1670+
func testUnterminatedString6() {
1671+
AssertParse(
1672+
#"""
1673+
"abc1️⃣\2️⃣
1674+
(def)3️⃣"4️⃣
1675+
"""#,
1676+
diagnostics: [
1677+
DiagnosticSpec(locationMarker: "1️⃣", message: "invalid escape sequence in literal"),
1678+
DiagnosticSpec(locationMarker: "2️⃣", message: #"expected '"' to end string literal"#),
1679+
DiagnosticSpec(locationMarker: "3️⃣", message: "consecutive statements on a line must be separated by ';'"),
1680+
DiagnosticSpec(locationMarker: "4️⃣", message: #"expected '"' to end string literal"#),
1681+
]
1682+
)
1683+
}
16691684
func testStringLiteralAfterKeyPath() {
16701685
AssertParse(
16711686
#"""

0 commit comments

Comments
 (0)