mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Minor API changes.
This commit is contained in:
parent
96785872cd
commit
979e110a84
4 changed files with 65 additions and 74 deletions
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -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) ->
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue