Skip to content

Use collection_element_name to provide better named add<ELEMENT_NAME> APIs #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Sources/SwiftSyntax/SyntaxBuilders.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ public struct ${Builder} {
% for child in node.children:
% child_node = NODE_MAP.get(child.syntax_kind)
% if child_node and child_node.is_syntax_collection():
% child_elt = child_node.collection_element_name
% child_elt = child.collection_element_name
% child_elt_type = child_node.collection_element_type
% child_elt_name = child.name + 'Member'
% if not child_elt:
% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name))
% end

public mutating func add${child_elt_name}(_ elt: ${child_elt_type}) {
public mutating func add${child_elt}(_ elt: ${child_elt_type}) {
let idx = ${node.name}.Cursor.${child.swift_name}.rawValue
if let list = layout[idx] {
layout[idx] = list.appending(elt.raw)
Expand Down
8 changes: 5 additions & 3 deletions Sources/SwiftSyntax/SyntaxNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,19 @@ public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
}
}
% if child_node and child_node.is_syntax_collection():
% child_elt = child_node.collection_element_name
% child_elt = child.collection_element_name
% child_elt_type = child_node.collection_element_type
% child_elt_name = child.name + 'Member'
% if not child_elt:
% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name))
% end

/// Adds the provided `${child_elt}` to the node's `${child.swift_name}`
/// collection.
/// - param element: The new `${child_elt}` to add to the node's
/// `${child.swift_name}` collection.
/// - returns: A copy of the receiver with the provided `${child_elt}`
/// appended to its `${child.swift_name}` collection.
public func add${child_elt_name}(_ element: ${child_elt_type}) -> ${node.name} {
public func add${child_elt}(_ element: ${child_elt_type}) -> ${node.name} {
var collection: RawSyntax
if let col = raw[Cursor.${child.swift_name}] {
collection = col.appending(element.raw)
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftSyntaxTest/ModifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fileprivate func cannedVarDecl() -> VariableDeclSyntax {
initializer: nil, accessor: nil, trailingComma: nil)
return VariableDeclSyntax {
$0.useLetOrVarKeyword(SyntaxFactory.makeLetKeyword())
$0.addBindingsMember(Pattern)
$0.addBinding(Pattern)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftSyntaxTest/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
let call = FunctionCallExprSyntax {
$0.useCalledExpression(printID)
$0.useLeftParen(SyntaxFactory.makeLeftParenToken())
$0.addArgumentListMember(arg)
$0.addArgument(arg)
$0.useRightParen(SyntaxFactory.makeRightParenToken())
}
XCTAssertEqual("\(call)", "print(\"Hello, world!\")")
Expand Down Expand Up @@ -133,7 +133,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
let call1 = FunctionCallExprSyntax {
$0.useCalledExpression(printID)
$0.useLeftParen(SyntaxFactory.makeLeftParenToken())
$0.addArgumentListMember(arg)
$0.addArgument(arg)
$0.useRightParen(SyntaxFactory.makeRightParenToken())
}
XCTAssertNotNil(call1.leftParen)
Expand All @@ -145,7 +145,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {

let call3 = FunctionCallExprSyntax {
$0.useCalledExpression(printID)
$0.addArgumentListMember(arg)
$0.addArgument(arg)
}
XCTAssertNil(call3.leftParen)
XCTAssertNil(call3.rightParen)
Expand Down