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

Fixes #2849: now the compilation errors thrown by CoffeeScript.compile will include the correct filename and source code information

This commit is contained in:
Demian Ferreiro 2013-03-19 04:27:34 -03:00
parent 57d3cfd67f
commit 67fd84fc1d
4 changed files with 31 additions and 12 deletions

View file

@ -24,7 +24,7 @@
exports.helpers = helpers;
exports.compile = compile = function(code, options) {
var answer, currentColumn, currentLine, fragment, fragments, header, js, map, merge, newLines, _i, _len;
var answer, currentColumn, currentLine, err, fragment, fragments, header, js, map, merge, newLines, _i, _len;
if (options == null) {
options = {};
}
@ -32,7 +32,14 @@
if (options.sourceMap) {
map = new SourceMap;
}
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
try {
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
} catch (_error) {
err = _error;
err.filename = options.filename;
err.code = code;
throw err;
}
currentLine = 0;
if (options.header || options.inline) {
currentLine += 1;

View file

@ -198,11 +198,13 @@
throw error;
};
exports.prettyErrorMessage = function(error, fileName, code, useColors) {
exports.prettyErrorMessage = function(error, filename, code, useColors) {
var codeLine, colorize, end, first_column, first_line, last_column, last_line, marker, message, start, _ref1;
if (!error.location) {
return error.stack || ("" + error);
}
filename = error.filename || filename;
code = error.code || code;
_ref1 = error.location, first_line = _ref1.first_line, first_column = _ref1.first_column, last_line = _ref1.last_line, last_column = _ref1.last_column;
codeLine = code.split('\n')[first_line];
start = first_column;
@ -215,7 +217,7 @@
codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
marker = colorize(marker);
}
message = "" + fileName + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + error.message + "\n" + codeLine + "\n" + marker;
message = "" + filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + error.message + "\n" + codeLine + "\n" + marker;
return message;
};

View file

@ -33,7 +33,13 @@ exports.compile = compile = (code, options = {}) ->
if options.sourceMap
map = new SourceMap
fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options
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
currentLine = 0
currentLine += 1 if options.header or options.inline

View file

@ -145,9 +145,13 @@ exports.throwSyntaxError = (message, location) ->
# Creates a nice error message like, following the "standard" format
# <filename>:<line>:<col>: <message> plus the line with the error and a marker
# showing where the error is.
exports.prettyErrorMessage = (error, fileName, code, useColors) ->
exports.prettyErrorMessage = (error, filename, code, useColors) ->
return error.stack or "#{error}" unless error.location
# Prefer original source file information stored in the error if present.
filename = error.filename or filename
code = error.code or code
{first_line, first_column, last_line, last_column} = error.location
codeLine = code.split('\n')[first_line]
start = first_column
@ -156,15 +160,15 @@ exports.prettyErrorMessage = (error, fileName, code, useColors) ->
marker = repeat(' ', start) + repeat('^', end - start)
if useColors
colorize = (str) -> "\x1B[1;31m#{str}\x1B[0m"
colorize = (str) -> "\x1B[1;31m#{str}\x1B[0m"
codeLine = codeLine[...start] + colorize(codeLine[start...end]) + codeLine[end..]
marker = colorize marker
marker = colorize marker
message = """
#{fileName}:#{first_line + 1}:#{first_column + 1}: error: #{error.message}
#{codeLine}
#{marker}
"""
#{filename}:#{first_line + 1}:#{first_column + 1}: error: #{error.message}
#{codeLine}
#{marker}
"""
# Uncomment to add stacktrace.
#message += "\n#{error.stack}"