Skip to content

Commit 74aa602

Browse files
committed
Fix wrong foratting of multiline string literal
1 parent 9a4c5e4 commit 74aa602

11 files changed

+110
-20
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ open class BasicFormat: SyntaxRewriter {
171171
}
172172
}
173173

174-
return false
174+
let syntaxToken: Syntax = Syntax(token)
175+
switch syntaxToken.keyPathInParent {
176+
case \StringLiteralExprSyntax.closeQuote:
177+
return token.tokenKind == .multilineStringQuote
178+
default:
179+
return false
180+
}
175181
}
176182

177183
open func requiresWhitespace(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool {

Sources/SwiftParserDiagnostics/DiagnosticExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ extension FixIt.MultiNodeChange {
167167
}
168168
}
169169

170-
if let previousToken = node.previousToken(viewMode: .all),
170+
if let previousToken = node.previousToken(viewMode: .fixedUp),
171171
previousToken.presence == .present,
172172
let firstToken = node.firstToken(viewMode: .all),
173173
previousToken.trailingTrivia.allSatisfy({ $0.isWhitespace }),

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,12 +1200,10 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
12001200
}
12011201
if case .stringSegment(let segment) = node.segments.last {
12021202
if let invalidContent = segment.unexpectedBeforeContent?.onlyToken(where: { $0.trailingTrivia.contains(where: { $0.isBackslash }) }) {
1203-
let leadingTrivia = segment.content.leadingTrivia
1204-
let trailingTrivia = segment.content.trailingTrivia
12051203
let fixIt = FixIt(
12061204
message: .removeBackslash,
12071205
changes: [
1208-
.makePresent(segment.content, leadingTrivia: leadingTrivia, trailingTrivia: trailingTrivia),
1206+
.makePresent(segment.content),
12091207
.makeMissing(invalidContent, transferTrivia: false),
12101208
]
12111209
)

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ final class ExpressionTests: XCTestCase {
603603
)
604604
],
605605
fixedSource: ##"""
606-
"""""""
606+
""""
607+
"""
607608
"""##
608609
)
609610

@@ -619,7 +620,8 @@ final class ExpressionTests: XCTestCase {
619620
)
620621
],
621622
fixedSource: ##"""
622-
""""""""
623+
"""""
624+
"""
623625
"""##
624626
)
625627

@@ -656,7 +658,8 @@ final class ExpressionTests: XCTestCase {
656658
DiagnosticSpec(message: ##"expected '"""#' to end string literal"##, fixIts: [##"insert '"""#'"##])
657659
],
658660
fixedSource: ##"""
659-
#""""""#
661+
#"""
662+
"""#
660663
"""##
661664
)
662665

@@ -668,7 +671,8 @@ final class ExpressionTests: XCTestCase {
668671
DiagnosticSpec(message: ##"expected '"""#' to end string literal"##, fixIts: [##"insert '"""#'"##])
669672
],
670673
fixedSource: ##"""
671-
#"""a"""#
674+
#"""a
675+
"""#
672676
"""##
673677
)
674678

Tests/SwiftParserTest/translated/AvailabilityQueryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ final class AvailabilityQueryTests: XCTestCase {
100100
DiagnosticSpec(message: "expected ',' joining parts of a multi-clause condition", fixIts: ["replace '&&' with ','"])
101101
],
102102
fixedSource: """
103-
if #available(OSX 10.51, *) , #available(OSX 10.52, *) {
103+
if #available(OSX 10.51, *), #available(OSX 10.52, *) {
104104
}
105105
"""
106106
)
@@ -440,7 +440,7 @@ final class AvailabilityQueryTests: XCTestCase {
440440
DiagnosticSpec(message: "expected ',' joining platforms in availability condition", fixIts: ["replace '||' with ','"])
441441
],
442442
fixedSource: """
443-
if #available(OSX 10.51 , iOS 8.0) {
443+
if #available(OSX 10.51, iOS 8.0) {
444444
}
445445
"""
446446
)

Tests/SwiftParserTest/translated/AvailabilityQueryUnavailabilityTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ final class AvailabilityQueryUnavailabilityTests: XCTestCase {
103103
)
104104
],
105105
fixedSource: """
106-
if #unavailable(OSX 10.51) , #unavailable(OSX 10.52) {
106+
if #unavailable(OSX 10.51), #unavailable(OSX 10.52) {
107107
}
108108
"""
109109
)
@@ -447,7 +447,7 @@ final class AvailabilityQueryUnavailabilityTests: XCTestCase {
447447
)
448448
],
449449
fixedSource: """
450-
if #unavailable(OSX 10.51 , iOS 8.0) {
450+
if #unavailable(OSX 10.51, iOS 8.0) {
451451
}
452452
"""
453453
)

