-
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
only_after_dot
configuration option to empty_count
rule
- Loading branch information
Showing
6 changed files
with
88 additions
and
12 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
26 changes: 26 additions & 0 deletions
26
Source/SwiftLintFramework/Rules/RuleConfigurations/EmptyCountConfiguration.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,26 @@ | ||
private enum ConfigurationKey: String { | ||
case severity = "severity" | ||
case onlyAfterDot = "only_after_dot" | ||
} | ||
|
||
public struct EmptyCountConfiguration: RuleConfiguration, Equatable { | ||
private(set) var severityConfiguration = SeverityConfiguration(.warning) | ||
private(set) var onlyAfterDot: Bool = false | ||
|
||
public var consoleDescription: String { | ||
return [severityConfiguration.consoleDescription, | ||
"\(ConfigurationKey.onlyAfterDot.rawValue): \(onlyAfterDot)"].joined(separator: ", ") | ||
} | ||
|
||
public mutating func apply(configuration: Any) throws { | ||
guard let configuration = configuration as? [String: Any] else { | ||
throw ConfigurationError.unknownConfiguration | ||
} | ||
|
||
if let severityString = configuration[ConfigurationKey.severity.rawValue] as? String { | ||
try severityConfiguration.apply(configuration: severityString) | ||
} | ||
|
||
onlyAfterDot = configuration[ConfigurationKey.onlyAfterDot.rawValue] as? Bool ?? 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
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,40 @@ | ||
@testable import SwiftLintFramework | ||
import XCTest | ||
|
||
class EmptyCountRuleTests: XCTestCase { | ||
func testEmptyCountWithDefaultConfiguration() { | ||
// Test with default parameters | ||
verifyRule(EmptyCountRule.description) | ||
} | ||
|
||
func testEmptyCountWithOnlyAfterDot() { | ||
// Test with `only_after_dot` set to true | ||
let nonTriggeringExamples = [ | ||
"var count = 0\n", | ||
"[Int]().isEmpty\n", | ||
"[Int]().count > 1\n", | ||
"[Int]().count == 1\n", | ||
"[Int]().count == 0xff\n", | ||
"[Int]().count == 0b01\n", | ||
"[Int]().count == 0o07\n", | ||
"discount == 0\n", | ||
"order.discount == 0\n", | ||
"count == 0\n" | ||
] | ||
let triggeringExamples = [ | ||
"[Int]().↓count == 0\n", | ||
"[Int]().↓count > 0\n", | ||
"[Int]().↓count != 0\n", | ||
"[Int]().↓count == 0x0\n", | ||
"[Int]().↓count == 0x00_00\n", | ||
"[Int]().↓count == 0b00\n", | ||
"[Int]().↓count == 0o00\n" | ||
] | ||
|
||
let description = EmptyCountRule.description | ||
.with(triggeringExamples: triggeringExamples) | ||
.with(nonTriggeringExamples: nonTriggeringExamples) | ||
|
||
verifyRule(description, ruleConfiguration: ["only_after_dot": true]) | ||
} | ||
} |