mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
Add new allowed_types option to legacy_objc_type rule (#6012)
This commit is contained in:
committed by
GitHub
parent
a4db2f65c0
commit
d2d1aacaac
@@ -26,6 +26,10 @@
|
||||
[Martin Redington](https://github.com/mildm8nnered)
|
||||
[#5681](https://github.com/realm/SwiftLint/issues/5681)
|
||||
|
||||
* Add new `allowed_types` option to `legacy_objc_type` rule to ignore certain types.
|
||||
[kapitoshka438](https://github.com/kapitoshka438)
|
||||
[#3723](https://github.com/realm/SwiftLint/issues/3723)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Fix issue referencing the Tests package from another Bazel workspace.
|
||||
|
||||
@@ -30,7 +30,7 @@ private let legacyObjcTypes = [
|
||||
|
||||
@SwiftSyntaxRule(optIn: true)
|
||||
struct LegacyObjcTypeRule: Rule {
|
||||
var configuration = SeverityConfiguration<Self>(.warning)
|
||||
var configuration = LegacyObjcTypeConfiguration()
|
||||
|
||||
static let description = RuleDescription(
|
||||
identifier: "legacy_objc_type",
|
||||
@@ -44,6 +44,12 @@ struct LegacyObjcTypeRule: Rule {
|
||||
Example("var className: String = NSStringFromClass(MyClass.self)"),
|
||||
Example("_ = URLRequest.CachePolicy.reloadIgnoringLocalCacheData"),
|
||||
Example(#"_ = Notification.Name("com.apple.Music.playerInfo")"#),
|
||||
Example(#"""
|
||||
class SLURLRequest: NSURLRequest {
|
||||
let data = NSData()
|
||||
let number: NSNumber
|
||||
}
|
||||
"""#, configuration: ["allowed_types": ["NSData", "NSNumber", "NSURLRequest"]]),
|
||||
],
|
||||
triggeringExamples: [
|
||||
Example("var array = ↓NSArray()"),
|
||||
@@ -71,20 +77,24 @@ struct LegacyObjcTypeRule: Rule {
|
||||
private extension LegacyObjcTypeRule {
|
||||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
|
||||
override func visitPost(_ node: IdentifierTypeSyntax) {
|
||||
if let typeName = node.typeName, legacyObjcTypes.contains(typeName) {
|
||||
if let typeName = node.typeName,
|
||||
legacyObjcTypes.contains(typeName),
|
||||
!configuration.allowedTypes.contains(typeName) {
|
||||
violations.append(node.positionAfterSkippingLeadingTrivia)
|
||||
}
|
||||
}
|
||||
|
||||
override func visitPost(_ node: DeclReferenceExprSyntax) {
|
||||
if legacyObjcTypes.contains(node.baseName.text) {
|
||||
if legacyObjcTypes.contains(node.baseName.text),
|
||||
!configuration.allowedTypes.contains(node.baseName.text) {
|
||||
violations.append(node.baseName.positionAfterSkippingLeadingTrivia)
|
||||
}
|
||||
}
|
||||
|
||||
override func visitPost(_ node: MemberTypeSyntax) {
|
||||
guard node.baseType.as(IdentifierTypeSyntax.self)?.typeName == "Foundation",
|
||||
legacyObjcTypes.contains(node.name.text)
|
||||
legacyObjcTypes.contains(node.name.text),
|
||||
!configuration.allowedTypes.contains(node.name.text)
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import SwiftLintCore
|
||||
|
||||
@AutoConfigParser
|
||||
struct LegacyObjcTypeConfiguration: SeverityBasedRuleConfiguration {
|
||||
typealias Parent = LegacyObjcTypeRule
|
||||
|
||||
@ConfigurationElement(key: "severity")
|
||||
private(set) var severityConfiguration = SeverityConfiguration<Parent>.warning
|
||||
@ConfigurationElement(key: "allowed_types")
|
||||
private(set) var allowedTypes: Set<String> = []
|
||||
}
|
||||
@@ -484,6 +484,7 @@ legacy_nsgeometry_functions:
|
||||
opt-in: false
|
||||
legacy_objc_type:
|
||||
severity: warning
|
||||
allowed_types: []
|
||||
meta:
|
||||
opt-in: true
|
||||
legacy_random:
|
||||
|
||||
Reference in New Issue
Block a user