From b4396f9fe1bcdafee204bd64902bb25749178fe7 Mon Sep 17 00:00:00 2001 From: TiagoMaiaL Date: Fri, 19 May 2023 00:24:16 -0300 Subject: [PATCH] Improve FixIt for function param with missing type If the `node.secondName` starts with a capital letter, we now assume the user wanted it to be the type of the parameter. This commit makes the following changes to the FixIt of `name1 Name2`: - from `name1 Name2: TypeText` - to `name1: Name2` --- Sources/SwiftParser/Parameters.swift | 24 +++++++++++++++++-- .../translated/InvalidTests.swift | 6 ++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftParser/Parameters.swift b/Sources/SwiftParser/Parameters.swift index 83867b21b9a..f689060d75b 100644 --- a/Sources/SwiftParser/Parameters.swift +++ b/Sources/SwiftParser/Parameters.swift @@ -92,10 +92,30 @@ extension Parser { let modifiers = parseParameterModifiers(isClosure: false) let misplacedSpecifiers = parseMisplacedSpecifiers() - let names = self.parseParameterNames() + var names = self.parseParameterNames() let (unexpectedBeforeColon, colon) = self.expect(.colon) - let type = self.parseType(misplacedSpecifiers: misplacedSpecifiers) + let type: RawTypeSyntax + + if colon.presence == .missing, let secondName = names.secondName, secondName.tokenText.isStartingWithUppercase { + // Synthesize the secondName parameter as a type node. + type = RawTypeSyntax( + RawSimpleTypeIdentifierSyntax( + name: secondName, + genericArgumentClause: nil, + arena: self.arena + ) + ) + names = ParameterNames( + unexpectedBeforeFirstName: names.unexpectedBeforeFirstName, + firstName: names.firstName, + unexpectedBeforeSecondName: nil, + secondName: nil + ) + } else { + // Parse the type node as we would normally do. + type = self.parseType(misplacedSpecifiers: misplacedSpecifiers) + } let ellipsis = self.consumeIfContextualPunctuator("...", remapping: .ellipsis) diff --git a/Tests/SwiftParserTest/translated/InvalidTests.swift b/Tests/SwiftParserTest/translated/InvalidTests.swift index b411b4f91a6..e94655871e4 100644 --- a/Tests/SwiftParserTest/translated/InvalidTests.swift +++ b/Tests/SwiftParserTest/translated/InvalidTests.swift @@ -312,19 +312,19 @@ final class InvalidTests: XCTestCase { do { class Starfish {} struct Salmon {} - func f(s Starfish1️⃣, + func f(s 1️⃣Starfish, _ ss: Salmon) -> [Int] {} func g() { f(Starfish(), Salmon()) } } """, diagnostics: [ - DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':' and type"]) + DiagnosticSpec(message: "expected ':' in parameter", fixIts: ["insert ':'"]) ], fixedSource: """ do { class Starfish {} struct Salmon {} - func f(s Starfish: <#type#>, + func f(s: Starfish, _ ss: Salmon) -> [Int] {} func g() { f(Starfish(), Salmon()) } }