-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability for rules to be dynamically enabled and disabled
- Loading branch information
Aaron Daub
authored and
Aaron Daub
committed
May 20, 2015
1 parent
d550d7d
commit 46290b2
Showing
5 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// RulePalette.swift | ||
// SwiftLint | ||
// | ||
// Created by Aaron Daub on 2015-05-19. | ||
// Copyright (c) 2015 Realm. All rights reserved. | ||
// | ||
|
||
public struct RulePalette { | ||
public var enabledRules: [Rule] = [LineLengthRule(), | ||
LeadingWhitespaceRule(), | ||
TrailingWhitespaceRule(), | ||
TrailingNewlineRule(), | ||
ForceCastRule(), | ||
FileLengthRule(), | ||
TodoRule(), | ||
ColonRule(), | ||
TypeNameRule(), | ||
VariableNameRule(), | ||
TypeBodyLengthRule(), | ||
FunctionBodyLengthRule(), | ||
NestingRule()] | ||
|
||
private var disabledRules: [Rule] = [] | ||
|
||
public init() { | ||
|
||
} | ||
|
||
func ruleWith(identifier: String, enabled: Bool) -> Rule? { | ||
let arrayToSearch = enabled ? self.enabledRules : self.disabledRules | ||
|
||
return filter(arrayToSearch) { | ||
return $0.identifier == identifier | ||
}.first | ||
} | ||
|
||
public mutating func enableRule(identifier: String) -> Bool { | ||
return changeRule(identifier, enabled: true) | ||
} | ||
|
||
public mutating func disableRule(identifier: String) -> Bool { | ||
return changeRule(identifier, enabled: false) | ||
} | ||
|
||
private mutating func changeRule(identifier: String, enabled: Bool) -> Bool { | ||
var (rulesToAddTo, rulesToRemoveFrom) = enabled ? (self.enabledRules, self.disabledRules) : (self.disabledRules, self.enabledRules) | ||
|
||
if let rule = ruleWith(identifier, enabled: !enabled) { | ||
moveRule(rule, enabled: enabled) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
private mutating func moveRule(rule: Rule, enabled: Bool) { | ||
let rulesTuple = enabled ? (self.enabledRules, self.disabledRules) : (self.disabledRules, self.enabledRules) | ||
var (rulesToAddTo, rulesToRemoveFrom): ([Rule], [Rule]) = rulesTuple | ||
|
||
rulesToAddTo.append(rule) | ||
rulesToRemoveFrom = filter(rulesToRemoveFrom) { (candidateRule: Rule) -> Bool in | ||
return candidateRule.identifier != rule.identifier | ||
} | ||
|
||
(self.enabledRules, self.disabledRules) = enabled ? (rulesToAddTo, rulesToRemoveFrom) : (rulesToRemoveFrom, rulesToAddTo) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// RulePaletteTests.swift | ||
// SwiftLint | ||
// | ||
// Created by Aaron Daub on 2015-05-19. | ||
// Copyright (c) 2015 Realm. All rights reserved. | ||
// | ||
|
||
//import Cocoa | ||
import XCTest | ||
import SwiftLintFramework | ||
|
||
class RulePaletteTests: XCTestCase { | ||
|
||
func testEnableRule() { | ||
var rulePalette = RulePalette() | ||
let beforeCount = rulePalette.enabledRules.count | ||
rulePalette.disableRule("line_length") | ||
let afterCount = rulePalette.enabledRules.count | ||
XCTAssertEqual(beforeCount, afterCount + 1, "Disabling a rule should decrease the number of enabled rules by 1") | ||
} | ||
|
||
func testDisableRule() { | ||
var rulePalette = RulePalette() | ||
let beforeCount = rulePalette.enabledRules.count | ||
rulePalette.disableRule("line_length") | ||
rulePalette.enableRule("line_length") | ||
let afterCount = rulePalette.enabledRules.count | ||
XCTAssertEqual(beforeCount, afterCount, "Disabling a rule and then enabling that rule shouldn't alter the number of enabled rules") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters