Skip to content

Documents do (x=1, y=2) -> syntax #2660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e46b129
Remove return statement on auto-generated constructors
epidemian Oct 28, 2012
52b0f76
Prevent constructors from returning values
epidemian Oct 28, 2012
040f800
Refs #2639, needs compilation via `rake doc`.
raganwald Jan 10, 2013
c7452a6
integrated do
raganwald Jan 17, 2013
7239074
Merge pull request #2685 from jordimassaguerpla/master
jashkenas Jan 29, 2013
3314025
Fixes #2681 -- removes old --require hook.
jashkenas Feb 1, 2013
21d69e3
Fixes #2617 -- implicit object call getting out of control.
jashkenas Feb 1, 2013
ac39899
Fixes #2613 -- bug with over-optimization of parentheses on LHS of de…
jashkenas Feb 1, 2013
0b1d4d3
Adding a test for #2613
jashkenas Feb 1, 2013
78891a0
Merging in @epedemian's fix for #2359 -- disallow other-typed constru…
jashkenas Feb 1, 2013
88bcc57
remove .js from pull request
raganwald Feb 1, 2013
b31cc70
Moving the 'generated by coffeescript version X' comment to the botto…
jashkenas Feb 1, 2013
de5e2c6
require a file name before checking for a fallback
jashkenas Feb 1, 2013
8a98cb3
Fixes #2531. Allow colors where Node says that colors are available.
jashkenas Feb 1, 2013
1f5b19b
slightly better conditions for range steps
jashkenas Feb 2, 2013
c37202e
Removing variable indirection for simple steps
jashkenas Feb 2, 2013
d72daca
simpler implementation of the previous commit
jashkenas Feb 2, 2013
1818e74
Fixes #2525, #1187, #1208, #1758, and many more -- allow looping over…
jashkenas Feb 2, 2013
3355383
Fixes #2690 -- tweak cake bench to handle literate coffeescript
jashkenas Feb 2, 2013
fc7f4ed
Fixes #2523 -- remove resetting of process.execPath
jashkenas Feb 2, 2013
21ea66e
CLoses #2700
raganwald Feb 12, 2013
c7426d1
Merge branch 'refs/heads/jashkenas/master'
raganwald Feb 12, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ CoffeeScript = require './lib/coffee-script'
{spawn, exec} = require 'child_process'

# ANSI Terminal Colors.
enableColors = no
unless process.platform is 'win32'
enableColors = not process.env.NODE_DISABLE_COLORS

bold = red = green = reset = ''
if enableColors
unless process.env.NODE_DISABLE_COLORS
bold = '\x1B[0;1m'
red = '\x1B[0;31m'
green = '\x1B[0;32m'
Expand All @@ -27,11 +23,6 @@ header = """
*/
"""

sources = [
'coffee-script', 'grammar', 'helpers'
'lexer', 'nodes', 'rewriter', 'scope'
].map (filename) -> "src/#{filename}.coffee"

# Run a CoffeeScript through our node/coffee interpreter.
run = (args, cb) ->
proc = spawn 'node', ['bin/coffee'].concat(args)
Expand Down Expand Up @@ -141,25 +132,25 @@ task 'doc:underscore', 'rebuild the Underscore.coffee documentation page', ->

task 'bench', 'quick benchmark of compilation time', ->
{Rewriter} = require './lib/coffee-script/rewriter'
co = sources.map((name) -> fs.readFileSync name).join '\n'
sources = ['coffee-script', 'grammar', 'helpers', 'lexer', 'nodes', 'rewriter']
coffee = sources.map((name) -> fs.readFileSync "src/#{name}.coffee").join '\n'
litcoffee = fs.readFileSync("src/scope.litcoffee").toString()
fmt = (ms) -> " #{bold}#{ " #{ms}".slice -4 }#{reset} ms"
total = 0
now = Date.now()
time = -> total += ms = -(now - now = Date.now()); fmt ms
tokens = CoffeeScript.tokens co, rewrite: false
tokens = CoffeeScript.tokens coffee, rewrite: no
littokens = CoffeeScript.tokens litcoffee, rewrite: no, literate: yes
tokens = tokens.concat(littokens)
console.log "Lex #{time()} (#{tokens.length} tokens)"
tokens = new Rewriter().rewrite tokens
console.log "Rewrite#{time()} (#{tokens.length} tokens)"
nodes = CoffeeScript.nodes tokens
console.log "Parse #{time()}"
js = nodes.compile bare: true
js = nodes.compile bare: yes
console.log "Compile#{time()} (#{js.length} chars)"
console.log "total #{ fmt total }"

