@@ -20,161 +20,34 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
20
20
21
21
for node in SYNTAX_NODES . compactMap ( \. collectionNode) {
22
22
let type = SyntaxBuildableType ( kind: . node( kind: node. kind) )
23
- let elementType = node. collectionElementType
24
- let expressionType : TypeSyntax = node. elementChoices. count == 1 ? elementType. parameterType : TypeSyntax ( " \( type. buildable) .Element " )
25
23
26
24
try ! StructDeclSyntax (
27
25
"""
26
+
27
+ // MARK: - \( type. resultBuilderType)
28
+
28
29
@resultBuilder
29
30
\( node. node. apiAttributes ( ) ) \
30
- public struct \( type. resultBuilderType)
31
+ public struct \( type. resultBuilderType) : ListBuilder
31
32
"""
32
33
) {
33
34
DeclSyntax (
34
35
"""
35
- /// The type of individual statement expressions in the transformed function,
36
- /// which defaults to Component if buildExpression() is not provided.
37
- public typealias Expression = \( expressionType)
38
- """
39
- )
40
-
41
- DeclSyntax (
42
- """
43
- /// The type of a partial result, which will be carried through all of the
44
- /// build methods.
45
- public typealias Component = [Expression]
46
- """
47
- )
48
-
49
- DeclSyntax (
50
- """
51
- /// The type of the final returned result, which defaults to Component if
52
- /// buildFinalResult() is not provided.
53
- public typealias FinalResult = \( type. buildable)
54
- """
55
- )
56
-
57
- DeclSyntax (
58
- """
59
- /// Required by every result builder to build combined results from
60
- /// statement blocks.
61
- public static func buildBlock(_ components: Self.Component...) -> Self.Component {
62
- return components.flatMap { $0 }
63
- }
64
- """
65
- )
66
-
67
- DeclSyntax (
68
- """
69
- /// If declared, provides contextual type information for statement
70
- /// expressions to translate them into partial results.
71
- public static func buildExpression(_ expression: Self.Expression) -> Self.Component {
72
- return [expression]
73
- }
36
+ public typealias FinalResult = \( type. syntaxBaseName)
74
37
"""
75
38
)
76
39
77
40
if node. elementChoices. count > 1 {
78
41
for elementChoice in node. elementChoices {
79
42
DeclSyntax (
80
43
"""
81
- /// If declared, provides contextual type information for statement
82
- /// expressions to translate them into partial results.
83
- public static func buildExpression(_ expression: \( elementChoice. syntaxType) ) -> Self.Component {
84
- return buildExpression(.init(expression))
44
+ public static func buildExpression(_ expression: \( elementChoice. syntaxType) ) -> Component {
45
+ buildExpression(.init(expression))
85
46
}
86
47
"""
87
48
)
88
49
}
89
50
}
90
-
91
- DeclSyntax (
92
- """
93
- /// Add all the elements of `expression` to this result builder, effectively flattening them.
94
- ///
95
- /// - Note: This overload is disfavored to resolve an ambiguity when both the final result and
96
- /// the elements are expressible by string interpolation. In that case we favor creating a
97
- /// single element from the string literal.
98
- @_disfavoredOverload
99
- public static func buildExpression(_ expression: Self.FinalResult) -> Self.Component {
100
- return expression.map { $0 }
101
- }
102
- """
103
- )
104
-
105
- DeclSyntax (
106
- """
107
- /// Enables support for `if` statements that do not have an `else`.
108
- public static func buildOptional(_ component: Self.Component?) -> Self.Component {
109
- return component ?? []
110
- }
111
- """
112
- )
113
-
114
- DeclSyntax (
115
- """
116
- /// With buildEither(second:), enables support for 'if-else' and 'switch'
117
- /// statements by folding conditional results into a single result.
118
- public static func buildEither(first component: Self.Component) -> Self.Component {
119
- return component
120
- }
121
- """
122
- )
123
-
124
- DeclSyntax (
125
- """
126
- /// With buildEither(first:), enables support for 'if-else' and 'switch'
127
- /// statements by folding conditional results into a single result.
128
- public static func buildEither(second component: Self.Component) -> Self.Component {
129
- return component
130
- }
131
- """
132
- )
133
-
134
- DeclSyntax (
135
- """
136
- /// Enables support for 'for..in' loops by combining the
137
- /// results of all iterations into a single result.
138
- public static func buildArray(_ components: [Self.Component]) -> Self.Component {
139
- return components.flatMap { $0 }
140
- }
141
- """
142
- )
143
-
144
- DeclSyntax (
145
- """
146
- /// If declared, this will be called on the partial result of an 'if'
147
- /// #available' block to allow the result builder to erase type
148
- /// information.
149
- public static func buildLimitedAvailability(_ component: Self.Component) -> Self.Component {
150
- return component
151
- }
152
- """
153
- )
154
-
155
- try FunctionDeclSyntax (
156
- """
157
- /// If declared, this will be called on the partial result from the outermost
158
- /// block statement to produce the final returned result.
159
- public static func buildFinalResult(_ component: Component) -> FinalResult
160
- """
161
- ) {
162
- if elementType. isToken {
163
- StmtSyntax ( " return .init(component) " )
164
- } else if elementType. hasWithTrailingCommaTrait {
165
- DeclSyntax ( " let lastIndex = component.count - 1 " )
166
-
167
- StmtSyntax (
168
- """
169
- return .init(component.enumerated().map { index, source in
170
- return index < lastIndex ? source.ensuringTrailingComma() : source
171
- })
172
- """
173
- )
174
- } else {
175
- StmtSyntax ( " return .init(component) " )
176
- }
177
- }
178
51
}
179
52
180
53
DeclSyntax (
0 commit comments