From fdae1df140c1d82b7ce30713b1de7daac6ef5a16 Mon Sep 17 00:00:00 2001 From: AbhinavOmprakash Date: Sat, 11 Jan 2025 14:01:58 +0200 Subject: [PATCH] Fix issue with cljs not running because of the mock analyzer file Background - I made this change so that uers can exclude clojurescript as a dependency if they are only using it for clojure but that caused a lot of problems. I guess the only way to get it to work is to have them as separate jars --- src/snitch/analyzer.cljc | 17 ----------------- src/snitch/core.cljc | 30 +++++++++++++++--------------- test/snitch/core_test.cljc | 9 +++++---- 3 files changed, 20 insertions(+), 36 deletions(-) delete mode 100644 src/snitch/analyzer.cljc diff --git a/src/snitch/analyzer.cljc b/src/snitch/analyzer.cljc deleted file mode 100644 index daf2169..0000000 --- a/src/snitch/analyzer.cljc +++ /dev/null @@ -1,17 +0,0 @@ -(ns snitch.analyzer - "Mocking the clojurescript.analyzer ns. - Required if you want to exclude cljs deps for a clj only project - " - (:refer-clojure :exclude [macroexpand-1])) - - -(try - (require '[cljs.analyzer :as ana]) - (catch Exception _)) - - -(defn macroexpand-1 - [env form] - (if (resolve 'ana) - (eval '(ana/macroexpand-1 env form)) - form)) diff --git a/src/snitch/core.cljc b/src/snitch/core.cljc index 9408dfd..dc71060 100644 --- a/src/snitch/core.cljc +++ b/src/snitch/core.cljc @@ -1,8 +1,8 @@ (ns snitch.core (:refer-clojure :exclude [#?(:cljs macroexpand)]) (:require - [clojure.string :as s] - [snitch.analyzer :as ana]) + [cljs.analyzer :as ana] + [clojure.string :as s]) #?(:cljs (:require-macros [snitch.core]))) @@ -116,7 +116,7 @@ (defn cc-destructure - "A slightly modified version of clj and cljs' destructure to + "A slightly modified version of clj and cljs' destructure to work with clj and cljs." [bindings] (let [bents (partition 2 bindings) @@ -244,8 +244,8 @@ (defn insert-into-let ([bindings] (reduce (fn [acc [var* value]] - ;; if the var is introduced by - ;; clojure's destructuring it makes + ;; if the var is introduced by + ;; clojure's destructuring it makes ;; no sense to globally define it, since no one ;; will know about it, and its wasteful. (if (autogensym? var*) @@ -263,7 +263,7 @@ (defn define-let-bindings - "injects inline defs inside let bindings, and adds metadata indicating + "injects inline defs inside let bindings, and adds metadata indicating that the form contains inline defs. This is to prevent let forms from repeatedly being passed to `insert-into-let`. " @@ -312,11 +312,11 @@ (defn namespaced-destructuring "namespaced maps can be destructured in two ways. [{:keys [a/b]}] #:a{:b 1} - or + or [{:a/keys [b]}] #:a{:b 1} - This function handles the 2nd case. - Converts the 2nd case to the first case + This function handles the 2nd case. + Converts the 2nd case to the first case " [k v] (let [v* (mapv (fn [sym] @@ -327,9 +327,9 @@ (def preserve-symbol? "Certain values will get printed in a way that's not readable. - for e.g functions when evaluated return function Objects - like this - - identity + for e.g functions when evaluated return function Objects + like this - + identity ;=> #function[clojure.core/identity] These can't be evaluated, so we preserve the symbol as it is. " @@ -406,7 +406,7 @@ ;; There is some hairy quoting going on here so let me explain ;; let's say we have a function (defn* foo [x] x) ;; and I call it (foo 1) - ;; what I want from `replay-function` is to define a + ;; what I want from `replay-function` is to define a ;; var such that when I evaluate it, it returns me a list (foo 1) ;; that I can evaluate. ;; notice that here foo is a symbol but 1 is the value of the symbol x @@ -437,7 +437,7 @@ (or (vector? (:pre (first forms))) (vector? (:post (first forms)))) ;; test for (rest form) - ;; because the map shouldn't be treated + ;; because the map shouldn't be treated ;; as a prep-post map IFF there is no body ;; since a fn can't exist without a body (seq (rest forms)))) @@ -491,7 +491,7 @@ (defmacro defn* - "Like the defn macro but injects inline defs for the arguments + "Like the defn macro but injects inline defs for the arguments and any let bindings, or lambdas inside it." [name & forms] diff --git a/test/snitch/core_test.cljc b/test/snitch/core_test.cljc index ad20d91..82cf681 100644 --- a/test/snitch/core_test.cljc +++ b/test/snitch/core_test.cljc @@ -163,7 +163,7 @@ (testing "prep-post map identification works correctly for variadic args" ;; This exists because define-in-variadic-forms - ;; treated any map in the body as a prepost-map? + ;; treated any map in the body as a prepost-map? ;; instead of checking whether there was a body or not (let [_ (defn* foo7 ([a] {:a a}) @@ -185,10 +185,11 @@ (let [_ (defn* foo9 ([b] {:pre [(even? b)]} b))] - ;; AssertionError is thrown when - ;; prep-post maps are correctly recognised + ;; AssertionError is thrown when + ;; prep-post maps are correctly recognised ;; because 1 is odd - (is (thrown? AssertionError (foo9 1))) + #?(:clj (is (thrown? AssertionError (foo9 1))) + :cljs (is (thrown? js/Error (foo9 1)))) (is (= 2 (foo9 2))))) ;; FIXME commenting out the history feature because it doesn't work in cljs yet.