From 7643c8be3ea05874d067f13ba748d8b49fab8519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Thu, 26 Jul 2018 12:41:23 +0200 Subject: [PATCH] Add config option allow_redundancy --- Rules.md | 21 ++++++++++++------ .../Rules/ExplicitTypeInterfaceRule.swift | 9 ++++---- .../ExplicitTypeInterfaceConfiguration.swift | 12 +++++++--- Tests/LinuxMain.swift | 3 ++- ...licitTypeInterfaceConfigurationTests.swift | 9 ++++++-- .../ExplicitTypeInterfaceRuleTests.swift | 22 +++++++++++++++++++ 6 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Rules.md b/Rules.md index 93e9dded750..cd40def8902 100644 --- a/Rules.md +++ b/Rules.md @@ -4905,13 +4905,6 @@ class Foo { ``` -```swift -class Foo { - static let shared = Foo() -} - -``` -
Triggering Examples @@ -4946,6 +4939,20 @@ class Foo { ``` +```swift +class Foo { + ↓let myVar = Int(0) +} + +``` + +```swift +class Foo { + ↓let myVar = Set(0) +} + +``` +
diff --git a/Source/SwiftLintFramework/Rules/ExplicitTypeInterfaceRule.swift b/Source/SwiftLintFramework/Rules/ExplicitTypeInterfaceRule.swift index d5d54477ddf..af004168aed 100644 --- a/Source/SwiftLintFramework/Rules/ExplicitTypeInterfaceRule.swift +++ b/Source/SwiftLintFramework/Rules/ExplicitTypeInterfaceRule.swift @@ -15,14 +15,15 @@ public struct ExplicitTypeInterfaceRule: ASTRule, OptInRule, ConfigurationProvid "class Foo {\n var myVar: Int? = 0\n}\n", "class Foo {\n let myVar: Int? = 0\n}\n", "class Foo {\n static var myVar: Int? = 0\n}\n", - "class Foo {\n class var myVar: Int? = 0\n}\n", - "class Foo {\n static let shared = Foo()\n}\n" + "class Foo {\n class var myVar: Int? = 0\n}\n" ], triggeringExamples: [ "class Foo {\n ↓var myVar = 0\n\n}\n", "class Foo {\n ↓let mylet = 0\n\n}\n", "class Foo {\n ↓static var myStaticVar = 0\n}\n", - "class Foo {\n ↓class var myClassVar = 0\n}\n" + "class Foo {\n ↓class var myClassVar = 0\n}\n", + "class Foo {\n ↓let myVar = Int(0)\n}\n", + "class Foo {\n ↓let myVar = Set(0)\n}\n" ] ) @@ -31,7 +32,7 @@ public struct ExplicitTypeInterfaceRule: ASTRule, OptInRule, ConfigurationProvid guard configuration.allowedKinds.contains(kind), !containsType(dictionary: dictionary), - !assigneeIsInitCall(file: file, dictionary: dictionary), + (!configuration.allowRedundancy || !assigneeIsInitCall(file: file, dictionary: dictionary)), let offset = dictionary.offset else { return [] } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/ExplicitTypeInterfaceConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/ExplicitTypeInterfaceConfiguration.swift index 351251c0c25..e550eee50ce 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/ExplicitTypeInterfaceConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/ExplicitTypeInterfaceConfiguration.swift @@ -45,14 +45,18 @@ public struct ExplicitTypeInterfaceConfiguration: RuleConfiguration, Equatable { .varStatic, .varClass] - public var severityConfiguration = SeverityConfiguration(.warning) + private(set) var severityConfiguration = SeverityConfiguration(.warning) - public var allowedKinds = ExplicitTypeInterfaceConfiguration.variableKinds + private(set) var allowedKinds = ExplicitTypeInterfaceConfiguration.variableKinds + + private(set) var allowRedundancy = false public var consoleDescription: String { let excludedKinds = ExplicitTypeInterfaceConfiguration.variableKinds.subtracting(allowedKinds) let simplifiedExcludedKinds = excludedKinds.compactMap { $0.variableKind?.rawValue }.sorted() - return severityConfiguration.consoleDescription + ", excluded: \(simplifiedExcludedKinds)" + return severityConfiguration.consoleDescription + + ", excluded: \(simplifiedExcludedKinds)" + + ", allow_redundancy: \(allowRedundancy)" } public init() {} @@ -68,6 +72,8 @@ public struct ExplicitTypeInterfaceConfiguration: RuleConfiguration, Equatable { case ("excluded", let excludedStrings as [String]): let excludedKinds = excludedStrings.compactMap(VariableKind.init(rawValue:)) allowedKinds.subtract(excludedKinds.map(SwiftDeclarationKind.init(variableKind:))) + case ("allow_redundancy", let allowRedundancy as Bool): + self.allowRedundancy = allowRedundancy default: throw ConfigurationError.unknownConfiguration } diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 2461f857973..f78ca7e95a5 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -341,7 +341,8 @@ extension ExplicitTypeInterfaceRuleTests { static var allTests: [(String, (ExplicitTypeInterfaceRuleTests) -> () throws -> Void)] = [ ("testExplicitTypeInterface", testExplicitTypeInterface), ("testExcludeLocalVars", testExcludeLocalVars), - ("testExcludeClassVars", testExcludeClassVars) + ("testExcludeClassVars", testExcludeClassVars), + ("testAllowRedundancy", testAllowRedundancy) ] } diff --git a/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceConfigurationTests.swift b/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceConfigurationTests.swift index ffb78327f4b..4bdd32082fc 100644 --- a/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceConfigurationTests.swift +++ b/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceConfigurationTests.swift @@ -12,9 +12,11 @@ class ExplicitTypeInterfaceConfigurationTests: XCTestCase { func testApplyingCustomConfiguration() throws { var config = ExplicitTypeInterfaceConfiguration() try config.apply(configuration: ["severity": "error", - "excluded": ["local"]]) + "excluded": ["local"], + "allow_redundancy": true]) XCTAssertEqual(config.severityConfiguration.severity, .error) XCTAssertEqual(config.allowedKinds, Set([.varInstance, .varClass, .varStatic])) + XCTAssertEqual(config.allowRedundancy, true) } func testInvalidKeyInCustomConfiguration() { @@ -41,6 +43,9 @@ class ExplicitTypeInterfaceConfigurationTests: XCTestCase { func testConsoleDescription() throws { var config = ExplicitTypeInterfaceConfiguration() try config.apply(configuration: ["excluded": ["class", "instance"]]) - XCTAssertEqual(config.consoleDescription, "warning, excluded: [\"class\", \"instance\"]") + XCTAssertEqual( + config.consoleDescription, + "warning, excluded: [\"class\", \"instance\"], allow_redundancy: false" + ) } } diff --git a/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceRuleTests.swift b/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceRuleTests.swift index 3206a60ee20..8d102760906 100644 --- a/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceRuleTests.swift @@ -36,4 +36,26 @@ class ExplicitTypeInterfaceRuleTests: XCTestCase { verifyRule(description, ruleConfiguration: ["excluded": ["static"]]) } + func testAllowRedundancy() { + let nonTriggeringExamples = [ + "class Foo {\n var myVar: Int? = 0\n}\n", + "class Foo {\n let myVar: Int? = 0\n}\n", + "class Foo {\n static var myVar: Int? = 0\n}\n", + "class Foo {\n class var myVar: Int? = 0\n}\n", + "class Foo {\n static let shared = Foo()\n}\n", + "class Foo {\n let myVar = Int(0)\n}\n", + "class Foo {\n let myVar = Set(0)\n}\n" + ] + let triggeringExamples = [ + "class Foo {\n ↓var myVar = 0\n\n}\n", + "class Foo {\n ↓let mylet = 0\n\n}\n", + "class Foo {\n ↓static var myStaticVar = 0\n}\n", + "class Foo {\n ↓class var myClassVar = 0\n}\n" + ] + let description = ExplicitTypeInterfaceRule.description + .with(triggeringExamples: triggeringExamples) + .with(nonTriggeringExamples: nonTriggeringExamples) + + verifyRule(description, ruleConfiguration: ["allow_redundancy": true]) + } }