From 33545d2f4ba3f08c65d7c77f7db587c2bb01e751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Thu, 22 Jun 2023 16:42:41 +0200 Subject: [PATCH] Add configuration for unallowed symbols severity (#5066) --- CHANGELOG.md | 5 +++++ .../Rules/Idiomatic/GenericTypeNameRule.swift | 2 +- .../Rules/Idiomatic/TypeNameRule.swift | 2 +- .../RuleConfigurations/NameConfiguration.swift | 10 ++++++++-- .../Rules/Style/IdentifierNameRule.swift | 2 +- .../NameConfigurationTests.swift | 13 +++++++++++++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54bb715fc..dda13898e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift index 7d193358c..167215da7 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift @@ -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, diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TypeNameRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TypeNameRule.swift index 49237ea09..b357f271e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TypeNameRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TypeNameRule.swift @@ -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 { diff --git a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NameConfiguration.swift b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NameConfiguration.swift index 434b0a2ce..08d0b0841 100644 --- a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NameConfiguration.swift +++ b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NameConfiguration.swift @@ -10,14 +10,16 @@ struct NameConfiguration: 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 - private(set) var validatesStartWithLowercase: StartWithLowercaseConfiguration private(set) var allowedSymbols: Set + 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: 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: RuleConfiguration, Equatable { try? NSRegularExpression.cached(pattern: "^\($0)$") }) self.allowedSymbols = Set(allowedSymbols) + self.unallowedSymbolsSeverity = unallowedSymbolsSeverity self.validatesStartWithLowercase = validatesStartWithLowercase } @@ -66,7 +70,9 @@ struct NameConfiguration: 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 { diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/IdentifierNameRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/IdentifierNameRule.swift index 736c4e35d..5314ddfd8 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/IdentifierNameRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/IdentifierNameRule.swift @@ -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 \ diff --git a/Tests/SwiftLintFrameworkTests/NameConfigurationTests.swift b/Tests/SwiftLintFrameworkTests/NameConfigurationTests.swift index 03a9fcd49..81e55f78e 100644 --- a/Tests/SwiftLintFrameworkTests/NameConfigurationTests.swift +++ b/Tests/SwiftLintFrameworkTests/NameConfigurationTests.swift @@ -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) + } }