Skip to content

Commit d0df37c

Browse files
committed
Tag imports for interop
Fix [#2](#2).
1 parent 1b98854 commit d0df37c

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

src/noahtheduke/spat/ns_parser.clj

+19-17
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@
1717
form))
1818

1919
(defn parse-imports [args]
20-
(reduce
21-
(fn [acc cur]
22-
(cond
23-
(symbol? cur)
24-
(assoc acc cur cur)
25-
(seq? cur)
26-
(let [prefix (first cur)
27-
aliases (rest cur)]
28-
(reduce
29-
(fn [acc alias_]
30-
(let [full-name (symbol (str prefix "." alias_))]
31-
(-> acc
32-
(assoc alias_ full-name)
33-
(assoc full-name full-name))))
34-
acc aliases))
35-
:else acc))
36-
{} args))
20+
(persistent!
21+
(reduce
22+
(fn [acc cur]
23+
(cond
24+
(symbol? cur)
25+
(assoc! acc cur cur)
26+
(seq? cur)
27+
(let [prefix (first cur)
28+
aliases (rest cur)]
29+
(reduce
30+
(fn [acc alias_]
31+
(let [full-name (symbol (str prefix "." alias_))]
32+
(-> acc
33+
(assoc! alias_ full-name)
34+
(assoc! full-name full-name))))
35+
acc aliases))
36+
:else acc))
37+
(transient {})
38+
args)))
3739

3840
(defmethod derive-aliases 'import
3941
[[_ & args]]

src/noahtheduke/spat/parser.clj

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
(set! *warn-on-reflection* true)
1212

13+
(defn attach-import-meta [ns-state obj]
14+
(if-let [ns_ (and (symbol? obj) (some-> obj namespace symbol))]
15+
(if-let [fqns (get-in @ns-state [:imports ns_])]
16+
(vary-meta obj assoc :spat/import-ns fqns)
17+
obj)
18+
obj))
19+
1320
(defn make-edamame-opts [ns-state]
1421
{:all true
1522
:row-key :line
@@ -35,7 +42,8 @@
3542
;; Gotta apply location data here as using `:postprocess` skips automatic
3643
;; location data
3744
(if (e/iobj? obj)
38-
(vary-meta obj merge loc)
45+
(->> (vary-meta obj merge loc)
46+
(attach-import-meta ns-state))
3947
obj))
4048
:uneval (fn [{:keys [uneval next]}]
4149
(cond

src/noahtheduke/splint/rules/lint/fn_wrapper.clj

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44

55
(ns ^:no-doc noahtheduke.splint.rules.lint.fn-wrapper
66
(:require
7-
[noahtheduke.splint.rules :refer [defrule]]))
7+
[noahtheduke.splint.rules :refer [defrule]]
8+
[noahtheduke.splint.diagnostic :refer [->diagnostic]]
9+
[noahtheduke.splint.rules.helpers :refer [default-import?]]))
810

911
(set! *warn-on-reflection* true)
1012

13+
(defn interop? [sexp]
14+
(or (some->> sexp namespace symbol default-import?)
15+
(some->> sexp meta :spat/import-ns)))
16+
1117
(defrule lint/fn-wrapper
1218
"Avoid wrapping functions in pass-through anonymous function defitions.
1319
@@ -27,5 +33,7 @@
2733
"
2834
{:patterns ['(%fn?? [?arg] (?fun ?arg))
2935
'(%fn?? ([?arg] (?fun ?arg)))]
30-
:message "No need to wrap function. Clojure supports first-class functions."
31-
:replace '?fun})
36+
:on-match (fn [ctx rule form {:syms [?fun ?args]}]
37+
(when-not (interop? ?fun)
38+
(->diagnostic rule form {:replace-form ?fun
39+
:message "No need to wrap function. Clojure supports first-class functions."})))})

test/noahtheduke/splint/rules/lint/fn_wrapper_test.clj

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
(ns noahtheduke.splint.rules.lint.fn-wrapper-test
66
(:require
77
[expectations.clojure.test :refer [defexpect expect]]
8-
[noahtheduke.splint-test :refer [check-alt]]))
8+
[noahtheduke.splint-test :refer [check-alt check-all]]))
99

1010
(defexpect fn-wrapper-test
1111
(expect 'f (check-alt "(fn* [arg] (f arg))"))
1212
(expect 'f (check-alt "(fn [arg] (f arg))"))
1313
(expect 'f (check-alt "#(f %)")))
14+
15+
(defexpect interop-static-test
16+
(expect nil (check-alt "#(Integer/parseInt %)"))
17+
(expect nil (check-all "(do (import (java.util.regex Pattern)) #(Pattern/compile %))")))

0 commit comments

Comments
 (0)