Skip to content

Commit a720201

Browse files
committed
Code refactor, not to replace a child by its index
1 parent 9c88948 commit a720201

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,15 +1059,7 @@ extension Parser {
10591059
}
10601060

10611061
// Parse the signature.
1062-
let signature = self.parseFunctionSignature()
1063-
1064-
// mark the unexpected result type of initializer
1065-
// and replace the ‘output‘ of signature with nil
1066-
var unexpectedResultType: RawUnexpectedNodesSyntax?
1067-
if let output = signature.output {
1068-
signature.raw = signature.layoutView.replacingChild(at: 5, with: nil, arena: self.arena)
1069-
unexpectedResultType = RawUnexpectedNodesSyntax([output.raw], arena: self.arena)
1070-
}
1062+
let signature = self.parseFunctionSignature(allowOutput: false)
10711063

10721064
let whereClause: RawGenericWhereClauseSyntax?
10731065
if self.at(.keyword(.where)) {
@@ -1086,7 +1078,6 @@ extension Parser {
10861078
optionalMark: failable,
10871079
genericParameterClause: generics,
10881080
signature: signature,
1089-
unexpectedResultType,
10901081
genericWhereClause: whereClause,
10911082
body: items,
10921083
arena: self.arena
@@ -1398,12 +1389,12 @@ extension Parser {
13981389
}
13991390

14001391
@_spi(RawSyntax)
1401-
public mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
1392+
public mutating func parseFunctionSignature(allowOutput: Bool = true) -> RawFunctionSignatureSyntax {
14021393
let input = self.parseParameterClause(for: .functionParameters)
14031394

14041395
var effectSpecifiers = self.parseDeclEffectSpecifiers()
14051396

1406-
let output: RawReturnClauseSyntax?
1397+
var output: RawReturnClauseSyntax?
14071398

14081399
/// Only allow recovery to the arrow with exprKeyword precedence so we only
14091400
/// skip over misplaced identifiers and don't e.g. recover to an arrow in a 'where' clause.
@@ -1413,10 +1404,19 @@ extension Parser {
14131404
output = nil
14141405
}
14151406

1407+
var unexpectedAfterOutput: RawUnexpectedNodesSyntax?
1408+
if !allowOutput,
1409+
let unexpectedOutput = output
1410+
{
1411+
output = nil
1412+
unexpectedAfterOutput = RawUnexpectedNodesSyntax([unexpectedOutput.raw], arena: self.arena)
1413+
}
1414+
14161415
return RawFunctionSignatureSyntax(
14171416
input: input,
14181417
effectSpecifiers: effectSpecifiers,
14191418
output: output,
1419+
unexpectedAfterOutput,
14201420
arena: self.arena
14211421
)
14221422
}

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,18 +753,26 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
753753
return .skipChildren
754754
}
755755

756-
if let unexpectedName = node.signature.input.unexpectedBeforeLeftParen {
756+
if let unexpectedName = node.signature.input.unexpectedBeforeLeftParen,
757+
let previous = unexpectedName.previousToken(viewMode: .sourceAccurate)
758+
{
757759
addDiagnostic(
758760
unexpectedName,
759761
.initializerCannotHaveName,
760762
fixIts: [
761-
FixIt(message: RemoveNodesFixIt(unexpectedName), changes: .makeMissing(unexpectedName))
763+
FixIt(
764+
message: RemoveNodesFixIt(unexpectedName),
765+
changes: [
766+
.makeMissing(unexpectedName),
767+
FixIt.Changes(changes: [.replaceTrailingTrivia(token: previous, newTrivia: .zero)])
768+
]
769+
)
762770
],
763771
handledNodes: [unexpectedName.id]
764772
)
765773
}
766774

767-
if let unexpectedOutput = node.unexpectedBetweenSignatureAndGenericWhereClause {
775+
if let unexpectedOutput = node.signature.unexpectedAfterOutput {
768776
addDiagnostic(
769777
unexpectedOutput,
770778
.initializerCannotHaveResultType,

0 commit comments

Comments
 (0)