-
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
variable_name rule should extend to all identifiers #954
Conversation
Current coverage is 81.91% (diff: 74.31%)@@ master #954 diff @@
==========================================
Files 168 168
Lines 8344 8388 +44
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 6835 6871 +36
- Misses 1509 1517 +8
Partials 0 0
|
Can you please rebase this so we can confirm it builds and passes tests on Linux? |
@@ -397,6 +397,7 @@ extension RulesTests { | |||
// ("testForceUnwrapping", testForceUnwrapping), | |||
("testFunctionBodyLength", testFunctionBodyLength), | |||
("testFunctionParameterCountRule", testFunctionParameterCountRule), | |||
// ("testIdentifierName", testIdentifierName), |
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've commented it because testVariableName
was commented too
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.
That's fine for now. When we merge this, we should add this test to #966.
CHANGELOG.md
Outdated
@@ -7,6 +7,11 @@ | |||
Guidelines but will shortly. | |||
[JP Simard](https://github.com/jpsim), | |||
[Norio Nomura](https://github.com/norio-nomura) | |||
|
|||
* `variable_name` rule is now `identifier_rule` as it validates other |
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.
identifier_name
. Also, it'd be nice to keep VariableNameRule
around, logging a warning to the console if used in comment commands or the config file, proxying its implementation to IdentifierNameRule
. Would lead to a nicer deprecation path.
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.
But wouldn't it make the rule run twice?
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.
Might have to think this through a bit more. Maybe having rule ID aliases is the right move there.
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've created #973 to track this
I've changed this to WIP as we need to figure out the deprecation part. However, I have no clue why the tests are failing on Linux 😭 |
I was able to reproduce the crash on a local docker instance. It can be reduced to this: let name = "matchPattern(_:excludingSyntaxKinds:excludingPattern:exclusionMapping:)"
let nameCharacterSet = CharacterSet(charactersIn: name) Stacktrace:
There are already two tickets on Swift bug tracking: SR-3215 and SR-3282. It seems that the crash happens with any string with more then 63 characters. |
Can you just not include those examples in the rule description when compiling on Linux? |
Not really, because it's failing to lint a real file in |
A note to future self and all others: you can run
|
// CharacterSet+LinuxHack.swift | ||
// SwiftLint | ||
// | ||
// Created by Marcelo Fabri on 17/12/16. |
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.
mm/dd/yy 🔥 🇺🇸
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.
🤧
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 know this is still WIP (until we can handle the deprecation a bit better), but I have some comments in the meantime.
// workaround for https://bugs.swift.org/browse/SR-3215 and | ||
// https://bugs.swift.org/browse/SR-3282 | ||
let sets = split(string, 63).map { CharacterSet(charactersIn: $0) } | ||
let finalSet = sets.reduce(CharacterSet()) { (acc, set) -> CharacterSet in |
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 a minor suggestion, but:
-let finalSet = sets.reduce(CharacterSet()) { (acc, set) -> CharacterSet in
- acc.union(set)
-}
-
-self = finalSet
+self = sets.reduce(CharacterSet()) { final, set in final.union(set) }
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.
👍
internal func validateVariableName(_ dictionary: [String: SourceKitRepresentable], | ||
kind: SwiftDeclarationKind) -> (name: String, offset: Int)? { | ||
internal func validateIdentifierName(_ dictionary: [String: SourceKitRepresentable], | ||
kind: SwiftDeclarationKind) -> (name: String, offset: 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.
indentation
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.
that was probably intentional to avoid breaking the return 😅
either way, I've moved this to the rule itself, since IMO it's not related to File
in any obvious way.
private func nameIsViolatingCase(_ name: String) -> Bool { | ||
let secondIndex = name.characters.index(after: name.startIndex) | ||
let firstCharacter = name.substring(to: secondIndex) | ||
if firstCharacter.isUppercase() { |
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 might benefit from using guard
s:
-if firstCharacter.isUppercase() {
- if name.characters.count > 1 {
- let range = secondIndex..<name.characters.index(after: secondIndex)
- let secondCharacter = name.substring(with: range)
- return secondCharacter.isLowercase()
- }
- return true
-}
-return false
+guard firstCharacter.isUppercase() else { return false }
+guard name.characters.count > 1 else { return true }
+let range = secondIndex..<name.characters.index(after: secondIndex)
+let secondCharacter = name.substring(with: range)
+return secondCharacter.isLowercase()
|
||
private func isOperator(name: String) -> Bool { | ||
let operators = ["/", "=", "-", "+", "!", "*", "|", "^", "~", "?", ".", "%", "<", ">", "&"] | ||
return !operators.filter { name.hasPrefix($0) }.isEmpty |
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.
Prefer passing function references over simple closures:
-return !operators.filter { name.hasPrefix($0) }.isEmpty
+return !operators.filter(name.hasPrefix).isEmpty
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.
Nice catch! 💯
Generated by 🚫 danger |
private func validateName(_ dictionary: [String: SourceKitRepresentable], | ||
kind: SwiftDeclarationKind) -> (name: String, offset: Int)? { | ||
let kinds = SwiftDeclarationKind.variableKinds() + | ||
SwiftDeclarationKind.functionKinds() + [.enumelement] |
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 probably should check the Swift version to add or not .enumelement
Maybe this rule shouldn't apply to // MARK: - migration support
extension Request {
@available(*, unavailable, renamed: "editorOpen(file:)")
public static func EditorOpen(_: File) -> Request { fatalError() }
...
} |
While I agree that it would be useful, I'm not convinced that it's worth the extra complexity. There's no way to get the In both Realm and SourceKitten it'd be pretty easy to just ignore the rule in these declarations since they're isolated inside extensions. |
@marcelofabri what's next for this? Is this just blocked on me reviewing? Are there certain high-level points I should consider when reviewing this? |
If you're cool with not verifying the I don't remember any important points, this is basically renaming the rule and adding more types to it. Also, some violations are skipped for functions. And this also validates that enum cases are lowercase on Swift 3 (I think there was an issue about it somewhere). |
CHANGELOG.md
Outdated
@@ -939,7 +939,10 @@ This release has seen a phenomenal uptake in community contributions! | |||
|
|||
##### Enhancements | |||
|
|||
* None. | |||
* `variable_name` rule is now `identifier_name` as it validates other |
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 now in the wrong section
* master: (44 commits) make valid_docs rule opt-in update LineLengthConfiguration.consoleDescription after #1264 small refactoring after #1264 fix for_where violation in CompilerProtocolInitRule.swift Add for_where rule explicitly set podspec dependency versions Add changelog entry Fix existing violations Navigate substructure even for different kinds Small style changes Modified so that kinds(forByteOffset:) does not require a separate filter() setup of the results Changes from PR feedback. Long comments following code in a line will now trigger, configuration will now fail if invalud value types are set for options Fix false positive on large_tuple when using generics inside a tuple fix wording in changelog disable docs rules in Swift 2.3 and later Fix deadlock when stderr fills up OS buffer small Danger/oss-check improvements Fix links Fix cleanup Always use Xcode reporter on oss-check ...
This definitely has a sizable impact on lint times, but that's to be expected. Hopefully with caching, no one feels the pain from this too much... On my MacBook Pro: Linting Alamofire with this PR took 2.01s vs 1.98s on master (1% slower) |
Fixes #663
I haven't applied all the rules to functions because I didn't think it was a good idea as their limits probably should be different.
This is a breaking change, I'm not sure how we should handle this. Perhaps we should keep a
variable_name
rule to avoid breaking configs?