mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
adding source file information to all coffeescript compiles
This commit is contained in:
parent
6ce869b3fb
commit
5b9ebd19d5
4 changed files with 33 additions and 17 deletions
|
@ -22,7 +22,14 @@
|
|||
// Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
||||
// compiler.
|
||||
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.
|
||||
exports.tokens = function tokens(code) {
|
||||
|
@ -38,9 +45,9 @@
|
|||
// setting `__filename`, `__dirname`, and relative `require()`.
|
||||
exports.run = function run(code, source, options) {
|
||||
var __dirname, __filename;
|
||||
__filename = source;
|
||||
options = options || {};
|
||||
module.filename = (__filename = (options.source = source));
|
||||
__dirname = path.dirname(source);
|
||||
module.filename = source;
|
||||
return eval(exports.compile(code, options));
|
||||
};
|
||||
// The real Lexer produces a generic stream of tokens. This object provides a
|
||||
|
|
|
@ -90,7 +90,7 @@
|
|||
} else if (o.run) {
|
||||
return CoffeeScript.run(code, source, compile_options());
|
||||
} else {
|
||||
js = CoffeeScript.compile(code, compile_options());
|
||||
js = CoffeeScript.compile(code, compile_options(source));
|
||||
if (o.compile) {
|
||||
return write_js(source, js);
|
||||
} else if (o.lint) {
|
||||
|
@ -119,7 +119,7 @@
|
|||
}
|
||||
});
|
||||
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
|
||||
|
@ -203,10 +203,13 @@
|
|||
return sources = options.arguments.slice(2, options.arguments.length);
|
||||
};
|
||||
// The compile-time options to pass to the CoffeeScript compiler.
|
||||
compile_options = function compile_options() {
|
||||
return options['no-wrap'] ? {
|
||||
no_wrap: true
|
||||
} : {};
|
||||
compile_options = function compile_options(source) {
|
||||
var o;
|
||||
o = {
|
||||
source: source
|
||||
};
|
||||
o['no-wrap'] = options['no-wrap'];
|
||||
return o;
|
||||
};
|
||||
// Print the `--help` usage message and exit.
|
||||
usage = function usage() {
|
||||
|
|
|
@ -23,7 +23,11 @@ exports.VERSION: '0.5.4'
|
|||
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
||||
# compiler.
|
||||
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.
|
||||
exports.tokens: (code) ->
|
||||
|
@ -38,9 +42,9 @@ exports.nodes: (code) ->
|
|||
# Compile and execute a string of CoffeeScript (on the server), correctly
|
||||
# setting `__filename`, `__dirname`, and relative `require()`.
|
||||
exports.run: (code, source, options) ->
|
||||
__filename: source
|
||||
__dirname: path.dirname source
|
||||
module.filename: source
|
||||
options ||= {}
|
||||
module.filename: __filename: options.source: source
|
||||
__dirname: path.dirname source
|
||||
eval exports.compile code, options
|
||||
|
||||
# The real Lexer produces a generic stream of tokens. This object provides a
|
||||
|
|
|
@ -80,7 +80,7 @@ compile_script: (source, code) ->
|
|||
else if o.nodes then puts CoffeeScript.nodes(code).toString()
|
||||
else if o.run then CoffeeScript.run code, source, compile_options()
|
||||
else
|
||||
js: CoffeeScript.compile code, compile_options()
|
||||
js: CoffeeScript.compile code, compile_options(source)
|
||||
if o.compile then write_js source, js
|
||||
else if o.lint then lint js
|
||||
else if o.print or o.eval then print js
|
||||
|
@ -95,7 +95,7 @@ compile_stdio: ->
|
|||
process.stdio.addListener 'data', (string) ->
|
||||
code += string if string
|
||||
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
|
||||
# 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]
|
||||
|
||||
# The compile-time options to pass to the CoffeeScript compiler.
|
||||
compile_options: ->
|
||||
if options['no-wrap'] then {no_wrap: true} else {}
|
||||
compile_options: (source) ->
|
||||
o: {source: source}
|
||||
o['no-wrap']: options['no-wrap']
|
||||
o
|
||||
|
||||
# Print the `--help` usage message and exit.
|
||||
usage: ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue