Permalink
Browse files

fixes #3363: modulo operator evaluation order

  • Loading branch information...
michaelficarra committed Feb 9, 2014
1 parent 4dfc75d commit a2c0106b3f2e0edce7de1a648d80b38ec4f44d51
Showing with 9 additions and 2 deletions.
  1. +1 −1 lib/coffee-script/nodes.js
  2. +1 −1 src/nodes.coffee
  3. +7 −0 test/operators.coffee

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -2194,7 +2194,7 @@ UTILITIES =
"
modulo: -> """
- function(a, b) { b = +b; return (a % b + b) % b; }
+ function(a, b) { return (+a % (b = +b) + b) % b; }
@xixixao

xixixao Feb 9, 2014

Contributor

The +a is not redundant anymore?

"""
# Shortcuts to speed up the lookup time for native functions.
View
@@ -363,3 +363,10 @@ test "#3361: Modulo operator coerces right operand once", ->
res = 42 %% valueOf: -> count += 1
eq 1, count
eq 0, res
+
+test "#3363: Modulo operator coercing order", ->
+ count = 2
+ a = valueOf: -> count *= 2
+ b = valueOf: -> count += 1
+ eq 4, a %% b
+ eq 5, count

3 comments on commit a2c0106

Contributor

davidchambers replied Feb 9, 2014

Nice fix. The test is clever, though the following would be more explicit:

log = ''
a = valueOf: -> log += 'a'; 42
b = valueOf: -> log += 'b'; 13
eq 3, a %% b
eq 'ab', log
Collaborator

michaelficarra replied Feb 9, 2014

Sorry, I don't know if I find that any more clear/explicit, especially not significantly enough to make a change.

Contributor

davidchambers replied Feb 9, 2014

It's fine as is. There's no harm in making the reader do some arithmetic. :)

Please sign in to comment.