Skip to content

Commit d2d7712

Browse files
committed
Merge branch 'main' into issue-746-macro-eval-pass
2 parents edd517b + 180902a commit d2d7712

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

src/nextjournal/clerk/analyzer.clj

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,7 @@
539539
init-state (if ran-macros?
540540
(init-state-fn)
541541
init-state)]
542-
(loop [{:as state :keys [->analysis-info analyzed-file-set counter]}
543-
init-state]
542+
(loop [{:as state :keys [->analysis-info analyzed-file-set counter]} init-state]
544543
(let [unhashed (unhashed-deps ->analysis-info)
545544
loc->syms (apply dissoc
546545
(group-by find-location unhashed)
@@ -569,6 +568,16 @@
569568
make-deps-inherit-no-cache
570569
(dissoc :analyzed-file-set :counter)))))))
571570

571+
(comment
572+
(reset! !file->analysis-cache {})
573+
574+
(def parsed (parser/parse-file {:doc? true} "src/nextjournal/clerk/webserver.clj"))
575+
(def analysis (time (-> parsed analyze-doc build-graph)))
576+
(-> analysis :->analysis-info keys set)
577+
(let [{:keys [->analysis-info]} analysis]
578+
(dissoc (group-by find-location (unhashed-deps ->analysis-info)) nil))
579+
(nextjournal.clerk/clear-cache!))
580+
572581
#_(do (time (build-graph (parser/parse-clojure-string (slurp "notebooks/how_clerk_works.clj")))) :done)
573582
#_(do (time (build-graph (parser/parse-clojure-string (slurp "notebooks/viewer_api.clj")))) :done)
574583

@@ -595,8 +604,7 @@
595604

596605

597606
(defn ^:private canonicalize-form
598-
"Undoes the non-deterministic transformations done by the splicing
599-
reader macro."
607+
"Undoes the non-deterministic transformations done by the splicing reader macro."
600608
[form]
601609
(walk/postwalk (fn [f]
602610
(if-let [orig-name (and (simple-symbol? f)
@@ -612,7 +620,7 @@
612620

613621
(defn hash-codeblock [->hash {:keys [ns graph record-missing-hash-fn]} {:as codeblock :keys [hash form id vars graph-node]}]
614622
(let [deps (when id (dep/immediate-dependencies graph id))
615-
hashed-deps (into #{} (keep ->hash) deps)]
623+
hashed-deps (into (sorted-set) (keep ->hash) deps)]
616624
;; NOTE: missing hashes on deps might occur e.g. when some dependencies are interned at runtime
617625
(when record-missing-hash-fn
618626
(when-some [dep-with-missing-hash
@@ -623,9 +631,14 @@
623631
(record-missing-hash-fn (assoc codeblock
624632
:dep-with-missing-hash dep-with-missing-hash
625633
:graph-node graph-node :ns ns))))
626-
(sha1-base58 (binding [*print-length* nil]
627-
(pr-str (set/union (conj hashed-deps (if form (-> form remove-type-meta canonicalize-form) hash))
628-
vars))))))
634+
(binding [*print-length* nil]
635+
(let [form-with-deps-sorted
636+
(-> hashed-deps
637+
(conj (if form
638+
(-> form remove-type-meta canonicalize-form pr-str)
639+
hash))
640+
(into (map str) vars))]
641+
(sha1-base58 (pr-str form-with-deps-sorted))))))
629642

630643
#_(hash-codeblock {} {:graph (dep/graph)} {})
631644
#_(hash-codeblock {} {:graph (dep/graph)} {:hash "foo"})

src/nextjournal/clerk/parser.cljc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
(defn read-string [s]
3535
(edamame/parse-string s {:all true
3636
:read-cond :allow
37-
:regex #(list `re-pattern %)
3837
:features #?(:bb #{:bb :clj}
3938
:default #{:clj})
4039
:end-location false

test/nextjournal/clerk/analyzer_test.clj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,25 @@
302302
(is (not (:no-cache? (get ->analysis-info 'nextjournal.clerk.analyzer-test.redefs/a#2))))))
303303

304304

305-
(testing "hashing is determistics for splicing reader macros"
305+
(testing "hashing is deterministic for auto-gensym-ed symbols in syntax-quote"
306306
(let [hash-single-form (fn [form]
307307
(ana/hash-codeblock {} {:graph (dep/graph)} (ana/analyze form)))]
308308
(is (= (hash-single-form `(let [a# 1]
309309
(inc a#)))
310310
(hash-single-form `(let [a# 1]
311-
(inc a#))))))))
311+
(inc a#)))))))
312+
313+
(testing "hashing is deterministic for regexes"
314+
(let [regex-graph #(->
315+
(parser/parse-clojure-string "
316+
(def id identity)
317+
318+
[+ - id #\"my\"]")
319+
(ana/analyze-doc)
320+
(ana/build-graph)
321+
(ana/hash)
322+
:->hash)]
323+
(apply = (repeatedly 100 regex-graph)))))
312324

313325
(deftest analyze-doc
314326
(utils/when-not-bb

test/nextjournal/clerk/eval_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,8 @@
310310
second (do (eval/eval-string ns)
311311
@@(resolve 'fixture-ns/state))]
312312
(is (= [2 0] [first second])))))
313+
314+
(deftest issue-741-can-eval-quoted-regex-test
315+
(is (match? {:blocks [{:type :code,
316+
:result {:nextjournal/value "foo"}}]}
317+
(eval/eval-string "(re-find '#\"foo\" \"foobar\")"))))

test/nextjournal/clerk/parser_test.clj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ par two"))))
167167
"^{:un :balanced :map} (do nothing)"))))
168168

169169
(deftest read-string-tests
170-
(testing "read-string should read regex's such that value equalility is preserved"
171-
(is (= '(fn [x] (clojure.string/split x (clojure.core/re-pattern "/")))
172-
(parser/read-string "(fn [x] (clojure.string/split x #\"/\"))"))))
173-
174170
(testing "read-string can handle syntax quote"
175171
(is (match? '['nextjournal.clerk.parser-test/foo 'nextjournal.clerk.view/foo 'nextjournal.clerk/foo]
176172
(binding [*ns* (find-ns 'nextjournal.clerk.parser-test)]

0 commit comments

Comments
 (0)