Skip to content

Commit

Permalink
Added disable rule test to autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
masters3d committed Aug 22, 2016
1 parent f4d9976 commit 46a6b09
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
[Sarr Blaise](https://github.com/bsarr007)
[#646](https://github.com/realm/SwiftLint/issues/646)

* Fix locally disabled comma rule autocorrect.
[J. Cheyo Jimenez](https://github.com/masters3d)
[#601](https://github.com/realm/SwiftLint/issues/601)

* Fix force_unwrapping false positive inside strings.
[Daniel Beard](https://github.com/daniel-beard)
[#721](https://github.com/realm/SwiftLint/issues/721)
Expand Down
8 changes: 7 additions & 1 deletion Source/SwiftLintFramework/Rules/ColonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,22 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule {
public func correctFile(file: File) -> [Correction] {
let matches = violationRangesInFile(file, withPattern: pattern)
guard !matches.isEmpty else { return [] }
let fileregions = file.regions()

let regularExpression = regex(pattern)
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents
for range in matches.reverse() {
let location = Location(file: file, characterOffset: range.location)
let region = fileregions.filter { $0.contains(location) }.first
if region?.isRuleDisabled(self) == true { // false when region is nil
continue
} else {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: "$1: $2")
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
}
file.write(contents)
return corrections
Expand Down
8 changes: 7 additions & 1 deletion Source/SwiftLintFramework/Rules/CommaRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,22 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
}

public func correctFile(file: File) -> [Correction] {
let fileregions = file.regions()
let matches = violationRangesInFile(file)
if matches.isEmpty { return [] }

var contents = file.contents as NSString
let description = self.dynamicType.description
var corrections = [Correction]()
for range in matches.reverse() {
contents = contents.stringByReplacingCharactersInRange(range, withString: ", ")
let location = Location(file: file, characterOffset: range.location)
let region = fileregions.filter { $0.contains(location) }.first
if region?.isRuleDisabled(self) == true {
continue
} else {
contents = contents.stringByReplacingCharactersInRange(range, withString: ", ")
corrections.append(Correction(ruleDescription: description, location: location))
}
}

file.write(contents as String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public struct LegacyCGGeometryFunctionsRule: CorrectableRule, ConfigurationProvi
location: Location(file: file, characterOffset: $0.location))
}
}

// swiftlint:disable function_body_length
public func correctFile(file: File) -> [Correction] {
let varName = RegexHelpers.varNameGroup
let twoVars = RegexHelpers.twoVars
Expand Down Expand Up @@ -144,12 +144,21 @@ public struct LegacyCGGeometryFunctionsRule: CorrectableRule, ConfigurationProvi
.map { ($0.0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed

if matches.isEmpty {return []}
let fileregions = file.regions()

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)

let region = fileregions.filter {$0.contains(location)}.first
if region?.isRuleDisabled(self) == true {
continue
}
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
11 changes: 9 additions & 2 deletions Source/SwiftLintFramework/Rules/LegacyConstantRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,25 @@ public struct LegacyConstantRule: CorrectableRule, ConfigurationProviderRule {
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents

let matches = patterns.map({ pattern, template in
file.matchPattern(pattern, withSyntaxKinds: [.Identifier])
.map { ($0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed

if matches.isEmpty {return []}
let fileregions = file.regions()

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
let region = fileregions.filter { $0.contains(location) }.first
if region?.isRuleDisabled(self) == true {
continue
}

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))
}

Expand Down
8 changes: 7 additions & 1 deletion Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,19 @@ public struct LegacyConstructorRule: CorrectableRule, ConfigurationProviderRule
.filter { $0.1.first == .Identifier }
.map { ($0.0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed
if matches.isEmpty {return []}
let fileregions = file.regions()

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
let region = fileregions.filter { $0.contains(location) }.first
if region?.isRuleDisabled(self) == true {
continue
}
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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,19 @@ public struct LegacyNSGeometryFunctionsRule: CorrectableRule, ConfigurationProvi
.filter { $0.1.first == .Identifier }
.map { ($0.0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed
if matches.isEmpty {return []}
let fileregions = file.regions()

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
let region = fileregions.filter { $0.contains(location) }.first
if region?.isRuleDisabled(self) == true {
continue
}

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))
}

Expand Down
11 changes: 7 additions & 4 deletions Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public struct ReturnArrowWhitespaceRule: CorrectableRule, ConfigurationProviderR

public func correctFile(file: File) -> [Correction] {
let matches = violationRangesInFile(file)
guard !matches.isEmpty else { return [] }
if matches.isEmpty { return [] }
let fileregions = file.regions()

let regularExpression = regex(pattern)
let description = self.dynamicType.description
Expand All @@ -75,15 +76,17 @@ public struct ReturnArrowWhitespaceRule: CorrectableRule, ConfigurationProviderR

for result in results {
guard result.numberOfRanges > replacementsByIndex.keys.maxElement() else { break }

let location = Location(file: file, characterOffset: result.range.location)
let region = fileregions.filter {$0.contains(location)}.first
if region?.isRuleDisabled(self) == true {
continue
}
for (index, string) in replacementsByIndex {
if let range = contents.nsrangeToIndexRange(result.rangeAtIndex(index)) {
contents.replaceRange(range, with: string)
break
}
}

let location = Location(file: file, characterOffset: result.range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
Expand Down
18 changes: 15 additions & 3 deletions Source/SwiftLintFramework/Rules/StatementPositionRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,24 @@ private extension StatementPositionRule {
func defaultCorrectFile(file: File) -> [Correction] {
let matches = defaultViolationRangesInFile(file,
withPattern: self.dynamicType.defaultPattern)
guard !matches.isEmpty else { return [] }
if matches.isEmpty { return [] }
let fileregions = file.regions()

let regularExpression = regex(self.dynamicType.defaultPattern)
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents
for range in matches.reverse() {
let location = Location(file: file, characterOffset: range.location)
let region = fileregions.filter { $0.contains(location)}.first
if region?.isRuleDisabled(self) == true {
continue
}

contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [],
range: range,
withTemplate: "} $1")
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
Expand Down Expand Up @@ -217,11 +223,18 @@ private extension StatementPositionRule {
syntaxMap: syntaxMap)

let validMatches = matches.flatMap(validator).filter(filterRanges)
if validMatches.isEmpty {return []}

let fileregions = file.regions()
let description = self.dynamicType.uncuddledDescription
var corrections = [Correction]()

for match in validMatches.reverse() {
let location = Location(file: file, characterOffset: match.range.location)
let region = fileregions.filter {$0.contains(location)}.first
if region?.isRuleDisabled(self) == true {
continue
}
let range1 = match.rangeAtIndex(1)
let nsRange2 = match.rangeAtIndex(3)
let newlineRange = match.rangeAtIndex(2)
Expand All @@ -239,7 +252,6 @@ private extension StatementPositionRule {
whitespace.insert("\n", atIndex: whitespace.startIndex)
}
contents.replaceRange(range2, with: whitespace)
let location = Location(file: file, characterOffset: match.range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/SwiftLintFramework/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ extension XCTestCase {
ruleDescription.corrections.forEach(config.assertCorrection)
// make sure strings that don't trigger aren't corrected
zip(nonTriggers, nonTriggers).forEach(config.assertCorrection)

//"disable" command do not correct
ruleDescription.corrections.forEach { ( before, _) in
let beforeDisabled = command + before
let expectedCleaned = cleanedContentsAndMarkerOffsets(from: beforeDisabled).0
config.assertCorrection(expectedCleaned, expected: expectedCleaned)
}

}

func checkError<T: protocol<ErrorType, Equatable>>(error: T, closure: () throws -> () ) {
Expand Down

0 comments on commit 46a6b09

Please sign in to comment.