Skip to content

Commit

Permalink
Mark error positions with colour
Browse files Browse the repository at this point in the history
And for contexts where colour doesn't show up, show the line number and
offset also.

Significantly improves #9.
  • Loading branch information
anko committed Sep 26, 2015
1 parent e8a3e99 commit bf7bb54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"tmp": "0.0.27"
},
"dependencies": {
"chalk": "^1.1.1",
"concat-stream": "^1.4.7",
"escodegen": "^1.4.1",
"esutils": "^2.0.2",
Expand Down
38 changes: 29 additions & 9 deletions src/cli.ls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ concat = require \concat-stream
{ zip } = require \prelude-ls
spawn = (require \child_process).spawn
esl = require \./index
require! <[ fs path nopt ]>
require! <[ fs path nopt chalk ]>

{ InvalidAstError } = require \esvalid

Expand Down Expand Up @@ -72,10 +72,15 @@ compile-and-show = (code) ->
console.log esl code, compiler-opts
catch err
if err instanceof InvalidAstError
console.error "[Error]" err.message
console.error (chalk.red "[Error]") + " " + err.message
point-at-problem code, err.node
else throw err

string-splice = (string, start, end, inserted-text="") ->
(string.slice 0, start) +
inserted-text +
(string.slice end, string.length)

# Use the node's location data (if present) to show the lines on which the
# problem occurred.
point-at-problem = (input, problematic-node) ->
Expand All @@ -86,17 +91,32 @@ point-at-problem = (input, problematic-node) ->
problematic-node
(k, v) -> if k is \location then undefined else v
console.error " #stringified-node"
console.error " [ #location ]"
console.error chalk.yellow " [ #location ]"
| \Object =>
{ start, end } = location
line = input
lines = input
.split "\n"
.slice (start.line - 1), end.line
.join "\n"
underline = " " * (start.offset - 1) +
"^" * (end.offset - start.offset)
console.error " " + line
console.error " " + underline

# Subtract 1 from both offsets because of open-paren that's implicitly
# added to the input
# inputs.
start-offset = start.offset - 1
end-offset = end.offset - 1

highlighted-part = chalk.black.bg-yellow (lines.slice start-offset, end-offset)

highlighted-lines = string-splice do
lines
start-offset
end-offset
highlighted-part

console.error "At line #{chalk.green start.line}, \
offset #{chalk.green start-offset}:"
console.error "\n#highlighted-lines\n"

| _ => throw Error "Internal error: unexpected location type"

if target-path
Expand Down Expand Up @@ -130,7 +150,7 @@ else
|> callback null, _
catch err
if err instanceof InvalidAstError
console.error "[Error]" err.message
console.error (chalk.red "[Error]") + " " + err.message
point-at-problem cmd, err.node
callback null
else throw err

0 comments on commit bf7bb54

Please sign in to comment.