Skip to content

Commit e65a90e

Browse files
committed
Fix it to emit a single diagnostic when both open and close quote are missing
1 parent 4c0ad1b commit e65a90e

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

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

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 \(nodesDescription([stringSegments], format: false)) 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 'baz' to be surrounded by '"'"#,
463+
fixIts: [#"insert '""'"#]
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 'abc' to be surrounded by '"'"#,
548+
fixIts: [#"insert '""'"#]
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 'OSX' to be surrounded by '"'"#,
90+
fixIts: [#"insert '""'"#]
9691
),
9792
DiagnosticSpec(
9893
locationMarker: "2️⃣",

0 commit comments

Comments
 (0)