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

Add support for filename-pattern on ns-group #1793

Merged
merged 2 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For a list of breaking changes, check [here](#breaking-changes).
- [#1771](https://github.com/clj-kondo/clj-kondo/issues/1771): don't crash on empty ns clauses: `(require '[])` and `(import '())`
- [#1774](https://github.com/clj-kondo/clj-kondo/issues/1774): Add support for sourcehut inferred git dep urls for the Clojure CLI
- [#1768](https://github.com/clj-kondo/clj-kondo/issues/1768): Expose a `tag` function in `clj-kondo.hooks-api`
- [#1790](https://github.com/clj-kondo/clj-kondo/issues/1790): Add support for `:filename-pattern` in `:ns-group`

## 2022.08.03

Expand Down
3 changes: 3 additions & 0 deletions corpus/config_in_ns/my_custom_ns.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns config-in-ns.my-custom-ns)

(defn ^:private foo [])
2 changes: 1 addition & 1 deletion src/clj_kondo/impl/analyzer/namespace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@
:syntax
"namespace name expected"))))
'user)
ns-group (config/ns-group global-config ns-name)
ns-group (config/ns-group global-config ns-name filename)
config-in-ns (let [config-in-ns (:config-in-ns global-config)]
(config/merge-config!
(get config-in-ns ns-group)
Expand Down
9 changes: 6 additions & 3 deletions src/clj_kondo/impl/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,14 @@
(fn [config sym]
(contains? (delayed-cfg config) sym))))

(defn ns-group* [config ns-name]
(defn ns-group* [config ns-name filename]
(or (some (fn [{:keys [pattern
filename-pattern
name]}]
(when (and (string? pattern) (symbol? name)
(re-matches (re-pattern pattern) (str ns-name)))
(when (or (and (string? pattern) (symbol? name)
(re-matches (re-pattern pattern) (str ns-name)))
(and (string? filename-pattern) (symbol? name)
(re-matches (re-pattern filename-pattern) filename)))
name))
(:ns-groups config))
ns-name))
Expand Down
10 changes: 5 additions & 5 deletions src/clj_kondo/impl/linters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
(let [discouraged-var-config
(get-in (:config call) [:linters :discouraged-var])]
(when-not (empty? (dissoc discouraged-var-config :level))
(let [fn-lookup-sym (symbol (str (config/ns-group call-config resolved-ns))
(let [fn-lookup-sym (symbol (str (config/ns-group call-config resolved-ns filename))
(str fn-name))
]
(when-let [cfg (get discouraged-var-config fn-lookup-sym)]
Expand Down Expand Up @@ -518,14 +518,14 @@
(doseq [ns (namespace/list-namespaces ctx)
ns-sym (:required ns)
:let [ns-config (:config ns)
m (meta ns-sym)
filename (:filename m)
config (or ns-config config)
config-ns-sym (config/ns-group config ns-sym)
config-ns-sym (config/ns-group config ns-sym filename)
linter-config (get-in config [:linters :discouraged-namespace config-ns-sym])]
:when (some? linter-config)
:let [{:keys [message]
:or {message (str "Discouraged namespace: " ns-sym)}} linter-config
m (meta ns-sym)
filename (:filename m)]]
:or {message (str "Discouraged namespace: " ns-sym)}} linter-config]]
(findings/reg-finding!
ctx
(-> (node->line filename ns-sym :discouraged-namespace message)
Expand Down
9 changes: 8 additions & 1 deletion test/clj_kondo/config_in_ns_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns clj-kondo.config-in-ns-test
(:require
[clj-kondo.test-utils :refer [lint! assert-submaps]]
[clj-kondo.test-utils :refer [assert-submaps lint!]]
[clojure.java.io :as io]
[clojure.test :as t :refer [deftest is testing]]))

(deftest config-in-ns-test
Expand All @@ -19,3 +20,9 @@
'{:ns-groups [{:pattern "my.*" :name mine}]
:config-in-ns {mine {:linters {:discouraged-var {clojure.core/assoc {:message "No"}}}}
my.namespace {:linters {:unresolved-symbol {:level :off}}}}})))

(deftest config-in-ns-file-pattern-test
(is (empty?
(lint! (io/file "corpus" "config_in_ns" "my_custom_ns.clj")
'{:ns-groups [{:filename-pattern ".*config_in_ns.*" :name mine}]
:config-in-ns {mine {:linters {:unused-private-var {:level :off}}}}}))))