-import Foundation
-import SourceKittenFramework
+import SwiftSyntax
 
-public struct UntypedErrorInCatchRule: OptInRule, ConfigurationProviderRule {
+public struct UntypedErrorInCatchRule: OptInRule, ConfigurationProviderRule, SwiftSyntaxCorrectableRule {
     public var configuration = SeverityConfiguration(.warning)
 
     public init() {}
-
-    private static let regularExpression =
-        "catch" + // The catch keyword
-        "(?:"   + // Start of the first non-capturing group
-        "\\s*"  + // Zero or multiple whitespace character
-        "\\("   + // The `(` character
-        "?"     + // Zero or one occurrence of the previous character
-        "\\s*"  + // Zero or multiple whitespace character
-        "(?:"   + // Start of the alternative non-capturing group
-        "let"   + // `let` keyword
-        "|"     + // OR
-        "var"   + // `var` keyword
-        ")"     + // End of the alternative non-capturing group
-        "\\s+"  + // At least one any type of whitespace character
-        "\\w+"  + // At least one any type of word character
-        "\\s*"  + // Zero or multiple whitespace character
-        "\\)"   + // The `)` character
-        "?"     + // Zero or one occurrence of the previous character
-        ")"     + // End of the first non-capturing group
-        "(?:"   + // Start of the second non-capturing group
-        "\\s*"  + // Zero or unlimited any whitespace character
-        ")"     + // End of the second non-capturing group
-        "\\{"     // Start scope character
 
     public static let description = RuleDescription(
         identifier: "untyped_error_in_catch",

            Example("do {\n    try foo() \n} ↓catch (let error) {}"): Example("do {\n    try foo() \n} catch {}")
        ])

    public func validate(file: SwiftLintFile) -> [StyleViolation] {
        return violationRanges(in: file).map {
            return StyleViolation(ruleDescription: Self.description,
                                  severity: configuration.severity,
                                  location: Location(file: file, characterOffset: $0.location))
        }
    }

    fileprivate func violationRanges(in file: SwiftLintFile) -> [NSRange] {
        return file.match(pattern: Self.regularExpression,
                          with: [.keyword, .keyword, .identifier])
    }
}

extension UntypedErrorInCatchRule: CorrectableRule {
    public func correct(file: SwiftLintFile) -> [Correction] {
        let violations = violationRanges(in: file)
        let matches = file.ruleEnabled(violatingRanges: violations, for: self)
        if matches.isEmpty { return [] }

        var contents = file.contents.bridge()
        let description = Self.description
        var corrections = [Correction]()

        for range in matches.reversed() where contents.substring(with: range).contains("let error") {
            contents = contents.replacingCharacters(in: range, with: "catch {").bridge()
            let location = Location(file: file, characterOffset: range.location)
            corrections.append(Correction(ruleDescription: description, location: location))
        }
        file.write(contents.bridge())
        return corrections
    }
}

              try foo()
            } catch var error as MyError {
            } catch {}
            """),
            Example("""
            do {
                try something()
            } catch let e where e.code == .fileError {
                // can be ignored
            } catch {
                print(error)
            }
            """)
        ],
        triggeringExamples: [
