Skip to content
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

Add WithOptionalCodeBlock trait and adopt it where there's a body #2359

Merged
merged 4 commits into from
Nov 16, 2023
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: 7 additions & 1 deletion CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public let DECL_NODES: [Node] = [
base: .decl,
nameForDiagnostics: "accessor",
parserFunction: "parseAccessorDecl",
traits: ["WithAttributes"],
traits: [
"WithOptionalCodeBlock",
"WithAttributes",
],
children: [
Child(
name: "attributes",
Expand Down Expand Up @@ -493,6 +496,7 @@ public let DECL_NODES: [Node] = [
traits: [
"WithAttributes",
"WithModifiers",
"WithOptionalCodeBlock",
],
children: [
Child(
Expand Down Expand Up @@ -879,6 +883,7 @@ public let DECL_NODES: [Node] = [
"WithAttributes",
"WithGenericParameters",
"WithModifiers",
"WithOptionalCodeBlock",
],
children: [
Child(
Expand Down Expand Up @@ -1217,6 +1222,7 @@ public let DECL_NODES: [Node] = [
"WithAttributes",
"WithGenericParameters",
"WithModifiers",
"WithOptionalCodeBlock",
],
children: [
Child(
Expand Down
6 changes: 6 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public let TRAITS: [Trait] = [
Child(name: "modifiers", kind: .node(kind: .declModifierList))
]
),
Trait(
traitName: "WithOptionalCodeBlock",
children: [
Child(name: "body", kind: .node(kind: .codeBlock), isOptional: true)
]
),
Trait(
traitName: "WithStatements",
children: [
Expand Down
4 changes: 4 additions & 0 deletions Release Notes/511.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
- Description: Enum to exhaustively switch over all different syntax nodes of each base type.
- Pull Request: https://github.com/apple/swift-syntax/pull/2351

- `WithOptionalCodeBlock`
- Description: A trait for syntax nodes that have an optional code block, such as `FunctionDeclSyntax` and `InitializerDeclSyntax`.
- Pull Request: https://github.com/apple/swift-syntax/pull/2359

## API Behavior Changes

## Deprecations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
- <doc:SwiftSyntax/WithCodeBlockSyntax>
- <doc:SwiftSyntax/WithGenericParametersSyntax>
- <doc:SwiftSyntax/WithModifiersSyntax>
- <doc:SwiftSyntax/WithOptionalCodeBlockSyntax>
- <doc:SwiftSyntax/WithStatementsSyntax>
- <doc:SwiftSyntax/WithTrailingCommaSyntax>

Expand Down
45 changes: 41 additions & 4 deletions Sources/SwiftSyntax/generated/SyntaxTraits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,43 @@ public extension SyntaxProtocol {
}
}

// MARK: - WithOptionalCodeBlockSyntax


public protocol WithOptionalCodeBlockSyntax: SyntaxProtocol {
var body: CodeBlockSyntax? {
get
set
}
}

public extension WithOptionalCodeBlockSyntax {
/// Without this function, the `with` function defined on `SyntaxProtocol`
/// does not work on existentials of this protocol type.
@_disfavoredOverload
func with<T>(_ keyPath: WritableKeyPath<WithOptionalCodeBlockSyntax, T>, _ newChild: T) -> WithOptionalCodeBlockSyntax {
var copy: WithOptionalCodeBlockSyntax = self
copy[keyPath: keyPath] = newChild
return copy
}
}

public extension SyntaxProtocol {
/// Check whether the non-type erased version of this syntax node conforms to
/// `WithOptionalCodeBlockSyntax`.
/// Note that this will incur an existential conversion.
func isProtocol(_: WithOptionalCodeBlockSyntax.Protocol) -> Bool {
return self.asProtocol(WithOptionalCodeBlockSyntax.self) != nil
}

/// Return the non-type erased version of this syntax node if it conforms to
/// `WithOptionalCodeBlockSyntax`. Otherwise return `nil`.
/// Note that this will incur an existential conversion.
func asProtocol(_: WithOptionalCodeBlockSyntax.Protocol) -> WithOptionalCodeBlockSyntax? {
return Syntax(self).asProtocol(SyntaxProtocol.self) as? WithOptionalCodeBlockSyntax
}
}

// MARK: - WithStatementsSyntax


Expand Down Expand Up @@ -649,7 +686,7 @@ public extension SyntaxProtocol {

extension AccessorBlockSyntax: BracedSyntax {}

extension AccessorDeclSyntax: WithAttributesSyntax {}
extension AccessorDeclSyntax: WithOptionalCodeBlockSyntax, WithAttributesSyntax {}

extension AccessorEffectSpecifiersSyntax: EffectSpecifiersSyntax {}

Expand Down Expand Up @@ -693,7 +730,7 @@ extension DeclNameArgumentsSyntax: ParenthesizedSyntax {}

extension DeferStmtSyntax: WithCodeBlockSyntax {}

extension DeinitializerDeclSyntax: WithAttributesSyntax, WithModifiersSyntax {}
extension DeinitializerDeclSyntax: WithAttributesSyntax, WithModifiersSyntax, WithOptionalCodeBlockSyntax {}

extension DictionaryElementSyntax: WithTrailingCommaSyntax {}

Expand Down Expand Up @@ -723,7 +760,7 @@ extension ExtensionDeclSyntax: DeclGroupSyntax, WithAttributesSyntax, WithModifi

extension ForStmtSyntax: WithCodeBlockSyntax {}

extension FunctionDeclSyntax: NamedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
extension FunctionDeclSyntax: NamedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax, WithOptionalCodeBlockSyntax {}

extension FunctionEffectSpecifiersSyntax: EffectSpecifiersSyntax {}

Expand All @@ -747,7 +784,7 @@ extension ImportDeclSyntax: WithAttributesSyntax, WithModifiersSyntax {}

extension InheritedTypeSyntax: WithTrailingCommaSyntax {}

extension InitializerDeclSyntax: WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
extension InitializerDeclSyntax: WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax, WithOptionalCodeBlockSyntax {}

extension LabeledExprSyntax: WithTrailingCommaSyntax {}

Expand Down
21 changes: 4 additions & 17 deletions Sources/SwiftSyntaxBuilder/SyntaxNodeWithBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ extension SyntaxStringInterpolation {
}
}

// MARK: - HasCodeBlock

public protocol HasTrailingCodeBlock {
var body: CodeBlockSyntax { get set }
// MARK: - HasTrailingCodeBlock

public protocol HasTrailingCodeBlock: WithCodeBlockSyntax {
/// Constructs a syntax node where `header` builds the text of the node before the body in braces and `bodyBuilder` is used to build the node’s body.
///
/// For example, you can construct
Expand Down Expand Up @@ -90,11 +88,9 @@ extension ForStmtSyntax: HasTrailingCodeBlock {}
extension GuardStmtSyntax: HasTrailingCodeBlock {}
extension WhileStmtSyntax: HasTrailingCodeBlock {}

// MARK: - HasOptionalCodeBlock

public protocol HasTrailingOptionalCodeBlock {
var body: CodeBlockSyntax? { get set }
// MARK: - WithOptionalCodeBlockSyntax

public extension WithOptionalCodeBlockSyntax where Self: DeclSyntaxProtocol {
/// Constructs a syntax node where `header` builds the text of the node before the body in braces and `bodyBuilder` is used to build the node’s body.
///
/// For example, you can construct
Expand All @@ -114,10 +110,6 @@ public protocol HasTrailingOptionalCodeBlock {
/// ```
///
/// Throws an error if `header` defines a different node type than the type the initializer is called on. E.g. if calling `try FunctionDeclSyntax("init") {}`
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) throws
}

public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) throws {
let decl = DeclSyntax("\(header) {}")
guard let castedDecl = decl.as(Self.self) else {
Expand All @@ -128,11 +120,6 @@ public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
}
}

extension AccessorDeclSyntax: HasTrailingOptionalCodeBlock {}
extension DeinitializerDeclSyntax: HasTrailingOptionalCodeBlock {}
extension FunctionDeclSyntax: HasTrailingOptionalCodeBlock {}
extension InitializerDeclSyntax: HasTrailingOptionalCodeBlock {}

// MARK: HasTrailingMemberDeclBlock

public protocol HasTrailingMemberDeclBlock {
Expand Down