@@ -4,6 +4,8 @@ spawn = (require \child_process).spawn
4
4
esl = require \./index
5
5
require ! <[ fs path nopt ]>
6
6
7
+ { InvalidAstError } = require \esvalid
8
+
7
9
print -version = ->
8
10
try
9
11
console .log (require \../package .json .version)
@@ -64,7 +66,38 @@ compiler-opts = {}
64
66
if parsed -options .transform
65
67
compiler -opts .transform-macros = that .map require
66
68
67
- compile -and -show = -> console .log esl it , compiler -opts
69
+ compile -and -show = (code ) ->
70
+ code .= to -string !
71
+ try
72
+ console .log esl code , compiler -opts
73
+ catch err
74
+ if err instanceof InvalidAstError
75
+ console .error "[Error]" err .message
76
+ point -at -problem code , err .node
77
+ else throw err
78
+
79
+ # Use the node's location data (if present) to show the lines on which the
80
+ # problem occurred.
81
+ point -at -problem = (input , problematic -node ) ->
82
+ { location } = problematic -node
83
+ switch typeof ! location
84
+ | \String =>
85
+ stringified -node = JSON .stringify do
86
+ problematic -node
87
+ (k , v ) -> if k is \location then undefined else v
88
+ console .error " #stringified-node"
89
+ console .error " [ #location ]"
90
+ | \Object =>
91
+ { start , end } = location
92
+ line = input
93
+ .split "\n "
94
+ .slice (start .line - 1 ), end .line
95
+ .join "\n "
96
+ underline = " " * (start .offset - 1 ) +
97
+ "^" * (end .offset - start .offset)
98
+ console .error " " + line
99
+ console .error " " + underline
100
+ | _ => throw Error "Internal error: unexpected location type"
68
101
69
102
if target -path
70
103
e , esl -code <- fs .read-file target -path , encoding : \utf8
91
124
# NOTE: will fail on older nodejs due to paren wrapping logic; see
92
125
# SO http://stackoverflow.com/questions/19182057/node-js-repl-funny-behavior-with-custom-eval-function
93
126
# GH https://github.com/nodejs/node-v0.x-archive/commit/9ef9a9dee54a464a46739b14e8a348bec673c5a5
94
- stateful -compiler cmd
95
- |> vm .run-in -this -context
96
- |> callback null , _
127
+ try
128
+ stateful -compiler cmd
129
+ |> vm .run-in -this -context
130
+ |> callback null , _
131
+ catch err
132
+ if err instanceof InvalidAstError
133
+ console .error "[Error]" err .message
134
+ point -at -problem cmd , err .node
135
+ callback null
136
+ else throw err
0 commit comments