Skip to content

Commit 9c270a7

Browse files
committed
Replace string-based keyword references with Keyword enum in CodeGeneration
The CodeGeneration module was previously referring to keywords using string-based references like switch, which is not idiomatic Swift. To make the code more concise and in line with Swift's best practices, this commit introduces a Keyword enum in the KeywordSpec.swift file. Now, instead of string-based references, the new Keyword enum contains all the possible keyword kinds. It also includes a computed property that returns the corresponding KeywordSpec for a given keyword, allowing for more concise and idiomatic references. The code has been refactored to replace all string-based keyword references with members of the newly introduced Keyword enum. Associated tests have also been updated to reflect these changes.
1 parent 2e3c42c commit 9c270a7

21 files changed

+870
-467
lines changed

CodeGeneration/Sources/SyntaxSupport/AttributeNodes.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public let ATTRIBUTE_NODES: [Node] = [
179179
Child(
180180
name: "availabilityLabel",
181181
deprecatedName: "label",
182-
kind: .token(choices: [.keyword(text: "availability")]),
182+
kind: .token(choices: [.keyword(.availability)]),
183183
nameForDiagnostics: "label",
184184
documentation: "The label of the argument"
185185
),
@@ -241,7 +241,7 @@ public let ATTRIBUTE_NODES: [Node] = [
241241
children: [
242242
Child(
243243
name: "beforeLabel",
244-
kind: .token(choices: [.keyword(text: "before")]),
244+
kind: .token(choices: [.keyword(.before)]),
245245
documentation: "The \"before\" label."
246246
),
247247
Child(
@@ -277,7 +277,7 @@ public let ATTRIBUTE_NODES: [Node] = [
277277
),
278278
Child(
279279
name: "cTypeLabel",
280-
kind: .token(choices: [.keyword(text: "cType")]),
280+
kind: .token(choices: [.keyword(.cType)]),
281281
isOptional: true
282282
),
283283
Child(
@@ -302,7 +302,7 @@ public let ATTRIBUTE_NODES: [Node] = [
302302
children: [
303303
Child(
304304
name: "witnessMethodLabel",
305-
kind: .token(choices: [.keyword(text: "witness_method")])
305+
kind: .token(choices: [.keyword(.witness_method)])
306306
),
307307
Child(
308308
name: "colon",
@@ -330,7 +330,7 @@ public let ATTRIBUTE_NODES: [Node] = [
330330
children: [
331331
Child(
332332
name: "ofLabel",
333-
kind: .token(choices: [.keyword(text: "of")]),
333+
kind: .token(choices: [.keyword(.of)]),
334334
documentation: "The \"of\" label."
335335
),
336336
Child(
@@ -352,7 +352,7 @@ public let ATTRIBUTE_NODES: [Node] = [
352352
Child(
353353
name: "accessorSpecifier",
354354
deprecatedName: "accessorKind",
355-
kind: .token(choices: [.keyword(text: "get"), .keyword(text: "set")]),
355+
kind: .token(choices: [.keyword(.get), .keyword(.set)]),
356356
documentation: "The accessor name.",
357357
isOptional: true
358358
),
@@ -392,7 +392,7 @@ public let ATTRIBUTE_NODES: [Node] = [
392392
Child(
393393
name: "argument",
394394
deprecatedName: "parameter",
395-
kind: .token(choices: [.token(.identifier), .token(.integerLiteral), .keyword(text: "self")])
395+
kind: .token(choices: [.token(.identifier), .token(.integerLiteral), .keyword(.self)])
396396
),
397397
Child(
398398
name: "trailingComma",
@@ -412,7 +412,7 @@ public let ATTRIBUTE_NODES: [Node] = [
412412
children: [
413413
Child(
414414
name: "wrtLabel",
415-
kind: .token(choices: [.keyword(text: "wrt")]),
415+
kind: .token(choices: [.keyword(.wrt)]),
416416
documentation: "The \"wrt\" label."
417417
),
418418
Child(
@@ -478,7 +478,7 @@ public let ATTRIBUTE_NODES: [Node] = [
478478
Child(
479479
name: "kindSpecifier",
480480
deprecatedName: "diffKind",
481-
kind: .token(choices: [.keyword(text: "_forward"), .keyword(text: "reverse"), .keyword(text: "_linear")]),
481+
kind: .token(choices: [.keyword(._forward), .keyword(.reverse), .keyword(._linear)]),
482482
isOptional: true
483483
),
484484
Child(
@@ -521,7 +521,7 @@ public let ATTRIBUTE_NODES: [Node] = [
521521
children: [
522522
Child(
523523
name: "label",
524-
kind: .token(choices: [.keyword(text: "visibility"), .keyword(text: "metadata")]),
524+
kind: .token(choices: [.keyword(.visibility), .keyword(.metadata)]),
525525
nameForDiagnostics: "label"
526526
),
527527
Child(
@@ -535,11 +535,11 @@ public let ATTRIBUTE_NODES: [Node] = [
535535
name: "token",
536536
kind: .token(choices: [
537537
.token(.identifier),
538-
.keyword(text: "private"),
539-
.keyword(text: "fileprivate"),
540-
.keyword(text: "internal"),
541-
.keyword(text: "public"),
542-
.keyword(text: "open"),
538+
.keyword(.private),
539+
.keyword(.fileprivate),
540+
.keyword(.internal),
541+
.keyword(.public),
542+
.keyword(.open),
543543
])
544544
), // Keywords can be: public, internal, private, fileprivate, open
545545
Child(
@@ -573,7 +573,7 @@ public let ATTRIBUTE_NODES: [Node] = [
573573
children: [
574574
Child(
575575
name: "forLabel",
576-
kind: .token(choices: [.keyword(text: "for")], requiresTrailingSpace: false)
576+
kind: .token(choices: [.keyword(.for)], requiresTrailingSpace: false)
577577
),
578578
Child(
579579
name: "colon",
@@ -662,13 +662,13 @@ public let ATTRIBUTE_NODES: [Node] = [
662662
Child(
663663
name: "label",
664664
kind: .token(choices: [
665-
.keyword(text: "target"),
666-
.keyword(text: "availability"),
667-
.keyword(text: "exported"),
668-
.keyword(text: "kind"),
669-
.keyword(text: "spi"),
670-
.keyword(text: "spiModule"),
671-
.keyword(text: "available"),
665+
.keyword(.target),
666+
.keyword(.availability),
667+
.keyword(.exported),
668+
.keyword(.kind),
669+
.keyword(.spi),
670+
.keyword(.spiModule),
671+
.keyword(.available),
672672
]),
673673
nameForDiagnostics: "label",
674674
documentation: "The label of the argument"
@@ -755,7 +755,7 @@ public let ATTRIBUTE_NODES: [Node] = [
755755
children: [
756756
Child(
757757
name: "moduleLabel",
758-
kind: .token(choices: [.keyword(text: "module")])
758+
kind: .token(choices: [.keyword(.module)])
759759
),
760760
Child(
761761
name: "colon",
@@ -803,7 +803,7 @@ public let ATTRIBUTE_NODES: [Node] = [
803803
Child(
804804
name: "targetLabel",
805805
deprecatedName: "label",
806-
kind: .token(choices: [.keyword(text: "target")]),
806+
kind: .token(choices: [.keyword(.target)]),
807807
nameForDiagnostics: "label",
808808
documentation: "The label of the argument"
809809
),
@@ -836,7 +836,7 @@ public let ATTRIBUTE_NODES: [Node] = [
836836
children: [
837837
Child(
838838
name: "messageLabel",
839-
kind: .token(choices: [.keyword(text: "message")])
839+
kind: .token(choices: [.keyword(.message)])
840840
),
841841
Child(
842842
name: "colon",
@@ -857,7 +857,7 @@ public let ATTRIBUTE_NODES: [Node] = [
857857
children: [
858858
Child(
859859
name: "sourceFileLabel",
860-
kind: .token(choices: [.keyword(text: "sourceFile")])
860+
kind: .token(choices: [.keyword(.sourceFile)])
861861
),
862862
Child(
863863
name: "colon",

CodeGeneration/Sources/SyntaxSupport/AvailabilityNodes.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public let AVAILABILITY_NODES: [Node] = [
6666
Child(
6767
name: "label",
6868
kind: .token(choices: [
69-
.keyword(text: "message"),
70-
.keyword(text: "renamed"),
71-
.keyword(text: "introduced"),
72-
.keyword(text: "obsoleted"),
73-
.keyword(text: "deprecated"),
69+
.keyword(.message),
70+
.keyword(.renamed),
71+
.keyword(.introduced),
72+
.keyword(.obsoleted),
73+
.keyword(.deprecated),
7474
]),
7575
nameForDiagnostics: "label",
7676
documentation: "The label of the argument"

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SwiftSyntax
1515
/// The kind of token a node can contain. Either a token of a specific kind or a
1616
/// keyword with the given text.
1717
public enum TokenChoice: Equatable {
18-
case keyword(text: String)
18+
case keyword(Keyword)
1919
case token(Token)
2020

2121
public var isKeyword: Bool {

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public let COMMON_NODES: [Node] = [
9191
children: [
9292
Child(
9393
name: "asyncSpecifier",
94-
kind: .token(choices: [.keyword(text: "async")]),
94+
kind: .token(choices: [.keyword(.async)]),
9595
isOptional: true
9696
),
9797
Child(
9898
name: "throwsSpecifier",
99-
kind: .token(choices: [.keyword(text: "throws")]),
99+
kind: .token(choices: [.keyword(.throws)]),
100100
isOptional: true
101101
),
102102
]
@@ -113,12 +113,12 @@ public let COMMON_NODES: [Node] = [
113113
children: [
114114
Child(
115115
name: "asyncSpecifier",
116-
kind: .token(choices: [.keyword(text: "async"), .keyword(text: "reasync")]),
116+
kind: .token(choices: [.keyword(.async), .keyword(.reasync)]),
117117
isOptional: true
118118
),
119119
Child(
120120
name: "throwsSpecifier",
121-
kind: .token(choices: [.keyword(text: "throws"), .keyword(text: "rethrows")]),
121+
kind: .token(choices: [.keyword(.throws), .keyword(.rethrows)]),
122122
isOptional: true
123123
),
124124
]
@@ -133,7 +133,7 @@ public let COMMON_NODES: [Node] = [
133133
children: [
134134
Child(
135135
name: "asyncSpecifier",
136-
kind: .token(choices: [.keyword(text: "async")]),
136+
kind: .token(choices: [.keyword(.async)]),
137137
isOptional: true
138138
)
139139
]
@@ -316,12 +316,12 @@ public let COMMON_NODES: [Node] = [
316316
children: [
317317
Child(
318318
name: "asyncSpecifier",
319-
kind: .token(choices: [.keyword(text: "async")]),
319+
kind: .token(choices: [.keyword(.async)]),
320320
isOptional: true
321321
),
322322
Child(
323323
name: "throwsSpecifier",
324-
kind: .token(choices: [.keyword(text: "throws")]),
324+
kind: .token(choices: [.keyword(.throws)]),
325325
isOptional: true
326326
),
327327
]

0 commit comments

Comments
 (0)