~pepe/jlnt.kak

edcb98100c1b6ff084ab8dc2c19112f4774bd27b — Josef Pospíšil 4 months ago bc526fb
Switch to flycheck, refactor warns
1 files changed, 22 insertions(+), 31 deletions(-)

M jlnt
M jlnt => jlnt +22 -31
@@ 1,5 1,7 @@
#!/usr/bin/env janet

(import spork/path)

(defn walk-for [sym]
  (fn [xs]
    (var a @[])


@@ 9,49 11,38 @@
    (postwalk walker xs)
    a))


(defn main [_ file]
(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`"))
    (if (some |(= (length $) 3) ((walk-for 'when) forms))
      (emit ;pos "wanrning" "`when` with only one form, use if"))
    (if (some |(= (length $) 3) ((walk-for 'unless) forms))
      (emit ;pos "wanrning" "`unless` with only one form, use if not")))
    (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 nocom (= file "project.janet"))
    (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)
      (if (= :error (:status p))
        (do
          (var [l c] (:where p))
          (when (> l lines) (set l lines) (set c 1))
          (emit l c "error" (:error p))
          (break))
        (unless nocom
          (def macros @[])
          (def pos (-> (array ;(:where p))
                       (update 0 dec)
                       (update 1 inc)))
          (when (:has-more p)
            (def pr (:produce p))
            (def cr (compile pr))
            (if (function? cr)
              (match pr
                [(i (importers i))] (cr)
                [(m (= 'defmacro m))] (cr)
      (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))))))

                (t (tuple? t))
                (warning-forms pos t))
              (do
                (var {:line l :column c :error e} cr)
                (unless (pos? l) (set l 1) (set c 1))
                (emit l c "error" e)))))))))
(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))))