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 30, 2016
1 parent 2eaac54 commit a080617
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 41 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@

* Adds `allow_private_set` configuration for the `private_outlet` rule.
[Rohan Dhaimade](https://github.com/HaloZero)

##### Bug Fixes

* Fixed regex bug in Vertical Whitespace Rule by using SourceKitten instead.
The rule now enabled by default again (no longer opt-in).
[J. Cheyo Jimenez](https://github.com/masters3d)
[#772](https://github.com/realm/SwiftLint/issues/772)

* Correctable rules no longer apply corrections if the rule is locally disabled.
[J. Cheyo Jimenez](https://github.com/masters3d)
[#601](https://github.com/realm/SwiftLint/issues/601)

## 0.12.0: Vertical Laundry

##### Breaking
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Extensions/File+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ extension File {
internal func ruleEnabledViolatingRanges(violatingRanges: [NSRange],
forRule rule: Rule) -> [NSRange] {
let fileRegions = regions()
if fileRegions.isEmpty { return violatingRanges }
let violatingRanges = violatingRanges.filter { range in
let region = fileRegions.filter {
$0.contains(Location(file: self, characterOffset: range.location))
Expand Down
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Rules/ColonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule {
}

public func correctFile(file: File) -> [Correction] {
let matches = violationRangesInFile(file, withPattern: pattern)
let violations = violationRangesInFile(file, withPattern: pattern)
let matches = file.ruleEnabledViolatingRanges(violations, forRule: self)
guard !matches.isEmpty else { return [] }

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)
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)
Expand Down
10 changes: 5 additions & 5 deletions Source/SwiftLintFramework/Rules/CommaRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
}

public func correctFile(file: File) -> [Correction] {
let matches = violationRangesInFile(file)
let violations = violationRangesInFile(file)
let matches = file.ruleEnabledViolatingRanges(violations, forRule: self)
if matches.isEmpty { return [] }

var contents = file.contents as NSString
var nsstring = 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)
nsstring = nsstring.stringByReplacingCharactersInRange(range, withString: ", ")
corrections.append(Correction(ruleDescription: description, location: location))
}

file.write(contents as String)
file.write(nsstring as String)
return corrections
}

Expand Down
8 changes: 4 additions & 4 deletions Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public struct LeadingWhitespaceRule: CorrectableRule, ConfigurationProviderRule,
if spaceCount == 0 {
return []
}
let region = file.regions().filter {
$0.contains(Location(file: file.path, line: max(file.lines.count, 1)))
}.first
if region?.isRuleDisabled(self) == true {
guard let firstLineRange = file.lines.first?.range else {
return []
}
if file.ruleEnabledViolatingRanges([firstLineRange], forRule: self).isEmpty {
return []
}
let indexEnd = file.contents.startIndex.advancedBy(spaceCount)
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 @@ -140,18 +140,20 @@ public struct LegacyCGGeometryFunctionsRule: CorrectableRule, ConfigurationProvi

let matches = patterns.map({ pattern, template in
file.matchPattern(pattern)
.filter { !file.ruleEnabledViolatingRanges([$0.0], forRule: self).isEmpty }
.filter { $0.1.first == .Identifier }
.map { ($0.0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed

if matches.isEmpty { return []}

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
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)
return corrections
}
Expand Down
6 changes: 4 additions & 2 deletions Source/SwiftLintFramework/Rules/LegacyConstantRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,20 @@ 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])
.filter { !file.ruleEnabledViolatingRanges([$0], forRule: self).isEmpty }
.map { ($0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed

if matches.isEmpty { return [] }

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
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
4 changes: 3 additions & 1 deletion Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,18 @@ public struct LegacyConstructorRule: CorrectableRule, ConfigurationProviderRule

let matches = patterns.map({ pattern, template in
file.matchPattern(pattern)
.filter { !file.ruleEnabledViolatingRanges([$0.0], forRule: self).isEmpty }
.filter { $0.1.first == .Identifier }
.map { ($0.0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed
if matches.isEmpty { return [] }

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
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 @@ -141,15 +141,17 @@ public struct LegacyNSGeometryFunctionsRule: CorrectableRule, ConfigurationProvi

let matches = patterns.map({ pattern, template in
file.matchPattern(pattern)
.filter { !file.ruleEnabledViolatingRanges([$0.0], forRule: self).isEmpty }
.filter { $0.1.first == .Identifier }
.map { ($0.0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed
if matches.isEmpty { return [] }

for (range, pattern, template) in matches {
let location = Location(file: file, characterOffset: range.location)
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 @@ -59,9 +59,8 @@ public struct ReturnArrowWhitespaceRule: CorrectableRule, ConfigurationProviderR
}

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

let matches = file.ruleEnabledViolatingRanges(violationRangesInFile(file), forRule: self)
if matches.isEmpty { return [] }
let regularExpression = regex(pattern)
let description = self.dynamicType.description
var corrections = [Correction]()
Expand All @@ -75,15 +74,13 @@ 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)
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
13 changes: 7 additions & 6 deletions Source/SwiftLintFramework/Rules/StatementPositionRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ private extension StatementPositionRule {
}

func defaultCorrectFile(file: File) -> [Correction] {
let matches = defaultViolationRangesInFile(file,
let violations = defaultViolationRangesInFile(file,
withPattern: self.dynamicType.defaultPattern)
guard !matches.isEmpty else { return [] }

let matches = file.ruleEnabledViolatingRanges(violations, forRule: self)
if matches.isEmpty { return [] }
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)
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 +217,13 @@ private extension StatementPositionRule {
syntaxMap: syntaxMap)

let validMatches = matches.flatMap(validator).filter(filterRanges)

.filter { !file.ruleEnabledViolatingRanges([$0.range], forRule: self).isEmpty }
if validMatches.isEmpty { return [] }
let description = self.dynamicType.uncuddledDescription
var corrections = [Correction]()

for match in validMatches.reverse() {
let location = Location(file: file, characterOffset: match.range.location)
let range1 = match.rangeAtIndex(1)
let nsRange2 = match.rangeAtIndex(3)
let newlineRange = match.rangeAtIndex(2)
Expand All @@ -239,7 +241,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: 4 additions & 4 deletions Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public struct TrailingNewlineRule: CorrectableRule, ConfigurationProviderRule, S
guard let count = file.contents.trailingNewlineCount() where count != 1 else {
return []
}
let region = file.regions().filter {
$0.contains(Location(file: file.path, line: max(file.lines.count, 1)))
}.first
if region?.isRuleDisabled(self) == true {
guard let lastLineRange = file.lines.last?.range else {
return []
}
if file.ruleEnabledViolatingRanges([lastLineRange], forRule: self).isEmpty {
return []
}
if count < 1 {
Expand Down
6 changes: 1 addition & 5 deletions Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public struct TrailingWhitespaceRule: CorrectableRule, ConfigurationProviderRule
let whitespaceCharacterSet = NSCharacterSet.whitespaceCharacterSet()
var correctedLines = [String]()
var corrections = [Correction]()
let fileRegions = file.regions()
for line in file.lines {
let correctedLine = (line.content as NSString)
.stringByTrimmingTrailingCharactersInSet(whitespaceCharacterSet)
Expand All @@ -54,10 +53,7 @@ public struct TrailingWhitespaceRule: CorrectableRule, ConfigurationProviderRule
continue
}

let region = fileRegions.filter {
$0.contains(Location(file: file.path, line: line.index, character: 0))
}.first
if region?.isRuleDisabled(self) == true {
if file.ruleEnabledViolatingRanges([line.range], forRule: self).isEmpty {
correctedLines.append(line.content)
continue
}
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 a080617

Please sign in to comment.