-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
82 additions
and
0 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
63 changes: 63 additions & 0 deletions
63
Source/SwiftLintFramework/Rules/Idiomatic/LegacyObjcTypeRule.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,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)) | ||
} | ||
} | ||
} |
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