Skip to content

Commit 373f320

Browse files
committed
Tag known-numeric infix and bit ops to skip nullish coalesce in str
1 parent b25101e commit 373f320

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

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 [#815](https://github.com/squint-cljs/squint/issues/815): `str` wrapping known-numeric infix expressions in `??''`, which esbuild flagged as `suspicious-nullish-coalescing`.
8+
59
## 0.11.189
610

711
- [#809](https://github.com/squint-cljs/squint/issues/809): add `squint.compiler/compile*` and `squint.compiler/transpile*` which accept either a string or a sequence of pre-parsed forms, skipping the `forms -> string -> forms` roundtrip for SSR use cases. The previous names `compile-string*` / `transpile-string*` are retained as deprecated aliases.

src/squint/compiler_common.cljc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@
242242
(def boolean-infix-operators
243243
#{"==" "===" "<" ">" "<=" ">=" "!=" "instanceof"})
244244

245+
(def numeric-infix-operators
246+
#{"+" "+=" "-" "-=" "/" "*" "%" "<<" ">>" "<<<" ">>>" "&" "|"
247+
"bit-or" "bit-and" "js-mod"})
248+
245249
(def chainable-infix-operators #{"+" "-" "*" "/" "&" "|" "&&" "||" "bit-or" "bit-and" "js-??"})
246250

247251
(defn infix-operator? [env expr]
@@ -263,7 +267,8 @@
263267
(let [env (assoc enc-env :context :expr :top-level false)
264268
acount (count args)
265269
op-name (name operator)
266-
bool? (contains? boolean-infix-operators op-name)]
270+
bool? (contains? boolean-infix-operators op-name)
271+
numeric? (contains? numeric-infix-operators op-name)]
267272
(if (and (not (chainable-infix-operators op-name)) (> acount 2))
268273
(emit (list 'cljs.core/&&
269274
(list operator (first args) (second args))
@@ -289,7 +294,8 @@
289294
wrap-parens
290295
(emit-return enc-env)
291296
(cond->
292-
bool? (tagged-expr 'boolean))))))
297+
bool? (tagged-expr 'boolean)
298+
numeric? (tagged-expr 'number))))))
293299

294300
(def core-vars (atom #{}))
295301

@@ -980,10 +986,12 @@
980986
(emit-return (wrap-parens (str "new " (emit class (expr-env env)) (comma-list (emit-args env args)))) env))
981987

982988
(defmethod emit-special 'dec [_type env [_ var]]
983-
(emit-return (str "(" (emit var (assoc env :context :expr)) " - " 1 ")") env))
989+
(-> (emit-return (str "(" (emit var (assoc env :context :expr)) " - " 1 ")") env)
990+
(tagged-expr 'number)))
984991

985992
(defmethod emit-special 'inc [_type env [_ var]]
986-
(emit-return (str "(" (emit var (assoc env :context :expr)) " + " 1 ")") env))
993+
(-> (emit-return (str "(" (emit var (assoc env :context :expr)) " + " 1 ")") env)
994+
(tagged-expr 'number)))
987995

988996
(defmethod emit-special 'while [_type env [_while test & body]]
989997
(str "while (" (emit test (expr-env env)) ") { \n"

src/squint/internal/macros.cljc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@
306306
(defn- bool-expr [e]
307307
(vary-meta e assoc :tag 'boolean))
308308

309+
(defn- num-expr [e]
310+
(vary-meta e assoc :tag 'number))
311+
309312
(defn core-exists?
310313
"Return true if argument exists, analogous to usage of typeof operator
311314
in JavaScript."
@@ -543,7 +546,7 @@
543546
(core/list 'js* "(~ ~{})" x))
544547

545548
(core/defmacro ^::ana/numeric bit-and
546-
([x y] (core/list 'js* "(~{} & ~{})" x y))
549+
([x y] (num-expr (core/list 'js* "(~{} & ~{})" x y)))
547550
([x y & more] `(cc/bit-and (cc/bit-and ~x ~y) ~@more)))
548551

549552
;; internal do not use
@@ -552,7 +555,7 @@
552555
([x y & more] `(cc/unsafe-bit-and (cc/unsafe-bit-and ~x ~y) ~@more)))
553556

554557
(core/defmacro ^::ana/numeric bit-or
555-
([x y] (core/list 'js* "(~{} | ~{})" x y))
558+
([x y] (num-expr (core/list 'js* "(~{} | ~{})" x y)))
556559
([x y & more] `(cc/bit-or (cc/bit-or ~x ~y) ~@more)))
557560

558561
(core/defmacro ^::ana/numeric int [x]

0 commit comments

Comments
 (0)