adding source file information to all coffeescript compiles

This commit is contained in:
Jeremy Ashkenas
2010-03-07 22:08:24 -05:00
parent 6ce869b3fb
commit 5b9ebd19d5
4 changed files with 33 additions and 17 deletions

View File

@@ -22,7 +22,14 @@
// Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison // Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
// compiler. // compiler.
exports.compile = function compile(code, options) { exports.compile = function compile(code, options) {
return (parser.parse(lexer.tokenize(code))).compile(options); try {
return (parser.parse(lexer.tokenize(code))).compile(options);
} catch (err) {
if (options.source) {
err.message = "In " + (options.source) + ", " + (err.message);
}
throw err;
}
}; };
// Tokenize a string of CoffeeScript code, and return the array of tokens. // Tokenize a string of CoffeeScript code, and return the array of tokens.
exports.tokens = function tokens(code) { exports.tokens = function tokens(code) {
@@ -38,9 +45,9 @@
// setting `__filename`, `__dirname`, and relative `require()`. // setting `__filename`, `__dirname`, and relative `require()`.
exports.run = function run(code, source, options) { exports.run = function run(code, source, options) {
var __dirname, __filename; var __dirname, __filename;
__filename = source; options = options || {};
module.filename = (__filename = (options.source = source));
__dirname = path.dirname(source); __dirname = path.dirname(source);
module.filename = source;
return eval(exports.compile(code, options)); return eval(exports.compile(code, options));
}; };
// The real Lexer produces a generic stream of tokens. This object provides a // The real Lexer produces a generic stream of tokens. This object provides a

View File

@@ -90,7 +90,7 @@
} else if (o.run) { } else if (o.run) {
return CoffeeScript.run(code, source, compile_options()); return CoffeeScript.run(code, source, compile_options());
} else { } else {
js = CoffeeScript.compile(code, compile_options()); js = CoffeeScript.compile(code, compile_options(source));
if (o.compile) { if (o.compile) {
return write_js(source, js); return write_js(source, js);
} else if (o.lint) { } else if (o.lint) {
@@ -119,7 +119,7 @@
} }
}); });
return process.stdio.addListener('close', function() { return process.stdio.addListener('close', function() {
return process.stdio.write(CoffeeScript.compile(code, compile_options())); return process.stdio.write(CoffeeScript.compile(code, compile_options('stdio')));
}); });
}; };
// Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling // Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling
@@ -203,10 +203,13 @@
return sources = options.arguments.slice(2, options.arguments.length); return sources = options.arguments.slice(2, options.arguments.length);
}; };
// The compile-time options to pass to the CoffeeScript compiler. // The compile-time options to pass to the CoffeeScript compiler.
compile_options = function compile_options() { compile_options = function compile_options(source) {
return options['no-wrap'] ? { var o;
no_wrap: true o = {
} : {}; source: source
};
o['no-wrap'] = options['no-wrap'];
return o;
}; };
// Print the `--help` usage message and exit. // Print the `--help` usage message and exit.
usage = function usage() { usage = function usage() {

View File

@@ -23,7 +23,11 @@ exports.VERSION: '0.5.4'
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison # Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
# compiler. # compiler.
exports.compile: (code, options) -> exports.compile: (code, options) ->
(parser.parse lexer.tokenize code).compile options try
(parser.parse lexer.tokenize code).compile options
catch err
err.message: "In ${options.source}, ${err.message}" if options.source
throw err
# Tokenize a string of CoffeeScript code, and return the array of tokens. # Tokenize a string of CoffeeScript code, and return the array of tokens.
exports.tokens: (code) -> exports.tokens: (code) ->
@@ -38,9 +42,9 @@ exports.nodes: (code) ->
# Compile and execute a string of CoffeeScript (on the server), correctly # Compile and execute a string of CoffeeScript (on the server), correctly
# setting `__filename`, `__dirname`, and relative `require()`. # setting `__filename`, `__dirname`, and relative `require()`.
exports.run: (code, source, options) -> exports.run: (code, source, options) ->
__filename: source options ||= {}
__dirname: path.dirname source module.filename: __filename: options.source: source
module.filename: source __dirname: path.dirname source
eval exports.compile code, options eval exports.compile code, options
# The real Lexer produces a generic stream of tokens. This object provides a # The real Lexer produces a generic stream of tokens. This object provides a

View File

@@ -80,7 +80,7 @@ compile_script: (source, code) ->
else if o.nodes then puts CoffeeScript.nodes(code).toString() else if o.nodes then puts CoffeeScript.nodes(code).toString()
else if o.run then CoffeeScript.run code, source, compile_options() else if o.run then CoffeeScript.run code, source, compile_options()
else else
js: CoffeeScript.compile code, compile_options() js: CoffeeScript.compile code, compile_options(source)
if o.compile then write_js source, js if o.compile then write_js source, js
else if o.lint then lint js else if o.lint then lint js
else if o.print or o.eval then print js else if o.print or o.eval then print js
@@ -95,7 +95,7 @@ compile_stdio: ->
process.stdio.addListener 'data', (string) -> process.stdio.addListener 'data', (string) ->
code += string if string code += string if string
process.stdio.addListener 'close', -> process.stdio.addListener 'close', ->
process.stdio.write CoffeeScript.compile code, compile_options() process.stdio.write CoffeeScript.compile code, compile_options('stdio')
# Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling # Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling
# them every time the files are updated. May be used in combination with other # them every time the files are updated. May be used in combination with other
@@ -143,8 +143,10 @@ parse_options: ->
sources: options.arguments[2...options.arguments.length] sources: options.arguments[2...options.arguments.length]
# The compile-time options to pass to the CoffeeScript compiler. # The compile-time options to pass to the CoffeeScript compiler.
compile_options: -> compile_options: (source) ->
if options['no-wrap'] then {no_wrap: true} else {} o: {source: source}
o['no-wrap']: options['no-wrap']
o
# Print the `--help` usage message and exit. # Print the `--help` usage message and exit.
usage: -> usage: ->