Skip to content

Commit 7e1695f

Browse files
committed
Fix string interpolation parsing errors in CodeGeneration
1 parent 2e8bff5 commit 7e1695f

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,19 @@ public extension Child {
5757
return self.type.optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(parameterBaseType)))
5858
}
5959

60-
/// If the child node has a default value, return an expression of the form
61-
/// ` = default_value` that can be used as the default value to for a
62-
/// function parameter. Otherwise, return `nil`.
63-
var defaultInitialization: InitializerClauseSyntax? {
60+
var defaultValue: ExprSyntax? {
6461
if isOptional || isUnexpectedNodes {
6562
if type.isBaseType && kind.isNodeChoicesEmpty {
66-
return InitializerClauseSyntax(value: ExprSyntax("\(type.buildable).none"))
63+
return ExprSyntax("\(type.buildable).none")
6764
} else {
68-
return InitializerClauseSyntax(value: NilLiteralExprSyntax())
65+
return ExprSyntax("nil")
6966
}
7067
}
7168
guard let token = token, isToken else {
72-
return type.defaultValue.map { InitializerClauseSyntax(value: $0) }
69+
return type.defaultValue
7370
}
7471
if token.text != nil {
75-
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
72+
return ExprSyntax(".\(raw: token.swiftKind)Token()")
7673
}
7774
guard case .token(let choices, _, _) = kind, choices.count == 1, token.associatedValueClass != nil else {
7875
return nil
@@ -85,7 +82,18 @@ public extension Child {
8582
if textChoice == "init" {
8683
textChoice = "`init`"
8784
}
88-
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))"))
85+
return ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))")
86+
}
87+
88+
/// If the child node has a default value, return an expression of the form
89+
/// ` = default_value` that can be used as the default value to for a
90+
/// function parameter. Otherwise, return `nil`.
91+
var defaultInitialization: InitializerClauseSyntax? {
92+
if let defaultValue {
93+
return InitializerClauseSyntax(equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space), value: defaultValue)
94+
} else {
95+
return nil
96+
}
8997
}
9098

9199
/// If this node is a token that can't contain arbitrary text, generate a Swift

CodeGeneration/Sources/generate-swiftsyntax/LayoutNode+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,5 @@ fileprivate func convertFromSyntaxProtocolToSyntaxType(child: Child, useDeprecat
198198
if child.type.isBaseType && !child.kind.isNodeChoices {
199199
return ExprSyntax("\(raw: child.type.syntaxBaseName)(fromProtocol: \(raw: childName))")
200200
}
201-
return ExprSyntax("\(raw: childName)")
201+
return ExprSyntax("\(raw: childName.backtickedIfNeeded)")
202202
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5252
try! StructDeclSyntax(
5353
"""
5454
@_spi(RawSyntax)
55-
public struct \(node.kind.rawType): \(node.kind.isBase ? node.kind.rawProtocolType : node.base.rawProtocolType))
55+
public struct \(node.kind.rawType): \(node.kind.isBase ? node.kind.rawProtocolType : node.base.rawProtocolType)
5656
"""
5757
) {
5858
for (name, choices) in node.childrenChoicesEnums {

0 commit comments

Comments
 (0)