-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add new invisible_characters rule #6424
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
base: main
Are you sure you want to change the base?
Conversation
01a6b0a to
21d78ba
Compare
Generated by 🚫 Danger |
3561c44 to
a89ae33
Compare
|
@SimplyDanny please review. I don't check NULL control character: \u0000 because it gets lost when copypasting so it's almost impossible to have it in string literals by accident. |
9382226 to
ceccd1d
Compare
SimplyDanny
left a comment
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.
Thanks for the contribution!
I wonder if this rule should catch even more characters of this kind, some maybe optionally. We could check what other linters do.
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharactersRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharactersRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharacterRule.swift
Outdated
Show resolved
Hide resolved
3a37574 to
708d32e
Compare
|
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.
Would it be wrong to have the rule auto-correct the findings by just removing the violating characters?
We could also think about making the rule configurable, so that people can add their own characters with descriptions. That's what the "tool" you cited allows as well.
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharacterRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharacterRule.swift
Outdated
Show resolved
Hide resolved
708d32e to
09b260e
Compare
09b260e to
eef6f03
Compare
The second commit makes the rule correctable. I've also made some performance optimizations. |
eef6f03 to
60290ce
Compare
| ReasonedRuleViolation( | ||
| position: segment.content.positionAfterSkippingLeadingTrivia | ||
| .advanced(by: utf8Offset), | ||
| reason: "String literal should not contain invisible character \(characterName)" |
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.
Corrections could a bit easier to achieve. You can add a correction parameter here that replaces just ranges in the original code. It's simple but might be sufficient. Rewriters tend to turn quite complex very fast.
| severity: error | ||
| meta: | ||
| opt-in: false | ||
| correctable: false |
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.
| correctable: false | |
| correctable: true |
| "\u{FEFF}": "U+FEFF (zero-width no-break space)", | ||
| ] | ||
|
|
||
| private static let invisibleCharacterSet: Set<Unicode.Scalar> = Set(invisibleCharacters.keys) |
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.
Dictionaries are like sets internally. So I guess that checking whether the dictionary contains a key is as fast as using this separate set.
Fine for me to skip the description or have it optional. |
#6045
Summary
invisible_characterslint rule that detects invisible characters in string literals: