Skip to content

Commit bf7bb54

Browse files
committed
Mark error positions with colour
And for contexts where colour doesn't show up, show the line number and offset also. Significantly improves #9.
1 parent e8a3e99 commit bf7bb54

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"tmp": "0.0.27"
4343
},
4444
"dependencies": {
45+
"chalk": "^1.1.1",
4546
"concat-stream": "^1.4.7",
4647
"escodegen": "^1.4.1",
4748
"esutils": "^2.0.2",

src/cli.ls

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ concat = require \concat-stream
22
{ zip } = require \prelude-ls
33
spawn = (require \child_process).spawn
44
esl = require \./index
5-
require! <[ fs path nopt ]>
5+
require! <[ fs path nopt chalk ]>
66

77
{ InvalidAstError } = require \esvalid
88

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

79+
string-splice = (string, start, end, inserted-text="") ->
80+
(string.slice 0, start) +
81+
inserted-text +
82+
(string.slice end, string.length)
83+
7984
# Use the node's location data (if present) to show the lines on which the
8085
# problem occurred.
8186
point-at-problem = (input, problematic-node) ->
@@ -86,17 +91,32 @@ point-at-problem = (input, problematic-node) ->
8691
problematic-node
8792
(k, v) -> if k is \location then undefined else v
8893
console.error " #stringified-node"
89-
console.error " [ #location ]"
94+
console.error chalk.yellow " [ #location ]"
9095
| \Object =>
9196
{ start, end } = location
92-
line = input
97+
lines = input
9398
.split "\n"
9499
.slice (start.line - 1), end.line
95100
.join "\n"
96-
underline = " " * (start.offset - 1) +
97-
"^" * (end.offset - start.offset)
98-
console.error " " + line
99-
console.error " " + underline
101+
102+
# Subtract 1 from both offsets because of open-paren that's implicitly
103+
# added to the input
104+
# inputs.
105+
start-offset = start.offset - 1
106+
end-offset = end.offset - 1
107+
108+
highlighted-part = chalk.black.bg-yellow (lines.slice start-offset, end-offset)
109+
110+
highlighted-lines = string-splice do
111+
lines
112+
start-offset
113+
end-offset
114+
highlighted-part
115+
116+
console.error "At line #{chalk.green start.line}, \
117+
offset #{chalk.green start-offset}:"
118+
console.error "\n#highlighted-lines\n"
119+
100120
| _ => throw Error "Internal error: unexpected location type"
101121

102122
if target-path
@@ -130,7 +150,7 @@ else
130150
|> callback null, _
131151
catch err
132152
if err instanceof InvalidAstError
133-
console.error "[Error]" err.message
153+
console.error (chalk.red "[Error]") + " " + err.message
134154
point-at-problem cmd, err.node
135155
callback null
136156
else throw err

0 commit comments

Comments
 (0)