Skip to content

Commit

Permalink
Improve #1817 solution (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 5, 2022
1 parent 7cfeea8 commit 1ac26fd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ For a list of breaking changes, check [here](#breaking-changes).
- [#1819](https://github.com/clj-kondo/clj-kondo/issues/1819): Fix "Too many open files" in java class definition analysis caused by files not being closed ([@rsauex](https://github.com/rsauex))
- [#1821](https://github.com/clj-kondo/clj-kondo/issues/1821): Include vectors in `:unused-binding` config `:exclude-destructured-as` flag. ([@NoahTheDuke](https://github.com/NoahTheDuke))
- [#1818](https://github.com/clj-kondo/clj-kondo/issues/1818): unresolved var when using interop on var in CLJS
- [#1817](https://github.com/clj-kondo/clj-kondo/issues/1817): improve warning with invalid require libspec ([@benjamin-asdf](https://github.com/benjamin-asdf))

## 2022.09.08

Expand Down
81 changes: 39 additions & 42 deletions src/clj_kondo/impl/analyzer/namespace.clj
Expand Up @@ -65,8 +65,8 @@
(when (and prefix
(str/includes? (str form) "."))
(find-fn!
(format "found lib name '%s' containing period with prefix '%s'. lib names inside prefix lists must not contain periods."
form prefix)))
(format "found lib name '%s' containing period with prefix '%s'. lib names inside prefix lists must not contain periods."
form prefix)))
[(with-meta (token-node full-form)
(cond-> (assoc (meta libspec-expr)
:raw-name form)
Expand All @@ -78,12 +78,8 @@
(do (find-fn! "require form is invalid: clauses must not be empty") nil)
:else
(do
(find-fn! (format "Unparsable libspec %s" form))
(throw
(ex-info
"Unparsable namespace form. Is there a syntax error in a require call somewhere in the file?"
{:reason ::unparsable-ns-form
:form form}))))))
(find-fn! (format "Unparsable libspec: %s" form))
nil))))

(defn lint-alias-consistency [ctx ns-name alias]
(let [consistent-aliases (get-in ctx [:config :linters :consistent-alias :aliases])]
Expand Down Expand Up @@ -201,10 +197,11 @@
(recur
(nnext children)
(assoc m
:as (with-meta opt
(cond-> (meta opt-expr)
(identical? :as-alias child-k)
(assoc :as-alias true)))))
:as (when opt
(with-meta opt
(cond-> (meta opt-expr)
(identical? :as-alias child-k)
(assoc :as-alias true))))))
;; shadow-cljs:
;; https://shadow-cljs.github.io/docs/UsersGuide.html#_about_default_exports
:default
Expand All @@ -226,13 +223,13 @@
;; for :refer it is sufficient to pretend they were never referred
(update :referred set/difference (set (keys opt))))))
(do (findings/reg-finding!
ctx
(node->line
filename
child-expr
:unknown-require-option
(format "Unknown require option: %s"
child-k)))
ctx
(node->line
filename
child-expr
:unknown-require-option
(format "Unknown require option: %s"
child-k)))
(recur (nnext children)
m))))
(let [{:keys [:as :referred :excluded :referred-all :renamed]} m
Expand Down Expand Up @@ -264,8 +261,8 @@
(with-meta v
(meta node))
(do (findings/reg-finding!
ctx
(node->line (:filename ctx) node :syntax "Expected: class symbol"))
ctx
(node->line (:filename ctx) node :syntax "Expected: class symbol"))
nil)))

(defn analyze-import [ctx _ns-name libspec-expr]
Expand All @@ -279,16 +276,16 @@
(run! #(utils/handle-ignore ctx %) imported-nodes)
(cond (empty? children)
(findings/reg-finding!
ctx
(node->line
(:filename ctx) libspec-expr
:syntax "import form is invalid: clauses must not be empty"))
ctx
(node->line
(:filename ctx) libspec-expr
:syntax "import form is invalid: clauses must not be empty"))
(empty? imported-nodes)
(findings/reg-finding!
ctx
(node->line
(:filename ctx) java-package-name-node
:syntax "Expected: package name followed by classes.")))
ctx
(node->line
(:filename ctx) java-package-name-node
:syntax "Expected: package name followed by classes.")))
(into {} (for [i imported]
[i java-package])))
:token (let [package+class (:value libspec-expr)
Expand Down Expand Up @@ -347,15 +344,15 @@
:ns->aliases (when-not (-> ctx :config :linters :aliased-namespace-symbol :level
(identical? :off))
(reduce
(fn [acc sc]
(let [n (:ns sc)
as (:as sc)
existing (or (acc n) #{})]
(if as
(assoc acc n (conj existing as))
acc)))
{}
analyzed))
(fn [acc sc]
(let [n (:ns sc)
as (:as sc)
existing (or (acc n) #{})]
(if as
(assoc acc n (conj existing as))
acc)))
{}
analyzed))
:referred-vars (into {} (mapcat :referred analyzed))
:refer-alls refer-alls
:used-namespaces
Expand Down Expand Up @@ -585,10 +582,10 @@
children)]
(when (some-> children first sexpr empty-spec?)
(findings/reg-finding!
ctx
(node->line (:filename ctx)
(first children)
:syntax "require form is invalid: clauses must not be empty")))
ctx
(node->line (:filename ctx)
(first children)
:syntax "require form is invalid: clauses must not be empty")))
(when (:analyze-keywords? ctx)
(run! #(usages/analyze-usages2 ctx % {:quote? true}) libspecs))
(let [analyzed
Expand Down
8 changes: 7 additions & 1 deletion test/clj_kondo/main_test.clj
Expand Up @@ -1352,7 +1352,13 @@ foo/foo ;; this does use the private var
:col 5,
:level :error,
:message "namespace name expected"})
(lint! "(ns \"hello\")")))
(lint! "(ns \"hello\")"))
(assert-submaps
'({:file "<stdin>", :row 1, :col 19, :level :error, :message "Unparsable libspec: [foo oh-no :as]"})
(lint! "(ns foo (:require [foo oh-no :as]))" {:linters {:syntax {:level :error}}}))
(assert-submaps
'({:file "<stdin>", :row 1, :col 11, :level :error, :message "Unparsable libspec: [foo oh-no :as]"})
(lint! "(require '[foo oh-no :as])" {:linters {:syntax {:level :error}}})))

(deftest call-as-use-test
(is (empty?
Expand Down

0 comments on commit 1ac26fd

Please sign in to comment.