Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to toggle Rules on and off. #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
FunctionBodyLength, Nesting, TypeBodyLength, TypeName, VariableName.
[JP Simard](https://github.com/jpsim)

* Add RuleEnabler, a struct that controls which rules are enabled and which are disabled.
[Aaron Daub](https://github.com/aarondaub)

##### Bug Fixes

None.


## 0.1.0


First Version!
25 changes: 7 additions & 18 deletions Source/SwiftLintFramework/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,21 @@ import SourceKittenFramework

public struct Linter {
private let file: File
private let rulePalette: RulePalette

public var styleViolations: [StyleViolation] {
return reduce(
[
LineLengthRule().validateFile(file),
LeadingWhitespaceRule().validateFile(file),
TrailingWhitespaceRule().validateFile(file),
TrailingNewlineRule().validateFile(file),
ForceCastRule().validateFile(file),
FileLengthRule().validateFile(file),
TodoRule().validateFile(file),
ColonRule().validateFile(file),
TypeNameRule().validateFile(file),
VariableNameRule().validateFile(file),
TypeBodyLengthRule().validateFile(file),
FunctionBodyLengthRule().validateFile(file),
NestingRule().validateFile(file)
], [], +)
let styleViolations: [[StyleViolation]] = map(rulePalette.enabledRules) {
return $0.validateFile(self.file)
}
return reduce(styleViolations, [], +)
}

/**
Initialize a Linter by passing in a File.

:param: file File to lint.
*/
public init(file: File) {
self.file = file
public init(file: File, rulePalette: RulePalette = RulePalette()) {
(self.file, self.rulePalette) = (file, rulePalette)
}
}
67 changes: 67 additions & 0 deletions Source/SwiftLintFramework/RulePalette.swift
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)
}
}
32 changes: 32 additions & 0 deletions Source/SwiftLintFrameworkTests/RulePaletteTests.swift
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")
}

}
8 changes: 8 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
34C0A7AA1B0B703300CBF683 /* RulePaletteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34C0A7A91B0B703300CBF683 /* RulePaletteTests.swift */; };
34C0A7AC1B0B711300CBF683 /* RulePalette.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34C0A7AB1B0B711300CBF683 /* RulePalette.swift */; };
D0AAAB5019FB0960007B24B3 /* SwiftLintFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D0D1217819E87B05005E4BAA /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; };
D0E7B65319E9C6AD00EDBA4D /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; };
Expand Down Expand Up @@ -98,6 +100,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
34C0A7A91B0B703300CBF683 /* RulePaletteTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RulePaletteTests.swift; sourceTree = "<group>"; };
34C0A7AB1B0B711300CBF683 /* RulePalette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RulePalette.swift; sourceTree = "<group>"; };
5499CA961A2394B700783309 /* Components.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Components.plist; sourceTree = "<group>"; };
5499CA971A2394B700783309 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D0D1211B19E87861005E4BAA /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; usesTabs = 0; };
Expand Down Expand Up @@ -312,6 +316,7 @@
E812249B1B04FADC001783D2 /* Linter.swift */,
E88DEA6E1B09843F00A66CB0 /* Location.swift */,
E88DEA761B098D0C00A66CB0 /* Rule.swift */,
34C0A7AB1B0B711300CBF683 /* RulePalette.swift */,
E88DEA781B098D4400A66CB0 /* RuleParameter.swift */,
E88DEA721B0984C400A66CB0 /* String+SwiftLint.swift */,
E88DEA6A1B0983FE00A66CB0 /* StyleViolation.swift */,
Expand Down Expand Up @@ -342,6 +347,7 @@
isa = PBXGroup;
children = (
E81224991B04F85B001783D2 /* LinterTests.swift */,
34C0A7A91B0B703300CBF683 /* RulePaletteTests.swift */,
D0D1212219E878CC005E4BAA /* Configuration */,
D0D1217C19E87B05005E4BAA /* Supporting Files */,
);
Expand Down Expand Up @@ -557,6 +563,7 @@
E88DEA961B099CF200A66CB0 /* NestingRule.swift in Sources */,
E88DEA711B09847500A66CB0 /* ViolationSeverity.swift in Sources */,
E88DEA8C1B0999A000A66CB0 /* ASTRule.swift in Sources */,
34C0A7AC1B0B711300CBF683 /* RulePalette.swift in Sources */,
E88DEA821B0990A700A66CB0 /* TodoRule.swift in Sources */,
E88DEA901B099A3100A66CB0 /* FunctionBodyLengthRule.swift in Sources */,
E88DEA6B1B0983FE00A66CB0 /* StyleViolation.swift in Sources */,
Expand All @@ -569,6 +576,7 @@
buildActionMask = 2147483647;
files = (
E812249A1B04F85B001783D2 /* LinterTests.swift in Sources */,
34C0A7AA1B0B703300CBF683 /* RulePaletteTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down