Skip to content

Commit 4c9dc69

Browse files
authored
Merge pull request #525 from fwcd/expressible-condition-elements
Make `OptionalBindingCondition` and `MatchingPatternCondition` expressible as `ConditionElement`
2 parents cce1d9c + 7262a36 commit 4c9dc69

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

Sources/SwiftSyntaxBuilder/generated/ExpressibleAsProtocols.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,18 +1488,26 @@ public extension ExpressibleAsAvailabilityCondition {
14881488
return createAvailabilityCondition()
14891489
}
14901490
}
1491-
public protocol ExpressibleAsMatchingPatternCondition: ExpressibleAsSyntaxBuildable {
1491+
public protocol ExpressibleAsMatchingPatternCondition: ExpressibleAsConditionElement {
14921492
func createMatchingPatternCondition() -> MatchingPatternCondition
14931493
}
14941494
public extension ExpressibleAsMatchingPatternCondition {
1495+
/// Conformance to ExpressibleAsConditionElement
1496+
func createConditionElement() -> ConditionElement {
1497+
return ConditionElement(condition: self)
1498+
}
14951499
func createSyntaxBuildable() -> SyntaxBuildable {
14961500
return createMatchingPatternCondition()
14971501
}
14981502
}
1499-
public protocol ExpressibleAsOptionalBindingCondition: ExpressibleAsSyntaxBuildable {
1503+
public protocol ExpressibleAsOptionalBindingCondition: ExpressibleAsConditionElement {
15001504
func createOptionalBindingCondition() -> OptionalBindingCondition
15011505
}
15021506
public extension ExpressibleAsOptionalBindingCondition {
1507+
/// Conformance to ExpressibleAsConditionElement
1508+
func createConditionElement() -> ConditionElement {
1509+
return ConditionElement(condition: self)
1510+
}
15031511
func createSyntaxBuildable() -> SyntaxBuildable {
15041512
return createOptionalBindingCondition()
15051513
}

Sources/SwiftSyntaxBuilder/gyb_helpers/ExpressibleAsConformances.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@
2222
'ExprList': [
2323
'ConditionElement'
2424
],
25+
'MatchingPatternCondition': [
26+
'ConditionElement'
27+
],
2528
'MemberDeclList': [
2629
'MemberDeclBlock'
2730
],
31+
'OptionalBindingCondition': [
32+
'ConditionElement'
33+
],
2834
'SequenceExpr': [
2935
'CodeBlockItem',
3036
'TupleExprElement'

Sources/SwiftSyntaxBuilderGeneration/gyb_generated/ExpressibleAsConformances.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ let SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES: [String: [String]] = [
3636
"ExprList": [
3737
"ConditionElement",
3838
],
39+
"MatchingPatternCondition": [
40+
"ConditionElement",
41+
],
3942
"MemberDeclList": [
4043
"MemberDeclBlock",
4144
],
45+
"OptionalBindingCondition": [
46+
"ConditionElement",
47+
],
4248
"SequenceExpr": [
4349
"CodeBlockItem",
4450
"TupleExprElement",

Tests/SwiftSyntaxBuilderTest/IfStmtTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,33 @@ final class IfStmtTests: XCTestCase {
3636
}
3737
""")
3838
}
39+
40+
func testIfLetStmt() {
41+
let buildable = IfStmt(
42+
conditions: OptionalBindingCondition(
43+
letOrVarKeyword: .let,
44+
pattern: "x",
45+
initializer: InitializerClause(value: "y")
46+
)
47+
) {}
48+
let syntax = buildable.buildSyntax(format: Format())
49+
XCTAssertEqual(syntax.description, """
50+
if let x = y {
51+
}
52+
""")
53+
}
54+
55+
func testIfCaseStmt() {
56+
let buildable = IfStmt(
57+
conditions: MatchingPatternCondition(
58+
pattern: ExpressionPattern(expression: MemberAccessExpr(name: "x")),
59+
initializer: InitializerClause(value: "y")
60+
)
61+
) {}
62+
let syntax = buildable.buildSyntax(format: Format())
63+
XCTAssertEqual(syntax.description, """
64+
if case .x = y {
65+
}
66+
""")
67+
}
3968
}

0 commit comments

Comments
 (0)