Skip to content

Commit a663fbb

Browse files
committed
Merge pull request #1517 from stevapple/fix-for-in
Address default parameter regressions and add tests for `ForInStmtSyntax`
1 parent f430866 commit a663fbb

File tree

18 files changed

+160
-120
lines changed

18 files changed

+160
-120
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public let KEYWORDS: [KeywordSpec] = [
9696
KeywordSpec("autoclosure"),
9797
KeywordSpec("availability"),
9898
KeywordSpec("available"),
99-
KeywordSpec("await"),
99+
KeywordSpec("await", requiresTrailingSpace: true),
100100
KeywordSpec("backDeployed"),
101101
KeywordSpec("before"),
102102
KeywordSpec("block"),

CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,17 @@ public let STMT_NODES: [Node] = [
290290
),
291291
Child(
292292
name: "TryKeyword",
293-
kind: .node(kind: "TryToken"),
293+
kind: .token(choices: [.keyword(text: "try")]),
294294
isOptional: true
295295
),
296296
Child(
297297
name: "AwaitKeyword",
298298
kind: .token(choices: [.keyword(text: "await")]),
299-
isOptional: true,
300-
classification: "Keyword"
299+
isOptional: true
301300
),
302301
Child(
303302
name: "CaseKeyword",
304-
kind: .node(kind: "CaseToken"),
303+
kind: .token(choices: [.keyword(text: "case")]),
305304
isOptional: true
306305
),
307306
Child(

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift

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

5151
/// Assuming this node has a single child without a default value, that child.
5252
var singleNonDefaultedChild: Child {
53-
let nonDefaultedParams = children.filter { $0.type.defaultInitialization == nil }
53+
let nonDefaultedParams = children.filter { $0.defaultInitialization == nil }
5454
precondition(nonDefaultedParams.count == 1)
5555
return nonDefaultedParams[0]
5656
}

CodeGeneration/Sources/Utils/SyntaxBuildableType.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ public struct SyntaxBuildableType: Hashable {
5050
}
5151

5252
/// If the type has a default value (because it is optional or a token
53-
/// with fixed test), return an expression of the form ` = defaultValue`
54-
/// that can be used as the default value for a function parameter.
55-
/// Otherwise, return the empty string.
56-
public var defaultInitialization: ExprSyntax? {
53+
/// with fixed test), return an expression that can be used as the
54+
/// default value for a function parameter. Otherwise, return `nil`.
55+
public var defaultValue: ExprSyntax? {
5756
if isOptional {
5857
return ExprSyntax(NilLiteralExprSyntax())
5958
} else if let token = token {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
197197
secondName: .identifier(child.swiftName),
198198
colon: .colonToken(),
199199
type: child.rawParameterType,
200-
defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization.map { InitializerClauseSyntax(value: $0) } : nil
200+
defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization : nil
201201
)
202202
}
203203

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

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -438,57 +438,3 @@ fileprivate extension Node {
438438
}
439439
}
440440
}
441-
442-
fileprivate extension Child {
443-
/// If the type has a default value, return an expression of the form
444-
/// ` = default_value` that can be used as the default value to for a
445-
/// function parameter. Otherwise, return an empty string.
446-
var defaultInitialization: InitializerClauseSyntax? {
447-
// Note that this should be Optional<BaseType>.none for defaulted generic,
448-
// but that doesn't work in Swift 5.6. To keep source compatibility with
449-
// previous SwiftSyntax, we instead create a second initializer that uses
450-
// `Missing<Node>` and defaults that to `nil` instead (and `Missing` is
451-
// used so that they can't be implicitly converted from a literal).
452-
453-
if isOptional || isUnexpectedNodes {
454-
return InitializerClauseSyntax(value: NilLiteralExprSyntax())
455-
}
456-
457-
guard let token = token, isToken else {
458-
return nil
459-
}
460-
461-
if token.isKeyword {
462-
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
463-
}
464-
465-
if token.text != nil {
466-
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
467-
}
468-
469-
guard case .token(let choices, _, _) = kind else {
470-
return nil
471-
}
472-
473-
guard choices.count == 1 else {
474-
return nil
475-
}
476-
477-
var textChoice: String
478-
479-
switch choices[0] {
480-
case .keyword(let text),
481-
.token(let text):
482-
textChoice = text
483-
}
484-
485-
if token.associatedValueClass != nil {
486-
if textChoice == "init" {
487-
textChoice = "`init`"
488-
}
489-
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))"))
490-
} else {
491-
return InitializerClauseSyntax(value: ExprSyntax(#".\#(raw: token.swiftKind)("\#(raw: textChoice)")"#))
492-
}
493-
}
494-
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private func createConvenienceInitializer(node: Node) throws -> InitializerDeclS
8383
firstName: .identifier(child.swiftName),
8484
colon: .colonToken(),
8585
type: child.parameterType,
86-
defaultArgument: child.defaultInitialization.map { InitializerClauseSyntax(value: $0) }
86+
defaultArgument: child.defaultInitialization
8787
)
8888
)
8989
}

Sources/IDEUtils/generated/SyntaxClassification.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ extension SyntaxClassification {
7979
return (.stringInterpolationAnchor, true)
8080
case \ExpressionSegmentSyntax.rightParen:
8181
return (.stringInterpolationAnchor, true)
82-
case \ForInStmtSyntax.awaitKeyword:
83-
return (.keyword, false)
8482
case \IfConfigClauseSyntax.poundKeyword:
8583
return (.buildConfigId, false)
8684
case \IfConfigClauseSyntax.condition:

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ open class BasicFormat: SyntaxRewriter {
285285
return true
286286
case .keyword(.async):
287287
return true
288+
case .keyword(.await):
289+
return true
288290
case .keyword(.`break`):
289291
return true
290292
case .keyword(.`case`):

0 commit comments

Comments
 (0)