From e27756cee88cdf863914fa3e5fcc188125a4e4d2 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 24 Dec 2009 15:31:00 -0800 Subject: [PATCH] with a working -n --no-wrap option to disable the top-level function safety wrapper --- lib/coffee-script.rb | 4 ++-- lib/coffee_script/command_line.rb | 5 ++++- lib/coffee_script/nodes.rb | 9 +++++---- test/fixtures/each_no_wrap.js | 29 +++++++++++++++++++++++++++++ test/unit/test_execution.rb | 5 +++-- test/unit/test_parser.rb | 5 +++++ 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/each_no_wrap.js diff --git a/lib/coffee-script.rb b/lib/coffee-script.rb index 5f316876..1459a83d 100644 --- a/lib/coffee-script.rb +++ b/lib/coffee-script.rb @@ -12,9 +12,9 @@ module CoffeeScript VERSION = '0.1.1' # Keep in sync with the gemspec. # Compile a script (String or IO) to JavaScript. - def self.compile(script) + def self.compile(script, options={}) script = script.read if script.respond_to?(:read) - Parser.new.parse(script).compile + Parser.new.parse(script).compile(options) end end diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index 3d2327f4..4cc01e52 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -112,7 +112,7 @@ Usage: # Compile a single source file to JavaScript. def compile(script, source='') begin - CoffeeScript.compile(script) + CoffeeScript.compile(script, :no_wrap => @options[:no_wrap]) rescue CoffeeScript::ParseError => e STDERR.puts e.message(source) exit(1) unless @options[:watch] @@ -160,6 +160,9 @@ Usage: opts.on('-v', '--verbose', 'print at every step of code generation') do |v| ENV['VERBOSE'] = 'true' end + opts.on('-n', '--no-wrap', 'suppress the top-level safety function wrapper') do |n| + @options[:no_wrap] = true + end opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i| install_bundle exit diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 41d94f63..31b2f295 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -75,16 +75,17 @@ module CoffeeScript end # If this is the top-level Expressions, wrap everything in a safety closure. - def root_compile - code = compile(:indent => TAB, :scope => Scope.new) + def root_compile(o={}) + indent = o[:no_wrap] ? '' : TAB + code = compile(:indent => indent, :scope => Scope.new) code.gsub!(STRIP_TRAILING_WHITESPACE, '') - "(function(){\n#{code}\n})();" + o[:no_wrap] ? code : "(function(){\n#{code}\n})();" end # The extra fancy is to handle pushing down returns and assignments # recursively to the final lines of inner statements. def compile(options={}) - return root_compile unless options[:scope] + return root_compile(options) unless options[:scope] code = @expressions.map { |node| o = super(options) if last?(node) && (o[:return] || o[:assign]) diff --git a/test/fixtures/each_no_wrap.js b/test/fixtures/each_no_wrap.js new file mode 100644 index 00000000..6ae498b7 --- /dev/null +++ b/test/fixtures/each_no_wrap.js @@ -0,0 +1,29 @@ + +// The cornerstone, an each implementation. +// Handles objects implementing forEach, arrays, and raw objects. +_.each = function(obj, iterator, context) { + var index = 0; + try { + if (obj.forEach) { + obj.forEach(iterator, context); + } else if (_.isArray(obj) || _.isArguments(obj)) { + var a = obj; + for (var b=0, c=a.length; b true) == File.read('test/fixtures/each_no_wrap.js') + end + end