Skip to content

Commit 103a9f5

Browse files
committed
Migrate SyntaxBuildableType.defaultInitialization to an expr node
1 parent 8875897 commit 103a9f5

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Sources/SwiftSyntaxBuilderGeneration/SyntaxBuildableNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension Node {
4545

4646
/// Assuming this node has a single child without a default value, that child.
4747
var singleNonDefaultedChild: Child {
48-
let nonDefaultedParams = children.filter(\.type.defaultInitialization.isEmpty)
48+
let nonDefaultedParams = children.filter { $0.type.defaultInitialization == nil }
4949
assert(nonDefaultedParams.count == 1)
5050
return nonDefaultedParams[0]
5151
}

Sources/SwiftSyntaxBuilderGeneration/SyntaxBuildableType.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
1316
/// Wrapper around the syntax kind strings to provide functionality specific to
1417
/// SwiftSyntaxBuilder. In particular, this includes the functionality to create
1518
/// the `*Buildable`, `ExpressibleAs*` and `*Syntax` Swift types from the syntax
@@ -43,17 +46,17 @@ struct SyntaxBuildableType: Hashable {
4346
/// with fixed test), return an expression of the form ` = defaultValue`
4447
/// that can be used as the default value for a function parameter.
4548
/// Otherwise, return the empty string.
46-
var defaultInitialization: String {
49+
var defaultInitialization: ExpressibleAsExprBuildable? {
4750
if isOptional {
48-
return " = nil"
51+
return "nil"
4952
} else if isToken {
5053
if let token = token, token.text != nil {
51-
return " = TokenSyntax.`\(lowercaseFirstWord(name: token.name))`"
54+
return MemberAccessExpr(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name))
5255
} else if tokenKind == "EOFToken" {
53-
return " = TokenSyntax.eof"
56+
return MemberAccessExpr(base: "TokenSyntax", name: "eof")
5457
}
5558
}
56-
return ""
59+
return nil
5760
}
5861

5962
/// Whether the type is a syntax collection.

0 commit comments

Comments
 (0)