Skip to content

Commit 9f8ad16

Browse files
author
dnolen
committed
CLJS-2764: exists? is not nil safe
1 parent ab00c86 commit 9f8ad16

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/main/clojure/cljs/core.cljc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,15 @@
965965
"Return true if argument exists, analogous to usage of typeof operator
966966
in JavaScript."
967967
[x]
968-
(bool-expr
969-
(core/list 'js* "typeof ~{} !== 'undefined'"
970-
(vary-meta x assoc :cljs.analyzer/no-resolve true))))
968+
(let [x (:name (cljs.analyzer/resolve-var &env x))
969+
segs (string/split (core/str (string/replace x #"\/" ".")) #"\.")
970+
n (count segs)
971+
syms (map
972+
#(vary-meta (symbol "js" (string/join "." %))
973+
assoc :cljs.analyzer/no-resolve true)
974+
(reverse (take n (iterate butlast segs))))
975+
js (string/join " && " (repeat n "(typeof ~{} !== 'undefined')"))]
976+
(bool-expr (concat (core/list 'js* js) syms))))
971977

972978
(core/defmacro undefined?
973979
"Return true if argument is identical to the JavaScript undefined value."

src/test/cljs/cljs/core_test.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,12 @@
727727
(is (false? (exists? js/jQuery)))
728728
(is (exists? exists?-test-val))))
729729

730+
(deftest test-2764
731+
(testing "Testing CLJS-2764, exists? on multi-segment symbols"
732+
(is (false? (exists? this.ns.does.not.exist)))
733+
(is (true? (exists? cljs.core.first)))
734+
(is (true? (exists? cljs.core/first)))))
735+
730736
(deftest test-518
731737
(is (nil? (:test "test"))))
732738

0 commit comments

Comments
 (0)