Skip to content

Commit

Permalink
Adding Legacy ObjC Type Rule #2758 (#3531)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim authored Feb 23, 2021
1 parent 4e75099 commit eca9ea4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
* Make `strong_iboutlet` rule correctable.
[MaxHaertwig](https://github.com/maxhaertwig)

* Add `legacy_objc_type` opt-in rule to warn against using
bridged Objective-C reference types instead of Swift value types.
[Blake](https://github.com/72A12F4E)
[#2758](https://github.com/realm/SwiftLint/issues/2758)

#### Bug Fixes

* Fix typos in configuration options for `file_name` rule.
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/PrimaryRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public let primaryRuleList = RuleList(rules: [
LegacyHashingRule.self,
LegacyMultipleRule.self,
LegacyNSGeometryFunctionsRule.self,
LegacyObjcTypeRule.self,
LegacyRandomRule.self,
LetVarWhitespaceRule.self,
LineLengthRule.self,
Expand Down
63 changes: 63 additions & 0 deletions Source/SwiftLintFramework/Rules/Idiomatic/LegacyObjcTypeRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Foundation
import SourceKittenFramework

private let legacyObjcTypes = [
"NSAffineTransform",
"NSArray",
"NSCalendar",
"NSCharacterSet",
"NSData",
"NSDateComponents",
"NSDateInterval",
"NSDate",
"NSDecimalNumber",
"NSDictionary",
"NSIndexPath",
"NSIndexSet",
"NSLocale",
"NSMeasurement",
"NSNotification",
"NSNumber",
"NSPersonNameComponents",
"NSSet",
"NSString",
"NSTimeZone",
"NSURL",
"NSURLComponents",
"NSURLQueryItem",
"NSURLRequest",
"NSUUID"
]

public struct LegacyObjcTypeRule: OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)

public init() {}

public static let description = RuleDescription(
identifier: "legacy_objc_type",
name: "Legacy Objective-C Reference Type",
description: "Prefer Swift value types to bridged Objective-C reference types",
kind: .idiomatic,
nonTriggeringExamples: [
Example("var array = Array<Int>()\n"),
Example("var calendar: Calendar? = nil")
],
triggeringExamples: [
Example("var array = NSArray()"),
Example("var calendar: NSCalendar? = nil")
]
)

private let pattern = legacyObjcTypes.joined(separator: "|")

public func validate(file: SwiftLintFile) -> [StyleViolation] {
return file.match(pattern: pattern)
.filter { !Set($0.1).isDisjoint(with: [.typeidentifier, .identifier]) }
.map {
StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.0.location))
}
}
}
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,12 @@ extension LegacyNSGeometryFunctionsRuleTests {
]
}

extension LegacyObjcTypeRuleTests {
static var allTests: [(String, (LegacyObjcTypeRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
]
}

extension LegacyRandomRuleTests {
static var allTests: [(String, (LegacyRandomRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
Expand Down Expand Up @@ -1878,6 +1884,7 @@ XCTMain([
testCase(LegacyHashingRuleTests.allTests),
testCase(LegacyMultipleRuleTests.allTests),
testCase(LegacyNSGeometryFunctionsRuleTests.allTests),
testCase(LegacyObjcTypeRuleTests.allTests),
testCase(LegacyRandomRuleTests.allTests),
testCase(LetVarWhitespaceRuleTests.allTests),
testCase(LineEndingTests.allTests),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ class LegacyNSGeometryFunctionsRuleTests: XCTestCase {
}
}

class LegacyObjcTypeRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(LegacyObjcTypeRule.description)
}
}

class LegacyRandomRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(LegacyRandomRule.description)
Expand Down

0 comments on commit eca9ea4

Please sign in to comment.