forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
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 realm#2396 from marcelofabri/static_operator
Add static_operator opt-in rule
- Loading branch information
Showing
7 changed files
with
216 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
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
101 changes: 101 additions & 0 deletions
101
Source/SwiftLintFramework/Rules/Idiomatic/StaticOperatorRule.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,101 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct StaticOperatorRule: ASTRule, ConfigurationProviderRule, OptInRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "static_operator", | ||
name: "Static Operator", | ||
description: "Operators should be declared as static functions, not free functions.", | ||
kind: .idiomatic, | ||
nonTriggeringExamples: [ | ||
""" | ||
class A: Equatable { | ||
static func == (lhs: A, rhs: A) -> Bool { | ||
return false | ||
} | ||
""", | ||
""" | ||
class A<T>: Equatable { | ||
static func == <T>(lhs: A<T>, rhs: A<T>) -> Bool { | ||
return false | ||
} | ||
""", | ||
""" | ||
public extension Array where Element == Rule { | ||
static func == (lhs: Array, rhs: Array) -> Bool { | ||
if lhs.count != rhs.count { return false } | ||
return !zip(lhs, rhs).contains { !$0.0.isEqualTo($0.1) } | ||
} | ||
} | ||
""", | ||
""" | ||
private extension Optional where Wrapped: Comparable { | ||
static func < (lhs: Optional, rhs: Optional) -> Bool { | ||
switch (lhs, rhs) { | ||
case let (lhs?, rhs?): | ||
return lhs < rhs | ||
case (nil, _?): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} | ||
""" | ||
], | ||
triggeringExamples: [ | ||
""" | ||
↓func == (lhs: A, rhs: A) -> Bool { | ||
return false | ||
} | ||
""", | ||
""" | ||
↓func == <T>(lhs: A<T>, rhs: A<T>) -> Bool { | ||
return false | ||
} | ||
""", | ||
""" | ||
↓func == (lhs: [Rule], rhs: [Rule]) -> Bool { | ||
if lhs.count != rhs.count { return false } | ||
return !zip(lhs, rhs).contains { !$0.0.isEqualTo($0.1) } | ||
} | ||
""", | ||
""" | ||
private ↓func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool { | ||
switch (lhs, rhs) { | ||
case let (lhs?, rhs?): | ||
return lhs < rhs | ||
case (nil, _?): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
""" | ||
] | ||
) | ||
|
||
public func validate(file: File, kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard kind == .functionFree, | ||
let offset = dictionary.offset, | ||
let name = dictionary.name?.split(separator: "(").first.flatMap(String.init) else { | ||
return [] | ||
} | ||
|
||
let characterSet = CharacterSet(charactersIn: name) | ||
guard characterSet.isDisjoint(with: .alphanumerics) else { | ||
return [] | ||
} | ||
|
||
return [ | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset)) | ||
] | ||
} | ||
} |
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