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

CLJS-3421: Throw when calling ana-api/ns-publics on non-existing ns #236

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 16 additions & 7 deletions src/main/clojure/cljs/analyzer/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"This is intended to be a stable api for those who need programmatic access
to the analyzer."
(:refer-clojure :exclude [all-ns ns-interns ns-resolve resolve find-ns
ns-publics remove-ns])
ns-publics remove-ns the-ns])
#?(:clj (:require [cljs.analyzer :as ana]
[cljs.env :as env]
[cljs.util :as util]
Expand Down Expand Up @@ -227,16 +227,27 @@
{:pre [(symbol? sym)]}
(get-in @state [::ana/namespaces sym])))

(defn the-ns
"Given a namespace return the corresponding namespace analysis map, throwing an
exception if not found. Analagous to clojure.core/the-ns."
([ns]
(the-ns env/*compiler* ns))
([state sym]
{:pre [(symbol? sym)]}
(or (find-ns state sym)
(throw (ex-info (str "No namespace found: " sym) {:ns sym})))))

(defn ns-interns
"Given a namespace return all the var analysis maps. Analagous to
clojure.core/ns-interns but returns var analysis maps not vars."
([ns]
(ns-interns env/*compiler* ns))
([state ns]
{:pre [(symbol? ns)]}
(merge
(get-in @state [::ana/namespaces ns :macros])
(get-in @state [::ana/namespaces ns :defs]))))
(let [ns (the-ns state ns)]
(merge
(:macros ns)
(:defs ns)))))

(defn ns-publics
"Given a namespace return all the public var analysis maps. Analagous to
Expand All @@ -245,9 +256,7 @@
(ns-publics env/*compiler* ns))
([state ns]
{:pre [(symbol? ns)]}
(->> (merge
(get-in @state [::ana/namespaces ns :macros])
(get-in @state [::ana/namespaces ns :defs]))
(->> (ns-interns state ns)
(remove (fn [[k v]] (:private v)))
(into {}))))

Expand Down
6 changes: 6 additions & 0 deletions src/test/clojure/cljs/analyzer_api_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@
(is (= {:a 1} (ana-api/get-js-index state)))
(ana-api/with-state state
(is (= {:a 1} (ana-api/get-js-index))))))

(deftest throw-test
(let [state (atom {})]
(is (thrown? Exception (ana-api/the-ns state 'non.existing)))
(is (thrown? Exception (ana-api/ns-interns state 'non.existing)))
(is (thrown? Exception (ana-api/ns-publics state 'non.existing)))))
Loading