Skip to content

Commit 301f801

Browse files
committed
Add documentation on pattern bindings
Another area that’s not super obvious. Add some documentation that hopefully sheds some light into why pattern bindings exist and what thye do.
1 parent 5412040 commit 301f801

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,29 +1619,56 @@ public let DECL_NODES: [Node] = [
16191619
kind: .patternBinding,
16201620
base: .syntax,
16211621
nameForDiagnostics: nil,
1622+
documentation: """
1623+
Defines variables inside a variable declaration.
1624+
""",
16221625
traits: [
16231626
"WithTrailingComma"
16241627
],
16251628
children: [
16261629
Child(
16271630
name: "Pattern",
1628-
kind: .node(kind: .pattern)
1631+
kind: .node(kind: .pattern),
1632+
documentation: """
1633+
The pattern that defines the variables.
1634+
1635+
In simple variable declarations this is an ``IdentifierPatternSyntax``, which defines
1636+
the name of a single variable.
1637+
1638+
In more complex variable declaration, this can, for example, be a ``TuplePatternSyntax``
1639+
that destructures a tuple.
1640+
1641+
```swift
1642+
let (x, y) = (1, 2)
1643+
```
1644+
"""
16291645
),
16301646
Child(
16311647
name: "TypeAnnotation",
16321648
kind: .node(kind: .typeAnnotation),
16331649
nameForDiagnostics: "type annotation",
1650+
documentation: """
1651+
The type of the variables defined by the pattern.
1652+
1653+
Can be omitted, in which case the variables’ types are inferred from the initializer.
1654+
""",
16341655
isOptional: true
16351656
),
16361657
Child(
16371658
name: "Initializer",
16381659
kind: .node(kind: .initializerClause),
1660+
documentation: """
1661+
If the variables have a default value, the clause that initializes them.
1662+
""",
16391663
isOptional: true
16401664
),
16411665
Child(
16421666
name: "AccessorBlock",
16431667
deprecatedName: "Accessor",
16441668
kind: .node(kind: .accessorBlock),
1669+
documentation: """
1670+
If the variable is computed, the accessors that get (and optionally set) the value.
1671+
""",
16451672
isOptional: true
16461673
),
16471674
Child(
@@ -2296,6 +2323,12 @@ public let DECL_NODES: [Node] = [
22962323
kind: .variableDecl,
22972324
base: .decl,
22982325
nameForDiagnostics: "variable",
2326+
documentation: """
2327+
Declaration of one or more variables
2328+
2329+
The core of a variable declaration consists of a binding specifier (`let` or `var),
2330+
followed by any number of pattern bindings, which define the variables.
2331+
""",
22992332
traits: [
23002333
"WithAttributes",
23012334
"WithModifiers",
@@ -2316,13 +2349,28 @@ public let DECL_NODES: [Node] = [
23162349
Child(
23172350
name: "BindingSpecifier",
23182351
deprecatedName: "BindingKeyword",
2319-
kind: .token(choices: [.keyword(text: "let"), .keyword(text: "var"), .keyword(text: "inout")])
2352+
kind: .token(choices: [.keyword(text: "let"), .keyword(text: "var"), .keyword(text: "inout")]),
2353+
documentation: """
2354+
The specifier that defines the type of the variables declared (`let` or `var`).
2355+
"""
23202356
),
23212357
Child(
23222358
name: "Bindings",
2323-
kind: .collection(kind: .patternBindingList, collectionElementName: "Binding")
2359+
kind: .collection(kind: .patternBindingList, collectionElementName: "Binding"),
2360+
documentation: """
2361+
The pattern bindings that define the actual variables.
2362+
2363+
The pattern bindings contain the declared variables’ names, their types,
2364+
initializers and accessors.
2365+
2366+
A variable declaration can contain multiple pattern bindings, because it’s possible
2367+
to define multiple variables after a single `let` keyword as follows.
2368+
2369+
```swift
2370+
let x: Int = 1, y: Int = 2
2371+
```
2372+
"""
23242373
),
23252374
]
23262375
),
2327-
23282376
]

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6969,6 +6969,11 @@ public struct TypeAliasDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
69696969

69706970
// MARK: - VariableDeclSyntax
69716971

6972+
/// Declaration of one or more variables
6973+
///
6974+
/// The core of a variable declaration consists of a binding specifier (`let` or `var),
6975+
/// followed by any number of pattern bindings, which define the variables.
6976+
///
69726977
/// ### Children
69736978
///
69746979
/// - `attributes`: ``AttributeListSyntax``
@@ -6995,6 +7000,8 @@ public struct VariableDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
69957000

69967001
/// - Parameters:
69977002
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
7003+
/// - bindingSpecifier: The specifier that defines the type of the variables declared (`let` or `var`).
7004+
/// - bindings: The pattern bindings that define the actual variables.
69987005
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
69997006
public init(
70007007
leadingTrivia: Trivia? = nil,
@@ -7142,6 +7149,7 @@ public struct VariableDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
71427149
}
71437150
}
71447151

7152+
/// The specifier that defines the type of the variables declared (`let` or `var`).
71457153
public var bindingSpecifier: TokenSyntax {
71467154
get {
71477155
return TokenSyntax(data.child(at: 5, parent: Syntax(self))!)
@@ -7160,6 +7168,17 @@ public struct VariableDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
71607168
}
71617169
}
71627170

7171+
/// The pattern bindings that define the actual variables.
7172+
///
7173+
/// The pattern bindings contain the declared variables’ names, their types,
7174+
/// initializers and accessors.
7175+
///
7176+
/// A variable declaration can contain multiple pattern bindings, because it’s possible
7177+
/// to define multiple variables after a single `let` keyword as follows.
7178+
///
7179+
/// ```swift
7180+
/// let x: Int = 1, y: Int = 2
7181+
/// ```
71637182
public var bindings: PatternBindingListSyntax {
71647183
get {
71657184
return PatternBindingListSyntax(data.child(at: 7, parent: Syntax(self))!)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14112,6 +14112,8 @@ public struct OriginallyDefinedInAttributeArgumentsSyntax: SyntaxProtocol, Synta
1411214112

1411314113
// MARK: - PatternBindingSyntax
1411414114

14115+
/// Defines variables inside a variable declaration.
14116+
///
1411514117
/// ### Children
1411614118
///
1411714119
/// - `pattern`: ``PatternSyntax``
@@ -14143,6 +14145,10 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1414314145

1414414146
/// - Parameters:
1414514147
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
14148+
/// - pattern: The pattern that defines the variables.
14149+
/// - typeAnnotation: The type of the variables defined by the pattern.
14150+
/// - initializer: If the variables have a default value, the clause that initializes them.
14151+
/// - accessorBlock: If the variable is computed, the accessors that get (and optionally set) the value.
1414614152
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
1414714153
public init(
1414814154
leadingTrivia: Trivia? = nil,
@@ -14210,6 +14216,17 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1421014216
}
1421114217
}
1421214218

14219+
/// The pattern that defines the variables.
14220+
///
14221+
/// In simple variable declarations this is an ``IdentifierPatternSyntax``, which defines
14222+
/// the name of a single variable.
14223+
///
14224+
/// In more complex variable declaration, this can, for example, be a ``TuplePatternSyntax``
14225+
/// that destructures a tuple.
14226+
///
14227+
/// ```swift
14228+
/// let (x, y) = (1, 2)
14229+
/// ```
1421314230
public var pattern: PatternSyntax {
1421414231
get {
1421514232
return PatternSyntax(data.child(at: 1, parent: Syntax(self))!)
@@ -14228,6 +14245,9 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1422814245
}
1422914246
}
1423014247

14248+
/// The type of the variables defined by the pattern.
14249+
///
14250+
/// Can be omitted, in which case the variables’ types are inferred from the initializer.
1423114251
public var typeAnnotation: TypeAnnotationSyntax? {
1423214252
get {
1423314253
return data.child(at: 3, parent: Syntax(self)).map(TypeAnnotationSyntax.init)
@@ -14246,6 +14266,7 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1424614266
}
1424714267
}
1424814268

14269+
/// If the variables have a default value, the clause that initializes them.
1424914270
public var initializer: InitializerClauseSyntax? {
1425014271
get {
1425114272
return data.child(at: 5, parent: Syntax(self)).map(InitializerClauseSyntax.init)
@@ -14264,6 +14285,7 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1426414285
}
1426514286
}
1426614287

14288+
/// If the variable is computed, the accessors that get (and optionally set) the value.
1426714289
public var accessorBlock: AccessorBlockSyntax? {
1426814290
get {
1426914291
return data.child(at: 7, parent: Syntax(self)).map(AccessorBlockSyntax.init)

0 commit comments

Comments
 (0)