jashkenas/coffee-script — pull request #3362 by epidemian (2014-02-08 16:34:12+00:00)
Title: Fix #3361, make %% coerce right operand only once

A very straightforward solution to #3361.

Other solutions could have been:

Forcing the coercion at the `__modulo` call site. Compiling `0 %% valueOf: -> console.log "!"` to:

```javascript
var __modulo = function(a, b) { return (a % b + b) % b; };

__modulo(0, +{
valueOf: function() {
return console.log("!");
}
});
```

(notice the `+` sign added to the call and removed from the `__modulo` definition)

Or inlining the whole thing:

```javascript
var __ref;

(__ref = +{
valueOf: function() {
return console.log("!");
}
}, (0 % __ref + __ref) % __ref);
```

But i prefer the solution in this PR because it results in the cleanest code on the call site (it also was the easiest to implement :stuck_out_tongue: ):

```javascript
var __modulo = function(a, b) { b = +b; return (a % b + b) % b; };

__modulo(0, {
valueOf: function() {
return console.log("!");
}
});
```