Skip to content

Commit 5411955

Browse files
Apply suggestions from code review
Co-authored-by: Danny Mösch <[email protected]>
1 parent 86e43b9 commit 5411955

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

Examples/MigrateToNewIfLetSyntax.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import Foundation
22
import SwiftSyntax
33
import SwiftParser
44

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.
88
///
9-
/// For example will it turn:
9+
/// For example, it will turn:
1010
/// ```
1111
/// if let foo = foo {
1212
/// ...
@@ -18,20 +18,20 @@ import SwiftParser
1818
/// ...
1919
/// }
2020
class MigrateToNewIfLetSyntax: SyntaxRewriter {
21-
// Visit over all IfStmtSyntax nodes
21+
// Visit all `if` statements.
2222
override func visit(_ node: IfStmtSyntax) -> StmtSyntax {
23-
// For each node, visit all conditions in this node
23+
// Visit all conditions in the node.
2424
let newConditions = node.conditions.enumerated().map { (index, condition) in
2525
var conditionCopy = condition
26-
// check if the condition is a OptionalBindingConditionSyntax...
26+
// Check if the condition is an optional binding ...
2727
if var binding = condition.condition.as(OptionalBindingConditionSyntax.self),
28-
// ...and has an initializer
28+
// ... and has an initializer ...
2929
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.
3131
binding.pattern.withoutTrivia().description == initializer.value.withoutTrivia().description {
32-
// Remove the initializer
32+
// Remove the initializer ...
3333
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).
3535
if index != node.conditions.count - 1 {
3636
binding.pattern = binding.pattern.withoutTrailingTrivia()
3737
}

0 commit comments

Comments
 (0)