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.
- Loading branch information
1 parent
58d374f
commit 1864145
Showing
7 changed files
with
251 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
129 changes: 129 additions & 0 deletions
129
Source/SwiftLintFramework/Rules/Lint/RawValueForCamelCasedCodableEnumRule.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,129 @@ | ||
import SourceKittenFramework | ||
|
||
public struct RawValueForCamelCasedCodableEnumRule: ASTRule, OptInRule, ConfigurationProviderRule, | ||
AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "raw_value_for_camel_cased_codable_enum", | ||
name: "Raw Value For Camel Cased Codable Enum", | ||
description: "Camel cased cases of Codable String enums should have raw value.", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
""" | ||
enum Numbers: Codable { | ||
case int(Int) | ||
case short(Int16) | ||
} | ||
""", | ||
""" | ||
enum Numbers: Int, Codable { | ||
case one = 1 | ||
case two = 2 | ||
} | ||
""", | ||
""" | ||
enum Numbers: Double, Codable { | ||
case one = 1.1 | ||
case two = 2.2 | ||
} | ||
""", | ||
""" | ||
enum Numbers: String, Codable { | ||
case one = "one" | ||
case two = "two" | ||
} | ||
""", | ||
""" | ||
enum Status: String { | ||
case ok | ||
case notAcceptable | ||
case maybeAcceptable = "maybe_acceptable" | ||
} | ||
""", | ||
""" | ||
enum Status: Int, Codable { | ||
case ok | ||
case notAcceptable | ||
case maybeAcceptable = -1 | ||
} | ||
""" | ||
], | ||
triggeringExamples: [ | ||
""" | ||
enum Status: String, Codable { | ||
case ok | ||
case ↓notAcceptable | ||
case maybeAcceptable = "maybe_acceptable" | ||
} | ||
""", | ||
""" | ||
enum Status: String, Decodable { | ||
case ok | ||
case ↓notAcceptable | ||
case maybeAcceptable = "maybe_acceptable" | ||
} | ||
""", | ||
""" | ||
enum Status: String, Encodable { | ||
case ok | ||
case ↓notAcceptable | ||
case maybeAcceptable = "maybe_acceptable" | ||
} | ||
""", | ||
""" | ||
enum Status: String, Codable { | ||
case ok | ||
case ↓notAcceptable | ||
case maybeAcceptable = "maybe_acceptable" | ||
} | ||
""" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard kind == .enum else { return [] } | ||
|
||
let codableTypesSet = Set(["Codable", "Decodable", "Encodable"]) | ||
let enumInheritedTypesSet = Set(dictionary.inheritedTypes) | ||
|
||
guard | ||
enumInheritedTypesSet.contains("String"), | ||
!enumInheritedTypesSet.isDisjoint(with: codableTypesSet) | ||
else { return [] } | ||
|
||
let violations = violatingOffsetsForEnum(dictionary: dictionary) | ||
return violations.map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: $0)) | ||
} | ||
} | ||
|
||
private func violatingOffsetsForEnum(dictionary: [String: SourceKitRepresentable]) -> [Int] { | ||
let locs = substructureElements(of: dictionary, matching: .enumcase) | ||
.compactMap { substructureElements(of: $0, matching: .enumelement) } | ||
.flatMap(camelCasedEnumCasesMissingRawValue) | ||
.compactMap { $0.offset } | ||
|
||
return locs | ||
} | ||
|
||
private func substructureElements(of dict: [String: SourceKitRepresentable], | ||
matching kind: SwiftDeclarationKind) -> [[String: SourceKitRepresentable]] { | ||
return dict.substructure.filter { $0.kind.flatMap(SwiftDeclarationKind.init) == kind } | ||
} | ||
|
||
private func camelCasedEnumCasesMissingRawValue( | ||
_ enumElements: [[String: SourceKitRepresentable]]) -> [[String: SourceKitRepresentable]] { | ||
return enumElements | ||
.filter { substructure in | ||
guard let name = substructure.name, name != name.lowercased() else { return false } | ||
return !substructure.elements.contains { $0.kind == "source.lang.swift.structure.elem.init_expr" } | ||
} | ||
} | ||
} |
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