Skip to content

Commit 2a455d0

Browse files
committed
Fix it to emit a single diagnostic when both open and close quote are missing
1 parent 707c617 commit 2a455d0

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,31 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
12571257
node.closeQuote.id,
12581258
].compactMap { $0 }
12591259
)
1260+
} else if node.openQuote.presence == .missing,
1261+
node.unexpectedBetweenOpenDelimiterAndOpenQuote == nil,
1262+
node.closeQuote.presence == .missing,
1263+
node.unexpectedBetweenCloseQuoteAndCloseDelimiter == nil,
1264+
!node.segments.isMissingAllTokens
1265+
{
1266+
addDiagnostic(
1267+
node,
1268+
MissingBothStringQuotesOfStringSegments(stringSegments: node.segments),
1269+
fixIts: [
1270+
FixIt(
1271+
message: InsertTokenFixIt(missingNodes: [Syntax(node.openQuote), Syntax(node.closeQuote)]),
1272+
changes: [
1273+
.makePresent(node.openQuote),
1274+
.makePresent(node.closeQuote),
1275+
]
1276+
)
1277+
],
1278+
handledNodes: [
1279+
node.openQuote.id,
1280+
node.closeQuote.id,
1281+
]
1282+
)
12601283
}
1284+
12611285
for (diagnostic, handledNodes) in MultiLineStringLiteralIndentatinDiagnosticsGenerator.diagnose(node) {
12621286
addDiagnostic(diagnostic, handledNodes: handledNodes)
12631287
}

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ public struct MissingAttributeArgument: ParserError {
386386
}
387387
}
388388

389+
public struct MissingBothStringQuotesOfStringSegments: ParserError {
390+
public let stringSegments: StringLiteralSegmentsSyntax
391+
392+
public var message: String {
393+
return #"expected \#(stringSegments.shortSingleLineContentDescription) to be surrounded by '"'"#
394+
}
395+
}
396+
389397
public struct MissingConditionInStatement: ParserError {
390398
let node: SyntaxProtocol
391399

Tests/SwiftParserTest/AttributeTests.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,14 @@ final class AttributeTests: XCTestCase {
454454

455455
assertParse(
456456
"""
457-
@_expose(Cxx, 1️⃣baz2️⃣) func foo() {}
457+
@_expose(Cxx, 1️⃣baz) func foo() {}
458458
""",
459459
diagnostics: [
460460
DiagnosticSpec(
461461
locationMarker: "1️⃣",
462-
message: #"expected '"' in string literal"#,
463-
fixIts: [#"insert '"'"#]
464-
),
465-
DiagnosticSpec(
466-
locationMarker: "2️⃣",
467-
message: #"expected '"' to end string literal"#,
468-
fixIts: [#"insert '"'"#]
469-
),
462+
message: #"expected code 'baz' to be surrounded by '"'"#,
463+
fixIts: [#"insert '"' and '"'"#]
464+
)
470465
],
471466
fixedSource: """
472467
@_expose(Cxx, "baz") func foo() {}
@@ -543,20 +538,15 @@ final class AttributeTests: XCTestCase {
543538

544539
assertParse(
545540
"""
546-
@_unavailableFromAsync(message: 1️⃣abc2️⃣)
541+
@_unavailableFromAsync(message: 1️⃣abc)
547542
func foo() {}
548543
""",
549544
diagnostics: [
550545
DiagnosticSpec(
551546
locationMarker: "1️⃣",
552-
message: #"expected '"' in string literal"#,
553-
fixIts: [#"insert '"'"#]
554-
),
555-
DiagnosticSpec(
556-
locationMarker: "2️⃣",
557-
message: #"expected '"' to end string literal"#,
558-
fixIts: [#"insert '"'"#]
559-
),
547+
message: #"expected code 'abc' to be surrounded by '"'"#,
548+
fixIts: [#"insert '"' and '"'"#]
549+
)
560550
],
561551
fixedSource: """
562552
@_unavailableFromAsync(message: "abc")

Tests/SwiftParserTest/translated/OriginalDefinedInAttrTests.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,8 @@ final class OriginalDefinedInAttrTests: XCTestCase {
8686
),
8787
DiagnosticSpec(
8888
locationMarker: "1️⃣",
89-
message: #"expected '"' in string literal"#,
90-
fixIts: [#"insert '"'"#]
91-
),
92-
DiagnosticSpec(
93-
locationMarker: "2️⃣",
94-
message: #"expected '"' to end string literal"#,
95-
fixIts: [#"insert '"'"#]
89+
message: #"expected code 'OSX' to be surrounded by '"'"#,
90+
fixIts: [#"insert '"' and '"'"#]
9691
),
9792
DiagnosticSpec(
9893
locationMarker: "2️⃣",

0 commit comments

Comments
 (0)