From 3c880bf60114b623f729ca01e38097850dc0b195 Mon Sep 17 00:00:00 2001 From: Demian Ferreiro Date: Sun, 9 Jun 2013 02:54:34 -0300 Subject: [PATCH] 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. --- lib/coffee-script/coffee-script.js | 30 +++++++++++++++--------------- src/coffee-script.coffee | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 553a5f84a0..448fc2ca76 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -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); }; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 2c813858f4..7486a05a7d 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -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