Skip to content

Commit 3c7b226

Browse files
committed
Remove the typealiases in SwiftSyntaxBuilder that refer to syntax nodes without the Syntax suffix
In the past, before SwiftSyntaxBuilder shared the same syntax nodes with SwiftSyntax, it made sense for it to have its own syntax nodes and thus drop the `Syntax` suffix. But since SwiftSyntaxBuilder is now just providing convenience initializers, it’s confusing to have two names to refer to the same type - the style was already pretty inconsitent in this code base. To make things consistent, drop the typealiases.
1 parent 8b21dbf commit 3c7b226

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1013
-1577
lines changed

CodeGeneration/Sources/Utils/SyntaxBuildableType.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public struct SyntaxBuildableType: Hashable {
5858
return Expr(NilLiteralExpr())
5959
} else if isToken {
6060
if let token = token, token.text != nil {
61-
return Expr(MemberAccessExpr(base: "Token", name: lowercaseFirstWord(name: token.name).backticked))
61+
return Expr(MemberAccessExpr(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name).backticked))
6262
} else if tokenKind == "EOFToken" {
63-
return Expr(MemberAccessExpr(base: "Token", name: "eof"))
63+
return Expr(MemberAccessExpr(base: "TokenSyntax", name: "eof"))
6464
}
6565
}
6666
return nil
@@ -87,7 +87,7 @@ public struct SyntaxBuildableType: Hashable {
8787
/// - For token: `TokenSyntax` (tokens don't have a dedicated type in SwiftSyntaxBuilder)
8888
/// If the type is optional, the type is wrapped in an `OptionalType`.
8989
public var buildable: Type {
90-
optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(shorthandName)))
90+
optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(syntaxBaseName)))
9191
}
9292

