mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-15 01:38:13 -05:00
Show colorized error messages
This commit is contained in:
@@ -141,7 +141,8 @@ compileScript = (file, input, base) ->
|
||||
return if CoffeeScript.listeners('failure').length
|
||||
|
||||
message = if err instanceof CompilerError
|
||||
err.prettyMessage file or '[stdin]', input
|
||||
useColors = process.stdout.isTTY and not process.env.NODE_DISABLE_COLORS
|
||||
err.prettyMessage file or '[stdin]', input, useColors
|
||||
else
|
||||
err.stack or "ERROR: #{err}"
|
||||
|
||||
|
||||
@@ -17,12 +17,17 @@ exports.CompilerError = class CompilerError extends Error
|
||||
# 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.
|
||||
prettyMessage: (fileName, code) ->
|
||||
prettyMessage: (fileName, code, useColors) ->
|
||||
errorLine = code.split('\n')[@startLine]
|
||||
start = @startColumn
|
||||
# Show only the first line on multi-line errors.
|
||||
end = if @startLine is @endLine then @endColumn else errorLine.length - 1
|
||||
marker = repeat(' ', start) + repeat('^', end - start + 1)
|
||||
end = if @startLine is @endLine then @endColumn + 1 else errorLine.length
|
||||
marker = repeat(' ', start) + repeat('^', end - start)
|
||||
|
||||
if useColors
|
||||
colorize = (str) -> "\x1B[1;31m#{str}\x1B[0m"
|
||||
errorLine = errorLine[...start] + colorize(errorLine[start...end]) + errorLine[end..]
|
||||
marker = colorize marker
|
||||
|
||||
message = """
|
||||
#{fileName}:#{@startLine + 1}:#{@startColumn + 1}: error: #{@message}
|
||||
|
||||
Reference in New Issue
Block a user