mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
with a working -n --no-wrap option to disable the top-level function safety wrapper
This commit is contained in:
parent
46f81c2de1
commit
e27756cee8
6 changed files with 48 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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])
|
||||
|
|
29
test/fixtures/each_no_wrap.js
vendored
Normal file
29
test/fixtures/each_no_wrap.js
vendored
Normal file
|
@ -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<c; b++) {
|
||||
var item = a[b];
|
||||
var i = b;
|
||||
iterator.call(context, item, i, obj);
|
||||
}
|
||||
} else {
|
||||
var d = _.keys(obj);
|
||||
for (var e=0, f=d.length; e<f; e++) {
|
||||
var key = d[e];
|
||||
iterator.call(context, obj[key], key, obj);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (e !== breaker) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
};
|
|
@ -10,8 +10,9 @@ class ExecutionTest < Test::Unit::TestCase
|
|||
starting_place = File.expand_path(Dir.pwd)
|
||||
Dir.chdir('/Users/jashkenas/Desktop/Beauty/Code/v8')
|
||||
sources.each do |source|
|
||||
# puts `./shell #{source}`
|
||||
assert `./shell #{source}`.chomp.to_sym == :true
|
||||
suceeded = `./shell #{source}`.chomp.to_sym == :true
|
||||
puts "failed: #{source}" unless suceeded
|
||||
assert suceeded
|
||||
end
|
||||
ensure
|
||||
Dir.chdir(starting_place)
|
||||
|
|
|
@ -72,4 +72,9 @@ class ParserTest < Test::Unit::TestCase
|
|||
assert nodes.compile == File.read('test/fixtures/each.js')
|
||||
end
|
||||
|
||||
def test_no_wrap
|
||||
nodes = @par.parse(File.read('test/fixtures/each.cs'))
|
||||
assert nodes.compile(:no_wrap => true) == File.read('test/fixtures/each_no_wrap.js')
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue