/
analyzer.clj
1085 lines (1042 loc) · 45 KB
/
analyzer.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns clj-kondo.impl.analyzer
{:no-doc true}
(:require
[clj-kondo.impl.config :as config]
[clj-kondo.impl.linters.keys :as key-linter]
[clj-kondo.impl.macroexpand :as macroexpand]
[clj-kondo.impl.metadata :as meta]
[clj-kondo.impl.namespace :as namespace :refer [analyze-ns-decl resolve-name]]
[clj-kondo.impl.node.seq] ;; load defrecord
[clj-kondo.impl.parser :as p]
[clj-kondo.impl.profiler :as profiler]
[clj-kondo.impl.schema :as schema]
[clj-kondo.impl.findings :as findings]
[clj-kondo.impl.utils :as utils :refer [some-call symbol-call keyword-call node->line
parse-string parse-string-all tag select-lang
vconj deep-merge one-of]]
[clojure.string :as str]
[rewrite-clj.node.protocols :as node]
[rewrite-clj.node.seq :as seq]
[rewrite-clj.node.token :as token])
(:import [clj_kondo.impl.node.seq NamespacedMapNode]))
(declare analyze-expression**)
(defn analyze-children [{:keys [:callstack :config] :as ctx} children]
(when-not (config/skip? config callstack)
(mapcat #(analyze-expression** ctx %) children)))
(defn analyze-keys-destructuring-defaults [ctx m defaults]
(let [defaults (into {}
(for [[k _v] (partition 2 (:children defaults))
:let [sym (:value k)]
:when sym]
[(:value k) (meta k)]))]
(doseq [[k v] defaults]
(when-not (contains? m k)
(findings/reg-finding!
(:findings ctx)
{:message (str k " is not bound in this destructuring form") :level :warning
:row (:row v)
:col (:col v)
:filename (:filename ctx)
:type :unbound-destructuring-default}))))
(analyze-children ctx (utils/map-node-vals defaults)))
(defn extract-bindings
([ctx expr] (extract-bindings ctx expr false))
([ctx expr keys-destructuring?]
(let [expr (meta/lift-meta-content ctx expr)
t (node/tag expr)
findings (:findings ctx)]
(case t
:token
(cond
;; symbol
(utils/symbol-token? expr)
(let [expr (meta/lift-meta-content ctx expr)
sym (:value expr)]
(when (not= '& sym)
(let [ns (namespace sym)
valid? (or (not ns)
keys-destructuring?)]
(if valid?
(let [s (symbol (name sym))
m (meta expr)
v (assoc m
:name s
:filename (:filename ctx))]
(namespace/reg-binding! ctx
(-> ctx :ns :name)
(assoc m
:name s
:filename (:filename ctx)))
{s v})
(findings/reg-finding!
findings
(node->line (:filename ctx)
expr
:error
:unsupported-binding-form
(str "unsupported binding form " sym)))))))
;; keyword
(:k expr)
(let [k (:k expr)]
(when (not= :as k)
(if keys-destructuring?
(let [s (-> expr :k name symbol)
m (meta expr)
v (assoc m
:name s
:filename (:filename ctx))]
(namespace/reg-binding! ctx
(-> ctx :ns :name)
v)
{s v})
(findings/reg-finding!
findings
(node->line (:filename ctx)
expr
:error
:unsupported-binding-form
(str "unsupported binding form " (:k expr)))))))
:else
(findings/reg-finding!
findings
(node->line (:filename ctx)
expr
:error
:unsupported-binding-form
(str "unsupported binding form " expr))))
:vector (into {} (map #(extract-bindings ctx %)) (:children expr))
:namespaced-map (extract-bindings ctx (first (:children expr)))
:map
(loop [[k v & rest-kvs] (:children expr)
res {}]
(if k
(let [k (meta/lift-meta-content ctx k)]
(cond (:k k)
(case (keyword (name (:k k)))
(:keys :syms :strs) (recur rest-kvs
(into res (map #(extract-bindings ctx % true))
(:children v)))
;; or doesn't introduce new bindings, it only gives defaults
:or
(if (empty? rest-kvs)
(recur rest-kvs (merge res {:analyzed (analyze-keys-destructuring-defaults
ctx res v)}))
;; analyze or after the rest
(recur (concat rest-kvs [k v]) res))
:as (recur rest-kvs (merge res (extract-bindings ctx v)))
(recur rest-kvs res))
(utils/symbol-token? k)
(recur rest-kvs (merge res (extract-bindings ctx k)
{:analyzed (analyze-expression** ctx v)}))
:else (recur rest-kvs res)))
res))
(findings/reg-finding!
findings
(node->line (:filename ctx)
expr
:error
:unsupported-binding-form
(str "unsupported binding form " expr)))))))
(defn analyze-in-ns [ctx {:keys [:children] :as _expr}]
(let [ns-name (-> children second :children first :value)
ns {:type :in-ns
:name ns-name
:lang (:lang ctx)
:vars #{}
:used #{}
:bindings #{}
:used-bindings #{}}]
(namespace/reg-namespace! ctx ns)
ns))
(defn fn-call? [expr]
(let [tag (node/tag expr)]
(and (= :list tag)
(symbol? (:value (first (:children expr)))))))
;;;; function arity
(defn analyze-arity [sexpr]
(loop [[arg & rest-args] sexpr
arity 0]
(if arg
(if (= '& arg)
{:min-arity arity
:varargs? true}
(recur rest-args
(inc arity)))
{:fixed-arity arity})))
(defn analyze-fn-arity [ctx body]
(let [children (:children body)
arg-vec (first children)
arg-list (node/sexpr arg-vec)
arg-bindings (extract-bindings ctx arg-vec)
arity (analyze-arity arg-list)]
{:arg-bindings (dissoc arg-bindings :analyzed)
:arity arity
:analyzed-arg-vec (:analyzed arg-bindings)}))
(defn analyze-fn-body [{:keys [bindings] :as ctx} body]
(let [{:keys [:arg-bindings
:arity :analyzed-arg-vec]} (analyze-fn-arity ctx body)
children (:children body)
body-exprs (rest children)
parsed
(analyze-children
(assoc ctx
:bindings (merge bindings arg-bindings)
:recur-arity arity
:fn-body true) body-exprs)]
(assoc arity
:parsed
(concat analyzed-arg-vec parsed))))
(defn fn-bodies [ctx children]
(loop [[expr & rest-exprs :as exprs] children]
(when expr
(let [expr (meta/lift-meta-content ctx expr)
t (node/tag expr)]
(case t
:vector [{:children exprs}]
:list exprs
(recur rest-exprs))))))
(defn analyze-defn [{:keys [base-lang lang] :as ctx} expr]
(let [children (:children expr)
children (rest children) ;; "my-fn docstring" {:no-doc true} [x y z] x
name-node (first children)
name-node (when name-node (meta/lift-meta-content ctx name-node))
fn-name (:value name-node)
var-meta (meta name-node)
call-sym (symbol-call expr)
macro? (or (= 'defmacro call-sym)
(:macro var-meta))
private? (or (= 'defn- call-sym)
(:private var-meta))
bodies (fn-bodies ctx (next children))
parsed-bodies (map #(analyze-fn-body
(assoc ctx
:in-def? true) %)
bodies)
fixed-arities (set (keep :fixed-arity parsed-bodies))
var-args-min-arity (:min-arity (first (filter :varargs? parsed-bodies)))
{:keys [:row :col]} (meta expr)
defn
(if fn-name
(cond-> {:type :defn
:name fn-name
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr}
macro? (assoc :macro true)
(seq fixed-arities) (assoc :fixed-arities fixed-arities)
private? (assoc :private? private?)
var-args-min-arity (assoc :var-args-min-arity var-args-min-arity))
{:type :debug
:level :info
:message "Could not parse defn form"
:row row
:col col
:lang lang})]
(cons defn (mapcat :parsed parsed-bodies))))
(defn analyze-case [ctx expr]
(let [exprs (-> expr :children)]
(loop [[constant expr :as exprs] exprs
parsed []]
(if-not expr
(into parsed (when constant
(analyze-expression** ctx constant)))
(recur
(nnext exprs)
(into parsed (analyze-expression** ctx expr)))))))
(defn expr-bindings [ctx binding-vector]
(->> binding-vector :children
(take-nth 2)
(map #(extract-bindings ctx %))
(reduce deep-merge {})))
(defn analyze-let-like-bindings [ctx binding-vector]
(let [call (-> ctx :callstack second second)
for-like? (one-of call [for doseq])]
(loop [[binding value & rest-bindings] (-> binding-vector :children)
bindings (:bindings ctx)
arities (:arities ctx)
analyzed []]
(if binding
(let [binding-sexpr (node/sexpr binding)
for-let? (and for-like?
(= :let binding-sexpr))]
(if for-let?
(let [{new-bindings :bindings
new-analyzed :analyzed
new-arities :arities}
(analyze-let-like-bindings
(update ctx :bindings
(fn [b]
(merge b bindings))) value)]
(recur rest-bindings
(merge bindings new-bindings)
(merge arities new-arities)
(concat analyzed new-analyzed)))
(let [binding (cond for-let? value
;; ignore :when and :while in for
(keyword? binding-sexpr) nil
:else binding)
new-bindings (when binding (extract-bindings ctx binding))
analyzed-binding (:analyzed new-bindings)
new-bindings (dissoc new-bindings :analyzed)
ctx* (-> ctx
(update :bindings (fn [b]
(merge b bindings)))
(update :arities merge arities))
analyzed-value (when (and value (not for-let?))
(analyze-expression** ctx* value))
next-arities (if-let [arity (:arity (meta analyzed-value))]
(assoc arities binding-sexpr arity)
arities)]
(recur rest-bindings
(merge bindings new-bindings)
next-arities (concat analyzed analyzed-binding analyzed-value)))))
{:arities arities
:bindings bindings
:analyzed analyzed}))))
(defn lint-even-forms-bindings! [ctx form-name bv]
(let [num-children (count (:children bv))
{:keys [:row :col]} (meta bv)]
(when (odd? num-children)
(findings/reg-finding!
(:findings ctx)
{:type :invalid-bindings
:message (format "%s binding vector requires even number of forms" form-name)
:row row
:col col
:level :error
:filename (:filename ctx)}))))
(defn analyze-like-let
[{:keys [:filename :callstack
:lang :base-lang
:maybe-redundant-let?] :as ctx} expr]
(let [children (:children expr)
call (-> callstack first second)
let? (= 'let call)
let-parent? (one-of (second callstack)
[[clojure.core let]
[cljs.core let]])
bv (-> expr :children second)
{:keys [:row :col]} (meta expr)
arg-count (count (rest children))]
(when (and let? let-parent? maybe-redundant-let?)
(findings/reg-finding!
(:findings ctx)
(node->line filename expr :warning :redundant-let "redundant let")))
(cons {:type :call
:name call
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr
:arity arg-count}
(when (and bv (= :vector (node/tag bv)))
(let [{analyzed-bindings :bindings
arities :arities
analyzed :analyzed}
(analyze-let-like-bindings
(-> ctx
;; prevent linting redundant let when using let in bindings
(update :callstack #(cons [nil :let-bindings] %))) bv)
let-body (nnext (:children expr))
single-child? (and let? (= 1 (count let-body)))]
(lint-even-forms-bindings! ctx 'let bv)
(concat analyzed
(analyze-children
(-> ctx
(update :bindings (fn [b]
(merge b analyzed-bindings)))
(update :arities merge arities)
(assoc :maybe-redundant-let? single-child?))
let-body)))))))
(defn analyze-do [{:keys [:filename :callstack] :as ctx} expr]
(let [parent-call (second callstack)
core? (one-of (first parent-call) [clojure.core cljs.core])
core-sym (when core?
(second parent-call))
redundant?
(and (not= 'fn* core-sym)
(not= 'let* core-sym)
(or
;; zero or one children
(< (count (rest (:children expr))) 2)
(and core?
(or
;; explicit do
(= 'do core-sym)
;; implicit do
(one-of core-sym [fn defn defn-
let loop binding with-open
doseq try])))))]
(when redundant?
(findings/reg-finding!
(:findings ctx)
(node->line filename expr :warning :redundant-do "redundant do"))))
(analyze-children ctx (next (:children expr))))
(defn lint-two-forms-binding-vector! [ctx form-name expr sexpr]
(let [num-children (count sexpr)
{:keys [:row :col]} (meta expr)]
(when (not= 2 num-children)
(findings/reg-finding!
(:findings ctx)
{:type :invalid-bindings
:message (format "%s binding vector requires exactly 2 forms" form-name)
:row row
:col col
:filename (:filename ctx)
:level :error}))))
(defn analyze-if-let [ctx expr]
(let [callstack (:callstack ctx)
call (-> callstack first second)
bv (-> expr :children second)
sexpr (and bv (node/sexpr bv))]
(when (vector? sexpr)
(let [bindings (expr-bindings ctx bv)
eval-expr (-> bv :children second)
body-exprs (-> expr :children nnext)]
(lint-two-forms-binding-vector! ctx call bv sexpr)
(concat (:analyzed bindings)
(analyze-expression** ctx eval-expr)
(analyze-children (update ctx :bindings
(fn [b] (merge b
(dissoc bindings
:analyzed))))
body-exprs))))))
(defn fn-arity [ctx bodies]
(let [arities (map #(analyze-fn-arity ctx %) bodies)
fixed-arities (set (keep (comp :fixed-arity :arity) arities))
var-args-min-arity (some #(when (:varargs? (:arity %))
(:min-arity (:arity %))) arities)]
(cond-> {}
(seq fixed-arities) (assoc :fixed-arities fixed-arities)
var-args-min-arity (assoc :var-args-min-arity var-args-min-arity))))
(defn analyze-fn [ctx expr]
(let [children (:children expr)
?fn-name (when-let [?name-expr (second children)]
(let [n (node/sexpr ?name-expr)]
(when (symbol? n)
n)))
bodies (fn-bodies ctx (next children))
;; we need the arity beforehand because this is valid in each body
arity (fn-arity ctx bodies)
parsed-bodies (map #(analyze-fn-body
(if ?fn-name
(-> ctx
(update :bindings conj [?fn-name
(assoc (meta (second children))
:name ?fn-name
:filename (:filename ctx))])
(update :arities assoc ?fn-name
arity))
ctx) %) bodies)]
(with-meta (mapcat :parsed parsed-bodies)
{:arity arity})))
(defn analyze-alias [ctx expr]
(let [ns (:ns ctx)
[alias-sym ns-sym]
(map #(-> % :children first :value)
(rest (:children expr)))]
(namespace/reg-alias! ctx (:name ns) alias-sym ns-sym)
(assoc-in ns [:qualify-ns alias-sym] ns-sym)))
(defn analyze-loop [ctx expr]
(let [bv (-> expr :children second)]
(when (and bv (= :vector (node/tag bv)))
(let [arg-count (let [c (count (:children bv))]
(when (even? c)
(/ c 2)))]
(analyze-like-let (assoc ctx
:recur-arity {:fixed-arity arg-count}) expr)))))
(defn analyze-recur [{:keys [:findings :call-as-use
:filename :recur-arity] :as ctx} expr]
(when-not call-as-use
(let [arg-count (count (rest (:children expr)))
expected-arity
(or (:fixed-arity recur-arity)
;; var-args must be passed as a seq or nil in recur
(when-let [min-arity (:min-arity recur-arity)]
(inc min-arity)))]
(cond
(not expected-arity)
(findings/reg-finding!
findings
(node->line
filename
expr
:warning
:unexpected-recur "unexpected recur"))
(not= expected-arity arg-count)
(findings/reg-finding!
findings
(node->line
filename
expr
:error
:invalid-arity
(format "recur argument count mismatch (expected %d, got %d)" expected-arity arg-count)))
:else nil)))
(analyze-children ctx (:children expr)))
(defn analyze-letfn [ctx expr]
(let [fns (-> expr :children second :children)
name-exprs (map #(-> % :children first) fns)
ctx (update ctx :bindings
(fn [b]
(into b (map (fn [name-expr]
[(:value name-expr)
(assoc (meta name-expr)
:name (:value name-expr)
:filename (:filename ctx))])
name-exprs))))
processed-fns (for [f fns
:let [children (:children f)
fn-name (:value (first children))
bodies (fn-bodies ctx (next children))
arity (fn-arity ctx bodies)]]
{:name fn-name
:arity arity
:bodies bodies})
ctx (reduce (fn [ctx pf]
(assoc-in ctx [:arities (:name pf)]
(:arity pf)))
ctx processed-fns)
parsed-fns (map #(analyze-fn-body ctx %) (mapcat :bodies processed-fns))
analyzed-children (analyze-children ctx (->> expr :children (drop 2)))]
(concat (mapcat (comp :parsed) parsed-fns) analyzed-children)))
(defn analyze-namespaced-map [ctx ^NamespacedMapNode expr]
(let [children (:children expr)
m (first children)
ns (:ns ctx)
ns-sym (-> expr :ns :k symbol)
used (when-let [resolved-ns (get (:qualify-ns ns) ns-sym)]
[{:type :use
:ns resolved-ns}])]
(concat used (analyze-expression** ctx m))))
(defn analyze-schema-defn [ctx expr]
(let [arg-count (count (rest (:children expr)))
{:keys [:base-lang :lang]} ctx
{:keys [:row :col]} (meta expr)
{:keys [:defn :schemas]} (schema/expand-schema-defn2 expr)]
(cons {:type :call
:name 'schema.core/defn
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr
:arity arg-count}
(concat
(namespace/analyze-usages ctx false {:children schemas})
(analyze-defn ctx defn)))))
(defn analyze-deftest [ctx _deftest-ns expr]
(let [arg-count (count (rest (:children expr)))
{:keys [:base-lang :lang]} ctx
{:keys [:row :col]} (meta expr)]
(cons {:type :call
:name (case lang
:clj 'clojure.test/deftest
:cljs 'cljs.test/deftest)
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr
:arity arg-count}
(analyze-defn ctx
(update expr :children
(fn [[_ name-expr & body]]
(list*
(token/token-node 'clojure.core/defn)
name-expr
(seq/vector-node [])
body)))))))
(defn cons* [x xs]
(if x (cons x xs)
xs))
(defn analyze-binding-call [{:keys [:callstack :config :findings] :as ctx} fn-name expr]
(namespace/reg-used-binding! ctx
(-> ctx :ns :name)
(get (:bindings ctx) fn-name))
(when-not (config/skip? config :invalid-arity callstack)
(let [filename (:filename ctx)
children (:children expr)]
(when-not (:call-as-use ctx)
(when-let [{:keys [:fixed-arities :var-args-min-arity]}
(get (:arities ctx) fn-name)]
(let [arg-count (count (rest children))]
(when-not (or (contains? fixed-arities arg-count)
(and var-args-min-arity (>= arg-count var-args-min-arity)))
(findings/reg-finding! findings (node->line filename expr :error
:invalid-arity
(format "wrong number of args (%s) passed to %s"
arg-count
fn-name)))))))
(analyze-children ctx (rest children)))))
(defn lint-inline-def! [{:keys [:in-def? :findings :filename]} expr]
(when in-def?
(findings/reg-finding!
findings
(node->line filename expr :warning :inline-def "inline def"))))
(defn analyze-def [ctx expr]
(analyze-children (assoc ctx :in-def? true)
(next (:children expr))))
(defn analyze-call
[{:keys [:fn-body :base-lang :lang :ns :config] :as ctx}
{:keys [:arg-count
:full-fn-name
:row :col
:expr]}]
(let [children (:children expr)
{resolved-namespace :ns
resolved-name :name}
(resolve-name
(namespace/get-namespace ctx base-lang lang (:name ns)) full-fn-name)
[resolved-as-namespace resolved-as-name lint-as?]
(or (when-let [[ns n] (config/lint-as config [resolved-namespace resolved-name])]
[ns n true])
[resolved-namespace resolved-name false])
fq-sym (when (and resolved-namespace
resolved-name)
(symbol (str resolved-namespace)
(str resolved-name)))
ctx (if fq-sym
(update ctx :callstack
(fn [cs]
(cons [resolved-namespace resolved-name] cs)))
ctx)
resolved-as-clojure-var-name
(when (one-of resolved-as-namespace [clojure.core cljs.core])
resolved-as-name)
use (when lint-as?
{:type :use
:ns resolved-namespace
:name resolved-name
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr})]
(cons* use
(case resolved-as-clojure-var-name
ns
(let [ns (analyze-ns-decl ctx expr)]
[ns])
in-ns (when-not fn-body [(analyze-in-ns ctx expr)])
alias
[(analyze-alias ctx expr)]
def (do (lint-inline-def! ctx expr)
(analyze-def ctx expr))
(defn defn- defmacro)
(do (lint-inline-def! ctx expr)
(cons {:type :call
:name resolved-as-clojure-var-name
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr
:arity arg-count}
(analyze-defn ctx expr)))
comment
(analyze-children ctx children)
(-> some->)
(analyze-expression** ctx (macroexpand/expand-> ctx expr))
(->> some->>)
(analyze-expression** ctx (macroexpand/expand->> ctx expr))
(cond-> cond->> . .. deftype
proxy extend-protocol doto reify definterface defrecord defprotocol
defcurried)
;; don't lint calls in these expressions, only register them as used vars
(analyze-children (assoc ctx :call-as-use true)
(:children expr))
(let let* for doseq with-open)
(analyze-like-let ctx expr)
letfn
(analyze-letfn ctx expr)
(if-let when-let)
(analyze-if-let ctx expr)
do
(analyze-do ctx expr)
(fn fn*)
(analyze-fn ctx expr)
case
(analyze-case ctx expr)
loop
(analyze-loop ctx expr)
recur
(analyze-recur ctx expr)
quote [{:type :call
:name full-fn-name
:arity arg-count
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr
:callstack (:callstack ctx)}]
;; catch-all
(case [resolved-namespace resolved-name]
[schema.core defn]
(analyze-schema-defn ctx expr)
([clojure.test deftest] [cljs.test deftest])
(do
(lint-inline-def! ctx expr)
(analyze-deftest ctx resolved-namespace expr))
;; catch-all
(let [call (if (:call-as-use ctx)
{:type :use
:ns resolved-namespace
:name resolved-name
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr}
{:type :call
:name full-fn-name
:arity arg-count
:row row
:col col
:base-lang base-lang
:lang lang
:expr expr
:callstack (:callstack ctx)})
next-ctx (cond-> ctx
(= '[clojure.core.async thread]
[resolved-namespace resolved-name])
(assoc-in [:recur-arity :fixed-arity] 0))]
(cons call (analyze-children next-ctx (rest children)))))))))
(defn lint-keyword-call! [{:keys [:callstack :config :findings] :as ctx} kw namespaced? arg-count expr]
(when-not (config/skip? config :invalid-arity callstack)
(let [ns (:ns ctx)
?resolved-ns (if namespaced?
(if-let [kw-ns (namespace kw)]
(or (get (:qualify-ns ns) (symbol kw-ns))
;; because we couldn't resolve the namespaced
;; keyword, we print it as is
(str ":" (namespace kw)))
;; if the keyword is namespace, but there is no
;; namespace, it's the current ns
(:name ns))
(namespace kw))
kw-str (if ?resolved-ns (str ?resolved-ns "/" (name kw))
(str (name kw)))]
(when (or (zero? arg-count)
(> arg-count 2))
(findings/reg-finding! findings
(node->line (:filename ctx) expr :error :invalid-arity
(format "wrong number of args (%s) passed to keyword :%s"
arg-count
kw-str)))))))
(defn lint-map-call! [{:keys [:callstack :config
:findings] :as ctx} _the-map arg-count expr]
(when-not (config/skip? config :invalid-arity callstack)
(when (or (zero? arg-count)
(> arg-count 2))
(findings/reg-finding!
findings
(node->line (:filename ctx) expr :error :invalid-arity
(format "wrong number of args (%s) passed to a map"
arg-count))))))
(defn lint-symbol-call! [{:keys [:callstack :config :findings] :as ctx} _the-symbol arg-count expr]
(when-not (config/skip? config :invalid-arity callstack)
(when (or (zero? arg-count)
(> arg-count 2))
(findings/reg-finding!
findings
(node->line (:filename ctx) expr :error :invalid-arity
(format "wrong number of args (%s) passed to a symbol"
arg-count))))))
(defn reg-not-a-function! [{:keys [:filename :callstack
:config :findings]} expr type]
(when-not (config/skip? config :not-a-function callstack)
(findings/reg-finding!
findings
(node->line filename expr :error :not-a-function (str "a " type " is not a function")))))
(defn analyze-expression**
[{:keys [:bindings] :as ctx}
{:keys [:children] :as expr}]
(let [t (node/tag expr)
{:keys [:row :col]} (meta expr)
arg-count (count (rest children))]
(case t
:quote nil
:syntax-quote (namespace/analyze-usages ctx expr)
(:unquote :unquote-splicing)
nil ;; TODO: this is an error, you can't use this outside syntax-quote!
:namespaced-map (analyze-namespaced-map (update ctx
:callstack #(cons [nil t] %))
expr)
:map (do (key-linter/lint-map-keys ctx expr)
(analyze-children (update ctx
:callstack #(cons [nil t] %)) children))
:set (do (key-linter/lint-set ctx expr)
(analyze-children (update ctx
:callstack #(cons [nil t] %))
children))
:fn (recur ctx (macroexpand/expand-fn expr))
:token (namespace/analyze-usages ctx expr)
:list
(when-let [function (first children)]
(let [t (node/tag function)]
(case t
:map
(do (lint-map-call! ctx function arg-count expr)
(analyze-children ctx children))
:quote
(let [quoted-child (-> function :children first)]
(if (utils/symbol-token? quoted-child)
(do (lint-symbol-call! ctx quoted-child arg-count expr)
(analyze-children ctx children))
(analyze-children ctx children)))
:token
(if-let [k (:k function)]
(do (lint-keyword-call! ctx k (:namespaced? function) arg-count expr)
(analyze-children ctx children))
(if-let [full-fn-name (when (utils/symbol-token? function) (:value function))]
(let [unqualified? (nil? (namespace full-fn-name))
binding-call? (and unqualified?
(contains? bindings full-fn-name))]
(if binding-call?
(analyze-binding-call ctx full-fn-name expr)
(analyze-call ctx {:arg-count arg-count
:full-fn-name full-fn-name
:row row
:col col
:expr expr})))
(cond
(utils/boolean-token? function)
(do (reg-not-a-function! ctx expr "boolean")
(analyze-children ctx (rest children)))
(utils/string-token? function)
(do (reg-not-a-function! ctx expr "string")
(analyze-children ctx (rest children)))
(utils/char-token? function)
(do (reg-not-a-function! ctx expr "character")
(analyze-children ctx (rest children)))
(utils/number-token? function)
(do (reg-not-a-function! ctx expr "number")
(analyze-children ctx (rest children)))
:else
(analyze-children ctx children))))
(analyze-children ctx children))))
;; catch-all
(analyze-children (update ctx
:callstack #(cons [nil t] %))
children))))
(defn analyze-expression*
[{:keys [:filename :base-lang :lang :results :ns
:expression :debug? :config :findings :namespaces]}]
(let [ctx {:filename filename
:base-lang base-lang
:lang lang
:ns ns
;; TODO: considering that we're introducing bindings here, we could do
;; the analysis of unused bindings already in the last step of the
;; loop, instead of collecting then in the namespace atom
:bindings {}
:config config
:findings findings
:namespaces namespaces}]
(loop [ns ns
[first-parsed & rest-parsed :as all] (analyze-expression** ctx expression)
results results]
(if (seq all)
(case (:type first-parsed)
nil (recur ns rest-parsed results)
(:ns :in-ns)
(recur
first-parsed
rest-parsed
(-> results
(assoc :ns first-parsed)
(update :used into (:used first-parsed))
(update :required into (:required first-parsed))))
:use
(do
(namespace/reg-usage! ctx (:name ns) (:ns first-parsed))
(recur
ns
rest-parsed
(-> results
(update :used conj (:ns first-parsed)))))
(:duplicate-map-key
:missing-map-value
:duplicate-set-key
:invalid-bindings
:invalid-arity)
(recur
ns
rest-parsed
(update results
:findings conj (assoc first-parsed
:filename filename)))
;; catch-all
(recur
ns
rest-parsed
(case (:type first-parsed)
:debug
(if debug?
(update-in results
[:findings]
conj
(assoc first-parsed
:filename filename))
results)
(let [;; TODO: can we do without this resolve since we already resolved in analyze-expression**?
resolved (resolve-name
(namespace/get-namespace ctx base-lang lang (:name ns)) (:name first-parsed))
first-parsed (assoc first-parsed
:name (:name resolved)
:ns (:name ns))]
(case (:type first-parsed)
:defn
(let [;; _ (println "LANG FP" (name (:lang first-parsed)))
path (if (= :cljc base-lang)
[:defs (:name ns) (:lang first-parsed) (:name resolved)]
[:defs (:name ns) (:name resolved)])
results
(if resolved
(do
(namespace/reg-var! ctx (:name ns) (:name resolved) (:expr first-parsed))
(assoc-in results path
(dissoc first-parsed
:type
:expr)))
results)]
(if debug?
(update-in results
[:findings]
vconj
(assoc first-parsed
:level :info
:filename filename
:message
(str/join " "
["Defn resolved as"
(str (:ns resolved) "/" (:name resolved)) "with arities"
"fixed:"(:fixed-arities first-parsed)
"varargs:"(:var-args-min-arity first-parsed)])
:type :debug))
results))
:call
(if resolved
(let [path [:calls (:ns resolved)]
unqualified? (:unqualified? resolved)
call (cond-> (assoc first-parsed
:filename filename
:resolved-ns (:ns resolved)
:ns-lookup ns)
(:clojure-excluded? resolved)
(assoc :clojure-excluded? true)
unqualified?
(assoc :unqualified? true))
results (do
(when-not unqualified?
(namespace/reg-usage! ctx (:name ns)
(:ns resolved)))
(cond-> (update-in results path vconj call)
(not unqualified?)
(update :used conj (:ns resolved))))]
(if debug? (update-in results [:findings] conj
(assoc call
:level :info
:message (str "Call resolved as "
(str (:ns resolved) "/" (:name resolved)))
:type :debug))
results))
(if debug?
(update-in results
[:findings]
conj
(assoc first-parsed
:level :info
:message (str "Unrecognized call to "
(:name first-parsed))
:type :debug))
results))
results)))))
[ns results]))))