Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [switch-exhaustiveness-check] enum members with new line or single quotes are not being fixed correctly#7806
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…ine or single quotes are not being fixed correctlyClosestypescript-eslint#7768
Thanks for the PR,@StyleShit! 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 onhttps://opencollective.com/typescript-eslint. |
netlifybot commentedOct 20, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
✅ Deploy Preview fortypescript-eslint ready!
To edit notification comments on pull requests, go to yourNetlify site configuration. |
Uh oh!
There was an error while loading.Please reload this page.
case Enum.a: { throw new Error('Not implemented yet: Enum.a case') } | ||
case Enum[\`key-with | ||
new-line\`]: { throw new Error('Not implemented yet: Enum[\`key-with\\n\\n new-line\`] case') } |
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.
wasn't sure about this. should we throw an error with actual new lines or\n
is fine?
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.
Yeah good question. It's such a rare case, I don't think it matters too much. But in general having error messages be on one line is easier to read.
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.
should we just strip out any spaces, tabs, and new lines?
OK... it might look weird in some cases... IDK
key=`key-with new-line`error="not implemented: 'key-withnew-line'";
anyway, as you said, it's a rare case that probably isn't worth the effort
<!--tabs--> | ||
### ❌ Incorrect | ||
StyleShitOct 20, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
it's probably not displayed correctly, would be happy to get some help here regarding how we should display this (maybe add a subtitle?):
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.
<!-- /tabs -->
is what you're looking for.
I'd also request a brief sentence before each of the examples explaining what they're for. It's kind of hard to piece together just from the code.
Vague starting proposal (just off the top of my head, not attached to this):
When the
switch
doesn't have exhaustive cases, either filling them all out or adding adefault
will correct the rule's complaint.
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.
yup
nx-cloudbot commentedOct 20, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
@@ -1,15 +1,15 @@ | |||
--- | |||
description: 'Require switch-case statements to be exhaustive with union type.' | |||
description: 'Require switch-case statements to be exhaustive with union type or enum.' |
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.
[Proofreading] Existing typo with the singular/plural, heh:
description: 'Require switch-case statements to be exhaustive with uniontype or enum.' | |
description: 'Require switch-case statements to be exhaustive with uniontypes and enums.' |
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.
Done
<!--tabs--> | ||
### ❌ Incorrect | ||
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.
<!-- /tabs -->
is what you're looking for.
I'd also request a brief sentence before each of the examples explaining what they're for. It's kind of hard to piece together just from the code.
Vague starting proposal (just off the top of my head, not attached to this):
When the
switch
doesn't have exhaustive cases, either filling them all out or adding adefault
will correct the rule's complaint.
Cherry, | ||
} | ||
const fruit = Fruit.Cherry as Fruit; |
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.
[Style] This is a little closer to the intent:
const fruit = Fruit.Cherry as Fruit; | |
declareconst fruit: Fruit; |
(this and elsewhere)
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.
yup, I actually wanted to do that, but followed the existing code
changed :)
case Enum.a: { throw new Error('Not implemented yet: Enum.a case') } | ||
case Enum[\`key-with | ||
new-line\`]: { throw new Error('Not implemented yet: Enum[\`key-with\\n\\n new-line\`] case') } |
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.
Yeah good question. It's such a rare case, I don't think it matters too much. But in general having error messages be on one line is easier to read.
} | ||
const errorMessage = `Not implemented yet: ${caseTest} case`; | ||
// escape single quotes and newlines, so that the error message is a readable and valid code. |
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.
// escape single quotes and newlines, so that the error message isareadable and valid code. | |
// escape single quotes and newlines, so that the error message is readable and valid code. |
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'm gonna learn English here more than ASTs
@@ -17,7 +17,7 @@ export default createRule({ | |||
type: 'suggestion', | |||
docs: { | |||
description: | |||
'Require switch-case statements to be exhaustive with union type', | |||
'Require switch-case statements to be exhaustive with union type or enum', |
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 existing typo-ish)
'Require switch-case statements to be exhaustive with uniontype orenum', | |
'Require switch-case statements to be exhaustive with uniontypes orenums', |
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.
Done
StyleShit commentedOct 23, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
…-exhaustiveness-check-new-line-and-single-quotes
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.
Suggested test case:
enumE{"'a' `b` \"c\""=1}declareconsta:Enum;switch(a){}
const requiresBackticks = missingBranchName?.match(/[\r\n]/g); | ||
caseTest = requiresBackticks | ||
? `${symbolName}[\`${missingBranchName}\`]` | ||
: `${symbolName}['${missingBranchName}']`; |
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 don't know if it's discussed yet, but if we are escaping characters in the error message anyway, I would prefer text here to also be escaped instead of wrapping in backticks. Otherwise, you would also need to deal with`
in the text. (Do we even deal with'
correctly?)
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.
OK, so your suggested case breaks the code (nice catch!)
But I'm not sure that I understand what you mean in this comment, can you maybe show some pseudo code and/or cases?
Josh-CenaOct 23, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
It means we simply outputcase '${escapedCaseTest}': { throw new Error('Not implemented yet: "${escapedCaseTest}" case') }
. One consistent string literal to be used in both places. Then, you would only need to take care of'
,\n
, and\r
in your escaping logic. By the way, if you want to be pedantic, you also need to take care of\u2028
and\u2029
, which are alsoline terminators.
In terms of cases, it means we output:
switch(a){case'a\nb':{thrownewError('Not implemented yet: "a\nb" case')}}
Instead of:
switch(a){case`ab`:{thrownewError('Not implemented yet: "a\nb" case')}}
I'm sure no one writes code like the latter, especially with deeper indentations.
StyleShitOct 23, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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'm not sure that we can use the sameescapedCaseTest
variable, because we still need to re-escape It before using it in the error, right? Or am I missing something?
StyleShitOct 23, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Have a look atthis one
Is that what you mean?
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 good to me :)
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.
Closes#7768