Skip to content

Commit 873cbce

Browse files
authored
Merge pull request #1703 from TTOzzi/fix-emit-single-diag-when-both-open-close-quote-missing
Fix it to emit a single diagnostic when both open and close quote are missing
2 parents c542b57 + 2a455d0 commit 873cbce

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
@@ -1271,7 +1271,31 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
12711271
node.closeQuote.id,
12721272
].compactMap { $0 }
12731273
)
1274+
} else if node.openQuote.presence == .missing,
1275+
node.unexpectedBetweenOpenDelimiterAndOpenQuote == nil,
1276+
node.closeQuote.presence == .missing,
1277+
node.unexpectedBetweenCloseQuoteAndCloseDelimiter == nil,
1278+
!node.segments.isMissingAllTokens
1279+
{
1280+
addDiagnostic(
1281+
node,
1282+
MissingBothStringQuotesOfStringSegments(stringSegments: node.segments),
1283+
fixIts: [
1284+
FixIt(
1285+
message: InsertTokenFixIt(missingNodes: [Syntax(node.openQuote), Syntax(node.closeQuote)]),
1286+
changes: [
1287+
.makePresent(node.openQuote),
1288+
.makePresent(node.closeQuote),
1289+
]
1290+
)
1291+
],
1292+
handledNodes: [
1293+
node.openQuote.id,
1294+
node.closeQuote.id,
1295+
]
1296+
)
12741297
}
1298+
12751299
for (diagnostic, handledNodes) in MultiLineStringLiteralIndentatinDiagnosticsGenerator.diagnose(node) {
12761300
addDiagnostic(diagnostic, handledNodes: handledNodes)
12771301
}

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@ public struct MissingAttributeArgument: ParserError {
389389
}
390390
}
391391

392+
public struct MissingBothStringQuotesOfStringSegments: ParserError {
393+
public let stringSegments: StringLiteralSegmentsSyntax
394+
395+
public var message: String {
396+
return #"expected \#(stringSegments.shortSingleLineContentDescription) to be surrounded by '"'"#
397+
}
398+
}
399+
392400
public struct MissingConditionInStatement: ParserError {
393401
let node: SyntaxProtocol
394402

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)