@@ -29,6 +29,11 @@ struct JSMacroFixItMessage: FixItMessage {
2929 var fixItID : MessageID { MessageID ( domain: " JavaScriptKitMacros " , id: message) }
3030}
3131
32+ enum JSMacroText {
33+ static let jsExceptionPropagation = " @JSFunction must propagate JavaScript errors as JSException. "
34+ static let jsSetterExceptionPropagation = " @JSSetter must propagate JavaScript errors as JSException. "
35+ }
36+
3237struct JSMacroWarningMessage : DiagnosticMessage {
3338 let message : String
3439 var diagnosticID : MessageID { MessageID ( domain: " JavaScriptKitMacros " , id: message) }
@@ -211,28 +216,15 @@ enum JSMacroHelper {
211216 guard !isJSException else { return }
212217 guard !isAllowedGenericError else { return }
213218
214- let throwsSpecifier = throwsClause. throwsSpecifier. with ( \. trailingTrivia, . spaces( 0 ) )
215- let leftParen = throwsClause. leftParen ?? . leftParenToken( )
216- let rightParen = ( throwsClause. rightParen ?? . rightParenToken( ) )
217- . with ( \. trailingTrivia, throwsClause. rightParen? . trailingTrivia ?? . space)
218- let newThrowsClause = throwsClause
219- . with ( \. throwsSpecifier, throwsSpecifier)
220- . with ( \. leftParen, leftParen)
221- . with ( \. type, TypeSyntax ( IdentifierTypeSyntax ( name: . identifier( " JSException " ) ) ) )
222- . with ( \. rightParen, rightParen)
219+ let newThrowsClause = jsExceptionThrowsClause ( from: throwsClause)
223220
224221 let fixIt = FixIt (
225222 message: JSMacroFixItMessage ( message: " Declare throws(JSException) " ) ,
226223 changes: [ . replace( oldNode: Syntax ( throwsClause) , newNode: Syntax ( newThrowsClause) ) ]
227224 )
228225
229226 var notes : [ Note ] = [
230- Note (
231- node: node,
232- message: JSMacroNoteMessage (
233- message: " @JSFunction must propagate JavaScript errors as JSException. "
234- )
235- )
227+ jsExceptionPropagationNote ( on: node)
236228 ]
237229 notes. append ( contentsOf: additionalNotes)
238230
@@ -276,4 +268,29 @@ enum JSMacroHelper {
276268 guard let base = setterPropertyBase ( from: firstParameter) else { return nil }
277269 return " set " + capitalizingFirstLetter( base)
278270 }
271+
272+ /// Build a typed throws(JSException) clause preserving trivia when possible.
273+ static func jsExceptionThrowsClause( from throwsClause: ThrowsClauseSyntax ? ) -> ThrowsClauseSyntax {
274+ let throwsSpecifier = ( throwsClause? . throwsSpecifier ?? . keyword( . throws, leadingTrivia: . space) )
275+ . with ( \. trailingTrivia, . spaces( 0 ) )
276+ let leftParen = throwsClause? . leftParen ?? . leftParenToken( )
277+ let rightParen = ( throwsClause? . rightParen ?? . rightParenToken( ) )
278+ . with ( \. trailingTrivia, throwsClause? . rightParen? . trailingTrivia ?? . space)
279+
280+ return ThrowsClauseSyntax (
281+ throwsSpecifier: throwsSpecifier,
282+ leftParen: leftParen,
283+ type: TypeSyntax ( IdentifierTypeSyntax ( name: . identifier( " JSException " ) ) ) ,
284+ rightParen: rightParen
285+ )
286+ }
287+
288+ static func jsExceptionPropagationNote( on node: Syntax , message: String = JSMacroText . jsExceptionPropagation) -> Note {
289+ Note (
290+ node: node,
291+ message: JSMacroNoteMessage (
292+ message: message
293+ )
294+ )
295+ }
279296}
0 commit comments