Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check for extracting var from definition #484

Merged
merged 10 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Changes can be:

* 💫 Assign `:name` to every viewer in `default-viewers`

* 🐞 Show correct non-var return value for deflike form, fixes [#499](https://github.com/nextjournal/clerk/issues/499)

## 0.14.919 (2023-06-13)

* 🚨 Breaking Changes:
Expand Down
2 changes: 2 additions & 0 deletions src/nextjournal/clerk/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
(keep :var)
(map symbol))
nodes)

var (when (and (= 1 (count vars))
(deflike? form))
(first vars))
Expand Down Expand Up @@ -217,6 +218,7 @@
#_(analyze '(intern *ns* 'foo :bar))
#_(analyze '(import javax.imageio.ImageIO))
#_(analyze '(defmulti foo :bar))
#_(analyze '(declare a))
#_(analyze '^{:nextjournal.clerk/hash-fn (fn [_] (clerk/valuehash (slurp "notebooks/hello.clj")))}
(def contents
(slurp "notebooks/hello.clj")))
Expand Down
9 changes: 7 additions & 2 deletions src/nextjournal/clerk/eval.clj
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,21 @@
(find-var var)
result)
var-value (cond-> result (and var (var? result)) deref)
var-from-def? (and var (var? result) (= var (symbol result)))
no-cache? (or ns-effect?
no-cache?
(boolean (seq @!interned-vars))
config/cache-disabled?)]
(when (and (not no-cache?) (not ns-effect?) freezable? (cachable-value? var-value))
(when (and (not no-cache?)
(not ns-effect?)
freezable?
(cachable-value? var-value)
(or (not var) var-from-def?))
(cache! digest-file var-value))
(let [blob-id (cond no-cache? (analyzer/->hash-str var-value)
(fn? var-value) nil
:else hash)
result (if var
result (if var-from-def?
(var-from-def var)
result)]
(cond-> (wrapped-with-metadata result blob-id)
Expand Down
6 changes: 6 additions & 0 deletions test/nextjournal/clerk/eval_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
{:result {:nextjournal/value var?}}]}
(eval/eval-string "(def foo :bar) (var foo)"))))

(testing "definitions occurring in side effects from macro expansions should not end up wrapped in var-from-def maps as the cell result"
(is (= :my-value
(-> (eval/eval-string "(ns nextjournal.clerk.eval-test.def-side-effects {:nextjournal.clerk/no-cache true})
(defmacro define [name val] `(do (def ~name ~val) ~val))
(define my-value :my-value)") :blocks peek :result :nextjournal/value))))

(testing "can handle unbounded sequences"
(is (match? {:blocks [{:result {:nextjournal/value seq?}}]}
(eval/eval-string "(range)")))
Expand Down