Skip to content

Commit

Permalink
clj-kondo#2031: Fix NPE when importing string (clj-kondo#2205)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosineblast authored Oct 26, 2023
1 parent 00e3ae8 commit e0c5839
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ For a list of breaking changes, check [here](#breaking-changes).
<!-- - [ ] update lein-clj-kondo -->
<!-- - [ ] update carve -->

## 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`
Expand Down
21 changes: 15 additions & 6 deletions src/clj_kondo/impl/analyzer/namespace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions test/clj_kondo/main_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<stdin>", :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
Expand Down

0 comments on commit e0c5839

Please sign in to comment.