@@ -45,21 +45,34 @@ public extension Child {
4545 flattened ( indentedDocumentation: description ?? " " )
4646 }
4747
48- var defaultInitialization : ExprSyntax ? {
49- switch kind {
50- case . token( choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _) :
51- if choices. count == 1 , case . keyword( text: let text) = choices. first {
52- var textChoice = text
53- if textChoice == " init " {
54- textChoice = " `init` "
55- }
56- return ExprSyntax ( " .keyword(. \( raw: textChoice) ) " )
57- } else {
58- return type. defaultInitialization
59- }
60- default :
61- return type. defaultInitialization
48+ /// If the child node has a default value, return an expression of the form
49+ /// ` = default_value` that can be used as the default value to for a
50+ /// function parameter. Otherwise, return `nil`.
51+ var defaultInitialization : InitializerClauseSyntax ? {
52+ if isOptional || isUnexpectedNodes {
53+ return InitializerClauseSyntax ( value: NilLiteralExprSyntax ( ) )
54+ }
55+ guard let token = token, isToken else {
56+ return type. defaultValue. map { InitializerClauseSyntax ( value: $0) }
57+ }
58+ if token. isKeyword {
59+ return InitializerClauseSyntax ( value: ExprSyntax ( " . \( raw: token. swiftKind) () " ) )
60+ }
61+ if token. text != nil {
62+ return InitializerClauseSyntax ( value: ExprSyntax ( " . \( raw: token. swiftKind) Token() " ) )
63+ }
64+ guard case . token( let choices, _, _) = kind, choices. count == 1 , token. associatedValueClass != nil else {
65+ return nil
66+ }
67+ var textChoice : String
68+ switch choices [ 0 ] {
69+ case . keyword( let text) , . token( let text) :
70+ textChoice = text
71+ }
72+ if textChoice == " init " {
73+ textChoice = " `init` "
6274 }
75+ return InitializerClauseSyntax ( value: ExprSyntax ( " . \( raw: token. swiftKind) (. \( raw: textChoice) ) " ) )
6376 }
6477
6578 /// If this node is a token that can't contain arbitrary text, generate a Swift
0 commit comments