Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Rule Suggestion: Disallow comma operator in switch 'case' clauses #1545

Closed
RyanCavanaugh opened this issue Sep 9, 2016 · 5 comments
Closed

Comments

@RyanCavanaugh
Copy link

let arr = [];
switch(arr.length) {
  case 0, 1:
    return 'zero or one';
  default:
    return 'more than one';
}

This doesn't behave as expected (see microsoft/TypeScript#10802) even though it looks like it might

@06needhamt
Copy link

This would of saved me from hours of pain as shown in microsoft/TypeScript#10802 (comment)

@IllusionMH
Copy link
Contributor

I've started looking on this and I'm curious: should new rule enforce more strict checks for expressions in case clauses with set of options?
For example:

  • literals-only - allows only literals
  • no-comma - disallows coma operator in expressions
  • no-call-expressions - option which disables call expressions
  • no-binary-expressions - option which disables binary expressions

@jkillian, @RyanCavanaugh, @adidahiya, what do you think?

@jkillian
Copy link
Contributor

jkillian commented Sep 13, 2016

I can't think of a case where I've wanted an complex switch pattern. However, literals-only might be a bit of overkill, as you couldn't do this:

switch (someVar) {
    case SomeEnum.VALUE_1:
        ...
}

@RyanCavanaugh
Copy link
Author

We'll likely take microsoft/TypeScript#10814 so probably this rule won't be needed

@jkillian
Copy link
Contributor

I'm going to close this issue since TS is likely picking this up. If things change on the TS side (or even if it's just going to be a long time before this is implemented) this would be a great rule and we should implement it

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants