-
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
Added disable rule test to autocorrect #766
Added disable rule test to autocorrect #766
Conversation
Current coverage is 88.80% (diff: 87.50%)@@ master #766 diff @@
==========================================
Files 81 81
Lines 2767 2768 +1
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 2446 2458 +12
+ Misses 321 310 -11
Partials 0 0
|
|
||
let regularExpression = regex(pattern) | ||
let description = self.dynamicType.description | ||
var corrections = [Correction]() | ||
var contents = file.contents | ||
for range in matches.reverse() { | ||
let location = Location(file: file, characterOffset: range.location) | ||
let region = fileregions.filter {$0.contains(location)}.first | ||
if region?.isRuleDisabled(self) == true { |
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 be:
if region?.isRuleDisabled(self) == false {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: "$1: $2")
corrections.append(Correction(ruleDescription: description, location: location))
}
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.
region?.isRuleDisabled(self)
returns false when region
is nil
if region?.isRuleDisabled(self) == true { // returns false when region is nil
continue
} else {...}
plus I was trying to stay away from double negatives ;)
https://twitter.com/NachoSoto/status/762358223122927616
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.
Good call. This then?
if let region = fileregions.filter {$0.contains(location)}.first
where !region.isRuleDisabled(self) {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: "$1: $2")
corrections.append(Correction(ruleDescription: description, location: location))
}
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.
tests fail. when region is nil it doesn't fire but we need it to fire sometimes when there are no regions.
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.
Alright, keep it as-is then
Rather than duplicate this code in a bunch of different rule implementations, this should be made generic. |
Yeah that was my initial implementation but it wasn't granular . Do you think we should go that route? it would be nice to have a default implementation that the correctable protocol would have. |
Yes, we should reuse the code for this as much as possible. |
c93689c
to
46a6b09
Compare
This needs a rebase. |
46a6b09
to
2c05b98
Compare
This is ready for now. I'll refactor all the correctable rules to pull this logic into the protocol in another PR. |
@masters3d I don't see how it's important to land this just for a subset of rules, only to do it more generically later. Can we just do it right the first time? |
I guess the important part is the added new test that makes sure new correctable rules do not omit disable regions checks. The rules I changed were the ones failing that test. I didn't touch other correctable rules that passed the test. I imagine if we provide a general facility for this checking that we would want to refactor all the correctable rules. |
I'd rather do this be done in a single swoop, personally. |
Sounds good. Currently all public func validateFile(file: File) -> [StyleViolation] { for (eachLastLine, eachSectionCount) in linesSections {
// Skips violation for areas where the rule is disabled
let region = file.regions().filter {
$0.contains(Location(file: file.path, line: eachLastLine.index, character: 0))
}.first
if region?.isRuleDisabled(self) == true {
continue
}
.... public func correctFile(file: File) -> [Correction] { for currentLine in file.lines {
// Doesnt correct lines where rule is disabled
let region = fileRegions.filter {
$0.contains(Location(file: file.path, line: currentLine.index, character: 0))
}.first
if region?.isRuleDisabled(self) == true {
correctedLines.append(currentLine.content)
continue
}
.... |
bbb50e5
to
57fdce4
Compare
This should be ready now. I was complicating the issue. I found a method on File that does what I needed. |
@@ -12,6 +12,10 @@ | |||
|
|||
* Adds `allow_private_set` configuration for the `private_outlet` rule. | |||
[Rohan Dhaimade](https://github.com/HaloZero) | |||
|
|||
* Fix locally disabled comma rule autocorrect. |
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 don't think this message is accurate anymore?
57fdce4
to
e58c0b9
Compare
@@ -12,6 +12,10 @@ | |||
|
|||
* Adds `allow_private_set` configuration for the `private_outlet` rule. | |||
[Rohan Dhaimade](https://github.com/HaloZero) | |||
|
|||
* Added test to correctable rules to check `swiftlint-disable` before auto-correcting file. |
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.
the command is swiftlint:disable
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.
Also, I'd call this a bug fix and the fact that you added tests is an implementation detail. The entry should read something like this IMO:
Correctable rules no longer apply corrections if the rule is locally disabled.
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.
sounds good. I think I've now mastered the git command line :)
faa8652
to
a080617
Compare
a080617
to
096951a
Compare
.filter { $0.1.first == .Identifier } | ||
.map { ($0.0, pattern, template) } | ||
}).flatten().sort { $0.0.location > $1.0.location } // reversed | ||
|
||
if matches.isEmpty { return []} |
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.
Closure spacing 😉 #770
Thanks for your continued work on this @masters3d! Nearly there. |
08c5397
to
ed52ab3
Compare
ed52ab3
to
48a085c
Compare
Looks good to me. |
none that I am aware |
@masters3d Thanks for doing this! 🙏 |
🎉 |
#748
closes #601