Skip to content

Commit

Permalink
CLJS-3421: Throw when calling ana-api/ns-publics on non-existing ns
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored and swannodette committed Oct 15, 2024
1 parent b7ede4b commit f1fc819
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
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)))))

0 comments on commit f1fc819

Please sign in to comment.