Skip to content

Commit 62003ba

Browse files
authored
Merge pull request swiftlang#121 from akyrtzi/gyb-syntax-element-name
Use `collection_element_name` to provide better named `add<ELEMENT_NAME>` APIs
2 parents 00ad0bb + ba987f2 commit 62003ba

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

Diff for: Sources/SwiftSyntax/SyntaxBuilders.swift.gyb

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public struct ${Builder} {
3737
% for child in node.children:
3838
% child_node = NODE_MAP.get(child.syntax_kind)
3939
% if child_node and child_node.is_syntax_collection():
40-
% child_elt = child_node.collection_element_name
40+
% child_elt = child.collection_element_name
4141
% child_elt_type = child_node.collection_element_type
42-
% child_elt_name = child.name + 'Member'
42+
% if not child_elt:
43+
% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name))
44+
% end
4345

44-
public mutating func add${child_elt_name}(_ elt: ${child_elt_type}) {
46+
public mutating func add${child_elt}(_ elt: ${child_elt_type}) {
4547
let idx = ${node.name}.Cursor.${child.swift_name}.rawValue
4648
if let list = layout[idx] {
4749
layout[idx] = list.appending(elt.raw)

Diff for: Sources/SwiftSyntax/SyntaxNodes.swift.gyb

+5-3
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,19 @@ public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
127127
}
128128
}
129129
% if child_node and child_node.is_syntax_collection():
130-
% child_elt = child_node.collection_element_name
130+
% child_elt = child.collection_element_name
131131
% child_elt_type = child_node.collection_element_type
132-
% child_elt_name = child.name + 'Member'
132+
% if not child_elt:
133+
% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name))
134+
% end
133135

134136
/// Adds the provided `${child_elt}` to the node's `${child.swift_name}`
135137
/// collection.
136138
/// - param element: The new `${child_elt}` to add to the node's
137139
/// `${child.swift_name}` collection.
138140
/// - returns: A copy of the receiver with the provided `${child_elt}`
139141
/// appended to its `${child.swift_name}` collection.
140-
public func add${child_elt_name}(_ element: ${child_elt_type}) -> ${node.name} {
142+
public func add${child_elt}(_ element: ${child_elt_type}) -> ${node.name} {
141143
var collection: RawSyntax
142144
if let col = raw[Cursor.${child.swift_name}] {
143145
collection = col.appending(element.raw)

Diff for: Tests/SwiftSyntaxTest/ModifierTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fileprivate func cannedVarDecl() -> VariableDeclSyntax {
1111
initializer: nil, accessor: nil, trailingComma: nil)
1212
return VariableDeclSyntax {
1313
$0.useLetOrVarKeyword(SyntaxFactory.makeLetKeyword())
14-
$0.addBindingsMember(Pattern)
14+
$0.addBinding(Pattern)
1515
}
1616
}
1717

Diff for: Tests/SwiftSyntaxTest/SyntaxFactory.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
102102
let call = FunctionCallExprSyntax {
103103
$0.useCalledExpression(printID)
104104
$0.useLeftParen(SyntaxFactory.makeLeftParenToken())
105-
$0.addArgumentListMember(arg)
105+
$0.addArgument(arg)
106106
$0.useRightParen(SyntaxFactory.makeRightParenToken())
107107
}
108108
XCTAssertEqual("\(call)", "print(\"Hello, world!\")")
@@ -133,7 +133,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
133133
let call1 = FunctionCallExprSyntax {
134134
$0.useCalledExpression(printID)
135135
$0.useLeftParen(SyntaxFactory.makeLeftParenToken())
136-
$0.addArgumentListMember(arg)
136+
$0.addArgument(arg)
137137
$0.useRightParen(SyntaxFactory.makeRightParenToken())
138138
}
139139
XCTAssertNotNil(call1.leftParen)
@@ -145,7 +145,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
145145

146146
let call3 = FunctionCallExprSyntax {
147147
$0.useCalledExpression(printID)
148-
$0.addArgumentListMember(arg)
148+
$0.addArgument(arg)
149149
}
150150
XCTAssertNil(call3.leftParen)
151151
XCTAssertNil(call3.rightParen)

0 commit comments

Comments
 (0)