/
debug.clj
96 lines (82 loc) · 2.52 KB
/
debug.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(ns reagent.debug
(:refer-clojure :exclude [prn println time]))
(defmacro log
"Print with console.log, if it exists."
[& forms]
`(when reagent.debug.has-console
(.log js/console ~@forms)))
(defmacro warn
"Print with console.warn."
[& forms]
(when *assert*
`(when reagent.debug.has-console
(.warn (if reagent.debug.tracking
reagent.debug.track-console js/console)
(str "Warning: " ~@forms)))))
(defmacro warn-unless
[cond & forms]
(when *assert*
`(when (not ~cond)
(warn ~@forms))))
(defmacro error
"Print with console.error."
[& forms]
(when *assert*
`(when reagent.debug.has-console
(.error (if reagent.debug.tracking
reagent.debug.track-console js/console)
(str ~@forms)))))
(defmacro println
"Print string with console.log"
[& forms]
`(log (str ~@forms)))
(defmacro prn
"Like standard prn, but prints using console.log (so that we get
nice clickable links to source in modern browsers)."
[& forms]
`(log (pr-str ~@forms)))
(defmacro dbg
"Useful debugging macro that prints the source and value of x,
as well as package name and line number. Returns x."
[x]
(let [ns (str cljs.analyzer/*cljs-ns*)]
`(let [x# ~x]
(println (str "dbg "
~ns ":"
~(:line (meta &form))
": "
~(pr-str x)
": "
(pr-str x#)))
x#)))
(defmacro dev?
"True if assertions are enabled."
[]
(if *assert* true false))
(defmacro time [& forms]
(let [ns (str cljs.analyzer/*cljs-ns*)
label (str ns ":" (:line (meta &form)))]
`(let [label# ~label
res# (do
(js/console.time label#)
~@forms)]
(js/console.timeEnd label#)
res#)))
(defmacro assert-some [value tag]
`(assert ~value (str ~tag " must not be nil")))
(defmacro assert-component [value]
`(assert (comp/reagent-component? ~value)
(str "Expected a reagent component, not "
(pr-str ~value))))
(defmacro assert-js-object [value]
`(assert (not (map? ~value))
(str "Expected a JS object, not "
(pr-str ~value))))
(defmacro assert-new-state [value]
`(assert (or (nil? ~value) (map? ~value))
(str "Expected a valid new state, not "
(pr-str ~value))))
(defmacro assert-callable [value]
`(assert (ifn? ~value)
(str "Expected something callable, not "
(pr-str ~value))))