~pepe/jlnt.kak

ref: 8e14d05f82de371f6713a336b733b7b8d21eb7d7 jlnt.kak/jlnt -rwxr-xr-x 1.3 KiB
8e14d05fJosef Pospíšil Add license, remove unused importers 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))

  (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))))