Skip to content

Commit e6ad026

Browse files
mfikesswannodette
authored andcommitted
CLJS-2267: Allow ^:const inlined vars to affect if emission
1 parent 39b6c26 commit e6ad026

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/main/clojure/cljs/compiler.cljc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,19 @@
447447
(when-not (= :statement (:context env))
448448
(emit-wrap env (emit-constant form))))
449449

450-
(defn truthy-constant? [{:keys [op form]}]
451-
(and (= op :constant)
452-
form
453-
(not (or (and (string? form) (= form ""))
454-
(and (number? form) (zero? form))))))
455-
456-
(defn falsey-constant? [{:keys [op form]}]
457-
(and (= op :constant)
458-
(or (false? form) (nil? form))))
450+
(defn truthy-constant? [{:keys [op form const-expr]}]
451+
(or (and (= op :constant)
452+
form
453+
(not (or (and (string? form) (= form ""))
454+
(and (number? form) (zero? form)))))
455+
(and (some? const-expr)
456+
(truthy-constant? const-expr))))
457+
458+
(defn falsey-constant? [{:keys [op form const-expr]}]
459+
(or (and (= op :constant)
460+
(or (false? form) (nil? form)))
461+
(and (some? const-expr)
462+
(falsey-constant? const-expr))))
459463

460464
(defn safe-test? [env e]
461465
(let [tag (ana/infer-tag env e)]

src/test/cljs/cljs/core_test.cljs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,23 @@
14111411
(is (= "#js {:foo/bar 33}" (pr-str (doto (js-obj) (gobject/set "foo/bar" 33)))))
14121412
(is (= "#js {:foo/bar #:var{:quux 66}}" (pr-str (doto (js-obj) (gobject/set "foo/bar" {:var/quux 66}))))))))
14131413

1414+
(def ^:const true-2267 true)
1415+
(def ^:const false-2267 false)
1416+
(def ^:const nil-2267 nil)
1417+
(def ^:const empty-string-2267 "")
1418+
(def ^:const non-empty-string-2267 "x")
1419+
(def ^:const zero-2267 0)
1420+
(def ^:const non-zero-2267 1)
1421+
1422+
(deftest test-cljs-2267
1423+
(is (= :then (if true-2267 :then :else)))
1424+
(is (= :else (if false-2267 :then :else)))
1425+
(is (= :else (if nil-2267 :then :else)))
1426+
(is (= :then (if empty-string-2267 :then :else)))
1427+
(is (= :then (if non-empty-string-2267 :then :else)))
1428+
(is (= :then (if zero-2267 :then :else)))
1429+
(is (= :then (if non-zero-2267 :then :else))))
1430+
14141431
(comment
14151432
;; ObjMap
14161433
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)