Skip to content

Commit e85288d

Browse files
committed
Validate that children don’t end with 'Expr', 'Expression', 'Type' etc
1 parent 1aa235d commit e85288d

File tree

17 files changed

+1371
-554
lines changed

17 files changed

+1371
-554
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2047,7 +2047,8 @@ public let DECL_NODES: [Node] = [
20472047
kind: .token(choices: [.token(tokenKind: "ArrowToken")])
20482048
),
20492049
Child(
2050-
name: "ReturnType",
2050+
name: "Type",
2051+
deprecatedName: "ReturnType",
20512052
kind: .node(kind: .type),
20522053
nameForDiagnostics: "return type"
20532054
),

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ public let EXPR_NODES: [Node] = [
635635
],
636636
children: [
637637
Child(
638-
name: "KeyExpression",
638+
name: "Key",
639+
deprecatedName: "KeyExpression",
639640
kind: .node(kind: .expr),
640641
nameForDiagnostics: "key"
641642
),
@@ -644,7 +645,8 @@ public let EXPR_NODES: [Node] = [
644645
kind: .token(choices: [.token(tokenKind: "ColonToken")])
645646
),
646647
Child(
647-
name: "ValueExpression",
648+
name: "Value",
649+
deprecatedName: "ValueExpression",
648650
kind: .node(kind: .expr),
649651
nameForDiagnostics: "value"
650652
),
@@ -1338,7 +1340,8 @@ public let EXPR_NODES: [Node] = [
13381340
kind: .token(choices: [.keyword(text: "repeat")])
13391341
),
13401342
Child(
1341-
name: "PatternExpr",
1343+
name: "Pack",
1344+
deprecatedName: "PatternExpr",
13421345
kind: .node(kind: .expr)
13431346
),
13441347
]
@@ -1395,7 +1398,8 @@ public let EXPR_NODES: [Node] = [
13951398
isOptional: true
13961399
),
13971400
Child(
1398-
name: "PostfixExpression",
1401+
name: "BaseExpression",
1402+
deprecatedName: "PostfixExpression",
13991403
kind: .node(kind: .expr)
14001404
),
14011405
]
@@ -1418,7 +1422,8 @@ public let EXPR_NODES: [Node] = [
14181422
kind: .token(choices: [.token(tokenKind: "RegexSlashToken")])
14191423
),
14201424
Child(
1421-
name: "RegexPattern",
1425+
name: "Regex",
1426+
deprecatedName: "RegexPattern",
14221427
kind: .token(choices: [.token(tokenKind: "RegexLiteralPatternToken")])
14231428
),
14241429
Child(
@@ -1696,7 +1701,8 @@ public let EXPR_NODES: [Node] = [
16961701
nameForDiagnostics: "ternay expression",
16971702
children: [
16981703
Child(
1699-
name: "ConditionExpression",
1704+
name: "Condition",
1705+
deprecatedName: "ConditionExpression",
17001706
kind: .node(kind: .expr),
17011707
nameForDiagnostics: "condition"
17021708
),

CodeGeneration/Sources/SyntaxSupport/PatternNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ public let PATTERN_NODES: [Node] = [
151151
kind: .token(choices: [.keyword(text: "let"), .keyword(text: "var"), .keyword(text: "inout")])
152152
),
153153
Child(
154-
name: "ValuePattern",
154+
name: "Pattern",
155+
deprecatedName: "ValuePattern",
155156
kind: .node(kind: .pattern)
156157
),
157158
]

CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ public let STMT_NODES: [Node] = [
316316
kind: .token(choices: [.keyword(text: "in")])
317317
),
318318
Child(
319-
name: "SequenceExpr",
319+
name: "Sequence",
320+
deprecatedName: "SequenceExpr",
320321
kind: .node(kind: .expr)
321322
),
322323
Child(

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public let TYPE_NODES: [Node] = [
2323
kind: .token(choices: [.token(tokenKind: "LeftSquareToken")])
2424
),
2525
Child(
26-
name: "ElementType",
26+
name: "Element",
27+
deprecatedName: "ElementType",
2728
kind: .node(kind: .type)
2829
),
2930
Child(
@@ -150,7 +151,8 @@ public let TYPE_NODES: [Node] = [
150151
kind: .token(choices: [.token(tokenKind: "LeftSquareToken")])
151152
),
152153
Child(
153-
name: "KeyType",
154+
name: "Key",
155+
deprecatedName: "KeyType",
154156
kind: .node(kind: .type),
155157
nameForDiagnostics: "key type"
156158
),
@@ -159,7 +161,8 @@ public let TYPE_NODES: [Node] = [
159161
kind: .token(choices: [.token(tokenKind: "ColonToken")])
160162
),
161163
Child(
162-
name: "ValueType",
164+
name: "Value",
165+
deprecatedName: "ValueType",
163166
kind: .node(kind: .type),
164167
nameForDiagnostics: "value type"
165168
),
@@ -251,7 +254,8 @@ public let TYPE_NODES: [Node] = [
251254
],
252255
children: [
253256
Child(
254-
name: "ArgumentType",
257+
name: "Argument",
258+
deprecatedName: "ArgumentType",
255259
kind: .node(kind: .type)
256260
),
257261
Child(
@@ -377,7 +381,8 @@ public let TYPE_NODES: [Node] = [
377381
kind: .token(choices: [.token(tokenKind: "PrefixOperatorToken")])
378382
),
379383
Child(
380-
name: "PatternType",
384+
name: "Type",
385+
deprecatedName: "PatternType",
381386
kind: .node(kind: .type)
382387
),
383388
]
@@ -394,7 +399,8 @@ public let TYPE_NODES: [Node] = [
394399
kind: .token(choices: [.keyword(text: "repeat")])
395400
),
396401
Child(
397-
name: "PatternType",
402+
name: "Pack",
403+
deprecatedName: "PatternType",
398404
kind: .node(kind: .type)
399405
),
400406
]
@@ -411,7 +417,8 @@ public let TYPE_NODES: [Node] = [
411417
kind: .token(choices: [.keyword(text: "each")])
412418
),
413419
Child(
414-
name: "PackType",
420+
name: "Pack",
421+
deprecatedName: "PackType",
415422
kind: .node(kind: .type)
416423
),
417424
]

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class ValidateSyntaxNodes: XCTestCase {
289289
// If the node is named the same as the token, we don't need to repeat the entire token name
290290
ValidationFailure(
291291
node: .regexLiteralExpr,
292-
message: "child 'RegexPattern' has a token as its only token choice and should thus be named 'RegexLiteralPattern'"
292+
message: "child 'Regex' has a token as its only token choice and should thus be named 'RegexLiteralPattern'"
293293
// No point repeating the `Literal` because the node name alredy contains it
294294
),
295295
ValidationFailure(
@@ -643,4 +643,51 @@ class ValidateSyntaxNodes: XCTestCase {
643643
expectedFailures: []
644644
)
645645
}
646+
647+
func testChildrenDontEndWithExprEtc() {
648+
var failures: [ValidationFailure] = []
649+
650+
let forbiddenSuffixes = ["Decl", "Declaration", "Expr", "Expression", "Pattern", "Stmt", "Statement", "Syntax", "Type"]
651+
652+
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
653+
for child in node.nonUnexpectedChildren {
654+
for forbiddenSuffix in forbiddenSuffixes {
655+
if child.name.hasSuffix(forbiddenSuffix) && child.name != forbiddenSuffix {
656+
failures.append(
657+
ValidationFailure(
658+
node: node.kind,
659+
message: "child '\(child.name)' should not end with '\(forbiddenSuffix)'"
660+
)
661+
)
662+
}
663+
}
664+
}
665+
}
666+
667+
assertFailuresMatchXFails(
668+
failures,
669+
expectedFailures: [
670+
// MARK: Adjective + Type
671+
// There’s no real better way to name these except to use an adjective followed by 'Type'
672+
ValidationFailure(node: .attributedType, message: "child 'BaseType' should not end with 'Type'"),
673+
ValidationFailure(node: .conformanceRequirement, message: "child 'LeftType' should not end with 'Type'"),
674+
ValidationFailure(node: .conformanceRequirement, message: "child 'RightType' should not end with 'Type'"),
675+
ValidationFailure(node: .constrainedSugarType, message: "child 'BaseType' should not end with 'Type'"),
676+
ValidationFailure(node: .extensionDecl, message: "child 'ExtendedType' should not end with 'Type'"),
677+
ValidationFailure(node: .genericParameter, message: "child 'InheritedType' should not end with 'Type'"),
678+
ValidationFailure(node: .implicitlyUnwrappedOptionalType, message: "child 'WrappedType' should not end with 'Type'"),
679+
ValidationFailure(node: .memberTypeIdentifier, message: "child 'BaseType' should not end with 'Type'"),
680+
ValidationFailure(node: .metatypeType, message: "child 'BaseType' should not end with 'Type'"),
681+
ValidationFailure(node: .namedOpaqueReturnType, message: "child 'BaseType' should not end with 'Type'"),
682+
ValidationFailure(node: .optionalType, message: "child 'WrappedType' should not end with 'Type'"),
683+
ValidationFailure(node: .qualifiedDeclName, message: "child 'BaseType' should not end with 'Type'"),
684+
ValidationFailure(node: .sameTypeRequirement, message: "child 'LeftType' should not end with 'Type'"),
685+
ValidationFailure(node: .sameTypeRequirement, message: "child 'RightType' should not end with 'Type'"),
686+
// MARK: Adjective + Expr
687+
ValidationFailure(node: .functionCallExpr, message: "child 'CalledExpression' should not end with 'Expression'"),
688+
ValidationFailure(node: .prefixOperatorExpr, message: "child 'BaseExpression' should not end with 'Expression'"),
689+
ValidationFailure(node: .subscriptExpr, message: "child 'CalledExpression' should not end with 'Expression'"),
690+
]
691+
)
692+
}
646693
}

Sources/SwiftParserDiagnostics/generated/ChildNameForDiagnostics.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
9494
return "attributes"
9595
case \DeinitializerDeclSyntax.modifiers:
9696
return "modifiers"
97-
case \DictionaryElementSyntax.keyExpression:
97+
case \DictionaryElementSyntax.key:
9898
return "key"
99-
case \DictionaryElementSyntax.valueExpression:
99+
case \DictionaryElementSyntax.value:
100100
return "value"
101-
case \DictionaryTypeSyntax.keyType:
101+
case \DictionaryTypeSyntax.key:
102102
return "key type"
103-
case \DictionaryTypeSyntax.valueType:
103+
case \DictionaryTypeSyntax.value:
104104
return "value type"
105105
case \DifferentiabilityParamsClauseSyntax.parameters:
106106
return "parameters"
@@ -300,7 +300,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
300300
return "body"
301301
case \RepeatWhileStmtSyntax.condition:
302302
return "condition"
303-
case \ReturnClauseSyntax.returnType:
303+
case \ReturnClauseSyntax.type:
304304
return "return type"
305305
case \SameTypeRequirementSyntax.leftType:
306306
return "left-hand type"
@@ -338,7 +338,7 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
338338
return "label"
339339
case \TargetFunctionEntrySyntax.declname:
340340
return "declaration name"
341-
case \TernaryExprSyntax.conditionExpression:
341+
case \TernaryExprSyntax.condition:
342342
return "condition"
343343
case \TernaryExprSyntax.firstChoice:
344344
return "first choice"

0 commit comments

Comments
 (0)