-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2679 from realm/marcelo/first-index
`contains_over_first_not_nil` rule now also checks for `firstIndex(where:)`
- Loading branch information
Showing
7 changed files
with
116 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 27 additions & 19 deletions
46
Source/SwiftLintFramework/Rules/Performance/ContainsOverFirstNotNilRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,42 @@ | ||
import SourceKittenFramework | ||
|
||
public struct ContainsOverFirstNotNilRule: CallPairRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule { | ||
public struct ContainsOverFirstNotNilRule: CallPairRule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "contains_over_first_not_nil", | ||
name: "Contains over first not nil", | ||
description: "Prefer `contains` over `first(where:) != nil`", | ||
description: "Prefer `contains` over `first(where:) != nil` and `firstIndex(where:) != nil`.", | ||
kind: .performance, | ||
nonTriggeringExamples: [ | ||
"let first = myList.first(where: { $0 % 2 == 0 })\n", | ||
"let first = myList.first { $0 % 2 == 0 }\n" | ||
], | ||
triggeringExamples: [ | ||
"↓myList.first { $0 % 2 == 0 } != nil\n", | ||
"↓myList.first(where: { $0 % 2 == 0 }) != nil\n", | ||
"↓myList.map { $0 + 1 }.first(where: { $0 % 2 == 0 }) != nil\n", | ||
"↓myList.first(where: someFunction) != nil\n", | ||
"↓myList.map { $0 + 1 }.first { $0 % 2 == 0 } != nil\n", | ||
"(↓myList.first { $0 % 2 == 0 }) != nil\n" | ||
] | ||
nonTriggeringExamples: ["first", "firstIndex"].flatMap { method in | ||
return [ | ||
"let \(method) = myList.\(method)(where: { $0 % 2 == 0 })\n", | ||
"let \(method) = myList.\(method) { $0 % 2 == 0 }\n" | ||
] | ||
}, | ||
triggeringExamples: ["first", "firstIndex"].flatMap { method in | ||
return [ | ||
"↓myList.\(method) { $0 % 2 == 0 } != nil\n", | ||
"↓myList.\(method)(where: { $0 % 2 == 0 }) != nil\n", | ||
"↓myList.map { $0 + 1 }.\(method)(where: { $0 % 2 == 0 }) != nil\n", | ||
"↓myList.\(method)(where: someFunction) != nil\n", | ||
"↓myList.map { $0 + 1 }.\(method) { $0 % 2 == 0 } != nil\n", | ||
"(↓myList.\(method) { $0 % 2 == 0 }) != nil\n" | ||
] | ||
} | ||
) | ||
|
||
public func validate(file: File) -> [StyleViolation] { | ||
return validate(file: file, | ||
pattern: "[\\}\\)]\\s*!=\\s*nil", | ||
patternSyntaxKinds: [.keyword], | ||
callNameSuffix: ".first", | ||
severity: configuration.severity) | ||
let pattern = "[\\}\\)]\\s*!=\\s*nil" | ||
let firstViolations = validate(file: file, pattern: pattern, patternSyntaxKinds: [.keyword], | ||
callNameSuffix: ".first", severity: configuration.severity, | ||
reason: "Prefer `contains` over `first(where:) != nil`") | ||
let firstIndexViolations = validate(file: file, pattern: pattern, patternSyntaxKinds: [.keyword], | ||
callNameSuffix: ".firstIndex", severity: configuration.severity, | ||
reason: "Prefer `contains` over `firstIndex(where:) != nil`") | ||
|
||
return firstViolations + firstIndexViolations | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Tests/SwiftLintFrameworkTests/ContainsOverFirstNotNilRuleTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import SwiftLintFramework | ||
import XCTest | ||
|
||
class ContainsOverFirstNotNilRuleTests: XCTestCase { | ||
func testWithDefaultConfiguration() { | ||
verifyRule(ContainsOverFirstNotNilRule.description) | ||
} | ||
|
||
// MARK: - Reasons | ||
|
||
func testFirstReason() { | ||
let string = "↓myList.first { $0 % 2 == 0 } != nil" | ||
let violations = self.violations(string) | ||
|
||
XCTAssertEqual(violations.count, 1) | ||
XCTAssertEqual(violations.first?.reason, "Prefer `contains` over `first(where:) != nil`") | ||
} | ||
|
||
func testFirstIndexReason() { | ||
let string = "↓myList.firstIndex { $0 % 2 == 0 } != nil" | ||
let violations = self.violations(string) | ||
|
||
XCTAssertEqual(violations.count, 1) | ||
XCTAssertEqual(violations.first?.reason, "Prefer `contains` over `firstIndex(where:) != nil`") | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func violations(_ string: String, config: Any? = nil) -> [StyleViolation] { | ||
guard let config = makeConfig(config, ContainsOverFirstNotNilRule.description.identifier) else { | ||
return [] | ||
} | ||
|
||
return SwiftLintFrameworkTests.violations(string, config: config) | ||
} | ||
} |