Skip to content

Commit da94bb0

Browse files
committed
Canonicalize form to fix cashing for splicing reader macro forms
1 parent 13cef99 commit da94bb0

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/nextjournal/clerk/analyzer.clj

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,23 @@
639639
(vary-meta dissoc :type)))
640640
form))
641641

642+
643+
(defn ^:private canonicalize-form
644+
"Undoes the non-deterministic transformations done by the splicing
645+
reader macro."
646+
[form]
647+
(walk/postwalk (fn [f]
648+
(if-let [orig-name (and (simple-symbol? f)
649+
(second (re-matches #"(.*)__\d+__auto__" (name f))))]
650+
(symbol (str orig-name "#"))
651+
f))
652+
form))
653+
654+
(comment
655+
(canonicalize-form `foo-bar###)
656+
(canonicalize-form `(let [a# 1]
657+
(inc a#))))
658+
642659
(defn hash-codeblock [->hash {:keys [ns graph record-missing-hash-fn]} {:as codeblock :keys [hash form id vars graph-node]}]
643660
(let [deps (when id (dep/immediate-dependencies graph id))
644661
hashed-deps (into #{} (keep ->hash) deps)]
@@ -653,7 +670,7 @@
653670
:dep-with-missing-hash dep-with-missing-hash
654671
:graph-node graph-node :ns ns))))
655672
(sha1-base58 (binding [*print-length* nil]
656-
(pr-str (set/union (conj hashed-deps (if form (remove-type-meta form) hash))
673+
(pr-str (set/union (conj hashed-deps (if form (-> form remove-type-meta canonicalize-form) hash))
657674
vars))))))
658675

659676
#_(hash-codeblock {} {:graph (dep/graph)} {})

test/nextjournal/clerk/analyzer_test.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,16 @@
287287
")]
288288
(is (not (:no-cache? (get ->analysis-info 'nextjournal.clerk.analyzer-test.redefs/a))))
289289
(is (not (:no-cache? (get ->analysis-info 'nextjournal.clerk.analyzer-test.redefs/b))))
290-
(is (not (:no-cache? (get ->analysis-info 'nextjournal.clerk.analyzer-test.redefs/a#2)))))))
290+
(is (not (:no-cache? (get ->analysis-info 'nextjournal.clerk.analyzer-test.redefs/a#2))))))
291+
292+
293+
(testing "hashing is determistics for splicing reader macros"
294+
(let [hash-single-form (fn [form]
295+
(ana/hash-codeblock {} {:graph (dep/graph)} (ana/analyze form)))]
296+
(is (= (hash-single-form `(let [a# 1]
297+
(inc a#)))
298+
(hash-single-form `(let [a# 1]
299+
(inc a#))))))))
291300

292301
(deftest analyze-doc
293302
(testing "reading a bad block shows block and file info in raised exception"

0 commit comments

Comments
 (0)