Skip to content

Commit 49dfc24

Browse files
committed
properly handle ##NaN and ##Inf (close #54)
The problem seems to be devtools JSON-ML implementation. They seem to coerce javascript's NaN and Infinity to null display. We would prefer clojure notation anyways, hence we force pr-str printing of these weird numbers. Also added custom styling options for those cases.
1 parent b96a512 commit 49dfc24

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns devtools-sample.tests.issue54
2+
(:require-macros [devtools-sample.logging :refer [log info]])
3+
(:require [devtools-sample.boot :refer [boot!]]))
4+
5+
(boot! "/src/tests/devtools_sample/tests/issue54.cljs")
6+
7+
(enable-console-print!)
8+
9+
; --- MEAT STARTS HERE -->
10+
(js/console.log ##NaN ##Inf ##-Inf)
11+
(js/console.log [##NaN ##Inf ##-Inf])
12+
13+
; <-- MEAT STOPS HERE ---

src/lib/devtools/defaults.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
:keyword [136 19 145]
2828
:integer [28 0 207]
2929
:float [28 136 207]
30+
:float-nan [213 60 27]
31+
:float-infinity [28 80 207]
3032
:string [196 26 22]
3133
:expanded-string [255 100 100]
3234
:symbol [0 0 0]

src/lib/devtools/defaults.cljs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
:symbol-tag [:span :symbol-style]
154154
:integer-tag [:span :integer-style]
155155
:float-tag [:span :float-style]
156+
:float-nan-tag [:span :float-nan-style]
157+
:float-infinity-tag [:span :float-infinity-style]
156158
:string-tag [:span :string-style]
157159
:expanded-string-tag [:span :expanded-string-style]
158160
:circular-reference-tag [:span :circular-reference-wrapper-style]
@@ -246,6 +248,8 @@
246248
:keyword-style (css (str "color: " (named-color :keyword) ";"))
247249
:integer-style (css (str "color: " (named-color :integer) ";"))
248250
:float-style (css (str "color: " (named-color :float) ";"))
251+
:float-nan-style (css (str "color: " (named-color :float-nan) ";"))
252+
:float-infinity-style (css (str "color: " (named-color :float-infinity) ";"))
249253
:string-style (css (str "color: " (named-color :string) ";"))
250254
:symbol-style (css (str "color: " (named-color :symbol) ";"))
251255
:bool-style (css (str "color: " (named-color :bool) ";"))

src/lib/devtools/formatters/markup.cljs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@
7979
[:symbol-tag (str symbol)])
8080

8181
(defn <number> [number]
82-
(if (integer? number)
83-
[:integer-tag number]
84-
[:float-tag number]))
82+
(if (js/isFinite number)
83+
(if (integer? number)
84+
[:integer-tag number]
85+
[:float-tag number])
86+
(if (js/isNaN number)
87+
[:float-nan-tag (pr-str number)]
88+
[:float-infinity-tag (pr-str number)])))
8589

8690
; -- string markup ----------------------------------------------------------------------------------------------------------
8791

test/src/tests/devtools/tests/format.cljs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,42 @@
10311031
[:meta-body-tag
10321032
[:header-tag
10331033
CIRCULAR]]))))))))
1034+
1035+
(deftest test-issue-54
1036+
(testing "properly handle ##NaN and ##Inf"
1037+
(let [nan ##NaN
1038+
wrapped-nan [##NaN]
1039+
p-inf ##Inf
1040+
n-inf ##-Inf
1041+
wrapped-infs [##Inf ##-Inf]]
1042+
(is-header nan
1043+
[:cljs-land-tag
1044+
[:header-tag
1045+
[:float-nan-tag "##NaN"]]])
1046+
(has-body? nan false)
1047+
(is-header wrapped-nan
1048+
[:cljs-land-tag
1049+
[:header-tag
1050+
"["
1051+
[:float-nan-tag "##NaN"]
1052+
"]"]])
1053+
(has-body? wrapped-nan false)
1054+
(is-header p-inf
1055+
[:cljs-land-tag
1056+
[:header-tag
1057+
[:float-infinity-tag "##Inf"]]])
1058+
(has-body? p-inf false)
1059+
(is-header n-inf
1060+
[:cljs-land-tag
1061+
[:header-tag
1062+
[:float-infinity-tag "##-Inf"]]])
1063+
(has-body? n-inf false)
1064+
(is-header wrapped-infs
1065+
[:cljs-land-tag
1066+
[:header-tag
1067+
"["
1068+
[:float-infinity-tag "##Inf"]
1069+
:spacer
1070+
[:float-infinity-tag "##-Inf"]
1071+
"]"]])
1072+
(has-body? wrapped-infs false))))

0 commit comments

Comments
 (0)