Skip to content

Commit 91639f1

Browse files
committed
Validate that all children containing a syntax collection have a plural name
1 parent 850fcc8 commit 91639f1

File tree

14 files changed

+1486
-597
lines changed

14 files changed

+1486
-597
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public let DECL_NODES: [Node] = [
7979
kind: .token(choices: [.token(tokenKind: "LeftParenToken")])
8080
),
8181
Child(
82-
name: "PropertyList",
82+
name: "Properties",
83+
deprecatedName: "PropertyList",
8384
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Property"),
8485
nameForDiagnostics: "properties"
8586
),
@@ -104,7 +105,8 @@ public let DECL_NODES: [Node] = [
104105
kind: .token(choices: [.token(tokenKind: "LeftParenToken")])
105106
),
106107
Child(
107-
name: "PropertyList",
108+
name: "Properties",
109+
deprecatedName: "PropertyList",
108110
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Property"),
109111
nameForDiagnostics: "properties"
110112
),
@@ -676,7 +678,8 @@ public let DECL_NODES: [Node] = [
676678
documentation: "The '(' to open the parameter clause."
677679
),
678680
Child(
679-
name: "ParameterList",
681+
name: "Parameters",
682+
deprecatedName: "ParameterList",
680683
kind: .collection(kind: .enumCaseParameterList, collectionElementName: "Parameter"),
681684
nameForDiagnostics: "parameters",
682685
documentation: "The actual parameters."
@@ -1469,7 +1472,8 @@ public let DECL_NODES: [Node] = [
14691472
isOptional: true
14701473
),
14711474
Child(
1472-
name: "ArgumentList",
1475+
name: "Arguments",
1476+
deprecatedName: "ArgumentList",
14731477
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Argument")
14741478
),
14751479
Child(
@@ -1642,7 +1646,8 @@ public let DECL_NODES: [Node] = [
16421646
kind: .token(choices: [.token(tokenKind: "LeftParenToken")])
16431647
),
16441648
Child(
1645-
name: "ParameterList",
1649+
name: "Parameters",
1650+
deprecatedName: "ParameterList",
16461651
kind: .collection(kind: .functionParameterList, collectionElementName: "Parameter"),
16471652
nameForDiagnostics: "parameters"
16481653
),
@@ -2266,7 +2271,8 @@ public let DECL_NODES: [Node] = [
22662271
kind: .token(choices: [.token(tokenKind: "ColonToken")])
22672272
),
22682273
Child(
2269-
name: "InheritedTypeCollection",
2274+
name: "InheritedTypes",
2275+
deprecatedName: "InheritedTypeCollection",
22702276
kind: .collection(kind: .inheritedTypeList, collectionElementName: "InheritedType")
22712277
),
22722278
]

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ public let EXPR_NODES: [Node] = [
443443
documentation: "The '(' to open the parameter clause."
444444
),
445445
Child(
446-
name: "ParameterList",
446+
name: "Properties",
447+
deprecatedName: "ParameterList",
447448
kind: .collection(kind: .closureParameterList, collectionElementName: "Parameter"),
448449
nameForDiagnostics: "parameters",
449450
documentation: "The actual parameters."
@@ -805,7 +806,8 @@ public let EXPR_NODES: [Node] = [
805806
isOptional: true
806807
),
807808
Child(
808-
name: "ArgumentList",
809+
name: "Arguments",
810+
deprecatedName: "ArgumentList",
809811
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Argument"),
810812
nameForDiagnostics: "arguments"
811813
),
@@ -1116,7 +1118,8 @@ public let EXPR_NODES: [Node] = [
11161118
kind: .token(choices: [.token(tokenKind: "LeftSquareToken")])
11171119
),
11181120
Child(
1119-
name: "ArgumentList",
1121+
name: "Arguments",
1122+
deprecatedName: "ArgumentList",
11201123
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Argument"),
11211124
nameForDiagnostics: "arguments"
11221125
),
@@ -1159,7 +1162,8 @@ public let EXPR_NODES: [Node] = [
11591162
isOptional: true
11601163
),
11611164
Child(
1162-
name: "ArgumentList",
1165+
name: "Arguments",
1166+
deprecatedName: "ArgumentList",
11631167
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Argument")
11641168
),
11651169
Child(
@@ -1524,7 +1528,8 @@ public let EXPR_NODES: [Node] = [
15241528
kind: .token(choices: [.token(tokenKind: "LeftSquareToken")])
15251529
),
15261530
Child(
1527-
name: "ArgumentList",
1531+
name: "Arguments",
1532+
deprecatedName: "ArgumentList",
15281533
kind: .collection(kind: .tupleExprElementList, collectionElementName: "Argument"),
15291534
nameForDiagnostics: "arguments"
15301535
),

CodeGeneration/Sources/SyntaxSupport/GenericNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ public let GENERIC_NODES: [Node] = [
177177
documentation: "The `where` keyword in the clause."
178178
),
179179
Child(
180-
name: "RequirementList",
180+
name: "Requirements",
181+
deprecatedName: "RequirementList",
181182
kind: .collection(kind: .genericRequirementList, collectionElementName: "Requirement"),
182183
documentation: "The list of requirements in the clause."
183184
),
@@ -254,7 +255,8 @@ public let GENERIC_NODES: [Node] = [
254255
kind: .token(choices: [.token(tokenKind: "LeftAngleToken")])
255256
),
256257
Child(
257-
name: "PrimaryAssociatedTypeList",
258+
name: "PrimaryAssociatedTypes",
259+
deprecatedName: "PrimaryAssociatedTypeList",
258260
kind: .collection(kind: .primaryAssociatedTypeList, collectionElementName: "PrimaryAssociatedType")
259261
),
260262
Child(

CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ public let STMT_NODES: [Node] = [
573573
kind: .token(choices: [.token(tokenKind: "LeftParenToken")])
574574
),
575575
Child(
576-
name: "ElementList",
576+
name: "Elements",
577+
deprecatedName: "ElementList",
577578
kind: .collection(kind: .yieldExprList, collectionElementName: "Element")
578579
),
579580
Child(

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ fileprivate extension ChildKind {
6868
return false
6969
}
7070
}
71+
72+
var isCollection: Bool {
73+
switch self {
74+
case .node: return false
75+
case .nodeChoices(let choices): return choices.contains(where: { $0.kind.isCollection })
76+
case .collection: return true
77+
case .token: return false
78+
}
79+
}
7180
}
7281

7382
fileprivate extension Child {
@@ -534,4 +543,51 @@ class ValidateSyntaxNodes: XCTestCase {
534543
]
535544
)
536545
}
546+
547+
func testAllCollectionChildrenAreMarkedAsSuch() {
548+
var failures: [ValidationFailure] = []
549+
550+
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
551+
for child in node.children {
552+
if case .node(kind: let kind) = child.kind, SYNTAX_NODE_MAP[kind]?.collectionNode != nil {
553+
failures.append(
554+
ValidationFailure(
555+
node: node.kind,
556+
message: "child '\(child.name)' is a SyntaxCollection but child is not marked as a collection"
557+
)
558+
)
559+
}
560+
}
561+
}
562+
563+
assertFailuresMatchXFails(
564+
failures,
565+
expectedFailures: []
566+
)
567+
}
568+
569+
func testSyntaxCollectionChildrenArePlural() {
570+
var failures: [ValidationFailure] = []
571+
572+
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
573+
for child in node.nonUnexpectedChildren where child.kind.isCollection {
574+
if !child.name.hasSuffix("s") {
575+
failures.append(
576+
ValidationFailure(
577+
node: node.kind,
578+
message: "child '\(child.name)' is a collection and should thus be named as a plural"
579+
)
580+
)
581+
}
582+
}
583+
}
584+
585+
assertFailuresMatchXFails(
586+
failures,
587+
expectedFailures: [
588+
// The child is singular here, the path just consists of multiple components
589+
ValidationFailure(node: .importDecl, message: "child 'Path' is a collection and should thus be named as a plural")
590+
]
591+
)
592+
}
537593
}

