Skip to content

Commit

Permalink
Add requiring-resolve
Browse files Browse the repository at this point in the history
Fixes #835
  • Loading branch information
mfikes committed Nov 20, 2018
1 parent efacffe commit 1ec186e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## [Unreleased]
### Added
- Add `requiring-resolve` ([#835](https://github.com/mfikes/planck/issues/835))

## [2.19.0] - 2018-11-02
### Changed
Expand Down
14 changes: 14 additions & 0 deletions planck-cljs/src/planck/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@
:args (s/cat :sym symbol?)
:ret (s/nilable var?))

(defn requiring-resolve
"Resolves namespace-qualified sym per 'resolve'. If initial resolve
fails, attempts to require sym's namespace and retries."
[sym]
(if (qualified-symbol? sym)
(or (resolve sym)
(do (eval `(require '~(-> sym namespace symbol)))
(resolve sym)))
(throw (js/Error. (str "Not a qualified symbol: " sym)))))

(s/fdef requiring-resolve
:args (s/cat :sym qualified-symbol?)
:ret (s/nilable var?))

(defn find-var
"Returns the global var named by the namespace-qualified symbol, or
nil if no var with that name."
Expand Down
10 changes: 9 additions & 1 deletion planck-cljs/src/planck/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2093,7 +2093,15 @@

(defn- ns-resolve
[ns sym]
(eval `(~'var ~sym) ns))
(let [result (atom nil)]
(binding [ana/*cljs-warnings* (zipmap (keys ana/*cljs-warnings*) (repeat false))]
(cljs/eval st `(~'var ~sym)
{:ns ns
:context :expr}
(fn [{:keys [value error]}]
(when-not error
(reset! result value)))))
@result))

(defn- resolve
[sym]
Expand Down
4 changes: 4 additions & 0 deletions planck-cljs/test/planck/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@
(deftest load-string-test
(is (= 3 (planck.core/load-string "1 2 3")))
(is (= "hi" (with-out-str (planck.core/load-string "1 (print \"hi\") 2")))))

(deftest requiring-resolve-test
(is (nil? (planck.core/resolve 'planck.requiring-resolve-ns/a)))
(is (= 3 @(planck.core/requiring-resolve 'planck.requiring-resolve-ns/a))))
3 changes: 3 additions & 0 deletions planck-cljs/test/planck/requiring_resolve_ns.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns planck.requiring-resolve-ns)

(def a 3)
11 changes: 11 additions & 0 deletions site/src/planck-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ _Vars_
[read-line](#read-line)<br/>
[read-password](#read-password)<br/>
[read-string](#read-string)<br/>
[requiring-resolve](#requiring-resolve)<br/>
[resolve](#resolve)<br/>
[sleep](#sleep)<br/>
[slurp](#slurp)<br/>
Expand Down Expand Up @@ -262,6 +263,16 @@ Spec<br/>
Spec<br/>
_args_: `(alt :unary (cat :s string?) :binary (cat :opts map? :s string?))`<br/><br/>

### <a name="requiring-resolve"></a>requiring-resolve
`([sym])`

Resolves namespace-qualified sym per [`resolve`](#resolve). If initial resolve
fails, attempts to `require` `sym`'s namespace and retries.

Spec<br/>
_args_: `(cat :sym qualified-symbol?)`<br/>
_ret_: `(nilable var?)`<br/>

### <a name="resolve"></a>resolve
`([sym])`

Expand Down

0 comments on commit 1ec186e

Please sign in to comment.