From efca94fd9c353ea514325b1aef73e8c9488b62bb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 2 Feb 2024 17:13:41 +0100 Subject: [PATCH] Fix #455: don't export public vars (#456) --- CHANGELOG.md | 4 ++++ src/squint/compiler.cljc | 6 +++--- src/squint/compiler_common.cljc | 3 ++- src/squint/internal/fn.cljc | 3 +++ test/squint/compiler_test.cljs | 5 +++++ 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 745d0ad6..010e6324 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ [Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect +## v0.6.89 (2024-02-01) + +- [#455](https://github.com/squint-cljs/squint/issues/455): don't export non-public vars + ## v0.6.88 (2024-01-10) - Fix infix operator in return position diff --git a/src/squint/compiler.cljc b/src/squint/compiler.cljc index 8118aad0..71702c07 100644 --- a/src/squint/compiler.cljc +++ b/src/squint/compiler.cljc @@ -17,11 +17,11 @@ #?(:cljs format) *aliases* *cljs-ns* *excluded-core-vars* *imported-vars* *public-vars* comma-list emit emit-args emit-infix emit-return escape-jsx - expr-env infix-operator? prefix-unary? statement suffix-unary?]] + expr-env infix-operator? prefix-unary? suffix-unary?]] [squint.defclass :as defclass] [squint.internal.deftype :as deftype] [squint.internal.destructure :refer [core-let]] - [squint.internal.fn :refer [core-defmacro core-defn core-fn]] + [squint.internal.fn :refer [core-defmacro core-defn core-defn- core-fn]] [squint.internal.loop :as loop] [squint.internal.macros :as macros] [squint.internal.protocols :as protocols]) @@ -83,7 +83,7 @@ 'extend-type protocols/core-extend-type 'deftype deftype/core-deftype 'defn core-defn - 'defn- core-defn + 'defn- core-defn- 'instance? macros/core-instance? 'time macros/core-time 'declare macros/core-declare diff --git a/src/squint/compiler_common.cljc b/src/squint/compiler_common.cljc index eac790bc..1c11f803 100644 --- a/src/squint/compiler_common.cljc +++ b/src/squint/compiler_common.cljc @@ -446,7 +446,8 @@ (defmethod emit-special 'def [_type env [_const & more :as expr]] (let [name (first more)] ;; TODO: move *public-vars* to :ns-state atom - (swap! *public-vars* conj (munge* name)) + (when-not (:private (meta name)) + (swap! *public-vars* conj (munge* name))) (swap! (:ns-state env) (fn [state] (let [current (:current state)] (assoc-in state [current name] {})))) diff --git a/src/squint/internal/fn.cljc b/src/squint/internal/fn.cljc index 4b117322..c7f55250 100644 --- a/src/squint/internal/fn.cljc +++ b/src/squint/internal/fn.cljc @@ -350,6 +350,9 @@ (with-meta (cons `fn fdecl) (assoc m ::def true))))) +(defn core-defn- [_&form _&env name & args] + `(clojure.core/defn ~(vary-meta name assoc :private true) ~@args)) + (defn core-defmacro "Like defn, but the resulting function name is declared as a macro and will be used as a macro by the compiler when it is diff --git a/test/squint/compiler_test.cljs b/test/squint/compiler_test.cljs index d08d6fe5..6e671e81 100644 --- a/test/squint/compiler_test.cljs +++ b/test/squint/compiler_test.cljs @@ -2215,5 +2215,10 @@ new Foo();") (deftest infix-return-test (is (true? (jsv! "(defn foo [x] (and (int? x) (< 10 x 18))) (foo 12)")))) +(deftest no-private-export-test + (let [exports (:exports (compiler/compile-string* "(defn- foo []) (defn bar [])"))] + (is (not (str/includes? exports "foo"))) + (is (str/includes? exports "bar")))) + (defn init [] (t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test))