From 9b2326492b7813d3d99a50c96e83b0250ff848e6 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 24 Dec 2009 17:37:24 -0800 Subject: [PATCH] the --no-wrap option now disables top-level var declarations --- documentation/index.html.erb | 8 ++++++++ index.html | 8 ++++++++ lib/coffee_script/command_line.rb | 2 +- lib/coffee_script/nodes.rb | 7 ++++--- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/documentation/index.html.erb b/documentation/index.html.erb index b19a5df1..0b39fd0a 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -151,6 +151,14 @@ gem install coffee-script AST. + + -n, --no-wrap + + Compile the JavaScript without the top-level function safety wrapper + or var declarations, for situations where you want to add every + variable to global scope. + + --install-bundle diff --git a/index.html b/index.html index c2d577e3..bdcdf689 100644 --- a/index.html +++ b/index.html @@ -221,6 +221,14 @@ gem install coffee-script AST. + + -n, --no-wrap + + Compile the JavaScript without the top-level function safety wrapper + or var declarations, for situations where you want to add every + variable to global scope. + + --install-bundle diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index eb749d93..7499cf81 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -156,7 +156,7 @@ 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| + opts.on('-n', '--no-wrap', 'raw output, no safety wrapper or vars') do |n| @options[:no_wrap] = true end opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i| diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 021cd8a3..93369c47 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -77,7 +77,7 @@ module CoffeeScript # If this is the top-level Expressions, wrap everything in a safety closure. def root_compile(o={}) indent = o[:no_wrap] ? '' : TAB - code = compile(:indent => indent, :scope => Scope.new) + code = compile(o.merge(:indent => indent, :scope => Scope.new)) code.gsub!(STRIP_TRAILING_WHITESPACE, '') o[:no_wrap] ? code : "(function(){\n#{code}\n})();" end @@ -340,9 +340,9 @@ module CoffeeScript return write("#{@variable}: #{@value.compile(o)}") if @context == :object return write("#{name} = #{@value.compile(o)}#{postfix}") if @variable.properties? && !@value.custom_assign? defined = o[:scope].find(name) - def_part = defined || @variable.properties? ? "" : "var #{name};\n#{o[:indent]}" + def_part = defined || @variable.properties? || o[:no_wrap] ? "" : "var #{name};\n#{o[:indent]}" return write(def_part + @value.compile(o)) if @value.custom_assign? - def_part = defined ? name : "var #{name}" + def_part = defined || o[:no_wrap] ? name : "var #{name}" val_part = @value.compile(o).sub(LEADING_VAR, '') write("#{def_part} = #{val_part}#{postfix}") end @@ -411,6 +411,7 @@ module CoffeeScript indent = o[:indent] o[:indent] += TAB o.delete(:assign) + o.delete(:no_wrap) @params.each {|id| o[:scope].find(id.to_s) } code = @body.compile(o) write("function(#{@params.join(', ')}) {\n#{code}\n#{indent}}")