Skip to content
Draft
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 @@ -14,6 +14,10 @@

### Enhancements

* Add a repeatable `--disable-rule` command-line option for disabling rules for a single invocation.
[aryansk](https://github.com/aryansk)
[#6831](https://github.com/realm/SwiftLint/issues/6831)

* Add `#examples` and `#corrections` macros that expand lists and
dictionaries of code strings into `Example`s, reducing boilerplate when
defining a rule's triggering/non-triggering examples and corrections. Adopt
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,11 @@ A simple example including just two configuration files looks like this:

`swiftlint --config .swiftlint.yml --config .swiftlint_child.yml`

To disable rules for one invocation without changing a configuration file, use
`--disable-rule`. The option can be repeated:

`swiftlint lint --disable-rule todo --disable-rule line_length`

### Nested Configurations

In addition to a main configuration (the `.swiftlint.yml` file in the root
Expand Down
6 changes: 6 additions & 0 deletions Source/SwiftLintFramework/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ extension Configuration {
onlyRule: options.onlyRule,
cachePath: options.cachePath
)

guard options.disabledRule.isNotEmpty else {
return
}

disableRules(options.disabledRule)
}
}

Expand Down
7 changes: 7 additions & 0 deletions Source/SwiftLintFramework/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ public struct Configuration {
}

// MARK: - Methods: Internal
internal mutating func disableRules(_ ruleIdentifiers: [String]) {
let disabledRulesConfiguration = Self(
rulesMode: .defaultConfiguration(disabled: Set(ruleIdentifiers), optIn: [])
)
rulesWrapper = rulesWrapper.merged(with: disabledRulesConfiguration.rulesWrapper)
}

mutating func makeIncludedAndExcludedPaths(relativeTo newBasePath: URL) {
includedPaths = includedPaths.map { $0.relative(to: newBasePath) }
excludedPaths = excludedPaths.map { $0.relative(to: newBasePath) }
Expand Down
3 changes: 3 additions & 0 deletions Source/SwiftLintFramework/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ package struct LintOrAnalyzeOptions {
let ignoreCache: Bool
let enableAllRules: Bool
let onlyRule: [String]
let disabledRule: [String]
let autocorrect: Bool
let format: Bool
let disableSourceKit: Bool
Expand Down Expand Up @@ -81,6 +82,7 @@ package struct LintOrAnalyzeOptions {
ignoreCache: Bool,
enableAllRules: Bool,
onlyRule: [String],
disabledRule: [String],
autocorrect: Bool,
format: Bool,
disableSourceKit: Bool,
Expand Down Expand Up @@ -109,6 +111,7 @@ package struct LintOrAnalyzeOptions {
self.ignoreCache = ignoreCache
self.enableAllRules = enableAllRules
self.onlyRule = onlyRule
self.disabledRule = disabledRule
self.autocorrect = autocorrect
self.format = format
self.disableSourceKit = disableSourceKit
Expand Down
1 change: 1 addition & 0 deletions Source/swiftlint/Commands/Analyze.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extension SwiftLint {
ignoreCache: true,
enableAllRules: false,
onlyRule: common.onlyRule,
disabledRule: common.disabledRule,
autocorrect: common.fix,
format: common.format,
disableSourceKit: false,
Expand Down
1 change: 1 addition & 0 deletions Source/swiftlint/Commands/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extension SwiftLint {
ignoreCache: noCache,
enableAllRules: enableAllRules,
onlyRule: common.onlyRule,
disabledRule: common.disabledRule,
autocorrect: common.fix,
format: common.format,
disableSourceKit: disableSourceKit,
Expand Down
6 changes: 6 additions & 0 deletions Source/swiftlint/Common/LintOrAnalyzeArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ struct LintOrAnalyzeArguments: ParsableArguments {
"""
)
var onlyRule: [String] = []
@Option(
name: .customLong("disable-rule"),
parsing: .singleValue,
help: "Disable the specified rule for this invocation. Can be specified repeatedly."
)
var disabledRule: [String] = []
}

// MARK: - Common Argument Help
Expand Down
59 changes: 59 additions & 0 deletions Tests/FileSystemAccessTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ struct ConfigurationTests { // swiftlint:disable:this type_body_length
#expect(onlyRuleIdentifiers.dropLast() == mergedConfiguration.enabledRuleIdentifiers)
}

@Test
func commandLineDisabledRules() {
Configuration.resetCache()

let allRulesConfiguration = Configuration(
options: .init(enableAllRules: true, disabledRule: ["todo"])
)
#expect(!allRulesConfiguration.enabledRuleIdentifiers.contains("todo"))
#expect(
allRulesConfiguration.rules.count == RuleRegistry.shared.list.list.count - 1
)

Configuration.resetCache()
let onlyRulesConfiguration = Configuration(
options: .init(onlyRule: ["line_length", "todo"], disabledRule: ["todo"])
)
#expect(onlyRulesConfiguration.enabledRuleIdentifiers == ["line_length"])
}

@Test
func onlyRules() throws {
let only = ["nesting", "todo"]
Expand Down Expand Up @@ -679,3 +698,43 @@ private extension Configuration {
rules.map { type(of: $0).identifier }.sorted()
}
}

private extension LintOrAnalyzeOptions {
init(
enableAllRules: Bool = false,
onlyRule: [String] = [],
disabledRule: [String] = []
) {
self.init(
mode: .lint,
paths: [],
useSTDIN: false,
configurationFiles: [],
strict: false,
lenient: false,
forceExclude: false,
useExcludingByPrefix: false,
useScriptInputFiles: false,
useScriptInputFileLists: false,
benchmark: false,
reporter: nil,
baseline: nil,
writeBaseline: nil,
workingDirectory: nil,
quiet: true,
output: nil,
progress: false,
cachePath: nil,
ignoreCache: true,
enableAllRules: enableAllRules,
onlyRule: onlyRule,
disabledRule: disabledRule,
autocorrect: false,
format: false,
disableSourceKit: false,
compilerLogPath: nil,
compileCommands: nil,
checkForUpdates: false
)
}
}
1 change: 1 addition & 0 deletions Tests/FrameworkTests/LintOrAnalyzeOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private extension LintOrAnalyzeOptions {
ignoreCache: false,
enableAllRules: false,
onlyRule: [],
disabledRule: [],
autocorrect: false,
format: false,
disableSourceKit: false,
Expand Down