-
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
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharacterRule.swift
Outdated
Show resolved
Hide resolved
Tests/IntegrationTests/Resources/default_rule_configurations.yml
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Lint/InvisibleCharacterRule.swift
Outdated
Show resolved
Hide resolved
Fine for me to skip the description or have it optional. |
I've added the ability to configure additional characters. I thought about it for a long time and initially decided not to allow disabling the validation of default characters. The hardest part was coming up with names for the parameter and variables 😅. But I'm still having doubts. Please review ) |
9c58429 to
c336b5f
Compare
…olating characters
c336b5f to
01b29ad
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.
This looks very good now! Thank you for all the updates.
I posted a suggestion for the option's name. Let me know what you think about it.
| key: "include_hex_codes", | ||
| postprocessor: { $0.formUnion(defaultCharacters) } | ||
| ) | ||
| private(set) var violatingCharacters = Set<String>() |
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.
If you make UnicodeScalar conform to AcceptableByConfigurationElement, it should end up in a Set<UnicodeScalar> right away. This avoids the conversion in violatingScalars() and the definition of defaultCharacters.
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 a good point, thanks. I've made changes.
| @ConfigurationElement(key: "severity") | ||
| private(set) var severityConfiguration = SeverityConfiguration<Parent>.error | ||
| @ConfigurationElement( | ||
| key: "include_hex_codes", |
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.
We often use additional_ if the values extend a set of defaults. I'm not sure about "hex codes". Would "code points" be clearer? Then we would end up with additional_code_points. Not sure either ...
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 reasonable for me. Done in the last commit.
| description: """ | ||
| Disallows invisible characters like zero-width space (U+200B), \ | ||
| zero-width non-joiner (U+200C), and FEFF formatting character (U+FEFF) \ | ||
| in string literals as they can cause hard-to-debug issues |
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.
| in string literals as they can cause hard-to-debug issues | |
| in string literals as they can cause hard-to-debug issues. |
#6045
Summary
invisible_characterslint rule that detects invisible characters in string literals: