Skip to content

Commit

Permalink
Fix #255: fn literal with rest args (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Dec 29, 2024
1 parent e7d84af commit a876b04
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## v0.8.132 (2024-12-29)

- Fix [#255](https://github.com/squint-cljs/squint/issues/255): fn literal with rest args

## v0.8.131 (2024-12-21)

- [#596](https://github.com/squint-cljs/squint/issues/596): fix unary division to produce reciprocal
Expand Down
40 changes: 23 additions & 17 deletions src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -834,23 +834,29 @@ break;}" body)
signature (first expr)
arrow? (or (:arrow env) (:=> (meta signature)))
env (assoc env :arrow arrow?)]
(-> (if name
(let [body (rest expr)]
(str (when *async*
"async ") "function"
;; TODO: why is this duplicated here and in emit-function?
(when (:gen env)
"*")
" "
(munge name) " "
(emit-function env name signature body true)))
(let [body (rest expr)]
(str (emit-function env nil signature body))))
(cond-> (and
(not (:squint.internal.fn/def opts))
(not arrow?)
(= :expr (:context env))) (wrap-parens))
(emit-return env))))
(if (some #(= '& %) signature)
;; this still needs macro-expansion, see issue #599
(let [new-f (with-meta
(cons 'fn expr)
(meta expr))]
(emit new-f env))
(-> (if name
(let [body (rest expr)]
(str (when *async*
"async ") "function"
;; TODO: why is this duplicated here and in emit-function?
(when (:gen env)
"*")
" "
(munge name) " "
(emit-function env name signature body true)))
(let [body (rest expr)]
(str (emit-function env nil signature body))))
(cond-> (and
(not (:squint.internal.fn/def opts))
(not arrow?)
(= :expr (:context env))) (wrap-parens))
(emit-return env)))))

(defmethod emit-special 'fn* [_type env [_fn & sigs :as expr]]
(let [m (meta expr)
Expand Down
3 changes: 3 additions & 0 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2349,5 +2349,8 @@ new Foo();")
(is (= 0.5 (jsv! "(/ 2)")))
(is (= 0.5 (jsv! "(let [f (fn [] (/ 2))] (f))"))))

(deftest issue-599-test
(is (eq [1 2 3] (jsv! "(def f #(apply vector %&)) (f 1 2 3)"))))

(defn init []
(t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test 'squint.html-test))

0 comments on commit a876b04

Please sign in to comment.