@@ -38,13 +38,20 @@ public extension Syntax {
38
38
public struct DeclSyntax : DeclSyntaxProtocol , SyntaxHashable {
39
39
public let _syntaxNode : Syntax
40
40
41
+ /// Create a `DeclSyntax` node from a specialized syntax node.
41
42
public init < S: DeclSyntaxProtocol > ( _ syntax: S ) {
42
43
// We know this cast is going to succeed. Go through init(_: SyntaxData)
43
44
// to do a sanity check and verify the kind matches in debug builds and get
44
45
// maximum performance in release builds.
45
46
self . init ( syntax. _syntaxNode. data)
46
47
}
47
48
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
+
48
55
/// Converts the given `Syntax` node to a `DeclSyntax` if possible. Returns
49
56
/// `nil` if the conversion is not possible.
50
57
public init ? ( _ syntax: Syntax ) {
@@ -135,13 +142,20 @@ public extension Syntax {
135
142
public struct ExprSyntax : ExprSyntaxProtocol , SyntaxHashable {
136
143
public let _syntaxNode : Syntax
137
144
145
+ /// Create a `ExprSyntax` node from a specialized syntax node.
138
146
public init < S: ExprSyntaxProtocol > ( _ syntax: S ) {
139
147
// We know this cast is going to succeed. Go through init(_: SyntaxData)
140
148
// to do a sanity check and verify the kind matches in debug builds and get
141
149
// maximum performance in release builds.
142
150
self . init ( syntax. _syntaxNode. data)
143
151
}
144
152
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
+
145
159
/// Converts the given `Syntax` node to a `ExprSyntax` if possible. Returns
146
160
/// `nil` if the conversion is not possible.
147
161
public init ? ( _ syntax: Syntax ) {
@@ -232,13 +246,20 @@ public extension Syntax {
232
246
public struct StmtSyntax : StmtSyntaxProtocol , SyntaxHashable {
233
247
public let _syntaxNode : Syntax
234
248
249
+ /// Create a `StmtSyntax` node from a specialized syntax node.
235
250
public init < S: StmtSyntaxProtocol > ( _ syntax: S ) {
236
251
// We know this cast is going to succeed. Go through init(_: SyntaxData)
237
252
// to do a sanity check and verify the kind matches in debug builds and get
238
253
// maximum performance in release builds.
239
254
self . init ( syntax. _syntaxNode. data)
240
255
}
241
256
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
+
242
263
/// Converts the given `Syntax` node to a `StmtSyntax` if possible. Returns
243
264
/// `nil` if the conversion is not possible.
244
265
public init ? ( _ syntax: Syntax ) {
@@ -329,13 +350,20 @@ public extension Syntax {
329
350
public struct TypeSyntax : TypeSyntaxProtocol , SyntaxHashable {
330
351
public let _syntaxNode : Syntax
331
352
353
+ /// Create a `TypeSyntax` node from a specialized syntax node.
332
354
public init < S: TypeSyntaxProtocol > ( _ syntax: S ) {
333
355
// We know this cast is going to succeed. Go through init(_: SyntaxData)
334
356
// to do a sanity check and verify the kind matches in debug builds and get
335
357
// maximum performance in release builds.
336
358
self . init ( syntax. _syntaxNode. data)
337
359
}
338
360
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
+
339
367
/// Converts the given `Syntax` node to a `TypeSyntax` if possible. Returns
340
368
/// `nil` if the conversion is not possible.
341
369
public init ? ( _ syntax: Syntax ) {
@@ -426,13 +454,20 @@ public extension Syntax {
426
454
public struct PatternSyntax : PatternSyntaxProtocol , SyntaxHashable {
427
455
public let _syntaxNode : Syntax
428
456
457
+ /// Create a `PatternSyntax` node from a specialized syntax node.
429
458
public init < S: PatternSyntaxProtocol > ( _ syntax: S ) {
430
459
// We know this cast is going to succeed. Go through init(_: SyntaxData)
431
460
// to do a sanity check and verify the kind matches in debug builds and get
432
461
// maximum performance in release builds.
433
462
self . init ( syntax. _syntaxNode. data)
434
463
}
435
464
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
+
436
471
/// Converts the given `Syntax` node to a `PatternSyntax` if possible. Returns
437
472
/// `nil` if the conversion is not possible.
438
473
public init ? ( _ syntax: Syntax ) {
0 commit comments