Skip to content

Commit

Permalink
Add config option allow_redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Jul 26, 2018
1 parent fde3559 commit 7643c8b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
21 changes: 14 additions & 7 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -4905,13 +4905,6 @@ class Foo {

```

```swift
class Foo {
static let shared = Foo()
}

```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down Expand Up @@ -4946,6 +4939,20 @@ class Foo {

```

```swift
class Foo {
let myVar = Int(0)
}

```

```swift
class Foo {
let myVar = Set<Int>(0)
}

```

</details>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>(0)\n}\n"
]
)

Expand All @@ -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 []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ extension ExplicitTypeInterfaceRuleTests {
static var allTests: [(String, (ExplicitTypeInterfaceRuleTests) -> () throws -> Void)] = [
("testExplicitTypeInterface", testExplicitTypeInterface),
("testExcludeLocalVars", testExcludeLocalVars),
("testExcludeClassVars", testExcludeClassVars)
("testExcludeClassVars", testExcludeClassVars),
("testAllowRedundancy", testAllowRedundancy)
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"
)
}
}
22 changes: 22 additions & 0 deletions Tests/SwiftLintFrameworkTests/ExplicitTypeInterfaceRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>(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])
}
}

0 comments on commit 7643c8b

Please sign in to comment.