Skip to content

Commit c8928df

Browse files
committed
Emit a single diagnostic if # and a non-identifier token are separate by a space
1 parent d9f839c commit c8928df

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
@@ -303,20 +303,35 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
303303
!unexpected.trailingTrivia.isEmpty,
304304
token.isMissing
305305
{
306-
let fixIt = FixIt(
307-
message: .removeExtraneousWhitespace,
308-
changes: [
309-
.makeMissing(unexpected, transferTrivia: false), // don't transfer trivia because trivia is the issue here
310-
.makePresent(token, leadingTrivia: unexpected.leadingTrivia),
311-
]
312-
)
313-
addDiagnostic(
314-
token,
315-
position: unexpected.endPositionBeforeTrailingTrivia,
316-
ExtraneousWhitespace(tokenWithWhitespace: unexpected),
317-
fixIts: [fixIt],
318-
handledNodes: [token.id, unexpected.id]
319-
)
306+
let changes: [FixIt.MultiNodeChange] = [
307+
.makeMissing(unexpected, transferTrivia: false), // don't transfer trivia because trivia is the issue here
308+
.makePresent(token, leadingTrivia: unexpected.leadingTrivia),
309+
]
310+
if let nextToken = token.nextToken(viewMode: .all),
311+
nextToken.isMissing
312+
{
313+
// If the next token is missing, the problem here isn’t actually the
314+
// space after token but that the missing token should be added after
315+
// `token` without a space. Generate a diagnsotic for that.
316+
_ = handleMissingSyntax(
317+
nextToken,
318+
overridePosition: unexpected.endPositionBeforeTrailingTrivia,
319+
additionalChanges: changes,
320+
additionalHandledNodes: [unexpected.id, token.id]
321+
)
322+
} else {
323+
let fixIt = FixIt(
324+
message: .removeExtraneousWhitespace,
325+
changes: changes
326+
)
327+
addDiagnostic(
328+
token,
329+
position: unexpected.endPositionBeforeTrailingTrivia,
330+
ExtraneousWhitespace(tokenWithWhitespace: unexpected),
331+
fixIts: [fixIt],
332+
handledNodes: [token.id, unexpected.id]
333+
)
334+
}
320335
}
321336
}
322337

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)