task 'loc', 'count the lines of source code in the CoffeeScript compiler', ->
exec "cat #{ sources.join(' ') } | grep -v '^\\( *#\\|\\s*$\\)' | wc -l | tr -s ' '", (err, stdout) ->
console.log stdout.trim()


# Run the CoffeeScript test suite.
runTests = (CoffeeScript) ->
Expand Down
8 changes: 8 additions & 0 deletions documentation/coffee/namespace.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name = 'Outer'

do (name = 'Inner', somethingElse = undefined) ->
# alerts 'Inner'
alert(name)

# alerts 'Outer'
alert(name)
38 changes: 26 additions & 12 deletions documentation/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,29 @@ Expressions
<p>
This behavior is effectively identical to Ruby's scope for local variables.
Because you don't have direct access to the <tt>var</tt> keyword,
it's impossible to shadow an outer variable on purpose, you may only refer
to it. So be careful that you're not reusing the name of an external
be careful that you're not reusing the name of an external
variable accidentally, if you're writing a deeply nested function.
</p>
<p>
If you need to protect a variable from being reused, CoffeeScript
provides the <tt>do</tt> keyword, which immediately invokes a passed function.
The function's variables are declared as parameters, and values assigned to
the parameters will be bound within the function.
</p>
<%= code_for('namespace') %>
<p>
When using a <a href="#loops">loop</a> to generate functions, it's common to insert
a closure wrapper in order to ensure that loop variables are closed over,
and all the generated functions don't just share the final values.
</p>
<p>
The <tt>do</tt> keyword helps again. Parameters without values will be
passed through.
</p>
<%= code_for('do') %>
<p>
Although suppressed within this documentation for clarity, all
CoffeeScript output is wrapped in an anonymous function:
CoffeeScript output is wrapped in an immediately invoked function expression:
<tt>(function(){ ... })();</tt> This safety wrapper, combined with the
automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult
to pollute the global namespace by accident.
Expand Down Expand Up @@ -547,14 +563,6 @@ Expressions
For readability, the <b>until</b> keyword is equivalent to <tt>while not</tt>,
and the <b>loop</b> keyword is equivalent to <tt>while true</tt>.
</p>
<p>
When using a JavaScript loop to generate functions, it's common to insert
a closure wrapper in order to ensure that loop variables are closed over,
and all the generated functions don't just share the final values. CoffeeScript
provides the <tt>do</tt> keyword, which immediately invokes a passed function,
forwarding any arguments.
</p>
<%= code_for('do') %>

<p>
<span id="slices" class="bookmark"></span>
Expand Down Expand Up @@ -970,7 +978,7 @@ Expressions
is a reimagination of the excellent book
<a href="http://eloquentjavascript.net/">Eloquent JavaScript</a>, as if
it had been written in CoffeeScript instead. Covers language features
as well a the functional and object oriented programming styles. By
as well as the functional and object oriented programming styles. By
<a href="https://github.com/autotelicum">E. Hoigaard</a>.
</li>
<li>
Expand All @@ -980,6 +988,12 @@ Expressions
a fast-paced multiplayer word game, writing both the client-side and Node.js
portions in CoffeeScript.
</li>
<li>
<a href="http://ristrettolo.gy">CoffeeScript Ristretto</a>
is a deep dive into CoffeeScript's semantics from simple functions up through
closures, higher-order functions, objects, classes, combinators, and decorators.
By <a href="http://braythwayt.com/">Reg Braithwaite</a>.
</li>
</ul>

<h2>
Expand Down
3 changes: 2 additions & 1 deletion lib/coffee-script/browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.5.0-pre
(function() {
var CoffeeScript, runScripts;

Expand Down Expand Up @@ -90,3 +89,5 @@
}

}).call(this);

// Generated by CoffeeScript 1.5.0-pre
3 changes: 2 additions & 1 deletion lib/coffee-script/cake.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.5.0-pre
(function() {
var CoffeeScript, cakefileDirectory, existsSync, fatalError, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks;

Expand Down Expand Up @@ -111,3 +110,5 @@
};

}).call(this);

