|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SyntaxSupport |
| 14 | + |
| 15 | +/// All nodes with base kind e.g. `ExprSyntax` should end with `ExprSyntax`. |
| 16 | +func validateBaseKindSuffix() -> [ValidationFailure] { |
| 17 | + var failures: [ValidationFailure] = [] |
| 18 | + for node in SYNTAX_NODES where node.base != .syntaxCollection { |
| 19 | + if !node.kind.syntaxType.description.hasSuffix(node.base.syntaxType.description) { |
| 20 | + failures.append( |
| 21 | + ValidationFailure(node: node.kind, message: "has base kind '\(node.base.syntaxType)' but type name doesn’t have '\(node.base.syntaxType)' suffix") |
| 22 | + ) |
| 23 | + } |
| 24 | + } |
| 25 | + return failures |
| 26 | +} |
| 27 | + |
| 28 | +/// Checks standardized naming of children with a single token choice |
| 29 | +/// |
| 30 | +/// - If a child has a single keyword as its choice, it should be named `*Keyword` (e.g. `ImportKeyword`) |
| 31 | +/// - If it’s another token kind, name it the same as the the token choice (e.g. `LeftParen`) |
| 32 | +func validateSingleTokenChoiceChildNaming() -> [ValidationFailure] { |
| 33 | + var failures: [ValidationFailure] = [] |
| 34 | + for node in SYNTAX_NODES.compactMap(\.layoutNode) { |
| 35 | + for child in node.children { |
| 36 | + if case .token(choices: let tokenChoices, _, _) = child.kind, |
| 37 | + let choice = tokenChoices.only |
| 38 | + { |
| 39 | + switch choice { |
| 40 | + case .keyword(text: let keyword): |
| 41 | + if child.name != "\(keyword.withFirstCharacterUppercased)Keyword" { |
| 42 | + failures.append( |
| 43 | + ValidationFailure( |
| 44 | + node: node.kind, |
| 45 | + message: |
| 46 | + "child '\(child.name)' has a single keyword as its only token choice and should thus be named '\(keyword.withFirstCharacterUppercased)Keyword'" |
| 47 | + ) |
| 48 | + ) |
| 49 | + } |
| 50 | + case .token(tokenKind: let tokenKind): |
| 51 | + switch tokenKind { |
| 52 | + case "IdentifierToken": |
| 53 | + // We allow arbitrary naming of identifiers |
| 54 | + break |
| 55 | + case "CommaToken": |
| 56 | + if child.name != "TrailingComma" && child.name != "Comma" { |
| 57 | + failures.append( |
| 58 | + ValidationFailure( |
| 59 | + node: node.kind, |
| 60 | + message: "child '\(child.name)' has a comma keyword as its only token choice and should thus be named 'Comma' or 'TrailingComma'" |
| 61 | + ) |
| 62 | + ) |
| 63 | + } |
| 64 | + default: |
| 65 | + if child.name != tokenKind.dropSuffix("Token") { |
| 66 | + failures.append( |
| 67 | + ValidationFailure( |
| 68 | + node: node.kind, |
| 69 | + message: "child '\(child.name)' has a token as its only token choice and should thus be named '\(tokenKind.dropSuffix("Token"))'" |
| 70 | + ) |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return failures |
| 79 | +} |
| 80 | + |
| 81 | +/// If a token only has keyword token choices, its name should end with `Keyword`. |
| 82 | +func validateMultipleKeywordChoicesNaming() -> [ValidationFailure] { |
| 83 | + var failures: [ValidationFailure] = [] |
| 84 | + for node in SYNTAX_NODES.compactMap(\.layoutNode) { |
| 85 | + for child in node.children { |
| 86 | + if case .token(choices: let tokenChoices, _, _) = child.kind, |
| 87 | + tokenChoices.count > 1, // single token choices are handled by `validateSingleTokenChoiceChildNaming` |
| 88 | + tokenChoices.allSatisfy({ $0.isKeyword }), |
| 89 | + !child.name.hasSuffix("Keyword") |
| 90 | + { |
| 91 | + failures.append( |
| 92 | + ValidationFailure( |
| 93 | + node: node.kind, |
| 94 | + message: "child '\(child.name)' only has keywords as its token choices and should thus and with 'Keyword'" |
| 95 | + ) |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return failures |
| 101 | +} |
| 102 | + |
| 103 | +/// Check that children that have the same type also have the same child name. |
| 104 | +func validateConsistentNamingOfChildren() -> [ValidationFailure] { |
| 105 | + var failures: [ValidationFailure] = [] |
| 106 | + |
| 107 | + var childrenByNodeKind: [SyntaxNodeKind: [(node: LayoutNode, child: Child)]] = [:] |
| 108 | + |
| 109 | + for node in SYNTAX_NODES.compactMap(\.layoutNode) { |
| 110 | + for child in node.children { |
| 111 | + guard case .node(kind: let childKind) = child.kind else { |
| 112 | + continue |
| 113 | + } |
| 114 | + childrenByNodeKind[childKind, default: []].append((node, child)) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + for (kind, children) in childrenByNodeKind where !kind.isBase && kind != .token { |
| 119 | + let childNames = children.map(\.child.name) |
| 120 | + let firstChildName = childNames.first! |
| 121 | + |
| 122 | + for (node, child) in children.dropFirst() { |
| 123 | + if child.name != firstChildName { |
| 124 | + failures.append( |
| 125 | + ValidationFailure( |
| 126 | + node: node.kind, |
| 127 | + message: |
| 128 | + "child '\(child.name)' is named inconsistently with '\(children.first!.node.kind.syntaxType).\(children.first!.child.name)', which has the same type ('\(kind.syntaxType)')" |
| 129 | + ) |
| 130 | + ) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return failures |
| 136 | +} |
0 commit comments