jashkenas / jashkenas/coffeescript

Bug: using "not in" operator in switch works as "in" instead

Open
#5,480 1 comment 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.