Skip to content

Commit 8bd739d

Browse files
committed
Emit a single diagnostic if # and a non-identifier token are separate by a space
1 parent 236c57f commit 8bd739d

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,35 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
323323
!unexpected.trailingTrivia.isEmpty,
324324
token.isMissing
325325
{
326-
let fixIt = FixIt(
327-
message: .removeExtraneousWhitespace,
328-
changes: [
329-
.makeMissing(unexpected, transferTrivia: false), // don't transfer trivia because trivia is the issue here
330-
.makePresent(token, leadingTrivia: unexpected.leadingTrivia),
331-
]
332-
)
333-
addDiagnostic(
334-
token,
335-
position: unexpected.endPositionBeforeTrailingTrivia,
336-
ExtraneousWhitespace(tokenWithWhitespace: unexpected),
337-
fixIts: [fixIt],
338-
handledNodes: [token.id, unexpected.id]
339-
)
326+
let changes: [FixIt.MultiNodeChange] = [
327+
.makeMissing(unexpected, transferTrivia: false), // don't transfer trivia because trivia is the issue here
328+
.makePresent(token, leadingTrivia: unexpected.leadingTrivia),
329+
]
330+
if let nextToken = token.nextToken(viewMode: .all),
331+
nextToken.isMissing
332+
{
333+
// If the next token is missing, the problem here isn’t actually the
334+
// space after token but that the missing token should be added after
335+
// `token` without a space. Generate a diagnsotic for that.
336+
_ = handleMissingSyntax(
337+
nextToken,
338+
overridePosition: unexpected.endPositionBeforeTrailingTrivia,
339+
additionalChanges: changes,
340+
additionalHandledNodes: [unexpected.id, token.id]
341+
)
342+
} else {
343+
let fixIt = FixIt(
344+
message: .removeExtraneousWhitespace,
345+
changes: changes
346+
)
347+
addDiagnostic(
348+
token,
349+
position: unexpected.endPositionBeforeTrailingTrivia,
350+
ExtraneousWhitespace(tokenWithWhitespace: unexpected),
351+
fixIts: [fixIt],
352+
handledNodes: [token.id, unexpected.id]
353+
)
354+
}
340355
}
341356
}
342357

Tests/SwiftParserTest/RegexLiteralTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,11 +753,10 @@ final class RegexLiteralTests: XCTestCase {
753753
func testBinOpDisambiguation26() {
754754
assertParse(
755755
"""
756-
#1️⃣ 2️⃣/^ x/
756+
#1️⃣ /^ x/
757757
""",
758758
diagnostics: [
759-
DiagnosticSpec(locationMarker: "1️⃣", message: "extraneous whitespace after '#' is not permitted", fixIts: ["remove whitespace"]),
760-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion", fixIts: ["insert identifier"]),
759+
DiagnosticSpec(message: "expected identifier in macro expansion", fixIts: ["insert identifier"])
761760
],
762761
fixedSource: """
763762
#<#identifier#> /^ x/

0 commit comments

Comments
 (0)