From a080617910179907075707188afd6403c4356eaa Mon Sep 17 00:00:00 2001 From: J Cheyo Jimenez Date: Mon, 29 Aug 2016 17:34:44 -0700 Subject: [PATCH] Added disable rule test to autocorrect --- CHANGELOG.md | 6 +++++- .../Extensions/File+SwiftLint.swift | 1 + Source/SwiftLintFramework/Rules/ColonRule.swift | 6 +++--- Source/SwiftLintFramework/Rules/CommaRule.swift | 10 +++++----- .../Rules/LeadingWhitespaceRule.swift | 8 ++++---- .../Rules/LegacyCGGeometryFunctionsRule.swift | 8 +++++--- .../Rules/LegacyConstantRule.swift | 6 ++++-- .../Rules/LegacyConstructorRule.swift | 4 +++- .../Rules/LegacyNSGeometryFunctionsRule.swift | 4 +++- .../Rules/ReturnArrowWhitespaceRule.swift | 9 +++------ .../Rules/StatementPositionRule.swift | 13 +++++++------ .../Rules/TrailingNewlineRule.swift | 8 ++++---- .../Rules/TrailingWhitespaceRule.swift | 6 +----- Tests/SwiftLintFramework/TestHelpers.swift | 8 ++++++++ 14 files changed, 56 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ffc16782b..92e242d1100 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ * 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. @@ -20,6 +20,10 @@ [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 diff --git a/Source/SwiftLintFramework/Extensions/File+SwiftLint.swift b/Source/SwiftLintFramework/Extensions/File+SwiftLint.swift index 9d467a21a8d..d87e593f4bd 100644 --- a/Source/SwiftLintFramework/Extensions/File+SwiftLint.swift +++ b/Source/SwiftLintFramework/Extensions/File+SwiftLint.swift @@ -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)) diff --git a/Source/SwiftLintFramework/Rules/ColonRule.swift b/Source/SwiftLintFramework/Rules/ColonRule.swift index 68606379c18..bd560daaed2 100644 --- a/Source/SwiftLintFramework/Rules/ColonRule.swift +++ b/Source/SwiftLintFramework/Rules/ColonRule.swift @@ -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) diff --git a/Source/SwiftLintFramework/Rules/CommaRule.swift b/Source/SwiftLintFramework/Rules/CommaRule.swift index 1615443b67e..51c29e1ae8b 100644 --- a/Source/SwiftLintFramework/Rules/CommaRule.swift +++ b/Source/SwiftLintFramework/Rules/CommaRule.swift @@ -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 } diff --git a/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift index f482a0fbbad..f3a20b0be5f 100644 --- a/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift @@ -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) diff --git a/Source/SwiftLintFramework/Rules/LegacyCGGeometryFunctionsRule.swift b/Source/SwiftLintFramework/Rules/LegacyCGGeometryFunctionsRule.swift index 7351ba1b145..9530f42c199 100644 --- a/Source/SwiftLintFramework/Rules/LegacyCGGeometryFunctionsRule.swift +++ b/Source/SwiftLintFramework/Rules/LegacyCGGeometryFunctionsRule.swift @@ -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 @@ -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 } diff --git a/Source/SwiftLintFramework/Rules/LegacyConstantRule.swift b/Source/SwiftLintFramework/Rules/LegacyConstantRule.swift index 9a33257ff76..f79ff633f40 100644 --- a/Source/SwiftLintFramework/Rules/LegacyConstantRule.swift +++ b/Source/SwiftLintFramework/Rules/LegacyConstantRule.swift @@ -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)) } diff --git a/Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift b/Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift index caa1f3d1570..cb5b936b49b 100644 --- a/Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift +++ b/Source/SwiftLintFramework/Rules/LegacyConstructorRule.swift @@ -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)) } diff --git a/Source/SwiftLintFramework/Rules/LegacyNSGeometryFunctionsRule.swift b/Source/SwiftLintFramework/Rules/LegacyNSGeometryFunctionsRule.swift index 094e02a59a4..9280fc29182 100644 --- a/Source/SwiftLintFramework/Rules/LegacyNSGeometryFunctionsRule.swift +++ b/Source/SwiftLintFramework/Rules/LegacyNSGeometryFunctionsRule.swift @@ -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)) } diff --git a/Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift index cbbfc2f162b..5e944f64ed1 100644 --- a/Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/ReturnArrowWhitespaceRule.swift @@ -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]() @@ -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) diff --git a/Source/SwiftLintFramework/Rules/StatementPositionRule.swift b/Source/SwiftLintFramework/Rules/StatementPositionRule.swift index 56c2a4cbf73..c70f968750c 100644 --- a/Source/SwiftLintFramework/Rules/StatementPositionRule.swift +++ b/Source/SwiftLintFramework/Rules/StatementPositionRule.swift @@ -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) @@ -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) @@ -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)) } diff --git a/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift b/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift index e8b3c94989e..999f4cc713c 100644 --- a/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift @@ -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 { diff --git a/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift index 786cfa45216..aa9bebea54f 100644 --- a/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift @@ -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) @@ -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 } diff --git a/Tests/SwiftLintFramework/TestHelpers.swift b/Tests/SwiftLintFramework/TestHelpers.swift index de7d3d1a8fb..9b86d7f5f1a 100644 --- a/Tests/SwiftLintFramework/TestHelpers.swift +++ b/Tests/SwiftLintFramework/TestHelpers.swift @@ -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>(error: T, closure: () throws -> () ) {