Skip to content

Instantly share code, notes, and snippets.

@thheller
Created November 7, 2017 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thheller/f8725dc11811a91a3ceca295c56ad36f to your computer and use it in GitHub Desktop.
Save thheller/f8725dc11811a91a3ceca295c56ad36f to your computer and use it in GitHub Desktop.
(in-ns 'cljs.compiler)
;; no perf impact, just easier to read
(defn source-map-inc-col [{:keys [gen-col] :as m} n]
(assoc m :gen-col (+ gen-col n)))
(defn source-map-inc-line [{:keys [gen-line] :as m}]
(assoc m
:gen-line (inc gen-line)
:gen-col 0))
;; string? provides pretty decent boost
(defn emit1 [x]
(cond
(nil? x) nil
(string? x)
(do (when-not (nil? *source-map-data*)
(swap! *source-map-data* source-map-inc-col (count x)))
(print x))
#?(:clj (map? x) :cljs (ana/cljs-map? x)) (emit x)
#?(:clj (seq? x) :cljs (ana/cljs-seq? x)) (run! emit1 x)
#?(:clj (fn? x) :cljs ^boolean (goog/isFunction x)) (x)
:else (let [s (print-str x)]
(when-not (nil? *source-map-data*)
(swap! *source-map-data* source-map-inc-col (count s)))
(print s))))
(defn emits [& xs]
(run! emit1 xs))
(defn emitln [& xs]
(run! emit1 xs)
(newline)
(when *source-map-data*
(swap! *source-map-data* source-map-inc-line))
nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment