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"
task :doc do
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) }
Signal.trap("INT") { exit }
loop do

View File

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

View File

@ -6,7 +6,7 @@
return eval(CoffeeScript.compile(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))();
};
if (!(typeof window !== "undefined" && window !== null)) {

View File

@ -10,7 +10,7 @@
helpers.extend(CoffeeScript, new EventEmitter);
global.CoffeeScript = CoffeeScript;
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 = {};
sources = [];
optionParser = null;
@ -210,12 +210,10 @@
return (sources = o.arguments);
};
compileOptions = function(fileName) {
var o;
o = {
fileName: fileName
return {
fileName: fileName,
bare: opts.bare
};
o.wrap = !opts['no-wrap'];
return o;
};
usage = function() {
puts(optionParser.help());

View File

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

View File

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

View File

@ -9,7 +9,7 @@ CoffeeScript.eval = (code, options) ->
# Running code does not provide access to this scope.
CoffeeScript.run = (code, options) ->
options?.wrap = no
options?.bare = on
Function(CoffeeScript.compile code, options)()
# 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']
['-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']
['-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']
@ -182,10 +182,7 @@ parseOptions = ->
sources = o.arguments
# The compile-time options to pass to the CoffeeScript compiler.
compileOptions = (fileName) ->
o = {fileName}
o.wrap = !opts['no-wrap']
o
compileOptions = (fileName) -> {fileName, bare: opts.bare}
# Print the `--help` usage message and exit.
usage = ->

View File

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

View File

@ -20,7 +20,7 @@ helpers.extend global, quit: -> process.exit(0)
# of exiting.
run = (buffer) ->
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
catch err
puts err.stack or err.toString()

View File

@ -1,10 +1,10 @@
# 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
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
try ok not CoffeeScript.nodes 'f(->'