Skip to content

Commit

Permalink
Fixes jashkenas#2849: now the compilation errors thrown by CoffeeScri…
Browse files Browse the repository at this point in the history
…pt.compile will include the correct filename and source code information
  • Loading branch information
epidemian committed Mar 19, 2013
1 parent 57d3cfd commit 67fd84f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
11 changes: 9 additions & 2 deletions lib/coffee-script/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions lib/coffee-script/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/coffee-script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down

0 comments on commit 67fd84f

Please sign in to comment.