forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to filter rules for
generate-docs
subcommand (realm#4195)
Added rules filtering options, like in the `rules` subcommand. <img width="880" alt="generate-docs--help" src="https://user-images.githubusercontent.com/7829589/189372666-2f5aba62-57a1-49dc-9155-c60f9e085984.png"> To achieve reuse with `rules` subcommand: - extracted filtering logic into `RulesFilter` (and write tests) - extracted filtering command line parameters into `RulesFilterOptions: ParsableArguments`
- Loading branch information
Showing
11 changed files
with
313 additions
and
46 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
20 changes: 20 additions & 0 deletions
20
Source/swiftlint/Commands/Common/RulesFilter.ExcludingOptions+RulesFilterOptions.swift
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,20 @@ | ||
extension RulesFilter.ExcludingOptions { | ||
static func excludingOptions(byCommandLineOptions rulesFilterOptions: RulesFilterOptions) -> Self { | ||
var excludingOptions: Self = [] | ||
|
||
switch rulesFilterOptions.ruleEnablement { | ||
case .enabled: | ||
excludingOptions.insert(.disabled) | ||
case .disabled: | ||
excludingOptions.insert(.enabled) | ||
case .none: | ||
break | ||
} | ||
|
||
if rulesFilterOptions.correctable { | ||
excludingOptions.insert(.uncorrectable) | ||
} | ||
|
||
return excludingOptions | ||
} | ||
} |
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,20 @@ | ||
import ArgumentParser | ||
|
||
enum RuleEnablementOptions: String, EnumerableFlag { | ||
case enabled, disabled | ||
|
||
static func name(for value: RuleEnablementOptions) -> NameSpecification { | ||
return .shortAndLong | ||
} | ||
|
||
static func help(for value: RuleEnablementOptions) -> ArgumentHelp? { | ||
return "Only show \(value.rawValue) rules" | ||
} | ||
} | ||
|
||
struct RulesFilterOptions: ParsableArguments { | ||
@Flag(exclusivity: .exclusive) | ||
var ruleEnablement: RuleEnablementOptions? | ||
@Flag(name: .shortAndLong, help: "Only display correctable rules") | ||
var correctable = false | ||
} |
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,48 @@ | ||
import SwiftLintFramework | ||
|
||
extension RulesFilter { | ||
struct ExcludingOptions: OptionSet { | ||
let rawValue: Int | ||
|
||
static let enabled = ExcludingOptions(rawValue: 1 << 0) | ||
static let disabled = ExcludingOptions(rawValue: 1 << 1) | ||
static let uncorrectable = ExcludingOptions(rawValue: 1 << 2) | ||
} | ||
} | ||
|
||
class RulesFilter { | ||
private let allRules: RuleList | ||
private let enabledRules: [Rule] | ||
|
||
init(allRules: RuleList = primaryRuleList, enabledRules: [Rule]) { | ||
self.allRules = allRules | ||
self.enabledRules = enabledRules | ||
} | ||
|
||
func getRules(excluding excludingOptions: ExcludingOptions) -> RuleList { | ||
if excludingOptions.isEmpty { | ||
return allRules | ||
} | ||
|
||
let filtered: [Rule.Type] = allRules.list.compactMap { ruleID, ruleType in | ||
let enabledRule = enabledRules.first { rule in | ||
type(of: rule).description.identifier == ruleID | ||
} | ||
let isRuleEnabled = enabledRule != nil | ||
|
||
if excludingOptions.contains(.enabled) && isRuleEnabled { | ||
return nil | ||
} | ||
if excludingOptions.contains(.disabled) && !isRuleEnabled { | ||
return nil | ||
} | ||
if excludingOptions.contains(.uncorrectable) && !(ruleType is CorrectableRule.Type) { | ||
return nil | ||
} | ||
|
||
return ruleType | ||
} | ||
|
||
return RuleList(rules: filtered) | ||
} | ||
} |
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
Oops, something went wrong.