-
Notifications
You must be signed in to change notification settings - Fork 441
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
Conversation
If you'd like to contribute this as an opt-in rule for swift-format, that would also be a very welcome contribution! |
There was a problem hiding this 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 👍
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😉
There was a problem hiding this 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.
Co-authored-by: Danny Mösch <[email protected]>
I applied the suggestions, thank you very much @SimplyDanny. I think its ready to merge now |
There was a problem hiding this 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.
@swift-ci please test |
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.