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] enum members with new line or single quotes are not being fixed correctly #7806

Conversation

StyleShit
Copy link
Contributor

Closes #7768

…ine or single quotes are not being fixed correctly

Closes typescript-eslint#7768
@typescript-eslint
Copy link
Contributor

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 on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Oct 20, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 82d5b37
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6536cd5a4249380008cbf566
😎 Deploy Preview https://deploy-preview-7806--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 90 (🔴 down 9 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

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') }
Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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

Comment on lines +104 to +107
<!--tabs-->

### ❌ Incorrect

Copy link
Contributor Author

@StyleShit StyleShit Oct 20, 2023

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?):

https://deploy-preview-7806--typescript-eslint.netlify.app/rules/switch-exhaustiveness-check#examples

Copy link
Member

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 a default will correct the rule's complaint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

@StyleShit StyleShit changed the title feat(eslint-plugin): [switch-exhaustiveness-check] members with new line or single quotes are not being fixed correctly feat(eslint-plugin): [switch-exhaustiveness-check] enum members with new line or single quotes are not being fixed correctly Oct 20, 2023
@StyleShit StyleShit changed the title feat(eslint-plugin): [switch-exhaustiveness-check] enum members with new line or single quotes are not being fixed correctly fix(eslint-plugin): [switch-exhaustiveness-check] enum members with new line or single quotes are not being fixed correctly Oct 21, 2023
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, thanks! With the exception of the /tabs bit, this is all just small non-blocking nitpicks from me.

Hammer hammering a nail in wood. Caption: 'nailed it'

@@ -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.'
Copy link
Member

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:

Suggested change
description: 'Require switch-case statements to be exhaustive with union type or enum.'
description: 'Require switch-case statements to be exhaustive with union types and enums.'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +104 to +107
<!--tabs-->

### ❌ Incorrect

Copy link
Member

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 a default will correct the rule's complaint.

Cherry,
}

const fruit = Fruit.Cherry as Fruit;
Copy link
Member

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:

Suggested change
const fruit = Fruit.Cherry as Fruit;
declare const fruit: Fruit;

(this and elsewhere)

Copy link
Contributor Author

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') }
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// escape single quotes and newlines, so that the error message is a readable and valid code.
// escape single quotes and newlines, so that the error message is readable and valid code.

Copy link
Contributor Author

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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same existing typo-ish)

Suggested change
'Require switch-case statements to be exhaustive with union type or enum',
'Require switch-case statements to be exhaustive with union types or enums',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Oct 22, 2023
@StyleShit
Copy link
Contributor Author

StyleShit commented Oct 23, 2023

Looks great to me, thanks! With the exception of the /tabs bit, this is all just small non-blocking nitpicks from me.

Hammer hammering a nail in wood. Caption: 'nailed it'

Nits are blockers for me 😄

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Oct 23, 2023
Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested test case:

enum E {
  "'a' `b` \"c\"" = 1
}

declare const a: Enum;

switch (a) {}

Comment on lines 75 to 79
const requiresBackticks = missingBranchName?.match(/[\r\n]/g);

caseTest = requiresBackticks
? `${symbolName}[\`${missingBranchName}\`]`
: `${symbolName}['${missingBranchName}']`;
Copy link
Member

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?)

Copy link
Contributor Author

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?

Copy link
Member

@Josh-Cena Josh-Cena Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means we simply output case '${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 also line terminators.

In terms of cases, it means we output:

switch (a) {
  case 'a\nb': { throw new Error('Not implemented yet: "a\nb" case') }
}

Instead of:

switch (a) {
  case `a
b`: { throw new Error('Not implemented yet: "a\nb" case') }
}

I'm sure no one writes code like the latter, especially with deeper indentations.

Copy link
Contributor Author

@StyleShit StyleShit Oct 23, 2023

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 same escapedCaseTest variable, because we still need to re-escape It before using it in the error, right? Or am I missing something?

Copy link
Contributor Author

@StyleShit StyleShit Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at this one

Is that what you mean?

@bradzacher bradzacher added the bug Something isn't working label Nov 10, 2023
Copy link
Member

@Josh-Cena Josh-Cena left a 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 :)

@Josh-Cena Josh-Cena added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Nov 15, 2023
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A guy chest shaking/dancing happily with his arms out

@JoshuaKGoldberg JoshuaKGoldberg merged commit a034d0a into typescript-eslint:main Nov 16, 2023
46 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [switch-exhaustiveness-check] Some cases for member names are not being fixed properly
4 participants