Skip to content

Commit

Permalink
Fix LegacyConstructorRule
Browse files Browse the repository at this point in the history
  • Loading branch information
norio-nomura committed Apr 17, 2016
1 parent becb691 commit cf91026
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ public struct LegacyConstructorRule: CorrectableRule, ConfigurationProviderRule
var corrections = [Correction]()
var contents = file.contents

for (pattern, template) in patterns {
let matches = file.matchPattern(pattern)
.filter({ $0.1.first == .Identifier })
.map({ $0.0 })

let regularExpression = regex(pattern)
for range in matches.reverse() {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: template)
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
let matches = patterns.map {
(pattern, template) -> [(NSRange, String, String)] in
let matches = file.matchPattern(pattern)
.filter { $0.1.first == .Identifier }
.map { ($0.0, pattern, template) }
return matches
}
.flatten()
.sort { $0.0.location > $1.0.location } // reversed

for (range, pattern, template) in matches {
contents = regex(pattern).stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: template)
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}

file.write(contents)
Expand Down

0 comments on commit cf91026

Please sign in to comment.