Skip to content

Commit b3d0d88

Browse files
authored
Merge pull request #1679 from TiagoMaiaL/fix-colon-fixit-for-function-signature
[Parser] Improve FixIt for function parameter with missing type (`name1 Name2`)
2 parents 6645096 + b4396f9 commit b3d0d88

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Sources/SwiftParser/Parameters.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,30 @@ extension Parser {
9292
let modifiers = parseParameterModifiers(isClosure: false)
9393
let misplacedSpecifiers = parseMisplacedSpecifiers()
9494

95-
let names = self.parseParameterNames()
95+
var names = self.parseParameterNames()
9696
let (unexpectedBeforeColon, colon) = self.expect(.colon)
9797

98-
let type = self.parseType(misplacedSpecifiers: misplacedSpecifiers)
98+
let type: RawTypeSyntax
99+
100+
if colon.presence == .missing, let secondName = names.secondName, secondName.tokenText.isStartingWithUppercase {
101+
// Synthesize the secondName parameter as a type node.
102+
type = RawTypeSyntax(
103+
RawSimpleTypeIdentifierSyntax(
104+
name: secondName,
105+
genericArgumentClause: nil,
106+
arena: self.arena
107+
)
108+
)
109+
names = ParameterNames(
110+
unexpectedBeforeFirstName: names.unexpectedBeforeFirstName,
111+
firstName: names.firstName,
112+
unexpectedBeforeSecondName: nil,
113+
secondName: nil
114+
)
115+
} else {
116+
// Parse the type node as we would normally do.
117+
type = self.parseType(misplacedSpecifiers: misplacedSpecifiers)
118+
}
99119

100120
let ellipsis = self.consumeIfContextualPunctuator("...", remapping: .ellipsis)
101121

Tests/SwiftParserTest/translated/InvalidTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,19 @@ final class InvalidTests: XCTestCase {
312312
do {
313313
class Starfish {}
314314
struct Salmon {}
315-
func f(s Starfish1️⃣,
315+
func f(s 1️⃣Starfish,
316316
_ ss: Salmon) -> [Int] {}
317317
func g() { f(Starfish(), Salmon()) }
318318
}
319319
""",
320320
diagnostics: [
321-
DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':' and type"])
321+
DiagnosticSpec(message: "expected ':' in parameter", fixIts: ["insert ':'"])
322322
],
323323
fixedSource: """
324324
do {
325325
class Starfish {}
326326
struct Salmon {}
327-
func f(s Starfish: <#type#>,
327+
func f(s: Starfish,
328328
_ ss: Salmon) -> [Int] {}
329329
func g() { f(Starfish(), Salmon()) }
330330
}

0 commit comments

Comments
 (0)