From 12342316a4eecf1c543f1f1dbfd283b6cc68dc68 Mon Sep 17 00:00:00 2001 From: Renan Ribeiro <55855728+cosineblast@users.noreply.github.com> Date: Sat, 11 Nov 2023 18:16:46 -0300 Subject: [PATCH 1/3] Fix #1753: :underscore-in-namespace linter (#2209) --- CHANGELOG.md | 1 + corpus/no_unused_namespace.clj | 2 +- doc/linters.md | 12 ++++++++++++ src/clj_kondo/impl/analyzer/namespace.clj | 11 +++++++++++ src/clj_kondo/impl/config.clj | 3 ++- test/clj_kondo/main_test.clj | 13 +++++++++++++ 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ba4d68bac..3c7489aeb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ For a list of breaking changes, check [here](#breaking-changes). ## Unreleased +- [#1753](https://github.com/clj-kondo/clj-kondo/issues/1753): new linter `:underscore-in-namespace` - [#2207](https://github.com/clj-kondo/clj-kondo/issues/2207): New `:condition-always-true` linter, see [docs](doc/linters.md) - [#2013](https://github.com/clj-kondo/clj-kondo/issues/2013): Fix NPE and similar errors when linting an import with an illegal token diff --git a/corpus/no_unused_namespace.clj b/corpus/no_unused_namespace.clj index a95d2b3874..a138db4954 100644 --- a/corpus/no_unused_namespace.clj +++ b/corpus/no_unused_namespace.clj @@ -1,4 +1,4 @@ -(ns no_unused-namespace +(ns no-unused-namespace (:require [clojure.string :as str])) (let [{score (if (str/starts-with? "foo" "f") diff --git a/doc/linters.md b/doc/linters.md index c4ef1e5965..cb9150e370 100644 --- a/doc/linters.md +++ b/doc/linters.md @@ -1948,3 +1948,15 @@ namespaces. Defaults to only warning when doing interop. The value of `:warn-only-on-interop` can be set to `false` to always warn in Clojure namespaces. + +## Underscore in namespace + +*Keyword:* `:underscore-in-namespace` + +*Description:* warns about the usage of the `_` character in the declaration of namespaces (as opposed to `-`). + +*Default level:* `:warning` + +*Example trigger:* `(ns special_files)` + +*Example message:* `Avoid underscore in namespace name: special_files` diff --git a/src/clj_kondo/impl/analyzer/namespace.clj b/src/clj_kondo/impl/analyzer/namespace.clj index f3bda95827..066a8e566e 100644 --- a/src/clj_kondo/impl/analyzer/namespace.clj +++ b/src/clj_kondo/impl/analyzer/namespace.clj @@ -548,6 +548,17 @@ ns-name-expr :namespace-name-mismatch (str "Namespace name does not match file name: " ns-name))))))) + + _ (when (and (not (identical? :off (-> ctx :config :linters :underscore-in-namespace :level))) + (symbol? ns-name) + (str/includes? (name ns-name) "_")) + (findings/reg-finding! + ctx + (node->line filename + ns-name-expr + :underscore-in-namespace + (str "Avoid underscore in namespace name: " ns-name)))) + clauses children _ (run! #(utils/handle-ignore ctx %) children) kw+libspecs (for [?require-clause clauses diff --git a/src/clj_kondo/impl/config.clj b/src/clj_kondo/impl/config.clj index 91bb9d8e44..3b32c82578 100644 --- a/src/clj_kondo/impl/config.clj +++ b/src/clj_kondo/impl/config.clj @@ -156,7 +156,8 @@ :protocol-method-varargs {:level :error} :unused-alias {:level :off} :self-requiring-namespace {:level :off} - :condition-always-true {:level :off}} + :condition-always-true {:level :off} + :underscore-in-namespace {:level :warning}} ;; :hooks {:macroexpand ... :analyze-call ...} :lint-as {cats.core/->= clojure.core/-> cats.core/->>= clojure.core/->> diff --git a/test/clj_kondo/main_test.clj b/test/clj_kondo/main_test.clj index 92183ad43d..b7baf028db 100644 --- a/test/clj_kondo/main_test.clj +++ b/test/clj_kondo/main_test.clj @@ -3426,6 +3426,19 @@ foo/"))) (assert-submaps2 expected (lint! "(import 3.14)")) (assert-submaps2 expected (lint! "(import -1)")))) +(deftest underscore-in-ns + + (let [expected #(list {:file "" + :row 1 :col 5 + :level :warning + :message (str "Avoid underscore in namespace name: " %)})] + + (assert-submaps2 (expected "beep_boop") (lint! "(ns beep_boop)")) + (assert-submaps2 (expected "beep_") (lint! "(ns beep_)")) + (assert-submaps2 (expected "_boop") (lint! "(ns _boop)")) + (assert-submaps2 (expected "never_give.you-up") (lint! "(ns never_give.you-up)")) + (assert-submaps2 (expected "a.large-smelly_dog") (lint! "(ns a.large-smelly_dog)")))) + ;;;; Scratch (comment From d32d9ff7f070f2287a51f806a6ae95ef5ea6639a Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 14 Nov 2023 22:26:24 +0100 Subject: [PATCH 2/3] README --- README.md | 6 +++++- doc/config.md | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 242f207822..826c4d7c78 100644 --- a/README.md +++ b/README.md @@ -276,11 +276,15 @@ pod](https://github.com/babashka/babashka.pods). ;;=> {:error 0, :warning 0, :info 0, :type :summary, :duration 779} ``` -## Podcast +## Podcasts + [defnpodcast](https://soundcloud.com/defn-771544745) + [ClojureScript Podcast](https://clojurescriptpodcast.com/) +## Articles + +- [Taking your linting to the next level](https://blog.tvaisanen.com/take-your-linting-game-to-the-next-level?showSharer=true#heading-benefits-of-types-in-the-editor) by Toni Vaisanen + ## Thanks to: - [joker](https://github.com/candid82/joker) for inspiration diff --git a/doc/config.md b/doc/config.md index bfdecc069b..94d410ffdb 100644 --- a/doc/config.md +++ b/doc/config.md @@ -658,6 +658,10 @@ This tells clj-kondo to copy clj-kondo configs from dependencies while linting. Typically, you will want to check copied configs into version control with your project. +See this +[article](https://blog.tvaisanen.com/take-your-linting-game-to-the-next-level?showSharer=true#heading-benefits-of-types-in-the-editor) +by Toni Vaisanen on how to use library configurations and malli! + ## Deprecations Some configuration keys have been renamed over time. The default configuration From c3366f95d43859b8d0d7792df5b49bd64b2d66ef Mon Sep 17 00:00:00 2001 From: Alexander Kouznetsov Date: Tue, 21 Nov 2023 14:06:35 -0800 Subject: [PATCH 3/3] Update hooks.md (#2220) Fix typo. --- doc/hooks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/hooks.md b/doc/hooks.md index 6ad8ffa904..65e16f1ed7 100644 --- a/doc/hooks.md +++ b/doc/hooks.md @@ -61,7 +61,7 @@ Use `println` or `prn` for debugging and `time` to measure performance. Hooks must be configured in clj-kondo's `config.edn` under `:hooks`, e.g.: ``` Clojure -{:hooks {:analyze-call {foo.weird-macro hooks.foo/weird-macro}}} +{:hooks {:analyze-call {foo/weird-macro hooks.foo/weird-macro}}} ``` ## analyze-call