From 75d9e23df4ab9eac94d983c04173e92de9bd8c2e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 1 Jan 2010 12:41:55 -0500 Subject: [PATCH] better existence test, with tests --- lib/coffee_script/nodes.rb | 3 ++- test/fixtures/execution/test_existence.coffee | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/execution/test_existence.coffee diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 55fdd6ef..00591410 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -724,7 +724,8 @@ module CoffeeScript end def compile(o={}) - write("(#{@expression.compile(super(o))} != undefined)") + val = @expression.compile(super(o)) + write("(typeof #{val} !== 'undefined' && #{val} !== null)") end end diff --git a/test/fixtures/execution/test_existence.coffee b/test/fixtures/execution/test_existence.coffee new file mode 100644 index 00000000..47bfbfab --- /dev/null +++ b/test/fixtures/execution/test_existence.coffee @@ -0,0 +1,5 @@ +print(if my_special_variable? then false else true) + +my_special_variable: false + +print(if my_special_variable? then true else false) \ No newline at end of file