From af53a04932a8a2de7fc626caa8541747ee33a395 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 24 Jan 2010 13:39:27 -0500 Subject: [PATCH] added test for lexical scope sharing through generated closure wrappers, something uncommonly used, but that was a regression --- lib/coffee_script/nodes.rb | 7 ++++--- test/fixtures/execution/test_existence.coffee | 1 + test/fixtures/execution/test_lexical_scope.coffee | 9 ++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 77016db4..3822e543 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -55,10 +55,11 @@ module CoffeeScript closure ? compile_closure(@options) : compile_node(@options) end + # Statements converted into expressions share scope with their parent + # closure, to preserve JavaScript-style lexical scope. def compile_closure(o={}) - indent = o[:indent] - @indent = (o[:indent] = idt(1)) - ClosureNode.wrap(self).compile(o) + @indent = o[:indent] + ClosureNode.wrap(self).compile(o.merge(:shared_scope => o[:scope])) end # Quick short method for the current indentation level, plus tabbing in. diff --git a/test/fixtures/execution/test_existence.coffee b/test/fixtures/execution/test_existence.coffee index a532b8e7..c572eabe 100644 --- a/test/fixtures/execution/test_existence.coffee +++ b/test/fixtures/execution/test_existence.coffee @@ -51,4 +51,5 @@ arr: ["--", "----"] print(arr.pop()?.length is 4) print(arr.pop()?.length is 2) print(arr.pop()?.length is undefined) +print(arr[0]?.length is undefined) print(arr.pop()?.length?.non?.existent()?.property is undefined) diff --git a/test/fixtures/execution/test_lexical_scope.coffee b/test/fixtures/execution/test_lexical_scope.coffee index 7f46928b..b8d8259f 100644 --- a/test/fixtures/execution/test_lexical_scope.coffee +++ b/test/fixtures/execution/test_lexical_scope.coffee @@ -1,3 +1,10 @@ num: 1 + 2 + (a: 3) -print(num is 6) \ No newline at end of file +print(num is 6) + + +result: if true + false + other: "result" + +print(result is "result" and other is "result") \ No newline at end of file