Skip to content

protocol ListBuilder #2451

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 4 commits into from
Feb 3, 2024
Merged

Conversation

RoBo-Inc
Copy link
Contributor

@RoBo-Inc RoBo-Inc commented Jan 31, 2024

Introducing the ListBuilder protocol with a default function implementation can significantly reduce the amount of generated code in the Sources/SwiftSyntaxBuilder/generated/ResultBuilders.swift file. Additionally, it makes the codebase more comprehensible by emphasizing similarities among all the result builders and highlighting differences between them.

Apart from this, the PR addresses another issue that requires detailed explanation. Two generated result builders (ExprListBuilder and UnexpectedNodesBuilder) stood out from the others. They were "special" because their Expression was not a concrete type; instead, it was a protocol (ExprSyntaxProtocol and SyntaxProtocol respectively). So we were dealing with existentials here, which are not very good. Furthermore, we had to create additional extensions dedicated to these two result builders, located at the bottom of the Sources/SwiftSyntaxBuilder/ResultBuilderExtensions.swift file, accompanied by a lengthy comment.

The key point is that these two result builders don't need to be treated as special cases, and they don't have to involve existentials. They can conform to the ListBuilder protocol, just like all the other builders do. Despite this, we can still make them work with protocols just as before, maintaining the same approach used for some other result builders in the same file (Sources/SwiftSyntaxBuilder/ResultBuilderExtensions.swift). This eliminates the need for special treatment and enhances codebase consistency.

In summary, this PR represents a refactoring. All the functionality remains unchanged. However, the refactoring is significant, making the code more structured, consistent, and efficient.

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.

Removing >3000 LOC. I love it 😍

@RoBo-Inc RoBo-Inc force-pushed the list_builder_protocol branch from f296439 to 9f5d39b Compare February 1, 2024 07:51
@RoBo-Inc
Copy link
Contributor Author

RoBo-Inc commented Feb 1, 2024

Updated ✅

  1. generated/ResultBuilders file:
  • Removed the unnecessary comments as requested.
  • Added "MARK: -" separators instead, for better readability.
  1. Put the extensions back, but:
  • Moved them from ResultBuilderExtensions to ConvenienceInitializers file. Because these extensions are not specifically related to the result builders anymore. At the same time they indeed are convenience initializers.
  • Improved its bodies a bit.
  • Made the both initializers to be public for consistency (before only one of them was public).

Note: The ConvenienceInitializers file has warning to insert new extensions in alphabetical order. But I just append the two extensions (which I moved) to the end of the file. Because the extensions are related and has the common comment. Besides the other extensions in the file actually are not in alphabetical order anyways... 😳

@RoBo-Inc RoBo-Inc requested a review from ahoppen February 1, 2024 07:52
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.

Thank you. Let’s keep the ordering in ConvenienceInitializers.swift alphabetical, though. Or at least not break it any more than it’s already broken.

@RoBo-Inc RoBo-Inc force-pushed the list_builder_protocol branch from 9f5d39b to bac1a1d Compare February 2, 2024 04:36
@RoBo-Inc
Copy link
Contributor Author

RoBo-Inc commented Feb 2, 2024

Done ✅
All alphabetical now 😃

@RoBo-Inc RoBo-Inc requested a review from ahoppen February 2, 2024 04:39
@ahoppen
Copy link
Member

ahoppen commented Feb 2, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge February 2, 2024 05:44
auto-merge was automatically disabled February 2, 2024 07:02

Head branch was pushed to by a user without write access

@RoBo-Inc RoBo-Inc force-pushed the list_builder_protocol branch from bac1a1d to c1dc8b3 Compare February 2, 2024 07:02
@ahoppen
Copy link
Member

ahoppen commented Feb 2, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge February 2, 2024 07:07
@ahoppen
Copy link
Member

ahoppen commented Feb 2, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 92dbcbc into swiftlang:main Feb 3, 2024
@RoBo-Inc RoBo-Inc deleted the list_builder_protocol branch February 3, 2024 04:36
KidiIT

This comment was marked as spam.

@KidiIT

This comment was marked as spam.

KidiIT

This comment was marked as spam.

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.

3 participants