From 979e110a84c370a362cef204d3bcd42ab39d794d Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Mon, 4 Mar 2013 09:45:25 -0500 Subject: [PATCH] Minor API changes. --- lib/coffee-script/coffee-script.js | 50 +++++++++++--------------- lib/coffee-script/command.js | 14 ++++---- src/coffee-script.coffee | 57 +++++++++++++++--------------- src/command.coffee | 18 ++++++---- 4 files changed, 65 insertions(+), 74 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 6e688f7a..b1c51bfc 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -46,21 +46,26 @@ }; exports.compile = compile = function(code, options) { - var currentColumn, currentLine, fragment, fragments, header, js, merge, newLines, _j, _len1; + var answer, coffeeFile, currentColumn, currentLine, fragment, fragments, header, js, jsFile, merge, newLines, sourceMap, _j, _len1; if (options == null) { options = {}; } merge = exports.helpers.merge; try { + if (options.sourceMap) { + coffeeFile = path.basename(options.filename); + jsFile = baseFileName(options.filename) + ".js"; + sourceMap = new sourcemap.SourceMap(); + } fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options); currentLine = 0; currentColumn = 0; js = ""; for (_j = 0, _len1 = fragments.length; _j < _len1; _j++) { fragment = fragments[_j]; - if (options.sourceMap) { + if (sourceMap) { if (fragment.locationData) { - options.sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], { + sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], { noReplace: true }); } @@ -70,42 +75,27 @@ } js += fragment.code; } - 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.compileWithSourceMap = function(code, options) { - var coffeeFile, compiledJs, jsFile, merge, v3SourceMap; - if (options == null) { - options = {}; + if (options.header) { + header = "Generated by CoffeeScript " + this.VERSION; + js = "// " + header + "\n" + js; } - merge = exports.helpers.merge; - try { - options = helpers.extend({}, options); - options.sourceMap = new sourcemap.SourceMap(); - coffeeFile = path.basename(options.filename); - jsFile = baseFileName(options.filename) + ".js"; - compiledJs = exports.compile(code, options); - v3SourceMap = sourcemap.generateV3SourceMap(options.sourceMap, coffeeFile, jsFile); - return { - compiledJs: compiledJs, - v3SourceMap: v3SourceMap, - sourceMap: options.sourceMap + if (options.sourceMap || options.returnObject) { + answer = { + js: js }; - } catch (err) { - if (options.filename) { - err.message = "In " + options.filename + ", " + err.message; + if (sourceMap) { + answer.sourceMap = sourceMap; + answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, coffeeFile, jsFile); } - throw err; + return answer; + } else { + return js; } }; diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index 5e93f227..defdd40d 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -168,13 +168,9 @@ sourceCode[sources.indexOf(t.file)] = t.input; return compileJoin(); } else { - if (o.maps) { - compiled = CoffeeScript.compileWithSourceMap(t.input, t.options); - t.output = compiled.compiledJs; - t.sourceMap = compiled.v3SourceMap; - } else { - t.output = CoffeeScript.compile(t.input, t.options); - } + compiled = CoffeeScript.compile(t.input, t.options); + t.output = compiled.js; + t.sourceMap = compiled.v3SourceMap; CoffeeScript.emit('success', task); if (o.print) { return printLine(t.output.trim()); @@ -479,7 +475,9 @@ filename: filename, literate: helpers.isLiterate(filename), bare: opts.bare, - header: opts.compile + header: opts.compile, + sourceMap: opts.maps, + returnObject: true }; }; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index e2d49479..16ee5c81 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -34,11 +34,23 @@ baseFileName = (fileName) -> extension = path.extname(fileName) return path.basename fileName, extension -# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison -# compiler. +# Compile CoffeeScript code to JavaScript, using the Coffee/Jison compiler. +# +# If `options.sourceMap` is specified, then `options.filename` must also be specified. +# +# This returns a javascript string, unless `options.sourceMap` or `options.returnObject` are true, +# in which case this returns a `{js, v3SourceMap, sourceMap} +# object, where sourceMap is a sourcemap.coffee#SourceMap object, handy for doing programatic +# lookups. exports.compile = compile = (code, options = {}) -> {merge} = exports.helpers try + + if options.sourceMap + coffeeFile = path.basename options.filename + jsFile = baseFileName(options.filename) + ".js" + sourceMap = new sourcemap.SourceMap() + fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options currentLine = 0 @@ -46,9 +58,9 @@ exports.compile = compile = (code, options = {}) -> js = "" for fragment in fragments # Update the sourcemap with data from each fragment - if options.sourceMap + if sourceMap if fragment.locationData - options.sourceMap.addMapping( + sourceMap.addMapping( [fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {noReplace: true}) @@ -59,35 +71,22 @@ exports.compile = compile = (code, options = {}) -> # Copy the code from each fragment into the final JavaScript. js += fragment.code - 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}" - -# Generates a compiled code and a source map for a string of CoffeeScript code. -# Callers should specifiy `options.filename`. Returns a `{compiledJs, v3SourceMap, sourceMap} -# object, where sourceMap is a sourcemap.coffee#SourceMap object, handy for doing programatic -# lookups. -exports.compileWithSourceMap = (code, options={}) -> - {merge} = exports.helpers - try - options = helpers.extend {}, options - options.sourceMap = new sourcemap.SourceMap() - coffeeFile = path.basename options.filename - jsFile = baseFileName(options.filename) + ".js" - compiledJs = exports.compile code, options - v3SourceMap = sourcemap.generateV3SourceMap options.sourceMap, coffeeFile, jsFile - return { - compiledJs, - v3SourceMap, - sourceMap: options.sourceMap - } catch err err.message = "In #{options.filename}, #{err.message}" if options.filename throw err + if options.header + header = "Generated by CoffeeScript #{@VERSION}" + js = "// #{header}\n#{js}" + + if options.sourceMap or options.returnObject + answer = {js} + if sourceMap + answer.sourceMap = sourceMap + answer.v3SourceMap = sourcemap.generateV3SourceMap sourceMap, coffeeFile, jsFile + answer + else + js # Tokenize a string of CoffeeScript code, and return the array of tokens. exports.tokens = (code, options) -> diff --git a/src/command.coffee b/src/command.coffee index 900c28d3..68ec920e 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -125,12 +125,9 @@ compileScript = (file, input, base) -> sourceCode[sources.indexOf(t.file)] = t.input compileJoin() else - if o.maps - compiled = CoffeeScript.compileWithSourceMap t.input, t.options - t.output = compiled.compiledJs - t.sourceMap = compiled.v3SourceMap - else - t.output = CoffeeScript.compile t.input, t.options + compiled = CoffeeScript.compile t.input, t.options + t.output = compiled.js + t.sourceMap = compiled.v3SourceMap CoffeeScript.emit 'success', task if o.print then printLine t.output.trim() @@ -322,7 +319,14 @@ parseOptions = -> # The compile-time options to pass to the CoffeeScript compiler. compileOptions = (filename) -> - {filename, literate: helpers.isLiterate(filename), bare: opts.bare, header: opts.compile} + { + filename + literate: helpers.isLiterate(filename) + bare: opts.bare + header: opts.compile + sourceMap: opts.maps + returnObject: yes + } # Start up a new Node.js instance with the arguments in `--nodejs` passed to # the `node` binary, preserving the other options.