Skip to content

Commit f9efc16

Browse files
authored
Merge pull request swiftlang#200 from ahoppen/base-not-optional-initialiser
For base nodes, add an initializer that takes an optional parameter
2 parents 0cd1fea + a3b7d7b commit f9efc16

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Sources/SwiftSyntax/SyntaxBaseNodes.swift.gyb

+7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ public extension Syntax {
5151
public struct ${node.name}: ${node.name}Protocol, SyntaxHashable {
5252
public let _syntaxNode: Syntax
5353

54+
/// Create a `${node.name}` node from a specialized syntax node.
5455
public init<S: ${node.name}Protocol>(_ syntax: S) {
5556
// We know this cast is going to succeed. Go through init(_: SyntaxData)
5657
// to do a sanity check and verify the kind matches in debug builds and get
5758
// maximum performance in release builds.
5859
self.init(syntax._syntaxNode.data)
5960
}
6061

62+
/// Create a `${node.name}` node from a specialized optional syntax node.
63+
public init?<S: ${node.name}Protocol>(_ syntax: S?) {
64+
guard let syntax = syntax else { return nil }
65+
self.init(syntax)
66+
}
67+
6168
/// Converts the given `Syntax` node to a `${node.name}` if possible. Returns
6269
/// `nil` if the conversion is not possible.
6370
public init?(_ syntax: Syntax) {

Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift

+35
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ public extension Syntax {
3838
public 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 {
135142
public 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 {
232246
public 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 {
329350
public 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 {
426454
public 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) {

Tests/SwiftSyntaxTest/SyntaxTests.swift

+3
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,8 @@ public class SyntaxTests: XCTestCase {
119119
case .integerLiteralExpr: break
120120
default: XCTFail("failed to convert to SyntaxEnum")
121121
}
122+
123+
XCTAssertNil(ExprSyntax(nil as IntegerLiteralExprSyntax?))
124+
XCTAssertEqual(ExprSyntax(integerExpr).as(IntegerLiteralExprSyntax.self)!, integerExpr)
122125
}
123126
}

0 commit comments

Comments
 (0)