From 36a69864e3bd0c74ec3a23ef24ace81931b8e1ef Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 10 Jan 2012 12:54:21 -0500 Subject: [PATCH 1/4] Adding support for a header with version number to CoffeeScript.compile (#1778) --- src/coffee-script.coffee | 4 +++- test/compilation.coffee | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 01751cc9..8ce90b34 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -34,7 +34,9 @@ exports.helpers = require './helpers' exports.compile = compile = (code, options = {}) -> {merge} = exports.helpers try - (parser.parse lexer.tokenize code).compile merge {}, options + header = "// Generated by CoffeeScript #{@VERSION}\n" if options.header + js = (parser.parse lexer.tokenize code).compile options + "#{[header]}#{js}" catch err err.message = "In #{options.filename}, #{err.message}" if options.filename throw err diff --git a/test/compilation.coffee b/test/compilation.coffee index 91c1f631..8ea599cb 100644 --- a/test/compilation.coffee +++ b/test/compilation.coffee @@ -13,6 +13,10 @@ test "--bare", -> eq -1, CoffeeScript.compile('x = y', bare: on).indexOf 'function' ok 'passed' is CoffeeScript.eval '"passed"', bare: on, filename: 'test' +test "header (#1778)", -> + header = "// Generated by CoffeeScript #{CoffeeScript.VERSION}\n" + eq 0, CoffeeScript.compile('x = y', header: on).indexOf header + test "multiple generated references", -> a = {b: []} a.b[true] = -> this == a.b From 48c17e2c3d83866382c632e235b1c792b334417a Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 10 Jan 2012 12:54:48 -0500 Subject: [PATCH 2/4] Adding header to files compiled with command.coffee --- src/command.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/command.coffee b/src/command.coffee index 6711a088..d754dae0 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -313,7 +313,8 @@ parseOptions = -> return # The compile-time options to pass to the CoffeeScript compiler. -compileOptions = (filename) -> {filename, bare: opts.bare} +compileOptions = (filename) -> + {filename, bare: opts.bare, header: opts.compile} # Start up a new Node.js instance with the arguments in `--nodejs` passed to # the `node` binary, preserving the other options. From 11342ef97b778072a29f7f870c06df5b9fcaa9b2 Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 10 Jan 2012 12:55:41 -0500 Subject: [PATCH 3/4] Rebuilding -> our JS files now include the generated header --- lib/coffee-script/browser.js | 1 + lib/coffee-script/cake.js | 6 ++++-- lib/coffee-script/coffee-script.js | 9 +++++++-- lib/coffee-script/command.js | 4 +++- lib/coffee-script/grammar.js | 1 + lib/coffee-script/helpers.js | 1 + lib/coffee-script/index.js | 1 + lib/coffee-script/lexer.js | 1 + lib/coffee-script/nodes.js | 1 + lib/coffee-script/optparse.js | 1 + lib/coffee-script/repl.js | 1 + lib/coffee-script/rewriter.js | 1 + lib/coffee-script/scope.js | 1 + 13 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/coffee-script/browser.js b/lib/coffee-script/browser.js index ec2e4793..cb5e501d 100644 --- a/lib/coffee-script/browser.js +++ b/lib/coffee-script/browser.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var CoffeeScript, runScripts; diff --git a/lib/coffee-script/cake.js b/lib/coffee-script/cake.js index 986d5cc6..e7cb2a1d 100644 --- a/lib/coffee-script/cake.js +++ b/lib/coffee-script/cake.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var CoffeeScript, cakefileDirectory, fatalError, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks; @@ -65,8 +66,9 @@ }; printTasks = function() { - var cakefilePath, desc, name, spaces, task; - cakefilePath = path.join(path.relative(__originalDirname, process.cwd()), 'Cakefile'); + var cakefilePath, desc, name, relative, spaces, task; + relative = path.relative || path.resolve; + cakefilePath = path.join(relative(__originalDirname, process.cwd()), 'Cakefile'); console.log("" + cakefilePath + " defines the following tasks:\n"); for (name in tasks) { task = tasks[name]; diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 18866f6a..a6c0232a 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref, __hasProp = Object.prototype.hasOwnProperty; @@ -33,11 +34,15 @@ exports.helpers = require('./helpers'); exports.compile = compile = function(code, options) { - var merge; + var header, js, merge; if (options == null) options = {}; merge = exports.helpers.merge; try { - return (parser.parse(lexer.tokenize(code))).compile(merge({}, options)); + if (options.header) { + header = "// Generated by CoffeeScript " + this.VERSION + "\n"; + } + js = (parser.parse(lexer.tokenize(code))).compile(options); + return "" + [header] + js; } catch (err) { if (options.filename) { err.message = "In " + options.filename + ", " + err.message; diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index 2afa7b6f..b7dc1b98 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, forkNode, fs, helpers, 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; @@ -406,7 +407,8 @@ compileOptions = function(filename) { return { filename: filename, - bare: opts.bare + bare: opts.bare, + header: opts.compile }; }; diff --git a/lib/coffee-script/grammar.js b/lib/coffee-script/grammar.js index 5b7a164a..15c58783 100644 --- a/lib/coffee-script/grammar.js +++ b/lib/coffee-script/grammar.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var Parser, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap; diff --git a/lib/coffee-script/helpers.js b/lib/coffee-script/helpers.js index 038f7810..7e6c85d6 100644 --- a/lib/coffee-script/helpers.js +++ b/lib/coffee-script/helpers.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var extend, flatten; diff --git a/lib/coffee-script/index.js b/lib/coffee-script/index.js index 5e6eba94..9f13919f 100644 --- a/lib/coffee-script/index.js +++ b/lib/coffee-script/index.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var key, val, _ref; diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index d5e68c23..d55fc7dc 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_ILLEGAL, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDEXABLE, INVERSES, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LINE_CONTINUER, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NOT_REGEX, NOT_SPACED_REGEX, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, TRAILING_SPACES, UNARY, WHITESPACE, compact, count, key, last, starts, _ref, _ref2, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index bfb242d9..247fee68 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var Access, Arr, Assign, Base, Block, Call, Class, Closure, Code, Comment, Existence, Extends, For, IDENTIFIER, IDENTIFIER_STR, IS_STRING, If, In, Index, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, METHOD_DEF, NEGATE, NO, Obj, Op, Param, Parens, RESERVED, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, Throw, Try, UTILITIES, Value, While, YES, compact, del, ends, extend, flatten, last, merge, multident, starts, unfoldSoak, utility, _ref, __hasProp = Object.prototype.hasOwnProperty, diff --git a/lib/coffee-script/optparse.js b/lib/coffee-script/optparse.js index 391818a3..71d90ebd 100644 --- a/lib/coffee-script/optparse.js +++ b/lib/coffee-script/optparse.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments; diff --git a/lib/coffee-script/repl.js b/lib/coffee-script/repl.js index 9513b483..0cf7eccb 100644 --- a/lib/coffee-script/repl.js +++ b/lib/coffee-script/repl.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, REPL_PROMPT_MULTILINE, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, getCompletions, inspect, multilineMode, readline, repl, stdin, stdout; diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index 56a10386..8efb129b 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, left, rite, _i, _len, _ref, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, diff --git a/lib/coffee-script/scope.js b/lib/coffee-script/scope.js index 6f21cc3f..ccadbe0d 100644 --- a/lib/coffee-script/scope.js +++ b/lib/coffee-script/scope.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.2.1-pre (function() { var Scope, extend, last, _ref; From 86e4d79ffbed374ad3a4cbb338830e68fb816c0b Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 10 Jan 2012 14:00:29 -0500 Subject: [PATCH 4/4] Early return from CoffeeScript.compile when header not enabled --- lib/coffee-script/coffee-script.js | 7 +++---- src/coffee-script.coffee | 5 +++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index a6c0232a..df1a8512 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -38,17 +38,16 @@ if (options == null) options = {}; merge = exports.helpers.merge; try { - if (options.header) { - header = "// Generated by CoffeeScript " + this.VERSION + "\n"; - } js = (parser.parse(lexer.tokenize(code))).compile(options); - return "" + [header] + js; + if (!options.header) return js; } catch (err) { if (options.filename) { err.message = "In " + options.filename + ", " + err.message; } throw err; } + header = "Generated by CoffeeScript " + this.VERSION; + return "// " + header + "\n" + js; }; exports.tokens = function(code, options) { diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 8ce90b34..043de3ac 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -34,12 +34,13 @@ exports.helpers = require './helpers' exports.compile = compile = (code, options = {}) -> {merge} = exports.helpers try - header = "// Generated by CoffeeScript #{@VERSION}\n" if options.header js = (parser.parse lexer.tokenize code).compile options - "#{[header]}#{js}" + return js unless options.header catch err err.message = "In #{options.filename}, #{err.message}" if options.filename throw err + header = "Generated by CoffeeScript #{@VERSION}" + "// #{header}\n#{js}" # Tokenize a string of CoffeeScript code, and return the array of tokens. exports.tokens = (code, options) ->