Sources/SwiftParserDiagnostics/generated/ChildNameForDiagnostics.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntax
1616

1717
private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
1818
switch keyPath {
19-
case \AccessesEffectSyntax.propertyList:
19+
case \AccessesEffectSyntax.properties:
2020
return "properties"
2121
case \AccessorDeclSyntax.attributes:
2222
return "attributes"
@@ -72,7 +72,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
7272
return "generic where clause"
7373
case \ClosureParamSyntax.name:
7474
return "name"
75-
case \ClosureParameterClauseSyntax.parameterList:
75+
case \ClosureParameterClauseSyntax.properties:
7676
return "parameters"
7777
case \ClosureParameterSyntax.attributes:
7878
return "attributes"
@@ -116,7 +116,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
116116
return "elements"
117117
case \EnumCaseElementSyntax.associatedValue:
118118
return "associated values"
119-
case \EnumCaseParameterClauseSyntax.parameterList:
119+
case \EnumCaseParameterClauseSyntax.parameters:
120120
return "parameters"
121121
case \EnumCaseParameterSyntax.modifiers:
122122
return "modifiers"
@@ -146,7 +146,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
146146
return "body"
147147
case \FunctionCallExprSyntax.calledExpression:
148148
return "called expression"
149-
case \FunctionCallExprSyntax.argumentList:
149+
case \FunctionCallExprSyntax.arguments:
150150
return "arguments"
151151
case \FunctionCallExprSyntax.trailingClosure:
152152
return "trailing closure"
@@ -210,11 +210,11 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
210210
return "function signature"
211211
case \InitializerDeclSyntax.genericWhereClause:
212212
return "generic where clause"
213-
case \InitializesEffectSyntax.propertyList:
213+
case \InitializesEffectSyntax.properties:
214214
return "properties"
215215
case \KeyPathExprSyntax.root:
216216
return "root"
217-
case \KeyPathSubscriptComponentSyntax.argumentList:
217+
case \KeyPathSubscriptComponentSyntax.arguments:
218218
return "arguments"
219219
case \LabeledSpecializeEntrySyntax.label:
220220
return "label"
@@ -262,7 +262,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
262262
return "fixity"
263263
case \OperatorPrecedenceAndTypesSyntax.precedenceGroup:
264264
return "precedence group"
265-
case \ParameterClauseSyntax.parameterList:
265+
case \ParameterClauseSyntax.parameters:
266266
return "parameters"
267267
case \PatternBindingSyntax.typeAnnotation:
268268
return "type annotation"
@@ -326,7 +326,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
326326
return "generic where clause"
327327
case \SubscriptExprSyntax.calledExpression:
328328
return "called expression"
329-
case \SubscriptExprSyntax.argumentList:
329+
case \SubscriptExprSyntax.arguments:
330330
return "arguments"
331331
case \SubscriptExprSyntax.trailingClosure:
332332
return "trailing closure"

0 commit comments

Comments
 (0)