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 15:00:38 -04:00
|
|
|
# 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'
|
2017-02-12 21:07:07 -05:00
|
|
|
|
|
|
|
test "new calls have a range including the new", ->
|
|
|
|
source = '''
|
|
|
|
a = new B().c(d)
|
|
|
|
'''
|
|
|
|
block = CoffeeScript.nodes source
|
|
|
|
|
|
|
|
assertColumnRange = (node, firstColumn, lastColumn) ->
|
|
|
|
eq node.locationData.first_line, 0
|
|
|
|
eq node.locationData.first_column, firstColumn
|
|
|
|
eq node.locationData.last_line, 0
|
|
|
|
eq node.locationData.last_column, lastColumn
|
|
|
|
|
|
|
|
[assign] = block.expressions
|
|
|
|
outerCall = assign.value
|
|
|
|
innerValue = outerCall.variable
|
|
|
|
innerCall = innerValue.base
|
|
|
|
|
|
|
|
assertColumnRange assign, 0, 15
|
|
|
|
assertColumnRange outerCall, 4, 15
|
|
|
|
assertColumnRange innerValue, 4, 12
|
|
|
|
assertColumnRange innerCall, 4, 10
|
|
|
|
|
|
|
|
test "location data is properly set for nested `new`", ->
|
|
|
|
source = '''
|
|
|
|
new new A()()
|
|
|
|
'''
|
|
|
|
block = CoffeeScript.nodes source
|
|
|
|
|
|
|
|
assertColumnRange = (node, firstColumn, lastColumn) ->
|
|
|
|
eq node.locationData.first_line, 0
|
|
|
|
eq node.locationData.first_column, firstColumn
|
|
|
|
eq node.locationData.last_line, 0
|
|
|
|
eq node.locationData.last_column, lastColumn
|
|
|
|
|
|
|
|
[outerCall] = block.expressions
|
|
|
|
innerCall = outerCall.variable
|
|
|
|
|
|
|
|
assertColumnRange outerCall, 0, 12
|
|
|
|
assertColumnRange innerCall, 4, 10
|