-
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
Add configuration for trailing_whitespace to ignore comments #854
Conversation
Current coverage is 85.67% (diff: 95.74%)@@ master #854 diff @@
==========================================
Files 108 108
Lines 4690 4767 +77
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 4010 4084 +74
- Misses 680 683 +3
Partials 0 0
|
I think you should use class Example {
// empty but with trailing spaces
} You probably can use something like this to check if the line only contains comments instead. |
Thanks for the suggestion @marcelofabri! I managed to change my implementation to use In your example, checking for |
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.
Thank you for doing this! 🙏
It looks there is some performance regression that may be resolvable.
Could you please check my feedbacks?
I also found false negative pattern with block comment:
let nsError = NSError(domain: NSCocoaErrorDomain,
code: NSFileNoSuchFileError,
userInfo: [
AnyHashable(NSFilePathErrorKey) : "/dev/null",
/* this line */ AnyHashable(NSStringEncodingErrorKey): /*ASCII=*/1,
])
) | ||
|
||
public func validateFile(file: File) -> [StyleViolation] { | ||
let filteredLines = file.lines.filter { | ||
$0.content.hasTrailingWhitespace() && | ||
let commentKinds = Set(SyntaxKind.commentKinds()) |
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.
commentKinds
is not need to be Set
.
$0.content.hasTrailingWhitespace() && | ||
let commentKinds = Set(SyntaxKind.commentKinds()) | ||
let lineSyntaxKinds = file.syntaxKindsByLines[$0.index] | ||
let lineContainsComment = !lineSyntaxKinds.filter { commentKinds.contains($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.
lineContainsComment
should be calculated only if $0.content.hasTrailingWhitespace()
and configuration.ignoresComments
are true
.
Please pay attention to reduce calculation. 🕵🏻
Changes made to address the feedback 🙂 Thank you! |
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 found false negative pattern that block comment has trailing whitespace:
let string1 = "string1" /* block comment has trailing whitespace */
But, maybe that won't be problems. 😃
So, it looks mostly good to me other than my comments.
Could you please check them?
return false | ||
} | ||
|
||
return $0.content.hasTrailingWhitespace() && |
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.
On non violating case, $0.content.hasTrailingWhitespace()
is evaluated twice. The closure should return false
first if $0.content.hasTrailingWhitespace()
returns false
.
@@ -46,6 +55,14 @@ public struct TrailingWhitespaceRule: CorrectableRule, ConfigurationProviderRule | |||
var corrections = [Correction]() | |||
let fileRegions = file.regions() | |||
for line in file.lines { | |||
let commentKinds = SyntaxKind.commentKinds() | |||
if line.content.hasTrailingWhitespace() && configuration.ignoresComments, |
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 with my comment on validateFile(_:)
.
This loop does not need to evaluate following operations if line.content.hasTrailingWhitespace()
returns false
.
if !line.content.hasTrailingWhitespace() {
correctedLines.append(line.content)
continue
}
Changes made. Thanks for your comments and patience, @norio-nomura! I can't think of a way to fix that without manually checking if the comment contains |
We can continue with filing issue of the false negative and another PR. 😉 |
|
Thanks! 🙏 |
Awesome! 🎉 |
Fixes #576
Rule
trailing_whitespace
can now be configured to ignore comments usingignores_comments
, its default value istrue