the --no-wrap option now disables top-level var declarations

This commit is contained in:
Jeremy Ashkenas 2009-12-24 17:37:24 -08:00
parent 93009e07f6
commit 9b2326492b
4 changed files with 21 additions and 4 deletions

View File

@ -151,6 +151,14 @@ gem install coffee-script</pre>
AST.
</td>
</tr>
<tr>
<td><code>-n, --no-wrap</code></td>
<td>
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.
</td>
</tr>
<tr>
<td><code>--install-bundle</code></td>
<td>

View File

@ -221,6 +221,14 @@ gem install coffee-script</pre>
AST.
</td>
</tr>
<tr>
<td><code>-n, --no-wrap</code></td>
<td>
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.
</td>
</tr>
<tr>
<td><code>--install-bundle</code></td>
<td>

View File

@ -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|

View File

@ -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}}")