1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Move a try/catch from compile to loadFile

This try/catch should only be necessary for dynamically loaded files. Also added a lengthier explanation of why this try/catch is needed.
This commit is contained in:
Demian Ferreiro 2013-06-09 02:54:34 -03:00
parent 8e90aaefc1
commit 3c880bf601
2 changed files with 25 additions and 23 deletions

View file

@ -24,7 +24,7 @@
exports.helpers = helpers;
exports.compile = compile = function(code, options) {
var answer, currentColumn, currentLine, err, fragment, fragments, header, js, map, merge, newLines, _i, _len;
var answer, currentColumn, currentLine, fragment, fragments, header, js, map, merge, newLines, _i, _len;
if (options == null) {
options = {};
}
@ -32,14 +32,7 @@
if (options.sourceMap) {
map = new SourceMap;
}
try {
fragments = parser.parse(lexer.tokenize(code, options)).compileToFragments(options);
} catch (_error) {
err = _error;
err.filename = options.filename;
err.code = code;
throw err;
}
fragments = parser.parse(lexer.tokenize(code, options)).compileToFragments(options);
currentLine = 0;
if (options.header) {
currentLine += 1;
@ -177,14 +170,21 @@
};
loadFile = function(module, filename) {
var answer, raw, stripped;
var answer, err, raw, stripped;
raw = fs.readFileSync(filename, 'utf8');
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
answer = compile(stripped, {
filename: filename,
sourceMap: true,
literate: helpers.isLiterate(filename)
});
try {
answer = compile(stripped, {
filename: filename,
sourceMap: true,
literate: helpers.isLiterate(filename)
});
} catch (_error) {
err = _error;
err.filename = filename;
err.code = stripped;
throw err;
}
sourceMaps[filename] = answer.sourceMap;
return module._compile(answer.js, filename);
};

View file

@ -33,13 +33,7 @@ exports.compile = compile = (code, options = {}) ->
if options.sourceMap
map = new SourceMap
try
fragments = parser.parse(lexer.tokenize code, options).compileToFragments options
catch err
# Add source file information to error so it can be pretty-printed later.
err.filename = options.filename
err.code = code
throw err
fragments = parser.parse(lexer.tokenize code, options).compileToFragments options
currentLine = 0
currentLine += 1 if options.header
@ -152,7 +146,15 @@ exports.eval = (code, options = {}) ->
loadFile = (module, filename) ->
raw = fs.readFileSync filename, 'utf8'
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
answer = compile(stripped, {filename, sourceMap: true, literate: helpers.isLiterate filename})
try
answer = compile(stripped, {filename, sourceMap: true, literate: helpers.isLiterate filename})
catch err
# As the filename and code of a dynamically loaded file will be different
# from the original file compiled with CoffeeScript.run, add that
# information to error so it can be pretty-printed later.
err.filename = filename
err.code = stripped
throw err
sourceMaps[filename] = answer.sourceMap
module._compile answer.js, filename