765: -b/--bare <- --no-wrap

This commit is contained in:
satyr
2010-10-14 04:09:56 +09:00
parent 6e89ad3401
commit 88cc1ee35d
13 changed files with 32 additions and 39 deletions

View File

@@ -17,7 +17,7 @@ EOS
desc "Build the documentation page" desc "Build the documentation page"
task :doc do task :doc do
source = 'documentation/index.html.erb' source = 'documentation/index.html.erb'
child = fork { exec "bin/coffee --no-wrap -cw -o documentation/js documentation/coffee/*.coffee" } child = fork { exec "bin/coffee -bcw -o documentation/js documentation/coffee/*.coffee" }
at_exit { Process.kill("INT", child) } at_exit { Process.kill("INT", child) }
Signal.trap("INT") { exit } Signal.trap("INT") { exit }
loop do loop do

View File

@@ -265,7 +265,7 @@ sudo npm install coffee-script</pre>
</td> </td>
</tr> </tr>
<tr> <tr>
<td><code>--no-wrap</code></td> <td><code>-b, --bare</code></td>
<td> <td>
Compile the JavaScript without the top-level function safety wrapper. Compile the JavaScript without the top-level function safety wrapper.
(Used for CoffeeScript as a Node.js module.) (Used for CoffeeScript as a Node.js module.)
@@ -1309,7 +1309,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
source = $('#repl_source').val() source = $('#repl_source').val()
window.compiled_js = '' window.compiled_js = ''
try try
window.compiled_js = CoffeeScript.compile source, wrap: false window.compiled_js = CoffeeScript.compile source, bare: on
$('#repl_results').text window.compiled_js $('#repl_results').text window.compiled_js
$('#error').hide() $('#error').hide()
catch error catch error

File diff suppressed because one or more lines are too long

View File

@@ -347,7 +347,7 @@ sudo npm install coffee-script</pre>
</td> </td>
</tr> </tr>
<tr> <tr>
<td><code>--no-wrap</code></td> <td><code>-b, --bare</code></td>
<td> <td>
Compile the JavaScript without the top-level function safety wrapper. Compile the JavaScript without the top-level function safety wrapper.
(Used for CoffeeScript as a Node.js module.) (Used for CoffeeScript as a Node.js module.)

View File

