Skip to content

Commit 5c2923f

Browse files
committed
Verify that children are not named 'Identifier'
1 parent 91639f1 commit 5c2923f

File tree

17 files changed

+1816
-562
lines changed

17 files changed

+1816
-562
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public let DECL_NODES: [Node] = [
242242
nameForDiagnostics: "actor",
243243
traits: [
244244
"DeclGroup",
245-
"IdentifiedDecl",
245+
"NamedDecl",
246246
"WithAttributes",
247247
"WithGenericParameters",
248248
"WithModifiers",
@@ -265,7 +265,8 @@ public let DECL_NODES: [Node] = [
265265
kind: .token(choices: [.keyword(text: "actor")])
266266
),
267267
Child(
268-
name: "Identifier",
268+
name: "Name",
269+
deprecatedName: "Identifier",
269270
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
270271
),
271272
Child(
@@ -330,7 +331,7 @@ public let DECL_NODES: [Node] = [
330331
```
331332
""",
332333
traits: [
333-
"IdentifiedDecl",
334+
"NamedDecl",
334335
"WithAttributes",
335336
"WithModifiers",
336337
],
@@ -355,7 +356,8 @@ public let DECL_NODES: [Node] = [
355356
documentation: "The `associatedtype` keyword for this declaration."
356357
),
357358
Child(
358-
name: "Identifier",
359+
name: "Name",
360+
deprecatedName: "Identifier",
359361
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
360362
documentation: "The name of this associated type."
361363
),
@@ -418,7 +420,7 @@ public let DECL_NODES: [Node] = [
418420
""",
419421
traits: [
420422
"DeclGroup",
421-
"IdentifiedDecl",
423+
"NamedDecl",
422424
"WithAttributes",
423425
"WithGenericParameters",
424426
"WithModifiers",
@@ -444,7 +446,8 @@ public let DECL_NODES: [Node] = [
444446
documentation: "The `class` keyword for this declaration."
445447
),
446448
Child(
447-
name: "Identifier",
449+
name: "Name",
450+
deprecatedName: "Identifier",
448451
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
449452
documentation: "The name of the class."
450453
),
@@ -806,7 +809,8 @@ public let DECL_NODES: [Node] = [
806809
],
807810
children: [
808811
Child(
809-
name: "Identifier",
812+
name: "Name",
813+
deprecatedName: "Identifier",
810814
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
811815
documentation: "The name of this case."
812816
),
@@ -839,7 +843,7 @@ public let DECL_NODES: [Node] = [
839843
documentation: "A Swift `enum` declaration.",
840844
traits: [
841845
"DeclGroup",
842-
"IdentifiedDecl",
846+
"NamedDecl",
843847
"WithAttributes",
844848
"WithGenericParameters",
845849
"WithModifiers",
@@ -865,7 +869,8 @@ public let DECL_NODES: [Node] = [
865869
documentation: "The `enum` keyword for this declaration."
866870
),
867871
Child(
868-
name: "Identifier",
872+
name: "Name",
873+
deprecatedName: "Identifier",
869874
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
870875
documentation: "Declares the name of this enum. If the name matches a reserved keyword use backticks to escape it."
871876
),
@@ -961,7 +966,7 @@ public let DECL_NODES: [Node] = [
961966
base: .decl,
962967
nameForDiagnostics: "function",
963968
traits: [
964-
"IdentifiedDecl",
969+
"NamedDecl",
965970
"WithAttributes",
966971
"WithGenericParameters",
967972
"WithModifiers",
@@ -984,7 +989,8 @@ public let DECL_NODES: [Node] = [
984989
kind: .token(choices: [.keyword(text: "func")])
985990
),
986991
Child(
987-
name: "Identifier",
992+
name: "Name",
993+
deprecatedName: "Identifier",
988994
kind: .token(choices: [
989995
.token(tokenKind: "IdentifierToken"),
990996
.token(tokenKind: "BinaryOperatorToken"),
@@ -1373,7 +1379,7 @@ public let DECL_NODES: [Node] = [
13731379
base: .decl,
13741380
nameForDiagnostics: "macro",
13751381
traits: [
1376-
"IdentifiedDecl",
1382+
"NamedDecl",
13771383
"WithAttributes",
13781384
"WithGenericParameters",
13791385
"WithModifiers",
@@ -1396,7 +1402,8 @@ public let DECL_NODES: [Node] = [
13961402
kind: .token(choices: [.keyword(text: "macro")])
13971403
),
13981404
Child(
1399-
name: "Identifier",
1405+
name: "Name",
1406+
deprecatedName: "Identifier",
14001407
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
14011408
),
14021409
Child(
@@ -1581,7 +1588,7 @@ public let DECL_NODES: [Node] = [
15811588
nameForDiagnostics: "operator declaration",
15821589
documentation: "A Swift `operator` declaration.",
15831590
traits: [
1584-
"IdentifiedDecl"
1591+
"NamedDecl"
15851592
],
15861593
children: [
15871594
Child(
@@ -1596,7 +1603,8 @@ public let DECL_NODES: [Node] = [
15961603
kind: .token(choices: [.keyword(text: "operator")])
15971604
),
15981605
Child(
1599-
name: "Identifier",
1606+
name: "Name",
1607+
deprecatedName: "Identifier",
16001608
kind: .token(choices: [.token(tokenKind: "BinaryOperatorToken"), .token(tokenKind: "PrefixOperatorToken"), .token(tokenKind: "PostfixOperatorToken")])
16011609
),
16021610
Child(
@@ -1853,7 +1861,7 @@ public let DECL_NODES: [Node] = [
18531861
nameForDiagnostics: "precedencegroup",
18541862
documentation: "A Swift `precedencegroup` declaration.",
18551863
traits: [
1856-
"IdentifiedDecl",
1864+
"NamedDecl",
18571865
"WithAttributes",
18581866
"WithModifiers",
18591867
],
@@ -1877,7 +1885,8 @@ public let DECL_NODES: [Node] = [
18771885
kind: .token(choices: [.keyword(text: "precedencegroup")])
18781886
),
18791887
Child(
1880-
name: "Identifier",
1888+
name: "Name",
1889+
deprecatedName: "Identifier",
18811890
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
18821891
documentation: "The name of this precedence group."
18831892
),
@@ -1966,7 +1975,7 @@ public let DECL_NODES: [Node] = [
19661975
""",
19671976
traits: [
19681977
"DeclGroup",
1969-
"IdentifiedDecl",
1978+
"NamedDecl",
19701979
"WithAttributes",
19711980
"WithModifiers",
19721981
],
@@ -1991,7 +2000,8 @@ public let DECL_NODES: [Node] = [
19912000
documentation: "The `protocol` keyword for this declaration."
19922001
),
19932002
Child(
1994-
name: "Identifier",
2003+
name: "Name",
2004+
deprecatedName: "Identifier",
19952005
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
19962006
documentation: "The name of the protocol."
19972007
),
@@ -2133,7 +2143,7 @@ public let DECL_NODES: [Node] = [
21332143
""",
21342144
traits: [
21352145
"DeclGroup",
2136-
"IdentifiedDecl",
2146+
"NamedDecl",
21372147
"WithAttributes",
21382148
"WithGenericParameters",
21392149
"WithModifiers",
@@ -2159,7 +2169,8 @@ public let DECL_NODES: [Node] = [
21592169
documentation: "The `struct` keyword for this declaration."
21602170
),
21612171
Child(
2162-
name: "Identifier",
2172+
name: "Name",
2173+
deprecatedName: "Identifier",
21632174
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
21642175
documentation: "Declares the name of this struct. If the name matches a reserved keyword use backticks to escape it."
21652176
),
@@ -2305,7 +2316,7 @@ public let DECL_NODES: [Node] = [
23052316
base: .decl,
23062317
nameForDiagnostics: "typealias declaration",
23072318
traits: [
2308-
"IdentifiedDecl",
2319+
"NamedDecl",
23092320
"WithAttributes",
23102321
"WithGenericParameters",
23112322
"WithModifiers",
@@ -2328,7 +2339,8 @@ public let DECL_NODES: [Node] = [
23282339
kind: .token(choices: [.keyword(text: "typealias")])
23292340
),
23302341
Child(
2331-
name: "Identifier",
2342+
name: "Name",
2343+
deprecatedName: "Identifier",
23322344
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
23332345
),
23342346
Child(

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,8 @@ public let EXPR_NODES: [Node] = [
706706
nameForDiagnostics: "editor placeholder",
707707
children: [
708708
Child(
709-
name: "Identifier",
709+
name: "Placeholder",
710+
deprecatedName: "Identifier",
710711
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
711712
)
712713
]
@@ -1082,7 +1083,8 @@ public let EXPR_NODES: [Node] = [
10821083
nameForDiagnostics: "key path property component",
10831084
children: [
10841085
Child(
1085-
name: "Identifier",
1086+
name: "Property",
1087+
deprecatedName: "Identifier",
10861088
kind: .token(choices: [
10871089
.token(tokenKind: "IdentifierToken"),
10881090
.keyword(text: "self"),

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public let TRAITS: [Trait] = [
7373
]
7474
),
7575
Trait(
76-
traitName: "IdentifiedDecl",
76+
traitName: "NamedDecl",
7777
children: [
78-
Child(name: "Identifier", kind: .token(choices: [.token(tokenKind: "IdentifierToken")]))
78+
Child(name: "Name", kind: .token(choices: [.token(tokenKind: "IdentifierToken")]))
7979
]
8080
),
8181
Trait(

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,18 @@ class ValidateSyntaxNodes: XCTestCase {
506506
failures,
507507
expectedFailures: [
508508
ValidationFailure(node: .accessesEffect, message: "could conform to trait 'Parenthesized' but does not"),
509+
ValidationFailure(node: .accessorParameter, message: "could conform to trait 'NamedDecl' but does not"),
509510
ValidationFailure(node: .availabilityCondition, message: "could conform to trait 'Parenthesized' but does not"),
510511
ValidationFailure(node: .canImportExpr, message: "could conform to trait 'Parenthesized' but does not"),
511512
ValidationFailure(node: .differentiabilityParams, message: "could conform to trait 'Parenthesized' but does not"),
512513
ValidationFailure(node: .editorPlaceholderDecl, message: "could conform to trait 'MissingNode' but does not"),
513-
ValidationFailure(node: .editorPlaceholderExpr, message: "could conform to trait 'IdentifiedDecl' but does not"),
514-
ValidationFailure(node: .enumCaseElement, message: "could conform to trait 'IdentifiedDecl' but does not"),
514+
ValidationFailure(node: .editorPlaceholderExpr, message: "could conform to trait 'MissingNode' but does not"),
515+
ValidationFailure(node: .enumCaseElement, message: "could conform to trait 'NamedDecl' but does not"),
516+
ValidationFailure(node: .genericParameter, message: "could conform to trait 'NamedDecl' but does not"),
515517
ValidationFailure(node: .initializesEffect, message: "could conform to trait 'Parenthesized' but does not"),
516518
ValidationFailure(node: .precedenceGroupDecl, message: "could conform to trait 'Braced' but does not"),
519+
ValidationFailure(node: .precedenceGroupNameElement, message: "could conform to trait 'NamedDecl' but does not"),
520+
ValidationFailure(node: .primaryAssociatedType, message: "could conform to trait 'NamedDecl' but does not"),
517521
ValidationFailure(node: .yieldList, message: "could conform to trait 'Parenthesized' but does not"),
518522
]
519523
)
@@ -590,4 +594,31 @@ class ValidateSyntaxNodes: XCTestCase {
590594
]
591595
)
592596
}
597+
598+
/// Identifier is a wonderful ambiguous term. Almost always, 'name' or something similar is more expressive
599+
func testNoChildIsNamedIdentifier() {
600+
var failures: [ValidationFailure] = []
601+
602+
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
603+
for child in node.children {
604+
if child.name == "Identifier" {
605+
failures.append(
606+
ValidationFailure(
607+
node: node.kind,
608+
message: "children should generally not be named 'Identifier'"
609+
)
610+
)
611+
}
612+
}
613+
}
614+
615+
assertFailuresMatchXFails(
616+
failures,
617+
expectedFailures: [
618+
// The identifier expr / pattern nodes do actually have a child that’s the identifier
619+
ValidationFailure(node: .identifierExpr, message: "children should generally not be named 'Identifier'"),
620+
ValidationFailure(node: .identifierPattern, message: "children should generally not be named 'Identifier'"),
621+
]
622+
)
623+
}
593624
}

Sources/SwiftIDEUtils/SyntaxClassification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extension SyntaxClassification {
8585
return (.buildConfigId, false)
8686
case \MemberTypeIdentifierSyntax.name:
8787
return (.typeIdentifier, false)
88-
case \OperatorDeclSyntax.identifier:
88+
case \OperatorDeclSyntax.name:
8989
return (.operatorIdentifier, false)
9090
case \PrecedenceGroupAssociativitySyntax.associativityLabel:
9191
return (.keyword, false)

Sources/SwiftParser/generated/Parser+TokenSpecSet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ extension EnumCaseParameterSyntax {
894894
}
895895

896896
extension FunctionDeclSyntax {
897-
enum IdentifierOptions: TokenSpecSet {
897+
enum NameOptions: TokenSpecSet {
898898
case identifier
899899
case binaryOperator
900900
case prefixOperator
@@ -1308,7 +1308,7 @@ extension KeyPathOptionalComponentSyntax {
13081308
}
13091309

13101310
extension KeyPathPropertyComponentSyntax {
1311-
enum IdentifierOptions: TokenSpecSet {
1311+
enum PropertyOptions: TokenSpecSet {
13121312
case identifier
13131313
case `self`
13141314
case `Self`
@@ -1557,7 +1557,7 @@ extension OperatorDeclSyntax {
15571557
}
15581558

15591559
extension OperatorDeclSyntax {
1560-
enum IdentifierOptions: TokenSpecSet {
1560+
enum NameOptions: TokenSpecSet {
15611561
case binaryOperator
15621562
case prefixOperator
15631563
case postfixOperator

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
376376
- <doc:SwiftSyntax/DeclGroupSyntax>
377377
- <doc:SwiftSyntax/EffectSpecifiersSyntax>
378378
- <doc:SwiftSyntax/FreestandingMacroExpansionSyntax>
379-
- <doc:SwiftSyntax/IdentifiedDeclSyntax>
379+
- <doc:SwiftSyntax/NamedDeclSyntax>
380380
- <doc:SwiftSyntax/MissingNodeSyntax>
381381
- <doc:SwiftSyntax/ParenthesizedSyntax>
382382
- <doc:SwiftSyntax/WithAttributesSyntax>

Sources/SwiftSyntax/SwiftSyntaxCompatibility.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,46 @@ public extension FreestandingMacroExpansionSyntax {
5858
}
5959
}
6060

61+
@available(*, deprecated, renamed: "NamedDecl")
62+
public protocol IdentifiedDeclSyntax {
63+
var identifier: TokenSyntax { get set }
64+
}
65+
66+
@available(*, deprecated, renamed: "NamedDecl")
67+
public extension IdentifiedDeclSyntax where Self: NamedDeclSyntax {
68+
var identifier: TokenSyntax {
69+
get {
70+
return self.name
71+
}
72+
set {
73+
self.name = newValue
74+
}
75+
}
76+
}
77+
78+
@available(*, deprecated)
79+
extension ActorDeclSyntax: IdentifiedDeclSyntax {}
80+
@available(*, deprecated)
81+
extension AssociatedtypeDeclSyntax: IdentifiedDeclSyntax {}
82+
@available(*, deprecated)
83+
extension ClassDeclSyntax: IdentifiedDeclSyntax {}
84+
@available(*, deprecated)
85+
extension EnumDeclSyntax: IdentifiedDeclSyntax {}
86+
@available(*, deprecated)
87+
extension FunctionDeclSyntax: IdentifiedDeclSyntax {}
88+
@available(*, deprecated)
89+
extension MacroDeclSyntax: IdentifiedDeclSyntax {}
90+
@available(*, deprecated)
91+
extension OperatorDeclSyntax: IdentifiedDeclSyntax {}
92+
@available(*, deprecated)
93+
extension PrecedenceGroupDeclSyntax: IdentifiedDeclSyntax {}
94+
@available(*, deprecated)
95+
extension ProtocolDeclSyntax: IdentifiedDeclSyntax {}
96+
@available(*, deprecated)
97+
extension StructDeclSyntax: IdentifiedDeclSyntax {}
98+
@available(*, deprecated)
99+
extension TypealiasDeclSyntax: IdentifiedDeclSyntax {}
100+
61101
public extension SyntaxProtocol {
62102
@available(*, deprecated, message: "Use detached computed property instead.")
63103
func detach() -> Self {

0 commit comments

Comments
 (0)