~pepe/jlnt.kak

ref: edcb98100c1b6ff084ab8dc2c19112f4774bd27b jlnt.kak/jlnt -rwxr-xr-x 1.4 KiB
edcb9810Josef Pospíšil Switch to flycheck, refactor warns 5 months ago
                                                                                
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
#!/usr/bin/env janet

(import spork/path)

(defn walk-for [sym]
  (fn [xs]
    (var a @[])
    (defn walker [form]
      (if (and (indexed? form) (= (first form) sym))
        (array/push a form) form))
    (postwalk walker xs)
    a))

(defn warns [file]
  (def emit (partial printf "%s:%i:%i: %s: %s" file))
  (defn warning-forms [pos forms]
    (defn three [sym subst]
      (if (some |(= (length $) 3) ((walk-for sym) forms))
        (emit ;pos "warning"
              (string/format "`%p` with only one form, use `%p`" sym subst))))
    (if (not (empty? ((walk-for 'tracev) forms)))
      (emit ;pos "warning" "`tracev`"))
    (three 'when 'if)
    (three 'unless 'if-not))
  (def- importers {'import true 'import* true 'dofile true 'require true})

  (with [f (file/open file :r)]
    (var lines 0)
    (def p (parser/new))
    (while (def line (:read f :line))
      (++ lines)
      (if (> (length line) 81) # \n
        (emit lines 80 "warning" "Line too long"))
      (:consume p line)
      (def pos (-> (array ;(:where p))
                   (update 0 dec)
                   (update 1 inc)))
      (when (:has-more p)
        (def pr (:produce p))
        (if (tuple? pr) (warning-forms pos pr))))))

(defn main [_ file]
  (def b @"")
  (if-not (= (path/basename file) "project.janet")
    (with-dyns [:err b] (flycheck file)))
  (if (empty? b)
    (warns file)
    (print ((peg/match ''(to "\n") b) 0))))