Skip to content

Commit 73a3a6c

Browse files
committed
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`
1 parent 93e905c commit 73a3a6c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,26 @@ final class InvalidTests: XCTestCase {
307307
func testInvalid13() {
308308
// https://github.com/apple/swift/issues/43190
309309
// Crash with invalid parameter declaration
310+
311+
// TODO: Remove the trailing trivia from the firstName node (`s` and space).
310312
assertParse(
311313
"""
312314
do {
313315
class Starfish {}
314316
struct Salmon {}
315-
func f(s Starfish1️⃣,
317+
func f(s 1️⃣Starfish,
316318
_ ss: Salmon) -> [Int] {}
317319
func g() { f(Starfish(), Salmon()) }
318320
}
319321
""",
320322
diagnostics: [
321-
DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':' and type"])
323+
DiagnosticSpec(message: "expected ':' in parameter", fixIts: ["insert ':'"])
322324
],
323325
fixedSource: """
324326
do {
325327
class Starfish {}
326328
struct Salmon {}
327-
func f(s Starfish: <#type#>,
329+
func f(s: Starfish,
328330
_ ss: Salmon) -> [Int] {}
329331
func g() { f(Starfish(), Salmon()) }
330332
}

0 commit comments

Comments
 (0)