@@ -55,39 +55,43 @@ public struct CallToTrailingClosures: SyntaxRefactoringProvider {
5555
5656 /// Apply the refactoring to a given syntax node. If either a
5757 /// 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+ /// an error is thrown.
6159 public static func refactor(
6260 syntax: Syntax ,
6361 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) )
6767 }
6868
6969 @available ( * , deprecated, message: " Pass a Syntax argument instead of FunctionCallExprSyntax " )
7070 public static func refactor(
7171 syntax call: FunctionCallExprSyntax ,
7272 in context: Context = Context ( )
73- ) -> FunctionCallExprSyntax ? {
74- _refactor ( syntax: call, in: context)
73+ ) throws -> FunctionCallExprSyntax {
74+ try _refactor ( syntax: call, in: context)
7575 }
7676
7777 internal static func _refactor< C: CallLikeSyntax > (
7878 syntax call: C ,
7979 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 ( " format error " )
85+ }
86+
87+ return formatted
8388 }
8489}
8590
8691extension CallLikeSyntax {
87- fileprivate func convertToTrailingClosures( from startAtArgument: Int ) -> Self ? {
92+ fileprivate func convertToTrailingClosures( from startAtArgument: Int ) throws -> Self {
8893 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 " )
9195 }
9296
9397 var closures = [ ( original: LabeledExprSyntax, closure: ClosureExprSyntax) ] ( )
@@ -106,7 +110,7 @@ extension CallLikeSyntax {
106110 }
107111
108112 guard !closures. isEmpty else {
109- return nil
113+ throw RefactoringNotApplicableError ( " no arguments to convert to closures " )
110114 }
111115
112116 // First trailing closure won't have label/colon. Transfer their trivia.
0 commit comments