-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [switch-exhaustiveness-check] handle special characters in enum keys #2207
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
Conversation
Thanks for the PR, @karishnu! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
Codecov Report
@@ Coverage Diff @@
## master #2207 +/- ##
==========================================
- Coverage 93.39% 93.12% -0.28%
==========================================
Files 173 283 +110
Lines 7871 9044 +1173
Branches 2247 2477 +230
==========================================
+ Hits 7351 8422 +1071
- Misses 247 301 +54
- Partials 273 321 +48
|
…notation when required by rule switch-exhaustiveness-check
…here enum key has special characters for rule switch-exhaustiveness-check
@bradzacher Are any changes required before this can be merged? |
This PR is in the queue of PRs to reviewed, and will be reviewed when we are able. |
): TSESLint.RuleFix | null { | ||
const identifierRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/; |
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 problem we've got is that a-zA-Z
doesn't include all of the "letter-like" characters that are actually allowed in the spec (chinese/japanese/korean/cyrillic/etc characters, accented characters), etc. So for a large number of codebases this regex will actually cause a false match. A false match isn't the end of the world, but it's going to be annoying for non-english speakers.
This is the actual regex of allowed characters within a variable name: https://github.com/estools/esutils/blob/a825f91fd1d1e3a9fff84227cb06c011d8a0b9e8/lib/code.js#L39-L44
It's a mess.
TypeScript needs to parse identifiers itself, and thankfully they export two functions that we can use here: isIdentifierStart
and isIdentifierPart
.
You can use this function instead to determine if the name requires quoting:
const compilerOptions = service.program.getCompilerOptions();
function requiresQuoting(name: string): boolean {
if (name.length === 0) {
return true;
}
if (!ts.isIdentifierStart(name.charCodeAt(0), compilerOptions.target)) {
return true;
}
for (let i = 1; i < name.length; i += 1) {
if (!ts.isIdentifierPart(name.charCodeAt(i), compilerOptions.target)) {
return true;
}
}
return 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.
Thank you! Will make the changes as soon as I can find some time.
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.
LGTM.
Thanks for your contribution!
Fixes #2181