Skip to content

Commit c1e5981

Browse files
committed
Code refactor, not to replace a child by its index
1 parent 382f597 commit c1e5981

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
@@ -1039,15 +1039,7 @@ extension Parser {
10391039
}
10401040

10411041
// Parse the signature.
1042-
let signature = self.parseFunctionSignature()
1043-
1044-
// mark the unexpected result type of initializer
1045-
// and replace the ‘output‘ of signature with nil
1046-
var unexpectedResultType: RawUnexpectedNodesSyntax?
1047-
if let output = signature.output {
1048-
signature.raw = signature.layoutView.replacingChild(at: 5, with: nil, arena: self.arena)
1049-
unexpectedResultType = RawUnexpectedNodesSyntax([output.raw], arena: self.arena)
1050-
}
1042+
let signature = self.parseFunctionSignature(allowOutput: false)
10511043

10521044
let whereClause: RawGenericWhereClauseSyntax?
10531045
if self.at(.keyword(.where)) {
@@ -1066,7 +1058,6 @@ extension Parser {
10661058
optionalMark: failable,
10671059
genericParameterClause: generics,
10681060
signature: signature,
1069-
unexpectedResultType,
10701061
genericWhereClause: whereClause,
10711062
body: items,
10721063
arena: self.arena
@@ -1378,23 +1369,32 @@ extension Parser {
13781369
}
13791370

13801371
@_spi(RawSyntax)
1381-
public mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
1372+
public mutating func parseFunctionSignature(allowOutput: Bool = true) -> RawFunctionSignatureSyntax {
13821373
let input = self.parseParameterClause(for: .functionParameters)
13831374

13841375
var effectSpecifiers = self.parseDeclEffectSpecifiers()
13851376

1386-
let output: RawReturnClauseSyntax?
1377+
var output: RawReturnClauseSyntax?
13871378

13881379
if self.at(.arrow) || self.canRecoverTo(TokenSpec(.arrow, allowAtStartOfLine: false)) != nil {
13891380
output = self.parseFunctionReturnClause(effectSpecifiers: &effectSpecifiers, allowNamedOpaqueResultType: true)
13901381
} else {
13911382
output = nil
13921383
}
13931384

1385+
var unexpectedAfterOutput: RawUnexpectedNodesSyntax?
1386+
if !allowOutput,
1387+
let unexpectedOutput = output
1388+
{
1389+
output = nil
1390+
unexpectedAfterOutput = RawUnexpectedNodesSyntax([unexpectedOutput.raw], arena: self.arena)
1391+
}
1392+
13941393
return RawFunctionSignatureSyntax(
13951394
input: input,
13961395
effectSpecifiers: effectSpecifiers,
13971396
output: output,
1397+
unexpectedAfterOutput,
13981398
arena: self.arena
13991399
)
14001400
}

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,18 +720,26 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
720720
return .skipChildren
721721
}
722722

723-
if let unexpectedName = node.signature.input.unexpectedBeforeLeftParen {
723+
if let unexpectedName = node.signature.input.unexpectedBeforeLeftParen,
724+
let previous = unexpectedName.previousToken(viewMode: .sourceAccurate)
725+
{
724726
addDiagnostic(
725727
unexpectedName,
726728
.initializerCannotHaveName,
727729
fixIts: [
728-
FixIt(message: RemoveNodesFixIt(unexpectedName), changes: .makeMissing(unexpectedName))
730+
FixIt(
731+
message: RemoveNodesFixIt(unexpectedName),
732+
changes: [
733+
.makeMissing(unexpectedName),
734+
FixIt.Changes(changes: [.replaceTrailingTrivia(token: previous, newTrivia: .zero)])
735+
]
736+
)
729737
],
730738
handledNodes: [unexpectedName.id]
731739
)
732740
}
733741

734-
if let unexpectedOutput = node.unexpectedBetweenSignatureAndGenericWhereClause {
742+
if let unexpectedOutput = node.signature.unexpectedAfterOutput {
735743
addDiagnostic(
736744
unexpectedOutput,
737745
.initializerCannotHaveResultType,

0 commit comments

Comments
 (0)