<commit_before>import Foundation
import SwiftSyntax
import SwiftParser

/// MigrateToNewIfLetSyntax will visit each if statement in the Syntax tree, and
/// checks if the there is an if condition which is of the pre Swift 5.7 "if-let-style"
/// and rewrites it to the new one
///
/// For example will it turn:
/// ```
/// if let foo = foo {
///   ...
/// }
/// ```
/// into:
/// ```
/// if let foo {
///   ...
/// }
class MigrateToNewIfLetSyntax: SyntaxRewriter {
  // Visit over all IfStmtSyntax nodes
  override func visit(_ node: IfStmtSyntax) -> StmtSyntax {
    // For each node, visit all conditions in this node
    let newConditions = node.conditions.enumerated().map { (index, condition) in
      var conditionCopy = condition
      // check if the condition is a OptionalBindingConditionSyntax...
      if var binding = condition.condition.as(OptionalBindingConditionSyntax.self),
         // ...and has an initializer
         let initializer = binding.initializer,
         // and the name of the variable pattern and the initializer is the same, ignoring any whitespace
         binding.pattern.withoutTrivia().description == initializer.value.withoutTrivia().description {
        // Remove the initializer
        binding.initializer = nil
        // And remove whitespace (the space 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")<commit_msg>Apply suggestions from code review<commit_after>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 {
        // 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")
{'fraction_non_alphanumeric': 0.11059223094884313, 'fraction_numerical': 0.0021226915729144554, 'mean_word_length': 2.8402607986960065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5921426', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1283, 'n_words': 397}