-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Foundation | ||
import SwiftSyntax | ||
import SwiftParser | ||
|
||
/// MigrateToNewIfLetSyntax will visit each `if` statement in the syntax tree | ||
/// replacing all "old style" optional bindings by the new shorter syntax available | ||
/// since Swift 5.7. | ||
/// | ||
/// For example, it will turn: | ||
/// ``` | ||
/// if let foo = foo { | ||
/// ... | ||
/// } | ||
/// ``` | ||
/// into: | ||
/// ``` | ||
/// if let foo { | ||
/// ... | ||
/// } | ||
class MigrateToNewIfLetSyntax: SyntaxRewriter { | ||
// Visit all `if` statements. | ||
override func visit(_ node: IfStmtSyntax) -> StmtSyntax { | ||
// Visit all conditions in the node. | ||
let newConditions = node.conditions.enumerated().map { (index, condition) in | ||
var conditionCopy = condition | ||
// Check if the condition is an optional binding ... | ||
if var binding = condition.condition.as(OptionalBindingConditionSyntax.self), | ||
// ... and has an initializer ... | ||
let initializer = binding.initializer, | ||
// ... and both sides of the assignment are the same identifiers. | ||
binding.pattern.withoutTrivia().description == initializer.value.withoutTrivia().description { | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Remove the initializer ... | ||
binding.initializer = nil | ||
// ... and remove whitespace before the comma (in `if` statements with multiple conditions). | ||
if index != node.conditions.count - 1 { | ||
binding.pattern = binding.pattern.withoutTrailingTrivia() | ||
} | ||
conditionCopy.condition = Syntax(binding) | ||
} | ||
return conditionCopy | ||
} | ||
return StmtSyntax(node.withConditions(ConditionElementListSyntax(newConditions))) | ||
} | ||
|
||
/// Utility function to migrate all swift files in a folder and its subfolders | ||
static func migrate(folderPath: String) { | ||
for case let fileURL as String in FileManager.default.enumerator(atPath: folderPath)! { | ||
if fileURL.hasSuffix("swift") { | ||
print("Rewriting", fileURL) | ||
let fullPath = folderPath + "/" + fileURL | ||
let tree = try! Parser.parse(source: String(data: FileManager.default.contents(atPath: fullPath)!, encoding: .utf8 )!) | ||
let newTree = MigrateToNewIfLetSyntax().visit(tree) | ||
try! newTree.description.write(toFile: fullPath, atomically: true, encoding: .utf8) | ||
} | ||
} | ||
} | ||
} | ||
|
||
MigrateToNewIfLetSyntax.migrate(folderPath: "/path/to/folder") | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 inguard
andwhile
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.
That was exactly my thought 😉