Skip to content

Commit

Permalink
Fix clj-kondo#2199: add :syntax check for var names starting or endin…
Browse files Browse the repository at this point in the history
…g with dot (clj-kondo#2201)
  • Loading branch information
borkdude authored Oct 19, 2023
1 parent 43f2f6f commit d782fbf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ For a list of breaking changes, check [here](#breaking-changes).
## Unreleased

- [#1804](https://github.com/clj-kondo/clj-kondo/issues/1804): new linter `:self-requiring-namespace`
- [#2199](https://github.com/clj-kondo/clj-kondo/issues/2199): add `:syntax` check for var names starting or ending with dot (reserved by Clojure)
- [#2179](https://github.com/clj-kondo/clj-kondo/issues/2179): consider alias-as-object usage in CLJS for :unused-alias linter
- [#2183](https://github.com/clj-kondo/clj-kondo/issues/2183): respect `:level` in `:discouraged-var` config
- [#2184](https://github.com/clj-kondo/clj-kondo/issues/2184): Add missing documentation for :single-logical-operand linter
Expand Down
4 changes: 3 additions & 1 deletion src/clj_kondo/impl/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@
[name-node & children] (next (:children expr))
name-node-meta-nodes (:meta name-node)
name-node (when name-node (meta/lift-meta-content2 ctx name-node))
fn-name (:value name-node)
_ (utils/handle-ignore ctx name-node)
fn-name (some-> (:value name-node)
(with-meta (meta name-node)))
call (name (symbol-call expr))
var-leading-meta (meta name-node)
_ (when (identical? :clj (:lang ctx))
Expand Down
9 changes: 9 additions & 0 deletions src/clj_kondo/impl/namespace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@
(analysis/reg-var! ctx filename expr-row expr-col
ns-sym var-sym
metadata))
(when (and (or (str/starts-with? var-sym ".")
(str/ends-with? var-sym "."))
(and (not= '.. var-sym)
(not (one-of ns-sym [cljs.core clojure.core]))))
(findings/reg-finding! ctx (node->line
filename (let [thing (if (meta var-sym) var-sym expr)]
thing)
:syntax
(str "Symbols starting or ending with dot (.) are reserved by Clojure: " var-sym) )))
(when-not (:skip-reg-var ctx)
(swap! namespaces update-in path
(fn [ns]
Expand Down
12 changes: 12 additions & 0 deletions test/clj_kondo/main_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,18 @@ foo/foo ;; this does use the private var
'({: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 varname-syntax-test
;; TODO: linter is triggered twice, why?
;; TODO: clj-kondo/ignore doesn't work yet
(assert-submaps2
[{:file "<stdin>", :row 2, :col 7, :level :error, :message "Symbols starting or ending with dot (.) are reserved by Clojure: .foo"}
{:file "<stdin>", :row 3, :col 7, :level :error, :message "Symbols starting or ending with dot (.) are reserved by Clojure: bar."}]
(lint! "
(defn .foo [])
(defn bar. [])
(defn #_:clj-kondo/ignore .dude. [])
#_:clj-kondo/ignore (defn .dude2 [])" {:linters {:syntax {:level :error}}})))

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

0 comments on commit d782fbf

Please sign in to comment.