Skip to content

Migrate from strings to typed buildable nodes in SyntaxBuildableWrappers #542

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 3 commits into from
Aug 4, 2022

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Aug 2, 2022

As proposed in #485 (review), this PR migrates the generated snippets of Swift code in SyntaxBuildableType and SyntaxBuildableChild to ExprBuildables and TypeBuildables.

This will pave the way for a future PR porting BuildableNodes.swift.gyb to SwiftSyntaxBuilderGeneration.

@fwcd fwcd marked this pull request as ready for review August 2, 2022 21:44
@fwcd fwcd requested a review from ahoppen as a code owner August 2, 2022 21:44
Copy link
Member Author

@fwcd fwcd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of notes on the implementation

Comment on lines -22 to -25
/// If optional `?`, otherwise the empty string.
var optionalQuestionMark: String {
isOptional ? "?" : ""
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is replaced with optionalWrapped(type:) and optionalChained(expr:), depending on whether a type or an expression is needed.

var expressibleAs: String {
/// The `ExpressibleAs*` Swift type for this syntax kind without any
/// question marks attached.
var expressibleAsBaseName: String {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR factors out a few ...BaseName properties (expressibleAs, syntax, buildable). These represent a non-optional type and will still be strings (as opposed to, e.g. ExpressibleAsTypeBuildables) to make them easy to embed in string interpolations and across type and expression contexts.

Thus .xyzBaseName replaces the earlier .nonOptional.xyz idiom.

Comment on lines +33 to +34
let buildableConformances: [String] = [type.expressibleAsBaseName, type.listBuildableBaseName] + (isSyntax ? [] : [syntaxType.buildableBaseName])
let listConformances: [String] = isSyntax ? [] : [syntaxType.listBuildableBaseName]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we relied on these types being non-optional. Since optional types wouldn't make sense e.g. in conformance lists and dealing with strings is easier, we use the base names now in a few more places (this shouldn't change anything in the generated code).

inheritanceClause: createTypeInheritanceClause(conformances: listConformances)
) {
FunctionDecl(
leadingTrivia: [
"/// Builds list of `\(type.syntax)`s.",
"/// Builds list of `\(type.syntaxBaseName)`s.",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes here (and for the other places where base names are now used).

@fwcd
Copy link
Member Author

fwcd commented Aug 2, 2022

@swift-ci please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor comment, otherwise LGTM.

Also, I think that optionalWrapped and optionalChained are good examples of how SwiftSyntaxBuilder simplifies source generation. 👍

@fwcd fwcd force-pushed the syntax-buildable-wrappers-buildable-nodes branch from ec34a35 to 966a50c Compare August 4, 2022 01:28
@fwcd
Copy link
Member Author

fwcd commented Aug 4, 2022

@swift-ci please test

@fwcd fwcd merged commit 6eee38b into swiftlang:main Aug 4, 2022
@fwcd fwcd deleted the syntax-buildable-wrappers-buildable-nodes branch August 4, 2022 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants