Skip to content

Commit 1e54e62

Browse files
committed
browser REPL source mapping for Chrome
1 parent 7e5d99c commit 1e54e62

File tree

1 file changed

+115
-23
lines changed

1 file changed

+115
-23
lines changed

src/clj/cljs/repl/browser.clj

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -199,37 +199,101 @@
199199

200200
(defmulti parse-stacktrace (fn [repl-env st err opts] (:ua-product err)))
201201

202+
(defn parse-file-line-column [flc]
203+
(let [xs (string/split flc #":")
204+
[pre [line column]]
205+
(reduce
206+
(fn [[pre post] [x i]]
207+
(if (<= i 2)
208+
[pre (conj post x)]
209+
[(conj pre x) post]))
210+
[[] []] (map vector xs (range (count xs) 0 -1)))
211+
file (string/join ":" pre)]
212+
[(cond-> file
213+
(.startsWith file "(") (string/replace "(" ""))
214+
(Long/parseLong
215+
(cond-> line
216+
(.endsWith line ")") (string/replace ")" "")))
217+
(Long/parseLong
218+
(cond-> column
219+
(.endsWith column ")") (string/replace ")" "")))]))
220+
221+
(defn parse-file [file opts]
222+
(if (re-find #"http://localhost:9000/" file)
223+
(-> file
224+
(string/replace #"http://localhost:9000/" "")
225+
(string/replace (Pattern/compile (str "^" (util/output-directory opts) "/")) ""))
226+
(if-let [asset-root (:asset-root opts)]
227+
(string/replace file asset-root "")
228+
(throw
229+
(ex-info (str "Could not relativize URL " file)
230+
{:type :parse-stacktrace
231+
:reason :relativize-url})))))
232+
233+
;; -----------------------------------------------------------------------------
234+
;; Chrome Stacktrace
235+
236+
(defn chrome-st-el->frame
237+
[st-el opts]
238+
(let [xs (-> st-el
239+
(string/replace #"\s+at\s+" "")
240+
(string/split #"\s+"))
241+
[function flc] (if (== (count xs) 1)
242+
[nil (first xs)]
243+
[(first xs) (last xs)])
244+
[file line column] (parse-file-line-column flc)]
245+
(if (and file function line column)
246+
{:file (parse-file file opts)
247+
:function (string/replace function #"Object\." "")
248+
:line line
249+
:column column}
250+
(when-not (string/blank? function)
251+
{:file nil
252+
:function (string/replace function #"Object\." "")
253+
:line nil
254+
:column nil}))))
255+
256+
(comment
257+
(chrome-st-el->frame
258+
"\tat cljs$core$ffirst (http://localhost:9000/out/cljs/core.js:5356:34)" {})
259+
)
260+
261+
(defmethod parse-stacktrace :chrome
262+
[repl-env st err opts]
263+
(->> st
264+
string/split-lines
265+
(drop 1) ;; drop the error message
266+
(map #(chrome-st-el->frame % opts))
267+
(remove nil?)
268+
vec))
269+
270+
(comment
271+
(parse-stacktrace nil
272+
"\tat Object.cljs$core$seq [as seq] (http://localhost:9000/out/cljs/core.js:4258:8)
273+
\tat Object.cljs$core$first [as first] (http://localhost:9000/out/cljs/core.js:4288:19)
274+
\tat cljs$core$ffirst (http://localhost:9000/out/cljs/core.js:5356:34)
275+
\tat http://localhost:9000/out/cljs/core.js:16971:89
276+
\tat cljs.core.map.cljs$core$map__2 (http://localhost:9000/out/cljs/core.js:16972:3)
277+
\tat http://localhost:9000/out/cljs/core.js:10981:129
278+
\tat cljs.core.LazySeq.sval (http://localhost:9000/out/cljs/core.js:10982:3)
279+
\tat cljs.core.LazySeq.cljs$core$ISeqable$_seq$arity$1 (http://localhost:9000/out/cljs/core.js:11073:10)
280+
\tat Object.cljs$core$seq [as seq] (http://localhost:9000/out/cljs/core.js:4239:13)
281+
\tat Object.cljs$core$pr_sequential_writer [as pr_sequential_writer] (http://localhost:9000/out/cljs/core.js:28706:14)"
282+
{:ua-product :chrome}
283+
nil)
284+
)
285+
202286
;; -----------------------------------------------------------------------------
203287
;; Safari Stacktrace
204288

205289
(defn safari-st-el->frame
206-
"Parses a stack line into a frame representation, returning nil
207-
if parse failed."
208290
[st-el opts]
209291
(let [[function flc] (if (re-find #"@" st-el)
210292
(string/split st-el #"@")
211293
[nil st-el])
212-
xs (string/split flc #":")
213-
[pre post]
214-
(reduce
215-
(fn [[pre post] [x i]]
216-
(if (<= i 2)
217-
[pre (conj post x)]
218-
[(conj pre x) post]))
219-
[[] []] (map vector xs (range (count xs) 0 -1)))
220-
file (string/join ":" pre)
221-
[line column] (map #(Long/parseLong %) post)]
294+
[file line column] (parse-file-line-column flc)]
222295
(if (and file function line column)
223-
{:file (if (re-find #"http://localhost:9000/" file)
224-
(-> file
225-
(string/replace #"http://localhost:9000/" "")
226-
(string/replace (Pattern/compile (str "^" (util/output-directory opts) "/")) ""))
227-
(if-let [asset-root (:asset-root opts)]
228-
(string/replace file asset-root "")
229-
(throw
230-
(ex-info (str "Could not relativize URL " file)
231-
{:type :parse-stacktrace
232-
:reason :relativize-url}))))
296+
{:file (parse-file file opts)
233297
:function function
234298
:line line
235299
:column column}
@@ -256,7 +320,35 @@
256320

257321
(comment
258322
(parse-stacktrace nil
259-
"cljs$core$seq@http://localhost:9000/out/cljs/core.js:4259:17\ncljs$core$first@http://localhost:9000/out/cljs/core.js:4289:22\ncljs$core$ffirst@http://localhost:9000/out/cljs/core.js:5357:39\nhttp://localhost:9000/out/cljs/core.js:16972:92\nhttp://localhost:9000/out/cljs/core.js:16973:3\nhttp://localhost:9000/out/cljs/core.js:10982:133\nsval@http://localhost:9000/out/cljs/core.js:10983:3\ncljs$core$ISeqable$_seq$arity$1@http://localhost:9000/out/cljs/core.js:11074:14\ncljs$core$seq@http://localhost:9000/out/cljs/core.js:4240:44\ncljs$core$pr_sequential_writer@http://localhost:9000/out/cljs/core.js:28707:17\ncljs$core$IPrintWithWriter$_pr_writer$arity$3@http://localhost:9000/out/cljs/core.js:29386:38\ncljs$core$pr_writer_impl@http://localhost:9000/out/cljs/core.js:28912:57\ncljs$core$pr_writer@http://localhost:9000/out/cljs/core.js:29011:32\ncljs$core$pr_seq_writer@http://localhost:9000/out/cljs/core.js:29015:20\ncljs$core$pr_sb_with_opts@http://localhost:9000/out/cljs/core.js:29078:24\ncljs$core$pr_str_with_opts@http://localhost:9000/out/cljs/core.js:29092:48\ncljs$core$pr_str__delegate@http://localhost:9000/out/cljs/core.js:29130:34\ncljs$core$pr_str@http://localhost:9000/out/cljs/core.js:29139:39\n\neval code\neval@[native code]\nhttp://localhost:9000/out/clojure/browser/repl.js:23:271\nclojure$browser$repl$evaluate_javascript@http://localhost:9000/out/clojure/browser/repl.js:26:4\nhttp://localhost:9000/out/clojure/browser/repl.js:121:173\ndeliver@http://localhost:9000/out/goog/messaging/abstractchannel.js:142:21\nxpcDeliver@http://localhost:9000/out/goog/net/xpc/crosspagechannel.js:733:19\nmessageReceived_@http://localhost:9000/out/goog/net/xpc/nativemessagingtransport.js:321:23\nfireListener@http://localhost:9000/out/goog/events/events.js:741:25\nhandleBrowserEvent_@http://localhost:9000/out/goog/events/events.js:862:34\nhttp://localhost:9000/out/goog/events/events.js:276:42"
323+
"cljs$core$seq@http://localhost:9000/out/cljs/core.js:4259:17
324+
cljs$core$first@http://localhost:9000/out/cljs/core.js:4289:22
325+
cljs$core$ffirst@http://localhost:9000/out/cljs/core.js:5357:39
326+
http://localhost:9000/out/cljs/core.js:16972:92
327+
http://localhost:9000/out/cljs/core.js:16973:3
328+
http://localhost:9000/out/cljs/core.js:10982:133
329+
sval@http://localhost:9000/out/cljs/core.js:10983:3
330+
cljs$core$ISeqable$_seq$arity$1@http://localhost:9000/out/cljs/core.js:11074:14
331+
cljs$core$seq@http://localhost:9000/out/cljs/core.js:4240:44
332+
cljs$core$pr_sequential_writer@http://localhost:9000/out/cljs/core.js:28707:17
333+
cljs$core$IPrintWithWriter$_pr_writer$arity$3@http://localhost:9000/out/cljs/core.js:29386:38
334+
cljs$core$pr_writer_impl@http://localhost:9000/out/cljs/core.js:28912:57
335+
cljs$core$pr_writer@http://localhost:9000/out/cljs/core.js:29011:32
336+
cljs$core$pr_seq_writer@http://localhost:9000/out/cljs/core.js:29015:20
337+
cljs$core$pr_sb_with_opts@http://localhost:9000/out/cljs/core.js:29078:24
338+
cljs$core$pr_str_with_opts@http://localhost:9000/out/cljs/core.js:29092:48
339+
cljs$core$pr_str__delegate@http://localhost:9000/out/cljs/core.js:29130:34
340+
cljs$core$pr_str@http://localhost:9000/out/cljs/core.js:29139:39
341+
eval code
342+
eval@[native code]
343+
http://localhost:9000/out/clojure/browser/repl.js:23:271
344+
clojure$browser$repl$evaluate_javascript@http://localhost:9000/out/clojure/browser/repl.js:26:4
345+
http://localhost:9000/out/clojure/browser/repl.js:121:173
346+
deliver@http://localhost:9000/out/goog/messaging/abstractchannel.js:142:21
347+
xpcDeliver@http://localhost:9000/out/goog/net/xpc/crosspagechannel.js:733:19
348+
messageReceived_@http://localhost:9000/out/goog/net/xpc/nativemessagingtransport.js:321:23
349+
fireListener@http://localhost:9000/out/goog/events/events.js:741:25
350+
handleBrowserEvent_@http://localhost:9000/out/goog/events/events.js:862:34
351+
http://localhost:9000/out/goog/events/events.js:276:42"
260352
{:ua-product :safari}
261353
nil)
262354
)

0 commit comments

Comments
 (0)