@@ -38,13 +38,20 @@ public extension Syntax {
3838public struct DeclSyntax : DeclSyntaxProtocol , SyntaxHashable {
3939 public let _syntaxNode : Syntax
4040
41+ /// Create a `DeclSyntax` node from a specialized syntax node.
4142 public init < S: DeclSyntaxProtocol > ( _ syntax: S ) {
4243 // We know this cast is going to succeed. Go through init(_: SyntaxData)
4344 // to do a sanity check and verify the kind matches in debug builds and get
4445 // maximum performance in release builds.
4546 self . init ( syntax. _syntaxNode. data)
4647 }
4748
49+ /// Create a `DeclSyntax` node from a specialized optional syntax node.
50+ public init ? < S: DeclSyntaxProtocol > ( _ syntax: S ? ) {
51+ guard let syntax = syntax else { return nil }
52+ self . init ( syntax)
53+ }
54+
4855 /// Converts the given `Syntax` node to a `DeclSyntax` if possible. Returns
4956 /// `nil` if the conversion is not possible.
5057 public init ? ( _ syntax: Syntax ) {
@@ -135,13 +142,20 @@ public extension Syntax {
135142public struct ExprSyntax : ExprSyntaxProtocol , SyntaxHashable {
136143 public let _syntaxNode : Syntax
137144
145+ /// Create a `ExprSyntax` node from a specialized syntax node.
138146 public init < S: ExprSyntaxProtocol > ( _ syntax: S ) {
139147 // We know this cast is going to succeed. Go through init(_: SyntaxData)
140148 // to do a sanity check and verify the kind matches in debug builds and get
141149 // maximum performance in release builds.
142150 self . init ( syntax. _syntaxNode. data)
143151 }
144152
153+ /// Create a `ExprSyntax` node from a specialized optional syntax node.
154+ public init ? < S: ExprSyntaxProtocol > ( _ syntax: S ? ) {
155+ guard let syntax = syntax else { return nil }
156+ self . init ( syntax)
157+ }
158+
145159 /// Converts the given `Syntax` node to a `ExprSyntax` if possible. Returns
146160 /// `nil` if the conversion is not possible.
147161 public init ? ( _ syntax: Syntax ) {
@@ -232,13 +246,20 @@ public extension Syntax {
232246public struct StmtSyntax : StmtSyntaxProtocol , SyntaxHashable {
233247 public let _syntaxNode : Syntax
234248
249+ /// Create a `StmtSyntax` node from a specialized syntax node.
235250 public init < S: StmtSyntaxProtocol > ( _ syntax: S ) {
236251 // We know this cast is going to succeed. Go through init(_: SyntaxData)
237252 // to do a sanity check and verify the kind matches in debug builds and get
238253 // maximum performance in release builds.
239254 self . init ( syntax. _syntaxNode. data)
240255 }
241256
257+ /// Create a `StmtSyntax` node from a specialized optional syntax node.
258+ public init ? < S: StmtSyntaxProtocol > ( _ syntax: S ? ) {
259+ guard let syntax = syntax else { return nil }
260+ self . init ( syntax)
261+ }
262+
242263 /// Converts the given `Syntax` node to a `StmtSyntax` if possible. Returns
243264 /// `nil` if the conversion is not possible.
244265 public init ? ( _ syntax: Syntax ) {
@@ -329,13 +350,20 @@ public extension Syntax {
329350public struct TypeSyntax : TypeSyntaxProtocol , SyntaxHashable {
330351 public let _syntaxNode : Syntax
331352
353+ /// Create a `TypeSyntax` node from a specialized syntax node.
332354 public init < S: TypeSyntaxProtocol > ( _ syntax: S ) {
333355 // We know this cast is going to succeed. Go through init(_: SyntaxData)
334356 // to do a sanity check and verify the kind matches in debug builds and get
335357 // maximum performance in release builds.
336358 self . init ( syntax. _syntaxNode. data)
337359 }
338360
361+ /// Create a `TypeSyntax` node from a specialized optional syntax node.
362+ public init ? < S: TypeSyntaxProtocol > ( _ syntax: S ? ) {
363+ guard let syntax = syntax else { return nil }
364+ self . init ( syntax)
365+ }
366+
339367 /// Converts the given `Syntax` node to a `TypeSyntax` if possible. Returns
340368 /// `nil` if the conversion is not possible.
341369 public init ? ( _ syntax: Syntax ) {
@@ -426,13 +454,20 @@ public extension Syntax {
426454public struct PatternSyntax : PatternSyntaxProtocol , SyntaxHashable {
427455 public let _syntaxNode : Syntax
428456
457+ /// Create a `PatternSyntax` node from a specialized syntax node.
429458 public init < S: PatternSyntaxProtocol > ( _ syntax: S ) {
430459 // We know this cast is going to succeed. Go through init(_: SyntaxData)
431460 // to do a sanity check and verify the kind matches in debug builds and get
432461 // maximum performance in release builds.
433462 self . init ( syntax. _syntaxNode. data)
434463 }
435464
465+ /// Create a `PatternSyntax` node from a specialized optional syntax node.
466+ public init ? < S: PatternSyntaxProtocol > ( _ syntax: S ? ) {
467+ guard let syntax = syntax else { return nil }
468+ self . init ( syntax)
469+ }
470+
436471 /// Converts the given `Syntax` node to a `PatternSyntax` if possible. Returns
437472 /// `nil` if the conversion is not possible.
438473 public init ? ( _ syntax: Syntax ) {
0 commit comments