@@ -55,39 +55,43 @@ public struct CallToTrailingClosures: SyntaxRefactoringProvider {
55
55
56
56
/// Apply the refactoring to a given syntax node. If either a
57
57
/// non-function-like syntax node is passed, or the refactoring fails,
58
- /// `nil` is returned.
59
- // TODO: Rather than returning nil, we should consider throwing errors with
60
- // appropriate messages instead.
58
+ /// error is thrown.
61
59
public static func refactor(
62
60
syntax: Syntax ,
63
61
in context: Context = Context ( )
64
- ) -> Syntax ? {
65
- guard let call = syntax. asProtocol ( CallLikeSyntax . self) else { return nil }
66
- return Syntax ( fromProtocol: _refactor ( syntax: call, in: context) )
62
+ ) throws -> Syntax {
63
+ guard let call = syntax. asProtocol ( CallLikeSyntax . self) else {
64
+ throw RefactoringNotApplicableError ( " not a call " )
65
+ }
66
+ return try Syntax ( fromProtocol: _refactor ( syntax: call, in: context) )
67
67
}
68
68
69
69
@available ( * , deprecated, message: " Pass a Syntax argument instead of FunctionCallExprSyntax " )
70
70
public static func refactor(
71
71
syntax call: FunctionCallExprSyntax ,
72
72
in context: Context = Context ( )
73
- ) -> FunctionCallExprSyntax ? {
74
- _refactor ( syntax: call, in: context)
73
+ ) throws -> FunctionCallExprSyntax {
74
+ try _refactor ( syntax: call, in: context)
75
75
}
76
76
77
77
internal static func _refactor< C: CallLikeSyntax > (
78
78
syntax call: C ,
79
79
in context: Context = Context ( )
80
- ) -> C ? {
81
- let converted = call. convertToTrailingClosures ( from: context. startAtArgument)
82
- return converted? . formatted ( ) . as ( C . self)
80
+ ) throws -> C {
81
+ let converted = try call. convertToTrailingClosures ( from: context. startAtArgument)
82
+
83
+ guard let formatted = converted. formatted ( ) . as ( C . self) else {
84
+ throw RefactoringNotApplicableError ( " cannot cast formatted call to \( C . self) " )
85
+ }
86
+
87
+ return formatted
83
88
}
84
89
}
85
90
86
91
extension CallLikeSyntax {
87
- fileprivate func convertToTrailingClosures( from startAtArgument: Int ) -> Self ? {
92
+ fileprivate func convertToTrailingClosures( from startAtArgument: Int ) throws -> Self {
88
93
guard trailingClosure == nil , additionalTrailingClosures. isEmpty, leftParen != nil , rightParen != nil else {
89
- // Already have trailing closures
90
- return nil
94
+ throw RefactoringNotApplicableError ( " call already uses trailing closures " )
91
95
}
92
96
93
97
var closures = [ ( original: LabeledExprSyntax, closure: ClosureExprSyntax) ] ( )
@@ -106,7 +110,7 @@ extension CallLikeSyntax {
106
110
}
107
111
108
112
guard !closures. isEmpty else {
109
- return nil
113
+ throw RefactoringNotApplicableError ( " no arguments to convert to closures " )
110
114
}
111
115
112
116
// First trailing closure won't have label/colon. Transfer their trivia.
0 commit comments