Tests/SwiftParserTest/translated/MultilineErrorsTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ final class MultilineErrorsTests: XCTestCase {
705705
],
706706
fixedSource: ##"""
707707
_ = """
708-
foo\"""
708+
foo\
709+
"""
709710
"""##
710711
)
711712
}

Tests/SwiftParserTest/translated/MultilinePoundDiagnosticArgRdar41154797Tests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ final class MultilinePoundDiagnosticArgRdar41154797Tests: XCTestCase {
3535
),
3636
],
3737
fixedSource: ##"""
38-
#error("""""")
38+
#error("""
39+
""")
3940
"""##
4041
)
4142
}

Tests/SwiftParserTest/translated/MultilineStringTests.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,80 @@ final class MultilineStringTests: XCTestCase {
411411
)
412412
}
413413

414+
func testMultilineString47() {
415+
assertParse(
416+
#"""
417+
_ = """1️⃣"""
418+
"""#,
419+
diagnostics: [
420+
DiagnosticSpec(
421+
message: "multi-line string literal closing delimiter must begin on a new line",
422+
fixIts: ["insert newline"]
423+
)
424+
],
425+
fixedSource: #"""
426+
_ = """
427+
"""
428+
"""#
429+
)
430+
}
431+
432+
func testMultilineString48() {
433+
assertParse(
434+
#"""
435+
_ = """A1️⃣"""
436+
"""#,
437+
diagnostics: [
438+
DiagnosticSpec(
439+
message: "multi-line string literal closing delimiter must begin on a new line",
440+
fixIts: ["insert newline"]
441+
)
442+
],
443+
fixedSource: #"""
444+
_ = """A
445+
"""
446+
"""#
447+
)
448+
}
449+
450+
func testMultilineString49() {
451+
assertParse(
452+
#"""
453+
_ = """1️⃣
454+
"""#,
455+
diagnostics: [
456+
DiagnosticSpec(
457+
message: #"expected '"""' to end string literal"#,
458+
fixIts: [#"insert '"""'"#]
459+
)
460+
],
461+
fixedSource: #"""
462+
_ = """
463+
"""
464+
"""#
465+
)
466+
}
467+
468+
func testMultilineString50() {
469+
assertParse(
470+
#"""
471+
_ = """
472+
A1️⃣
473+
"""#,
474+
diagnostics: [
475+
DiagnosticSpec(
476+
message: #"expected '"""' to end string literal"#,
477+
fixIts: [#"insert '"""'"#]
478+
)
479+
],
480+
fixedSource: #"""
481+
_ = """
482+
A
483+
"""
484+
"""#
485+
)
486+
}
487+
414488
func testEscapeNewlineInRawString() {
415489
assertParse(
416490
##"""

Tests/SwiftParserTest/translated/StringLiteralEofTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ final class StringLiteralEofTests: XCTestCase {
133133
fixedSource: #"""
134134
// NOTE: DO NOT add a newline at EOF.
135135
_ = """
136-
foo"""
136+
foo
137+
"""
137138
"""#
138139
)
139140
}
@@ -160,7 +161,8 @@ final class StringLiteralEofTests: XCTestCase {
160161
fixedSource: ##"""
161162
_ = """
162163
foo
163-
\(<#expression#>)"""
164+
\(<#expression#>)
165+
"""
164166
"""##
165167
// FIXME: The closing delimiter should be put on the new line
166168
)
@@ -197,7 +199,8 @@ final class StringLiteralEofTests: XCTestCase {
197199
fixedSource: ##"""
198200
_ = """
199201
foo
200-
\("bar")"""
202+
\("bar")
203+
"""
201204
"""##
202205
)
203206
}
@@ -236,7 +239,8 @@ final class StringLiteralEofTests: XCTestCase {
236239
fixedSource: ##"""
237240
_ = """
238241
\("bar"
239-
baz)"""
242+
baz)
243+
"""
240244
"""##
241245
)
242246
}

Tests/SwiftParserTest/translated/UnclosedStringInterpolationTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ final class UnclosedStringInterpolationTests: XCTestCase {
187187
fixedSource: ##"""
188188
_ = """
189189
\(
190-
"""""")"""
190+
"""
191+
""")
192+
"""
191193
"""##
192194
)
193195
}

0 commit comments

Comments
 (0)