-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicit Enum Raw Value Rule #1788
Conversation
|
||
let locs = substructureElements(of: dictionary, matching: .enumcase) | ||
.flatMap { substructureElements(of: $0, matching: .enumelement) } | ||
.flatMap(enumElementsMissingInitExpr(_:)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can safely remove the (_:)
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very cool! Thanks
private func substructureElements(of dict: [String: SourceKitRepresentable], | ||
matching kind: SwiftDeclarationKind) -> [[String: SourceKitRepresentable]] { | ||
return dict.substructure | ||
.filter { $0.kind.flatMap(SwiftDeclarationKind.init(rawValue:)) == kind } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here for (rawValue:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, I've just made some comments. Thanks for your contribution!
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "explicit_associated_enum_value", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a good name, because when talking about enums, associated values are generally referred to cases with "parameters". Instead, we should use "raw values"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought those were associated types, since jpsim called them associated values. But sure, I don't have any attachments to the name :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
|
||
// Check if it's an associated value enum | ||
guard !dictionary.inheritedTypes.isEmpty else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not enough, because enums can implement protocols, for example:
protocol Foo {}
enum Bar: Foo {
case bar
}
Instead, we should keep a list of possible raw types that support implicit raw values. As far as I know, that means String
and all numeric types (Int
, Float
, Int64
, ...). Types like Character
conform to RawRepresentable
, but they need an explicit value.
} | ||
} | ||
|
||
private func violatingOffsetsForEnum(dictionary: [String: SourceKitRepresentable], file: File) -> [Int] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't actually use the file
parameter here
I guess we should add |
After reading more on |
Codecov Report
@@ Coverage Diff @@
## master #1788 +/- ##
==========================================
+ Coverage 88.17% 88.21% +0.04%
==========================================
Files 223 224 +1
Lines 11027 11068 +41
==========================================
+ Hits 9723 9764 +41
Misses 1304 1304
Continue to review full report at Codecov.
|
Int.self, Int8.self, Int16.self, Int32.self, Int64.self, | ||
UInt.self, UInt8.self, UInt16.self, UInt32.self, UInt64.self, | ||
Double.self, Float.self, Float80.self, Decimal.self, NSNumber.self, | ||
NSDecimalNumber.self, "NSInteger", String.self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL that you can use NSNumber
and other types as raw values 😅
I don't think so, because to add an explicit value, you still need to "inherit" from a raw value type. |
|
||
private func violatingOffsetsForEnum(dictionary: [String: SourceKitRepresentable]) -> [Int] { | ||
|
||
let locs = substructureElements(of: dictionary, matching: .enumcase) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please combine the 3 flatMap
s? I'd also recommend not storing the result in an intermediate variable, since it's unused other than just being returned right away, so doesn't make things any clearer IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can see this logic using a single loop with guard
s and continue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I don't think my suggestion would improve things after all.
Hey, as @marcelofabri suggested, this was a good rule for new contributors to get their hands dirty (assuming I did this with reasonable correctness!).
I feel the PR is self-explanatory, but just to reiterate:
init_expr
Int
enums, andString
enums.Fixes #1778