Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option allow_redundancy to rule explicit_type_interface #2313

Merged
merged 6 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
* Add `private_only` configuration to `prefixed_toplevel_constant` rule
[Keith Smiley](https://github.com/keith)
[#2315](https://github.com/realm/SwiftLint/pull/2315)

* Make rule `explicit_type_interface` compatible with rule
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please mention that this is possible with the allow_redundancy configuration key?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

`redundant_type_annotation`.
[Cihat Gündüz](https://github.com/Dschee)
[#2312](https://github.com/realm/SwiftLint/issues/2312)

#### Bug Fixes

Expand Down
14 changes: 14 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -4939,6 +4939,20 @@ class Foo {

```

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

```

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

```

</details>


Expand Down
20 changes: 19 additions & 1 deletion Source/SwiftLintFramework/Rules/ExplicitTypeInterfaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public struct ExplicitTypeInterfaceRule: ASTRule, OptInRule, ConfigurationProvid
"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 @@ -30,6 +32,7 @@ public struct ExplicitTypeInterfaceRule: ASTRule, OptInRule, ConfigurationProvid

guard configuration.allowedKinds.contains(kind),
!containsType(dictionary: dictionary),
(!configuration.allowRedundancy || !assigneeIsInitCall(file: file, dictionary: dictionary)),
let offset = dictionary.offset else {
return []
}
Expand All @@ -44,4 +47,19 @@ public struct ExplicitTypeInterfaceRule: ASTRule, OptInRule, ConfigurationProvid
private func containsType(dictionary: [String: SourceKitRepresentable]) -> Bool {
return dictionary.typeName != nil
}

private func assigneeIsInitCall(file: File, dictionary: [String: SourceKitRepresentable]) -> Bool {
guard
let nameOffset = dictionary.nameOffset,
let nameLength = dictionary.nameLength,
let afterNameRange = file.contents.bridge().byteRangeToNSRange(start: nameOffset + nameLength, length: 0)
else {
return false
}

let contentAfterName = file.contents.bridge().substring(from: afterNameRange.location)
let initCallRegex = regex("^\\s*=\\s*\\p{Lu}[^\\(\\s<]*(?:<[^\\>]*>)?\\(")

return initCallRegex.firstMatch(in: contentAfterName, options: [], range: contentAfterName.fullNSRange) != nil
}
}
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])
}
}