@@ -371,6 +371,45 @@ extension SwitchExprSyntax {
371371 }
372372}
373373
374+ // MARK: - SubscriptDeclSyntax
375+ // SubscriptDeclSyntax is a special scenario as it don't have body or members
376+ // So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
377+
378+ extension SubscriptDeclSyntax {
379+ /// Construct a subscript with a single `get` accessor where `header` builds
380+ /// the text before the opening `{` and `accessor` builds the accessor body.
381+ ///
382+ /// For example, to construct
383+ ///
384+ /// ```swift
385+ /// subscript(_ index: Int) -> Element {
386+ /// return storage[index]
387+ /// }
388+ /// ```
389+ ///
390+ /// using this call
391+ ///
392+ /// ```swift
393+ /// try SubscriptDeclSyntax("subscript(_ index: Int) -> Element") {
394+ /// StmtSyntax("return storage[index]")
395+ /// }
396+ /// ```
397+ ///
398+ /// Throws an error if `header` does not start a subscript declaration.
399+ /// E.g. if calling `try SubscriptDeclSyntax("func foo") {}`
400+ public init (
401+ _ header: SyntaxNodeString ,
402+ @CodeBlockItemListBuilder accessor: ( ) throws -> CodeBlockItemListSyntax
403+ ) throws {
404+ let decl = DeclSyntax ( " \( header) {} " )
405+ guard let castedDecl = decl. as ( Self . self) else {
406+ throw SyntaxStringInterpolationInvalidNodeTypeError ( expectedType: Self . self, actualNode: decl)
407+ }
408+ self = castedDecl
409+ self . accessorBlock = AccessorBlockSyntax ( accessors: . getter( try accessor ( ) ) )
410+ }
411+ }
412+
374413// MARK: - VariableDeclSyntax
375414// VariableDeclSyntax is a special scenario as it don't have body or members
376415// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
0 commit comments