diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb24556cb..1418529484 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,10 @@ [Aaron McTavish](https://github.com/aamctustwo) [#202](https://github.com/realm/SwiftLint/issues/202) +* Opt-in rules are now supported. + [JP Simard](https://github.com/jpsim) + [#256](https://github.com/realm/SwiftLint/issues/256) + ##### Bug Fixes * AutoCorrect for TrailingNewlineRule only removes at most one line. diff --git a/Source/SwiftLintFramework/Models/Configuration.swift b/Source/SwiftLintFramework/Models/Configuration.swift index cb425cd65e..8f9f24dda1 100644 --- a/Source/SwiftLintFramework/Models/Configuration.swift +++ b/Source/SwiftLintFramework/Models/Configuration.swift @@ -37,6 +37,7 @@ public struct Configuration: Equatable { } public init?(disabledRules: [String] = [], + enabledRules: [String] = [], included: [String] = [], excluded: [String] = [], reporter: String = "xcode", @@ -48,7 +49,6 @@ public struct Configuration: Equatable { self.useNestedConfigs = useNestedConfigs // Validate that all rule identifiers map to a defined rule - let validRuleIdentifiers = Configuration.rulesFromDict().map { $0.dynamicType.description.identifier } @@ -64,9 +64,7 @@ public struct Configuration: Equatable { } // Validate that rule identifiers aren't listed multiple times - - let ruleSet = Set(validDisabledRules) - if ruleSet.count != validDisabledRules.count { + if Set(validDisabledRules).count != validDisabledRules.count { let duplicateRules = validDisabledRules.reduce([String: Int]()) { (var accu, element) in accu[element] = accu[element]?.successor() ?? 1 return accu @@ -78,14 +76,17 @@ public struct Configuration: Equatable { } self.disabledRules = validDisabledRules - self.rules = rules.filter { - !validDisabledRules.contains($0.dynamicType.description.identifier) + self.rules = rules.filter { rule in + let id = rule.dynamicType.description.identifier + if validDisabledRules.contains(id) { return false } + return enabledRules.contains(id) || !(rule is OptInRule) } } public init?(dict: [String: AnyObject]) { self.init( disabledRules: dict["disabled_rules"] as? [String] ?? [], + enabledRules: dict["enabled_rules"] as? [String] ?? [], included: dict["included"] as? [String] ?? [], excluded: dict["excluded"] as? [String] ?? [], reporter: dict["reporter"] as? String ?? XcodeReporter.identifier, @@ -119,7 +120,7 @@ public struct Configuration: Equatable { } public static func rulesFromDict(dict: [String: AnyObject]? = nil, - ruleList: RuleList = masterRuleList) -> [Rule] { + ruleList: RuleList = masterRuleList) -> [Rule] { var rules = [Rule]() for rule in ruleList.list.values { let identifier = rule.description.identifier @@ -148,8 +149,7 @@ public struct Configuration: Equatable { } public func lintableFilesForPath(path: String) -> [File] { - let allPaths = self.lintablePathsForPath(path) - return allPaths.flatMap { File(path: $0) } + return lintablePathsForPath(path).flatMap { File(path: $0) } } public func configForFile(file: File) -> Configuration { diff --git a/Source/SwiftLintFramework/Protocols/Rule.swift b/Source/SwiftLintFramework/Protocols/Rule.swift index c527d61512..c903216749 100644 --- a/Source/SwiftLintFramework/Protocols/Rule.swift +++ b/Source/SwiftLintFramework/Protocols/Rule.swift @@ -8,6 +8,8 @@ import SourceKittenFramework +public protocol OptInRule {} + public protocol Rule { init() // Rules need to be able to be initialized with default values static var description: RuleDescription { get }