jashkenas / jashkenas/coffeescript
Bug: using "not in" operator in switch works as "in" instead
Nobody has claimed this yet.
- Dominant language
- CoffeeScript
- Stars
- 16.6k
- Forks
- 2k
- PR merge metrics
- No merged PRs in 30d
Description
```coffee
switch
when thing in arr
console.log 'this'
when thing not in arr
console.log 'that'
```
### Expected Behavior
The above should probably compile to something along the lines of this:
```js
var indexOf = [].indexOf;
switch (false) {
case indexOf.call(arr, thing) < 0:
console.log('this');
break;
case indexOf.call(arr, thing) >= 0:
console.log('that');
}
```
### Current Behavior
It however compiles to this:
```js
var indexOf = [].indexOf;
switch (false) {
case indexOf.call(arr, thing) < 0:
console.log('this');
break;
case indexOf.call(arr, thing) < 0:
console.log('that');
}
```
### Environment
* CoffeeScript version: 2.7.0
* Node.js version: some version of 14 for legacy reasons
But the thing happens even on the "try coffeescript" page on the website too
### Other context
This does not happen in coffee v1 nor in civet with coffee compat
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the switch example with CoffeeScript 2.7.0 or on the try CoffeeScript page, then trace the compiler path that handles `in` and `not in` conditions in switch cases. Done means the generated cases use opposite comparisons so the membership and non-membership branches behave as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- coffeescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100