@@ -2,11 +2,11 @@ import Foundation
2
2
import SwiftSyntax
3
3
import SwiftParser
4
4
5
- /// MigrateToNewIfLetSyntax will visit each if statement in the Syntax tree, and
6
- /// checks if the there is an if condition which is of the pre Swift 5.7 "if-let-style"
7
- /// and rewrites it to the new one
5
+ /// MigrateToNewIfLetSyntax will visit each `if` statement in the syntax tree
6
+ /// replacing all "old style" optional bindings by the new shorter syntax available
7
+ /// since Swift 5.7.
8
8
///
9
- /// For example will it turn:
9
+ /// For example, it will turn:
10
10
/// ```
11
11
/// if let foo = foo {
12
12
/// ...
@@ -18,20 +18,20 @@ import SwiftParser
18
18
/// ...
19
19
/// }
20
20
class MigrateToNewIfLetSyntax : SyntaxRewriter {
21
- // Visit over all IfStmtSyntax nodes
21
+ // Visit all `if` statements.
22
22
override func visit( _ node: IfStmtSyntax ) -> StmtSyntax {
23
- // For each node, visit all conditions in this node
23
+ // Visit all conditions in the node.
24
24
let newConditions = node. conditions. enumerated ( ) . map { ( index, condition) in
25
25
var conditionCopy = condition
26
- // check if the condition is a OptionalBindingConditionSyntax ...
26
+ // Check if the condition is an optional binding ...
27
27
if var binding = condition. condition. as ( OptionalBindingConditionSyntax . self) ,
28
- // ...and has an initializer
28
+ // ... and has an initializer ...
29
29
let initializer = binding. initializer,
30
- // and the name of the variable pattern and the initializer is the same, ignoring any whitespace
30
+ // ... and both sides of the assignment are the same identifiers.
31
31
binding. pattern. withoutTrivia ( ) . description == initializer. value. withoutTrivia ( ) . description {
32
- // Remove the initializer
32
+ // Remove the initializer ...
33
33
binding. initializer = nil
34
- // And remove whitespace (the space before the comma) in if statements with multiple conditions
34
+ // ... and remove whitespace before the comma ( in `if` statements with multiple conditions).
35
35
if index != node. conditions. count - 1 {
36
36
binding. pattern = binding. pattern. withoutTrailingTrivia ( )
37
37
}
0 commit comments