Files
coffeescript/test/parser.coffee
Alan Pierce e14946b3e6 Define proper operator precedence for bitwise/logical operators
This is an upstream port for the patch https://github.com/decaffeinate/coffeescript/pull/8

See https://github.com/decaffeinate/decaffeinate/issues/291 for the bug that this fixed.

For the most part, CoffeeScript and JavaScript have the same precedence rules,
but in some cases, the intermediate AST format didn't represent the actual
evaluation order. For example, in the expression `a or b and c`, the `and` is
evaluated first, but the parser treated the two operators with equal precedence.
This was still correct end-to-end because CoffeeScript simply emitted the result
without parens, but any intermediate tools using the CoffeeScript parser could
become confused.

Here are the JS operator precedence rules:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

For the most part, CoffeeScript already follows these. `COMPARE` operators
already behave differently due to chained comparisons, so I think we don't need
to worry about following JS precedence for those. So I think the only case where
it was behaving differently in an important way was for the binary/bitwise
operators that are being changed here.

As part of this change, I also introduced a new token tag, `BIN?`, for the
binary form of the `?` operator.
2016-10-09 14:45:25 -07:00

41 lines
1.2 KiB
CoffeeScript

# Parser
# ---------
test "operator precedence for logical operators", ->
source = '''
a or b and c
'''
block = CoffeeScript.nodes source
[expression] = block.expressions
eq expression.first.base.value, 'a'
eq expression.operator, '||'
eq expression.second.first.base.value, 'b'
eq expression.second.operator, '&&'
eq expression.second.second.base.value, 'c'
test "operator precedence for bitwise operators", ->
source = '''
a | b ^ c & d
'''
block = CoffeeScript.nodes source
[expression] = block.expressions
eq expression.first.base.value, 'a'
eq expression.operator, '|'
eq expression.second.first.base.value, 'b'
eq expression.second.operator, '^'
eq expression.second.second.first.base.value, 'c'
eq expression.second.second.operator, '&'
eq expression.second.second.second.base.value, 'd'
test "operator precedence for binary ? operator", ->
source = '''
a ? b and c
'''
block = CoffeeScript.nodes source
[expression] = block.expressions
eq expression.first.base.value, 'a'
eq expression.operator, '?'
eq expression.second.first.base.value, 'b'
eq expression.second.operator, '&&'
eq expression.second.second.base.value, 'c'