From b6c08ed54a41d3a3443ff008170cac373b78ed51 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 8 Oct 2024 14:40:18 +0200 Subject: [PATCH] CLJS-3421: Throw when calling ana-api/ns-publics on non-existing ns --- src/main/clojure/cljs/analyzer/api.cljc | 23 ++++++++++++++------ src/test/clojure/cljs/analyzer_api_tests.clj | 6 +++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/clojure/cljs/analyzer/api.cljc b/src/main/clojure/cljs/analyzer/api.cljc index cff4b97f4a..2d143a42b6 100644 --- a/src/main/clojure/cljs/analyzer/api.cljc +++ b/src/main/clojure/cljs/analyzer/api.cljc @@ -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] @@ -227,6 +227,16 @@ {: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." @@ -234,9 +244,10 @@ (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 @@ -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 {})))) diff --git a/src/test/clojure/cljs/analyzer_api_tests.clj b/src/test/clojure/cljs/analyzer_api_tests.clj index 831734c41e..243281ff6b 100644 --- a/src/test/clojure/cljs/analyzer_api_tests.clj +++ b/src/test/clojure/cljs/analyzer_api_tests.clj @@ -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)))))