added the conditional existence operator

This commit is contained in:
Jeremy Ashkenas
2010-01-16 22:17:55 -05:00
parent 0bc4da2b51
commit 95b362499f
4 changed files with 16 additions and 3 deletions

View File

@@ -556,7 +556,7 @@ module CoffeeScript
:not => '!'
}
CHAINABLE = [:<, :>, :>=, :<=, :===, :'!===']
CONDITIONALS = [:'||=', :'&&=']
CONDITIONALS = [:'||=', :'&&=', :'?=']
PREFIX_OPERATORS = [:typeof, :delete]
def initialize(operator, first, second=nil, flip=false)
@@ -593,7 +593,9 @@ module CoffeeScript
def compile_conditional(o)
first, second = @first.compile(o), @second.compile(o)
o[:scope].find(first) if @first.unwrap.is_a?(Value)
sym = @operator[0..1]
return "#{first} = (typeof #{first} !== \"undefined\" && #{first} !== null) ? #{first} : #{second}" if @operator == '?='
"#{first} = #{first} #{sym} #{second}"
end