9393
/// Whether parameters of this type should be initializable by a result builder.
@@ -101,17 +101,11 @@ public struct SyntaxBuildableType: Hashable {
101101
/// returns `CodeBlockItemList` for `CodeBlock`) and otherwise itself.
102102
public var builderInitializableType: Self {
103103
Self(
104-
syntaxKind: BUILDER_INITIALIZABLE_TYPES[shorthandName].flatMap { $0 } ?? shorthandName,
104+
syntaxKind: BUILDER_INITIALIZABLE_TYPES[syntaxKind].flatMap { $0 } ?? syntaxKind,
105105
isOptional: isOptional
106106
)
107107
}
108108

109-
/// The type name without the `Syntax`. `SwiftSyntaxBuilder` declares typealiases
110-
/// that map these to the corresponding `*Syntax` nodes.
111-
public var shorthandName: String {
112-
return syntaxKind
113-
}
114-
115109
/// The corresponding `*Syntax` type defined in the `SwiftSyntax` module,
116110
/// without any question marks attached.
117111
public var syntaxBaseName: String {
@@ -135,7 +129,7 @@ public struct SyntaxBuildableType: Hashable {
135129
if isBaseType {
136130
return "\(syntaxBaseName)Protocol"
137131
} else {
138-
return shorthandName
132+
return syntaxBaseName
139133
}
140134
}
141135

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ struct GenerateSwiftSyntax: ParsableCommand {
7070
TemplateSpec(sourceFile: resultBuildersFile, module: swiftSyntaxBuilderDir, filename: "ResultBuilders.swift"),
7171
TemplateSpec(sourceFile: syntaxExpressibleByStringInterpolationConformancesFile, module: swiftSyntaxBuilderDir, filename: "SyntaxExpressibleByStringInterpolationConformances.swift"),
7272
TemplateSpec(sourceFile: tokenFile, module: swiftSyntaxBuilderDir, filename: "Token.swift"),
73-
TemplateSpec(sourceFile: typealiasesFile, module: swiftSyntaxBuilderDir, filename: "Typealiases.swift"),
7473
]
7574

7675
var errors: [Error] = []

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableCollectionNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let buildableCollectionNodesFile = SourceFile {
2727

2828
let docComment = node.documentation.isEmpty ? "" : "/// \(node.documentation)\n"
2929
// Generate collection node struct
30-
ExtensionDecl("\(docComment)extension \(node.type.shorthandName): ExpressibleByArrayLiteral") {
30+
ExtensionDecl("\(docComment)extension \(node.type.syntaxBaseName): ExpressibleByArrayLiteral") {
3131
// Generate initializers
3232
if elementType.isBaseType && node.collectionElementChoices?.isEmpty ?? true {
3333
InitializerDecl(

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let buildableNodesFile = SourceFile {
3030
let docComment: SwiftSyntax.Trivia = node.documentation.isEmpty ? [] : .docLineComment("/// \(node.documentation)") + .newline
3131
ExtensionDecl(
3232
leadingTrivia: docComment,
33-
extendedType: SimpleTypeIdentifier(name: .identifier(type.shorthandName)),
33+
extendedType: SimpleTypeIdentifier(name: .identifier(type.syntaxBaseName)),
3434
inheritanceClause: hasTrailingComma ? TypeInheritanceClause { InheritedType(typeName: Type("HasTrailingComma")) } : nil
3535
) {
3636
if let convenienceInit = convenienceInit {
@@ -113,7 +113,7 @@ private func createConvenienceInitializer(node: Node) -> InitializerDecl? {
113113
// Allow initializing identifiers and other tokens without default text with a String
114114
shouldCreateInitializer = true
115115
let paramType = child.type.optionalWrapped(type: Type("String"))
116-
let tokenExpr = MemberAccessExpr("Token.\(raw: token.swiftKind.withFirstCharacterLowercased.backticked)")
116+
let tokenExpr = MemberAccessExpr("TokenSyntax.\(raw: token.swiftKind.withFirstCharacterLowercased.backticked)")
117117
if child.type.isOptional {
118118
produceExpr = Expr(FunctionCallExpr("\(raw: child.swiftName).map { \(tokenExpr)($0) }"))
119119
} else {

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let resultBuildersFile = SourceFile {
7878
"""
7979
/// If declared, provides contextual type information for statement
8080
/// expressions to translate them into partial results.
81-
public static func buildExpression(_ expression: \(raw: elementChoice)) -> Self.Component {
81+
public static func buildExpression(_ expression: \(raw: elementChoice)Syntax) -> Self.Component {
8282
return buildExpression(.init(expression))
8383
}
8484
"""
@@ -168,8 +168,8 @@ let resultBuildersFile = SourceFile {
168168

169169
ExtensionDecl(
170170
"""
171-
public extension \(raw: type.shorthandName) {
172-
init(@\(raw: type.resultBuilderBaseName) itemsBuilder: () -> \(raw: type.shorthandName)) {
171+
public extension \(raw: type.syntaxBaseName) {
172+
init(@\(raw: type.resultBuilderBaseName) itemsBuilder: () -> \(raw: type.syntaxBaseName)) {
173173
self = itemsBuilder()
174174
}
175175
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/TokenFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let tokenFile = SourceFile {
2626
if token.isKeyword {
2727
VariableDecl("""
2828
/// The `\(raw: token.text!)` keyword
29-
static var \(raw: token.name.withFirstCharacterLowercased.backticked): Token {
29+
static var \(raw: token.name.withFirstCharacterLowercased.backticked): TokenSyntax {
3030
return .\(raw: token.swiftKind)()
3131
}
3232
"""

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/TypealiasesFile.swift

Lines changed: 0 additions & 30 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ add_swift_host_library(SwiftSyntaxBuilder
1919
generated/BuildableNodes.swift
2020
generated/ResultBuilders.swift
2121
generated/Token.swift
22-
generated/Typealiases.swift
2322
generated/SyntaxExpressibleByStringInterpolationConformances.swift
2423
)
2524

0 commit comments

Comments
 (0)