@@ -6,7 +6,7 @@
return eval(CoffeeScript.compile(code, options)); return eval(CoffeeScript.compile(code, options));
}; };
CoffeeScript.run = function(code, options) { CoffeeScript.run = function(code, options) {
((options != null) ? (options.wrap = false) : undefined); ((options != null) ? (options.bare = true) : undefined);
return Function(CoffeeScript.compile(code, options))(); return Function(CoffeeScript.compile(code, options))();
}; };
if (!(typeof window !== "undefined" && window !== null)) { if (!(typeof window !== "undefined" && window !== null)) {

View File

@@ -10,7 +10,7 @@
helpers.extend(CoffeeScript, new EventEmitter); helpers.extend(CoffeeScript, new EventEmitter);
global.CoffeeScript = CoffeeScript; global.CoffeeScript = CoffeeScript;
BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee'; BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee';
SWITCHES = [['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['--no-wrap', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--nodes', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']]; SWITCHES = [['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['-b', '--bare', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--nodes', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
opts = {}; opts = {};
sources = []; sources = [];
optionParser = null; optionParser = null;
@@ -210,12 +210,10 @@
return (sources = o.arguments); return (sources = o.arguments);
}; };
compileOptions = function(fileName) { compileOptions = function(fileName) {
var o; return {
o = { fileName: fileName,
fileName: fileName bare: opts.bare
}; };
o.wrap = !opts['no-wrap'];
return o;
}; };
usage = function() { usage = function() {
puts(optionParser.help()); puts(optionParser.help());

View File

@@ -205,16 +205,15 @@
_result.push(this.compileExpression(node, merge(o))); _result.push(this.compileExpression(node, merge(o)));
} }
return _result; return _result;
}).call(this).join("\n"); }).call(this).join('\n');
}; };
Expressions.prototype.compileRoot = function(o) { Expressions.prototype.compileRoot = function(o) {
var code, wrap; var code;
wrap = (o.wrap != null) ? o.wrap : true; o.indent = (this.tab = o.bare ? '' : TAB);
o.indent = (this.tab = wrap ? TAB : '');
o.scope = new Scope(null, this, null); o.scope = new Scope(null, this, null);
code = this.compileWithDeclarations(o); code = this.compileWithDeclarations(o);
code = code.replace(TRAILING_WHITESPACE, ''); code = code.replace(TRAILING_WHITESPACE, '');
return wrap ? ("(function() {\n" + code + "\n}).call(this);\n") : code; return o.bare ? code : ("(function() {\n" + code + "\n}).call(this);\n");
}; };
Expressions.prototype.compileWithDeclarations = function(o) { Expressions.prototype.compileWithDeclarations = function(o) {
var code; var code;
@@ -1138,8 +1137,8 @@
o.top = true; o.top = true;
o.indent = this.idt(1); o.indent = this.idt(1);
empty = this.body.expressions.length === 0; empty = this.body.expressions.length === 0;
del(o, 'wrap'); delete o.bare;
del(o, 'globals'); delete o.globals;
splat = undefined; splat = undefined;
params = []; params = [];
for (i = 0, _len = (_ref2 = this.params).length; i < _len; i++) { for (i = 0, _len = (_ref2 = this.params).length; i < _len; i++) {

View File

@@ -13,7 +13,7 @@
var val; var val;
try { try {
val = CoffeeScript.eval(buffer.toString(), { val = CoffeeScript.eval(buffer.toString(), {
wrap: false, bare: true,
globals: true, globals: true,
fileName: 'repl' fileName: 'repl'
}); });

View File

@@ -9,7 +9,7 @@ CoffeeScript.eval = (code, options) ->
# Running code does not provide access to this scope. # Running code does not provide access to this scope.
CoffeeScript.run = (code, options) -> CoffeeScript.run = (code, options) ->
options?.wrap = no options?.bare = on
Function(CoffeeScript.compile code, options)() Function(CoffeeScript.compile code, options)()
# If we're not in a browser environment, we're finished with the public API. # If we're not in a browser environment, we're finished with the public API.

View File

@@ -36,7 +36,7 @@ SWITCHES = [
['-s', '--stdio', 'listen for and compile scripts over stdio'] ['-s', '--stdio', 'listen for and compile scripts over stdio']
['-e', '--eval', 'compile a string from the command line'] ['-e', '--eval', 'compile a string from the command line']
['-r', '--require [FILE*]', 'require a library before executing your script'] ['-r', '--require [FILE*]', 'require a library before executing your script']
[ '--no-wrap', 'compile without the top-level function wrapper'] ['-b', '--bare', 'compile without the top-level function wrapper']
['-t', '--tokens', 'print the tokens that the lexer produces'] ['-t', '--tokens', 'print the tokens that the lexer produces']
['-n', '--nodes', 'print the parse tree that Jison produces'] ['-n', '--nodes', 'print the parse tree that Jison produces']
['-v', '--version', 'display CoffeeScript version'] ['-v', '--version', 'display CoffeeScript version']
@@ -182,10 +182,7 @@ parseOptions = ->
sources = o.arguments sources = o.arguments
# The compile-time options to pass to the CoffeeScript compiler. # The compile-time options to pass to the CoffeeScript compiler.
compileOptions = (fileName) -> compileOptions = (fileName) -> {fileName, bare: opts.bare}
o = {fileName}
o.wrap = !opts['no-wrap']
o
# Print the `--help` usage message and exit. # Print the `--help` usage message and exit.
usage = -> usage = ->

View File

@@ -193,19 +193,18 @@ exports.Expressions = class Expressions extends Base
if o.scope then super(o) else @compileRoot(o) if o.scope then super(o) else @compileRoot(o)
compileNode: (o) -> compileNode: (o) ->
(@compileExpression(node, merge(o)) for node in @expressions).join("\n") (@compileExpression node, merge o for node in @expressions).join '\n'
# If we happen to be the top-level **Expressions**, wrap everything in # If we happen to be the top-level **Expressions**, wrap everything in
# a safety closure, unless requested not to. # a safety closure, unless requested not to.
# It would be better not to generate them in the first place, but for now, # It would be better not to generate them in the first place, but for now,
# clean up obvious double-parentheses. # clean up obvious double-parentheses.
compileRoot: (o) -> compileRoot: (o) ->
wrap = if o.wrap? then o.wrap else true o.indent = @tab = if o.bare then '' else TAB
o.indent = @tab = if wrap then TAB else '' o.scope = new Scope null, this, null
o.scope = new Scope(null, this, null) code = @compileWithDeclarations o
code = @compileWithDeclarations(o) code = code.replace TRAILING_WHITESPACE, ''
code = code.replace(TRAILING_WHITESPACE, '') if o.bare then code else "(function() {\n#{code}\n}).call(this);\n"
if wrap then "(function() {\n#{code}\n}).call(this);\n" else code
# Compile the expressions body for the contents of a function, with # Compile the expressions body for the contents of a function, with
# declarations of all inner variables pushed up to the top. # declarations of all inner variables pushed up to the top.
@@ -981,8 +980,8 @@ exports.Code = class Code extends Base
o.top = true o.top = true
o.indent = @idt(1) o.indent = @idt(1)
empty = @body.expressions.length is 0 empty = @body.expressions.length is 0
del o, 'wrap' delete o.bare
del o, 'globals' delete o.globals
splat = undefined splat = undefined
params = [] params = []
for param, i in @params for param, i in @params

View File

@@ -20,7 +20,7 @@ helpers.extend global, quit: -> process.exit(0)
# of exiting. # of exiting.
run = (buffer) -> run = (buffer) ->
try try
val = CoffeeScript.eval buffer.toString(), wrap: false, globals: true, fileName: 'repl' val = CoffeeScript.eval buffer.toString(), bare: on, globals: on, fileName: 'repl'
puts inspect val if val isnt undefined puts inspect val if val isnt undefined
catch err catch err
puts err.stack or err.toString() puts err.stack or err.toString()

View File

@@ -1,10 +1,10 @@
# Ensure that carriage returns don't break compilation on Windows. # Ensure that carriage returns don't break compilation on Windows.
eq CoffeeScript.compile('one\r\ntwo', wrap: off), 'one;\ntwo;' eq CoffeeScript.compile('one\r\ntwo', bare: on), 'one;\ntwo;'
# `globals: on` removes `var`s # `globals: on` removes `var`s
eq CoffeeScript.compile('x = y', wrap: off, globals: on), 'x = y;' eq CoffeeScript.compile('x = y', bare: on, globals: on), 'x = y;'
ok 'passed' is CoffeeScript.eval '"passed"', wrap: off, fileName: 'test' ok 'passed' is CoffeeScript.eval '"passed"', bare: on, fileName: 'test'
#750 #750
try ok not CoffeeScript.nodes 'f(->' try ok not CoffeeScript.nodes 'f(->'