Add configuration for unallowed symbols severity (#5066)

This commit is contained in:
Danny Mösch
2023-06-22 16:42:41 +02:00
committed by GitHub
parent b69ae7cc4d
commit 33545d2f4b
6 changed files with 29 additions and 5 deletions
+5
View File
@@ -10,6 +10,11 @@
#### Enhancements
* Make severity for unallowed symbols configurable. The option name is
`unallowed_symbols_severity`. It accepts the two values `warning` and `error`
(default) as usual.
[SimplyDanny](https://github.com/SimplyDanny)
* Mention a rule's identifier in the console message that is printed when the
rule's associated configuration entry contains invalid values.
[SimplyDanny](https://github.com/SimplyDanny)
@@ -73,7 +73,7 @@ private extension GenericTypeNameRule {
reason: """
Generic type name '\(name)' should only contain alphanumeric and other allowed characters
""",
severity: .error
severity: configuration.unallowedSymbolsSeverity.severity
)
)
} else if let caseCheckSeverity = configuration.validatesStartWithLowercase.severity,
@@ -96,7 +96,7 @@ private extension TypeNameRule {
return ReasonedRuleViolation(
position: identifier.positionAfterSkippingLeadingTrivia,
reason: "Type name '\(name)' should only contain alphanumeric and other allowed characters",
severity: .error
severity: nameConfiguration.unallowedSymbolsSeverity.severity
)
} else if let caseCheckSeverity = nameConfiguration.validatesStartWithLowercase.severity,
name.first?.isLowercase == true {
@@ -10,14 +10,16 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
"(max_length) \(maxLength.shortConsoleDescription), " +
"excluded: \(excludedRegularExpressions.map { $0.pattern }.sorted()), " +
"allowed_symbols: \(allowedSymbols.sorted()), " +
"unallowed_symbols_severity: \(unallowedSymbolsSeverity.consoleDescription), " +
"validates_start_with_lowercase: \(validatesStartWithLowercase.consoleDescription)"
}
private(set) var minLength: SeverityLevels
private(set) var maxLength: SeverityLevels
private(set) var excludedRegularExpressions: Set<NSRegularExpression>
private(set) var validatesStartWithLowercase: StartWithLowercaseConfiguration
private(set) var allowedSymbols: Set<String>
private(set) var unallowedSymbolsSeverity: Severity
private(set) var validatesStartWithLowercase: StartWithLowercaseConfiguration
var minLengthThreshold: Int {
return max(minLength.warning, minLength.error ?? minLength.warning)
@@ -37,6 +39,7 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
maxLengthError: Int,
excluded: [String] = [],
allowedSymbols: [String] = [],
unallowedSymbolsSeverity: Severity = .error,
validatesStartWithLowercase: StartWithLowercaseConfiguration = .error) {
minLength = SeverityLevels(warning: minLengthWarning, error: minLengthError)
maxLength = SeverityLevels(warning: maxLengthWarning, error: maxLengthError)
@@ -44,6 +47,7 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
try? NSRegularExpression.cached(pattern: "^\($0)$")
})
self.allowedSymbols = Set(allowedSymbols)
self.unallowedSymbolsSeverity = unallowedSymbolsSeverity
self.validatesStartWithLowercase = validatesStartWithLowercase
}
@@ -66,7 +70,9 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
if let allowedSymbols = [String].array(of: configurationDict["allowed_symbols"]) {
self.allowedSymbols = Set(allowedSymbols)
}
if let unallowedSymbolsSeverity = configurationDict["unallowed_symbols_severity"] {
try self.unallowedSymbolsSeverity.apply(configuration: unallowedSymbolsSeverity)
}
if let validatesStartWithLowercase = configurationDict["validates_start_with_lowercase"] as? String {
try self.validatesStartWithLowercase.apply(configuration: validatesStartWithLowercase)
} else if let validatesStartWithLowercase = configurationDict["validates_start_with_lowercase"] as? Bool {
@@ -40,7 +40,7 @@ struct IdentifierNameRule: ASTRule, ConfigurationProviderRule {
if !configuration.allowedSymbolsAndAlphanumerics.isSuperset(of: CharacterSet(charactersIn: name)) {
return [
StyleViolation(ruleDescription: Self.description,
severity: .error,
severity: configuration.unallowedSymbolsSeverity.severity,
location: Location(file: file, byteOffset: offset),
reason: """
\(type) name '\(name)' should only contain alphanumeric and other \
@@ -87,4 +87,17 @@ class NameConfigurationTests: SwiftLintTestCase {
excluded: [])
XCTAssertEqual(nameConfig.maxLengthThreshold, 7)
}
func testUnallowedSymbolsSeverity() throws {
var nameConfig = TesteeType(minLengthWarning: 3,
minLengthError: 1,
maxLengthWarning: 17,
maxLengthError: 22,
unallowedSymbolsSeverity: .warning)
XCTAssertEqual(nameConfig.unallowedSymbolsSeverity, .warning)
try nameConfig.apply(configuration: ["unallowed_symbols_severity": "error"])
XCTAssertEqual(nameConfig.unallowedSymbolsSeverity, .error)
}
}