@@ -323,20 +323,35 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
323
323
!unexpected. trailingTrivia. isEmpty,
324
324
token. isMissing
325
325
{
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
+ }
340
355
}
341
356
}
342
357
0 commit comments