Skip to content
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

fix(eslint-plugin): [switch-exhaustiveness-check] handle special characters in enum keys #2207

merged 4 commits into from Jul 10, 2020


Copy link

@karishnu karishnu commented Jun 11, 2020

Fixes #2181

Copy link

@typescript-eslint typescript-eslint bot commented Jun 11, 2020

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 As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

Copy link

@codecov codecov bot commented Jun 11, 2020

Codecov Report

Merging #2207 into master will decrease coverage by 0.27%.
The diff coverage is 100.00%.

@@            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     
Flag Coverage Δ
#unittest 93.12% <100.00%> (-0.28%) ⬇️
Impacted Files Coverage Δ
...nt-plugin/src/rules/switch-exhaustiveness-check.ts 98.27% <100.00%> (+0.60%) ⬆️
packages/typescript-estree/src/simple-traverse.ts 75.00% <0.00%> (-20.00%) ⬇️
...-internal/src/rules/no-typescript-estree-import.ts 87.50% <0.00%> (-12.50%) ⬇️
packages/parser/src/scope/scopes.ts 100.00% <0.00%> (ø)
packages/eslint-plugin/src/configs/all.ts 100.00% <0.00%> (ø)
packages/eslint-plugin/src/configs/base.ts 100.00% <0.00%> (ø)
packages/parser/src/scope/scope-manager.ts 100.00% <0.00%> (ø)
packages/eslint-plugin/src/rules/no-namespace.ts 100.00% <0.00%> (ø)
packages/eslint-plugin/src/configs/recommended.ts 100.00% <0.00%> (ø)
packages/typescript-estree/src/ts-estree/index.ts 100.00% <0.00%> (ø)
... and 128 more

@bradzacher bradzacher added the bug label Jun 11, 2020
@bradzacher bradzacher changed the title feat(eslint-plugin): [switch-exhaustiveness-check] Handle special characters in enum keys fix(eslint-plugin): [switch-exhaustiveness-check] handle special characters in enum keys Jun 11, 2020
karishnu added 2 commits Jun 12, 2020
…notation when required by rule switch-exhaustiveness-check
…here enum key has special characters for rule switch-exhaustiveness-check
Copy link
Contributor Author

@karishnu karishnu commented Jun 16, 2020

@bradzacher Are any changes required before this can be merged?

Copy link

@bradzacher bradzacher commented Jun 16, 2020

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_$]*$/;
Copy link

@bradzacher bradzacher Jun 19, 2020

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:

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), {
    return true;

  for (let i = 1; i < name.length; i += 1) {
    if (!ts.isIdentifierPart(name.charCodeAt(i), {
      return true;

  return false;

Copy link
Contributor Author

@karishnu karishnu Jun 21, 2020

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.

@bradzacher bradzacher added the awaiting response label Jun 19, 2020
@bradzacher bradzacher removed the awaiting response label Jul 8, 2020
Copy link

@bradzacher bradzacher left a comment

Thanks for your contribution!

@bradzacher bradzacher merged commit 98ab010 into typescript-eslint:master Jul 10, 2020
10 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

Successfully merging this pull request may close these issues.

2 participants