Skip to content

Remove the typealiases in SwiftSyntaxBuilder that refer to syntax nodes without the Syntax suffix #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions CodeGeneration/Sources/Utils/SyntaxBuildableType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public struct SyntaxBuildableType: Hashable {
return Expr(NilLiteralExpr())
} else if isToken {
if let token = token, token.text != nil {
return Expr(MemberAccessExpr(base: "Token", name: lowercaseFirstWord(name: token.name).backticked))
return Expr(MemberAccessExpr(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name).backticked))
} else if tokenKind == "EOFToken" {
return Expr(MemberAccessExpr(base: "Token", name: "eof"))
return Expr(MemberAccessExpr(base: "TokenSyntax", name: "eof"))
}
}
return nil
Expand All @@ -87,7 +87,7 @@ public struct SyntaxBuildableType: Hashable {
/// - For token: `TokenSyntax` (tokens don't have a dedicated type in SwiftSyntaxBuilder)
/// If the type is optional, the type is wrapped in an `OptionalType`.
public var buildable: Type {
optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(shorthandName)))
optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(syntaxBaseName)))
}

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

/// The type name without the `Syntax`. `SwiftSyntaxBuilder` declares typealiases
/// that map these to the corresponding `*Syntax` nodes.
public var shorthandName: String {
return syntaxKind
}

/// The corresponding `*Syntax` type defined in the `SwiftSyntax` module,
/// without any question marks attached.
public var syntaxBaseName: String {
Expand All @@ -135,7 +129,7 @@ public struct SyntaxBuildableType: Hashable {
if isBaseType {
return "\(syntaxBaseName)Protocol"
} else {
return shorthandName
return syntaxBaseName
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ struct GenerateSwiftSyntax: ParsableCommand {
TemplateSpec(sourceFile: resultBuildersFile, module: swiftSyntaxBuilderDir, filename: "ResultBuilders.swift"),
TemplateSpec(sourceFile: syntaxExpressibleByStringInterpolationConformancesFile, module: swiftSyntaxBuilderDir, filename: "SyntaxExpressibleByStringInterpolationConformances.swift"),
TemplateSpec(sourceFile: tokenFile, module: swiftSyntaxBuilderDir, filename: "Token.swift"),
TemplateSpec(sourceFile: typealiasesFile, module: swiftSyntaxBuilderDir, filename: "Typealiases.swift"),
]

var errors: [Error] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let buildableCollectionNodesFile = SourceFile {

let docComment = node.documentation.isEmpty ? "" : "/// \(node.documentation)\n"
// Generate collection node struct
ExtensionDecl("\(docComment)extension \(node.type.shorthandName): ExpressibleByArrayLiteral") {
ExtensionDecl("\(docComment)extension \(node.type.syntaxBaseName): ExpressibleByArrayLiteral") {
// Generate initializers
if elementType.isBaseType && node.collectionElementChoices?.isEmpty ?? true {
InitializerDecl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let buildableNodesFile = SourceFile {
let docComment: SwiftSyntax.Trivia = node.documentation.isEmpty ? [] : .docLineComment("/// \(node.documentation)") + .newline
ExtensionDecl(
leadingTrivia: docComment,
extendedType: SimpleTypeIdentifier(name: .identifier(type.shorthandName)),
extendedType: SimpleTypeIdentifier(name: .identifier(type.syntaxBaseName)),
inheritanceClause: hasTrailingComma ? TypeInheritanceClause { InheritedType(typeName: Type("HasTrailingComma")) } : nil
) {
if let convenienceInit = convenienceInit {
Expand Down Expand Up @@ -113,7 +113,7 @@ private func createConvenienceInitializer(node: Node) -> InitializerDecl? {
// Allow initializing identifiers and other tokens without default text with a String
shouldCreateInitializer = true
let paramType = child.type.optionalWrapped(type: "\(raw: token.associatedValueClass ?? "String")" as TypeSyntax)
let tokenExpr = MemberAccessExpr("Token.\(raw: token.swiftKind.withFirstCharacterLowercased.backticked)")
let tokenExpr = MemberAccessExpr("TokenSyntax.\(raw: token.swiftKind.withFirstCharacterLowercased.backticked)")
if child.type.isOptional {
produceExpr = Expr(FunctionCallExpr("\(raw: child.swiftName).map { \(tokenExpr)($0) }"))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let resultBuildersFile = SourceFile {
"""
/// If declared, provides contextual type information for statement
/// expressions to translate them into partial results.
public static func buildExpression(_ expression: \(raw: elementChoice)) -> Self.Component {
public static func buildExpression(_ expression: \(raw: elementChoice)Syntax) -> Self.Component {
return buildExpression(.init(expression))
}
"""
Expand Down Expand Up @@ -168,8 +168,8 @@ let resultBuildersFile = SourceFile {

ExtensionDecl(
"""
public extension \(raw: type.shorthandName) {
init(@\(raw: type.resultBuilderBaseName) itemsBuilder: () -> \(raw: type.shorthandName)) {
public extension \(raw: type.syntaxBaseName) {
init(@\(raw: type.resultBuilderBaseName) itemsBuilder: () -> \(raw: type.syntaxBaseName)) {
self = itemsBuilder()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let tokenFile = SourceFile {
if token.isKeyword {
VariableDecl("""
/// The `\(raw: token.text!)` keyword
static var \(raw: token.name.withFirstCharacterLowercased.backticked): Token {
static var \(raw: token.name.withFirstCharacterLowercased.backticked): TokenSyntax {
return .\(raw: token.swiftKind)()
}
"""
Expand Down

This file was deleted.

9 changes: 5 additions & 4 deletions Examples/CodeGenerationUsingSwiftSyntaxBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftSyntax
import SwiftSyntaxBuilder

extension String {
Expand Down Expand Up @@ -44,12 +45,12 @@ struct Main {
"age": "Int",
]

let source = SourceFile {
StructDecl(identifier: "Person") {
let source = SourceFileSyntax {
StructDeclSyntax(identifier: "Person") {
for (propertyName, propertyType) in properties {
VariableDecl("var \(raw: propertyName): \(raw: propertyType)")
VariableDeclSyntax("var \(raw: propertyName): \(raw: propertyType)")

FunctionDecl("""
FunctionDeclSyntax("""
func with\(raw: propertyName.withFirstLetterUppercased())(_ \(raw: propertyName): \(raw: propertyType)) -> Person {
var result = self
result.\(raw: propertyName) = \(raw: propertyName)
Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftSyntaxBuilder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ add_swift_host_library(SwiftSyntaxBuilder
generated/BuildableNodes.swift
generated/ResultBuilders.swift
generated/Token.swift
generated/Typealiases.swift
generated/SyntaxExpressibleByStringInterpolationConformances.swift
)

Expand Down
Loading