Created
April 28, 2020 19:32
-
-
Save roman01la/5abc95a4c43d0ddf9fe9c5c4639161a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns user | |
#?(:cljs (:require-macros [user]) | |
:clj (:require [cljs.source-map :as sm] | |
[clojure.data.json :as json] | |
[clojure.java.io :as io] | |
[clojure.string :as str] | |
[cljs.env :as env]))) | |
#?(:clj | |
(defn map-location [env form & {:keys [offset]}] | |
(try | |
(let [form-str (str form) | |
ns-name (-> env :ns :name) | |
ns-path (-> ns-name munge (str/replace #"\." "/")) | |
out-path (-> @env/*compiler* :options :output-to (or "out")) | |
out-file-path (str/join "/" [out-path ns-path]) | |
src-file-path (str/join "/" ["src" ns-path]) | |
cljs-file-path (str src-file-path ".cljs") | |
cljs-contents (line-seq (io/reader cljs-file-path)) | |
js-file-path (str out-file-path ".js") | |
source-map-path (str js-file-path ".map") | |
source-map-file (io/file source-map-path)] | |
(if-not (.exists source-map-file) | |
(binding [*out* *err*] | |
(println (str "Couldn't find source map \"" source-map-file "\""))) | |
(when-let [line (->> cljs-contents | |
(keep-indexed #(when (str/includes? %2 form-str) %1)) | |
first)] | |
(let [line->cols (-> (slurp source-map-file) | |
(json/read-str :key-fn keyword) | |
sm/decode-reverse | |
first | |
val) | |
target-line (->> (seq line->cols) | |
(sort-by #(Math/abs (- (key %) line))) | |
first | |
key) | |
lines-n (-> (get line->cols target-line) | |
(->> (mapcat val) | |
(map :gline)) | |
sort) | |
js-lines (vec (line-seq (io/reader js-file-path))) | |
start-line (let [v (- (first lines-n) offset)] | |
(if (neg? v) 0 v)) | |
end-line (let [v (+ (last lines-n) offset)] | |
(if (>= v (count js-lines)) | |
(count js-lines) | |
v)) | |
mapped-js (->> (subvec js-lines start-line end-line) | |
(str/join "\n"))] | |
mapped-js)))) | |
(catch Exception e | |
(prn e))))) | |
#?(:clj | |
(defmacro prn-js-location [& args] | |
(when-let [v (apply map-location &env &form args)] | |
(binding [*out* *err*] | |
(println v))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment