Skip to content

Commit 69f94a0

Browse files
author
dnolen
committed
stacktrace mapping functions need to take Map<LibraryName,SourceMap>
1 parent a56bf59 commit 69f94a0

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

src/main/cljs/cljs/stacktrace.cljc

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -490,35 +490,46 @@ goog.events.getProxy/f<@http://localhost:9000/out/goog/events/events.js:276:16"
490490
;; -----------------------------------------------------------------------------
491491
;; Stacktrace Mapping
492492

493+
(defn remove-ext [file]
494+
(-> file
495+
(string/replace #"\.js$" "")
496+
(string/replace #"\.cljs$" "")
497+
(string/replace #"\.cljc$" "")
498+
(string/replace #"\.clj$" "")))
499+
493500
(defn mapped-line-column-call
494501
"Given a cljs.source-map source map data structure map a generated line
495502
and column back to the original line, column, and function called."
496-
[source-map line column]
497-
;; source maps are 0 indexed for columns
498-
;; multiple segments may exist at column
499-
;; the last segment seems most accurate
500-
(letfn [(get-best-column [columns column]
501-
(last (or (get columns
502-
(last (filter #(<= % (dec column))
503-
(sort (keys columns)))))
504-
(second (first columns)))))
505-
(adjust [mapped]
506-
(vec (map #(%1 %2) [inc inc identity] mapped)))]
507-
(let [default [line column nil]]
508-
;; source maps are 0 indexed for lines
509-
(if-let [columns (get source-map (dec line))]
510-
(adjust (map (get-best-column columns column) [:line :col :name]))
511-
default))))
503+
[sms file line column]
504+
(let [source-map (get sms (symbol (string/replace (remove-ext file) "/" ".")))]
505+
;; source maps are 0 indexed for columns
506+
;; multiple segments may exist at column
507+
;; the last segment seems most accurate
508+
(letfn [(get-best-column [columns column]
509+
(last (or (get columns
510+
(last (filter #(<= % (dec column))
511+
(sort (keys columns)))))
512+
(second (first columns)))))
513+
(adjust [mapped]
514+
(vec (map #(%1 %2) [inc inc identity] mapped)))]
515+
(let [default [line column nil]]
516+
;; source maps are 0 indexed for lines
517+
(if-let [columns (get source-map (dec line))]
518+
(adjust (map (get-best-column columns column) [:line :col :name]))
519+
default)))))
512520

513521
(defn mapped-frame
514522
"Given opts and a canonicalized JavaScript stacktrace frame, return the
515523
ClojureScript frame."
516-
[{:keys [function file line column]} sm opts]
524+
[{:keys [function file line column]} sms opts]
517525
(let [no-source-file? (if-not file true (starts-with? file "<"))
518-
[line' column' call] (mapped-line-column-call sm line column)
519-
file' (if (ends-with? file ".js")
520-
(str (subs file 0 (- (count file) 3)) ".cljs")
521-
file)]
526+
[line' column' call] (if no-source-file?
527+
[line column nil]
528+
(mapped-line-column-call sms file line column))
529+
file' (when-not no-source-file?
530+
(if (ends-with? file ".js")
531+
(str (subs file 0 (- (count file) 3)) ".cljs")
532+
file))]
522533
{:function function
523534
:call call
524535
:file (if no-source-file?
@@ -541,8 +552,9 @@ goog.events.getProxy/f<@http://localhost:9000/out/goog/events/events.js:276:16"
541552
identifier delimited by angle brackets. The returned mapped stacktrace will
542553
also contain :url entries to the original sources if it can be determined
543554
from the classpath."
544-
([stacktrace sm] (mapped-stacktrace stacktrace sm nil))
545-
([stacktrace sm opts]
555+
([stacktrace sms]
556+
(mapped-stacktrace stacktrace sms nil))
557+
([stacktrace sms opts]
546558
(letfn [(call->function [x]
547559
(if (:call x)
548560
(hash-map :function (:call x))
@@ -555,7 +567,7 @@ goog.events.getProxy/f<@http://localhost:9000/out/goog/events/events.js:276:16"
555567
unmunged-call-name
556568
munged-fn-name))
557569
function call))]
558-
(let [mapped-frames (map (memoize #(mapped-frame % sm opts)) stacktrace)]
570+
(let [mapped-frames (map (memoize #(mapped-frame % sms opts)) stacktrace)]
559571
;; take each non-nil :call and optionally merge it into :function one-level
560572
;; up to avoid replacing with local symbols, we only replace munged name if
561573
;; we can munge call symbol back to it
@@ -564,14 +576,15 @@ goog.events.getProxy/f<@http://localhost:9000/out/goog/events/events.js:276:16"
564576
(concat (rest (map call->function mapped-frames)) [{}])))))))
565577

566578
(defn mapped-stacktrace-str
567-
"Given a vector representing the canonicalized JavaScript stacktrace
568-
print the ClojureScript stacktrace. See mapped-stacktrace."
569-
([stacktrace sm]
570-
(mapped-stacktrace-str stacktrace sm nil))
571-
([stacktrace sm opts]
579+
"Given a vector representing the canonicalized JavaScript stacktrace and a map
580+
of library names to decoded source maps, print the ClojureScript stacktrace .
581+
See mapped-stacktrace."
582+
([stacktrace sms]
583+
(mapped-stacktrace-str stacktrace sms nil))
584+
([stacktrace sms opts]
572585
(with-out-str
573586
(doseq [{:keys [function file line column]}
574-
(mapped-stacktrace stacktrace sm opts)]
587+
(mapped-stacktrace stacktrace sms opts)]
575588
(println "\t"
576589
(str (when function (str function " "))
577590
"(" file (when line (str ":" line))
@@ -589,26 +602,27 @@ goog.events.getProxy/f<@http://localhost:9000/out/goog/events/events.js:276:16"
589602
:output-to "samples/hello/out/hello.js"
590603
:source-map true})
591604

592-
(def sm
593-
(sm/decode
594-
(json/read-str
595-
(slurp "samples/hello/out/hello/core.js.map")
596-
:key-fn keyword)))
605+
(def sms
606+
{'hello.core
607+
(sm/decode
608+
(json/read-str
609+
(slurp "samples/hello/out/hello/core.js.map")
610+
:key-fn keyword))})
597611

598-
(pp/pprint sm)
612+
(pp/pprint sms)
599613

600614
;; maps to :line 5 :column 24
601615
(mapped-stacktrace
602616
[{:file "hello/core.js"
603617
:function "first"
604618
:line 6
605619
:column 0}]
606-
sm {:output-dir "samples/hello/out"})
620+
sms {:output-dir "samples/hello/out"})
607621

608622
(mapped-stacktrace-str
609623
[{:file "hello/core.js"
610624
:function "first"
611625
:line 6
612626
:column 0}]
613-
sm {:output-dir "samples/hello/out"})
627+
sms {:output-dir "samples/hello/out"})
614628
)

0 commit comments

Comments
 (0)