@@ -2,7 +2,7 @@ concat = require \concat-stream
2
2
{ zip } = require \prelude -ls
3
3
spawn = (require \child_process ).spawn
4
4
esl = require \./index
5
- require ! <[ fs path nopt ]>
5
+ require ! <[ fs path nopt chalk ]>
6
6
7
7
{ InvalidAstError } = require \esvalid
8
8
@@ -72,10 +72,15 @@ compile-and-show = (code) ->
72
72
console .log esl code , compiler -opts
73
73
catch err
74
74
if err instanceof InvalidAstError
75
- console .error "[Error]" err .message
75
+ console .error ( chalk .red "[Error]" ) + " " + err .message
76
76
point -at -problem code , err .node
77
77
else throw err
78
78
79
+ string -splice = (string , start , end , inserted -text ="" ) ->
80
+ (string .slice 0 , start ) +
81
+ inserted -text +
82
+ (string .slice end , string .length)
83
+
79
84
# Use the node's location data (if present) to show the lines on which the
80
85
# problem occurred.
81
86
point -at -problem = (input , problematic -node ) ->
@@ -86,17 +91,32 @@ point-at-problem = (input, problematic-node) ->
86
91
problematic -node
87
92
(k , v ) -> if k is \location then undefined else v
88
93
console .error " #stringified-node"
89
- console .error " [ #location ]"
94
+ console .error chalk .yellow " [ #location ]"
90
95
| \Object =>
91
96
{ start , end } = location
92
- line = input
97
+ lines = input
93
98
.split "\n "
94
99
.slice (start .line - 1 ), end .line
95
100
.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
+
100
120
| _ => throw Error "Internal error: unexpected location type"
101
121
102
122
if target -path
130
150
|> callback null , _
131
151
catch err
132
152
if err instanceof InvalidAstError
133
- console .error "[Error]" err .message
153
+ console .error ( chalk .red "[Error]" ) + " " + err .message
134
154
point -at -problem cmd , err .node
135
155
callback null
136
156
else throw err
0 commit comments