Skip to content

Add new example: Migrate a Swift Codebase to the new if-let-Syntax #887

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 2 commits into from
Oct 18, 2022

Conversation

flashspys
Copy link
Contributor

We used this piece of code to transform our codebase to the new if let Syntax. I think it is a nice beginner project and would fit as a nice, not too easy, not too hard, example project. I commented the source code as much as possible. Maybe someone has even some suggestions for improvement 😊 I'm for example not sure about the var conditionCopy = condition, maybe there is a more clean way to do this.

@flashspys flashspys requested a review from ahoppen as a code owner October 6, 2022 15:41
@allevato
Copy link
Member

allevato commented Oct 6, 2022

If you'd like to contribute this as an opt-in rule for swift-format, that would also be a very welcome contribution!

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.

Nice, that’s a cool example 👍

@CodaFi
Copy link
Contributor

CodaFi commented Oct 6, 2022

This is quite cool! My one comment here is that you might consider checking for an IdentifierPatternSyntax node and an IdentifierExprSyntax node rather than comparing their textual output directly.

/// }
class MigrateToNewIfLetSyntax: SyntaxRewriter {
// Visit over all IfStmtSyntax nodes
override func visit(_ node: IfStmtSyntax) -> StmtSyntax {
Copy link
Contributor

Choose a reason for hiding this comment

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

Only if statements are currently considered, but optional bindings are allowed in guard and while statements as well. For an example, it might be fine to cover only one case and leave the rest up to the motivated user, though. 😉

Shameless plug by the way: There already is such a rule available in SwiftLint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For an example, it might be fine to cover only one case and leave the rest up to the motivated user, though.

That was exactly my thought 😉

Copy link
Contributor

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

Allow me to be pedantic and suggest some style and formulation changes to better match the way the existing examples are documented.

@flashspys
Copy link
Contributor Author

I applied the suggestions, thank you very much @SimplyDanny. I think its ready to merge now

Copy link
Contributor

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

LGTM! Considering the decision against merging #900, I'm not sure if the migrate method should remain in the example, though.

@DougGregor
Copy link
Member

@swift-ci please test

@ahoppen ahoppen merged commit b9f5805 into swiftlang:main Oct 18, 2022
@CodaFi CodaFi mentioned this pull request Nov 3, 2022
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.

6 participants