// Generated by CoffeeScript 1.5.0-pre
9 changes: 5 additions & 4 deletions lib/coffee-script/coffee-script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.5.0-pre
(function() {
var Lexer, compile, ext, extensions, fs, lexer, loadFile, parser, path, vm, _i, _len,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
Expand Down Expand Up @@ -37,7 +36,7 @@
exports.helpers = require('./helpers');

exports.compile = compile = function(code, options) {
var header, js, merge;
var footer, js, merge;
if (options == null) {
options = {};
}
Expand All @@ -53,8 +52,8 @@
}
throw err;
}
header = "Generated by CoffeeScript " + this.VERSION;
return "// " + header + "\n" + js;
footer = "Generated by CoffeeScript " + this.VERSION;
return "" + js + "\n// " + footer + "\n";
};

exports.tokens = function(code, options) {
Expand Down Expand Up @@ -168,3 +167,5 @@
parser.yy = require('./nodes');

}).call(this);

// Generated by CoffeeScript 1.5.0-pre
25 changes: 5 additions & 20 deletions lib/coffee-script/command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Generated by CoffeeScript 1.5.0-pre
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, coffee_exts, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, exists, forkNode, fs, helpers, hidden, joinTimeout, lint, loadRequires, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, version, wait, watch, watchDir, watchers, writeJs, _ref,
var BANNER, CoffeeScript, EventEmitter, SWITCHES, coffee_exts, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, exists, forkNode, fs, helpers, hidden, joinTimeout, lint, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, version, wait, watch, watchDir, watchers, writeJs, _ref,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

fs = require('fs');
Expand Down Expand Up @@ -35,7 +34,7 @@

BANNER = 'Usage: coffee [options] path/to/script.coffee -- [args]\n\nIf called without options, `coffee` will run your script.';

SWITCHES = [['-b', '--bare', 'compile without a top-level function wrapper'], ['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-e', '--eval', 'pass a string from the command line as input'], ['-h', '--help', 'display this help message'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling'], ['-l', '--lint', 'pipe the compiled JavaScript through JavaScript Lint'], ['-n', '--nodes', 'print out the parse tree that the parser produces'], ['--nodejs [ARGS]', 'pass options directly to the "node" binary'], ['-o', '--output [DIR]', 'set the output directory for compiled JavaScript'], ['-p', '--print', 'print out the compiled JavaScript'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-t', '--tokens', 'print out the tokens that the lexer/rewriter produce'], ['-v', '--version', 'display the version number'], ['-w', '--watch', 'watch scripts for changes and rerun commands']];
SWITCHES = [['-b', '--bare', 'compile without a top-level function wrapper'], ['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-e', '--eval', 'pass a string from the command line as input'], ['-h', '--help', 'display this help message'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling'], ['-l', '--lint', 'pipe the compiled JavaScript through JavaScript Lint'], ['-n', '--nodes', 'print out the parse tree that the parser produces'], ['--nodejs [ARGS]', 'pass options directly to the "node" binary'], ['-o', '--output [DIR]', 'set the output directory for compiled JavaScript'], ['-p', '--print', 'print out the compiled JavaScript'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-t', '--tokens', 'print out the tokens that the lexer/rewriter produce'], ['-v', '--version', 'display the version number'], ['-w', '--watch', 'watch scripts for changes and rerun commands']];

opts = {};

Expand Down Expand Up @@ -63,9 +62,6 @@
if (opts.version) {
return version();
}
if (opts.require) {
loadRequires();
}
if (opts.interactive) {
return require('./repl').start();
}
Expand All @@ -84,7 +80,6 @@
literals = opts.run ? sources.splice(1) : [];
process.argv = process.argv.slice(0, 2).concat(literals);
process.argv[0] = 'coffee';
process.execPath = require.main.filename;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
source = sources[_i];
Expand All @@ -100,7 +95,7 @@
throw err;
}
if ((err != null ? err.code : void 0) === 'ENOENT') {
if (topLevel && (_ref1 = path.extname(source), __indexOf.call(coffee_exts, _ref1) < 0)) {
if (topLevel && source && (_ref1 = path.extname(source), __indexOf.call(coffee_exts, _ref1) < 0)) {
source = sources[sources.indexOf(source)] = "" + source + ".coffee";
return compilePath(source, topLevel, base);
}
Expand Down Expand Up @@ -236,18 +231,6 @@
}
};

loadRequires = function() {
var realFilename, req, _i, _len, _ref1;
realFilename = module.filename;
module.filename = '.';
_ref1 = opts.require;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
req = _ref1[_i];
require(req);
}
return module.filename = realFilename;
};

watch = function(source, base) {
var compile, compileTimeout, prevStats, rewatch, watchErr, watcher;
prevStats = null;
Expand Down Expand Up @@ -507,3 +490,5 @@
};

}).call(this);

// Generated by CoffeeScript 1.5.0-pre
Loading