Skip to content

Commit 55f0b1d

Browse files
authored
Merge pull request #2035 from ahoppen/ahoppen/binding-documentation
Add documentation on pattern bindings
2 parents 1e8b158 + 9aa3d0a commit 55f0b1d

File tree

4 files changed

+104
-7
lines changed

4 files changed

+104
-7
lines changed

CodeGeneration/Sources/SyntaxSupport/AttributeNodes.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ public let ATTRIBUTE_NODES: [Node] = [
153153
kind: .node(kind: .documentationAttributeArgumentList)
154154
),
155155
]),
156-
documentation: "The arguments of the attribute. In case the attribute takes multiple arguments, they are gather in the appropriate takes first.",
156+
documentation: """
157+
The arguments of the attribute.
158+
159+
In case of user-defined attributes, such as macros, property wrappers or result builders,
160+
this is always either an `argumentList` of type ``LabeledExprListSyntax`` or `nil`.
161+
""",
157162
isOptional: true
158163
),
159164
Child(

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, for example
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, for example
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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ public struct AttributeSyntax: SyntaxProtocol, SyntaxHashable {
913913
/// - atSign: The `@` sign.
914914
/// - attributeName: The name of the attribute.
915915
/// - leftParen: If the attribute takes arguments, the opening parenthesis.
916-
/// - arguments: The arguments of the attribute. In case the attribute takes multiple arguments, they are gather in the appropriate takes first.
916+
/// - arguments: The arguments of the attribute.
917917
/// - rightParen: If the attribute takes arguments, the closing parenthesis.
918918
/// - 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.
919919
public init(
@@ -1039,7 +1039,10 @@ public struct AttributeSyntax: SyntaxProtocol, SyntaxHashable {
10391039
}
10401040
}
10411041

1042-
/// The arguments of the attribute. In case the attribute takes multiple arguments, they are gather in the appropriate takes first.
1042+
/// The arguments of the attribute.
1043+
///
1044+
/// In case of user-defined attributes, such as macros, property wrappers or result builders,
1045+
/// this is always either an `argumentList` of type ``LabeledExprListSyntax`` or `nil`.
10431046
public var arguments: Arguments? {
10441047
get {
10451048
return data.child(at: 7, parent: Syntax(self)).map(Arguments.init)
@@ -14112,6 +14115,8 @@ public struct OriginallyDefinedInAttributeArgumentsSyntax: SyntaxProtocol, Synta
1411214115

1411314116
// MARK: - PatternBindingSyntax
1411414117

14118+
/// Defines variables inside a variable declaration.
14119+
///
1411514120
/// ### Children
1411614121
///
1411714122
/// - `pattern`: ``PatternSyntax``
@@ -14143,6 +14148,10 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1414314148

1414414149
/// - Parameters:
1414514150
/// - 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.
14151+
/// - pattern: The pattern that defines the variables.
14152+
/// - typeAnnotation: The type of the variables defined by the pattern.
14153+
/// - initializer: If the variables have a default value, the clause that initializes them.
14154+
/// - accessorBlock: If the variable is computed, the accessors that get (and optionally set) the value.
1414614155
/// - 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.
1414714156
public init(
1414814157
leadingTrivia: Trivia? = nil,
@@ -14210,6 +14219,17 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1421014219
}
1421114220
}
1421214221

14222+
/// The pattern that defines the variables.
14223+
///
14224+
/// In simple variable declarations this is an ``IdentifierPatternSyntax``, which defines
14225+
/// the name of a single variable.
14226+
///
14227+
/// In more complex variable declaration, this can, for example, be a ``TuplePatternSyntax``
14228+
/// that destructures a tuple.
14229+
///
14230+
/// ```swift
14231+
/// let (x, y) = (1, 2)
14232+
/// ```
1421314233
public var pattern: PatternSyntax {
1421414234
get {
1421514235
return PatternSyntax(data.child(at: 1, parent: Syntax(self))!)
@@ -14228,6 +14248,9 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1422814248
}
1422914249
}
1423014250

14251+
/// The type of the variables defined by the pattern.
14252+
///
14253+
/// Can be omitted, in which case the variables’ types are inferred from the initializer.
1423114254
public var typeAnnotation: TypeAnnotationSyntax? {
1423214255
get {
1423314256
return data.child(at: 3, parent: Syntax(self)).map(TypeAnnotationSyntax.init)
@@ -14246,6 +14269,7 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1424614269
}
1424714270
}
1424814271

14272+
/// If the variables have a default value, the clause that initializes them.
1424914273
public var initializer: InitializerClauseSyntax? {
1425014274
get {
1425114275
return data.child(at: 5, parent: Syntax(self)).map(InitializerClauseSyntax.init)
@@ -14264,6 +14288,7 @@ public struct PatternBindingSyntax: SyntaxProtocol, SyntaxHashable {
1426414288
}
1426514289
}
1426614290

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

0 commit comments

Comments
 (0)