Skip to content

Commit b9af4a0

Browse files
authored
Fix #647: emit explicit nil in else of if branch (#648)
1 parent e0218ee commit b9af4a0

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect
44

5+
## Unreleased
6+
7+
- Fix [#647](https://github.com/squint-cljs/squint/issues/647): emit explicit `null` when written in else branch of `if`
8+
59
## v0.8.142 (2025-03-19)
610

711
- Fix [#640](https://github.com/squint-cljs/squint/issues/640): don't emit anonymous function if it is a statement ([@jonasseglare](https://github.com/jonasseglare))

src/squint/compiler_common.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ break;}" body)
10801080
(str (format "if (%s) {\n" condition)
10811081
(emit then env)
10821082
"}"
1083-
(when (some? else)
1083+
(when (= 4 (count expr)) ;; explicit else branch
10841084
(str " else {\n"
10851085
(emit else env)
10861086
"}"))))))

test/squint/compiler_test.cljs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@
199199
(let [s (jss! "(let [x (do 1 (if (inc 1) (inc 2) (inc 3)))]
200200
x)")]
201201
(is (= 3 (js/eval s))))
202-
(is (eq #js {:a 1} (jsv! "{:a (or 1 (cond true (prn :yes)) 2)}"))))
202+
(is (eq #js {:a 1} (jsv! "{:a (or 1 (cond true (prn :yes)) 2)}")))
203+
(is (let [v (jsv! "(let [x false] (if x 1 nil))")]
204+
(and (nil? v)
205+
(not (undefined? v))))))
203206

204207
(deftest zero?-test
205208
(is (str/includes? (jss! "(if (zero? x) 1 2)") "== 0"))

0 commit comments

Comments
 (0)