From e0c58395e07d2f52bbe4d8b52df9471115ed4d28 Mon Sep 17 00:00:00 2001 From: Renan Ribeiro Date: Thu, 26 Oct 2023 09:39:43 +0000 Subject: [PATCH] #2031: Fix NPE when importing string (#2205) --- CHANGELOG.md | 4 ++++ src/clj_kondo/impl/analyzer/namespace.clj | 21 +++++++++++++++------ test/clj_kondo/main_test.clj | 6 ++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 090e6db464..613f2c17cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ For a list of breaking changes, check [here](#breaking-changes). +## Unreleased + +- [#2013](https://github.com/clj-kondo/clj-kondo/issues/2013): Fix NPE and similar errors when linting an import with an illegal token + ## 2023.10.20 - [#1804](https://github.com/clj-kondo/clj-kondo/issues/1804): new linter `:self-requiring-namespace` diff --git a/src/clj_kondo/impl/analyzer/namespace.clj b/src/clj_kondo/impl/analyzer/namespace.clj index 8585af57e5..f3bda95827 100644 --- a/src/clj_kondo/impl/analyzer/namespace.clj +++ b/src/clj_kondo/impl/analyzer/namespace.clj @@ -342,12 +342,21 @@ :syntax "Expected: package name followed by classes."))) (into {} (for [i imported] [i java-package]))) - :token (let [package+class (:value libspec-expr) - splitted (-> package+class name (str/split #"\.")) - java-package (symbol (str/join "." (butlast splitted))) - imported (with-meta (symbol (last splitted)) - (meta libspec-expr))] - {imported java-package}) + :token (if (symbol? (:value libspec-expr)) + (let [package+class (:value libspec-expr) + splitted (-> package+class name (str/split #"\.")) + java-package (symbol (str/join "." (butlast splitted))) + imported (with-meta (symbol (last splitted)) + (meta libspec-expr))] + {imported java-package}) + (do + (findings/reg-finding! + ctx + (node->line + (:filename ctx) + libspec-expr + :syntax "Import target is not a package")) + {})) nil)) (defn analyze-require-clauses [ctx ns-name kw+libspecs] diff --git a/test/clj_kondo/main_test.clj b/test/clj_kondo/main_test.clj index 3c55cf77ed..92183ad43d 100644 --- a/test/clj_kondo/main_test.clj +++ b/test/clj_kondo/main_test.clj @@ -3420,6 +3420,12 @@ foo/"))) (is (= "error" (:fail-level (#'clj-kondo.main/parse-opts ["--fail-level" "error"])))) (is (= "error" (:fail-level (#'clj-kondo.main/parse-opts ["--fail-level=error"]))))) +(deftest import-illegal-token + (let [expected (list {:file "", :row 1, :col 9, :level :error, :message "Import target is not a package"})] + (assert-submaps2 expected (lint! "(import \"\")")) + (assert-submaps2 expected (lint! "(import 3.14)")) + (assert-submaps2 expected (lint! "(import -1)")))) + ;;;; Scratch (comment