mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-17 19:11:22 -05:00
ensure that functions are only called once, when chaining comparators
This commit is contained in:
@@ -544,6 +544,7 @@ module CoffeeScript
|
|||||||
class OpNode < Node
|
class OpNode < Node
|
||||||
children :first, :second
|
children :first, :second
|
||||||
attr_reader :operator
|
attr_reader :operator
|
||||||
|
attr_accessor :second
|
||||||
|
|
||||||
CONVERSIONS = {
|
CONVERSIONS = {
|
||||||
:== => "===",
|
:== => "===",
|
||||||
@@ -581,7 +582,13 @@ module CoffeeScript
|
|||||||
# Mimic Python's chained comparisons. See:
|
# Mimic Python's chained comparisons. See:
|
||||||
# http://docs.python.org/reference/expressions.html#notin
|
# http://docs.python.org/reference/expressions.html#notin
|
||||||
def compile_chain(o)
|
def compile_chain(o)
|
||||||
write("(#{@first.compile(o)}) && (#{@first.unwrap.second.compile(o)} #{@operator} #{@second.compile(o)})")
|
shared = @first.unwrap.second
|
||||||
|
if shared.is_a?(CallNode)
|
||||||
|
temp = o[:scope].free_variable
|
||||||
|
@first.second = ParentheticalNode.new(AssignNode.new(temp, shared))
|
||||||
|
shared = temp
|
||||||
|
end
|
||||||
|
write("(#{@first.compile(o)}) && (#{shared.compile(o)} #{@operator} #{@second.compile(o)})")
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_conditional(o)
|
def compile_conditional(o)
|
||||||
|
|||||||
11
test/fixtures/execution/test_operations.coffee
vendored
11
test/fixtures/execution/test_operations.coffee
vendored
@@ -6,4 +6,13 @@ print(true is not false is true is not false)
|
|||||||
|
|
||||||
print(10 < 20 > 10)
|
print(10 < 20 > 10)
|
||||||
|
|
||||||
print(50 > 10 > 5 is parseInt('5', 10))
|
print(50 > 10 > 5 is parseInt('5', 10))
|
||||||
|
|
||||||
|
|
||||||
|
# Make sure that each argument is only evaluated once, even if used
|
||||||
|
# more than once.
|
||||||
|
|
||||||
|
i: 0
|
||||||
|
func: => i++
|
||||||
|
|
||||||
|
print(1 > func() < 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user