Skip to content

Override trailing comma for elements in ResultBuilders #454

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
Jun 20, 2022
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
26 changes: 24 additions & 2 deletions Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
from gyb_syntax_support import SYNTAX_NODES
from gyb_helpers import SyntaxBuildableNode
from gyb_helpers import SyntaxBuildableNode, conformance_clause
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
Expand All @@ -23,10 +23,15 @@ import SwiftSyntax
% for node in [SyntaxBuildableNode(syntax_node) for syntax_node in SYNTAX_NODES if syntax_node.is_buildable()]:
% type = node.type()
% base_type = node.base_type()
% conformances = [base_type.buildable(), type.expressible_as()]
% has_with_trailing_comma_trait = node.node.traits and 'WithTrailingComma' in node.node.traits
% if has_with_trailing_comma_trait:
% conformances.append("HasTrailingComma")
% end
% if node.documentation():
/// ${node.documentation()}
% end
public struct ${type.buildable()}: ${base_type.buildable()}, ${type.expressible_as()} {
public struct ${type.buildable()}${conformance_clause(conformances)} {
% children = node.children()
% for child in children:
let ${child.name()}: ${child.type().buildable()}
Expand Down Expand Up @@ -114,6 +119,23 @@ public struct ${type.buildable()}: ${base_type.buildable()}, ${type.expressible_
public func create${type.buildable_base_name()}() -> ${type.buildable()} {
return self
}
% if has_with_trailing_comma_trait:
% trailing_comma_init_args = []
% for child in children:
% if child.name() == 'trailingComma':
% trailing_comma_init_args.append('%s: withComma ? .comma : nil' % (child.name()))
% else:
% trailing_comma_init_args.append('%s: %s' % (child.name(), child.name()))
% end
% end

/// Conformance to `HasTrailingComma`.
public func withTrailingComma(_ withComma: Bool) -> Self {
return Self.init(
${',\n '.join(trailing_comma_init_args)}
)
}
% end

/// `${type.buildable()}` might conform to `${base_type.expressible_as()}` via different `ExpressibleAs*` paths.
/// Thus, there are multiple default implementations for `create${base_type.buildable_base_name()}`, some of which perform conversions through `ExpressibleAs*` protocols.
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftSyntaxBuilder/HasTrailingComma.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

public protocol HasTrailingComma {
/// Returns this node overriding presence of the trailing comma
func withTrailingComma(_ withComma: Bool) -> Self
}
6 changes: 6 additions & 0 deletions Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public struct ${type.result_builder()} {
public static func buildFinalResult(_ component: Component) -> FinalResult {
% if element_type.is_token():
return .init(component)
% elif element_type.has_with_trailing_comma_trait():
let lastIndex = component.count - 1
return .init(component.enumerated().map({ index, source in
let element = source.create${element_type.buildable_base_name()}()
return index < lastIndex ? element.withTrailingComma(true) : element
}))
% else:
return .init(component.map { $0.create${element_type.buildable()}() })
% end
Expand Down
Loading