~pepe/jlnt.kak

76214a1827159998327d47b1e0cce00cf99a12dc — Josef Pospíšil 8 months ago 9ee79d5
Changed logic, walk for bad patterns
2 files changed, 33 insertions(+), 16 deletions(-)

M jlnt
A warns.janet
M jlnt => jlnt +26 -16
@@ 1,15 1,33 @@
#!/usr/bin/env janet

(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 main [_ file]
  (def com (= file "project.janet"))
  (var lines 0)
  (def p (parser/new))
  (def emit (partial printf "%s:%i:%i: %s: %s" file))
  (defn warning-forms [pos forms]
    (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")))
  (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)
      (when (> (length line) 80)
      (if (> (length line) 80)
        (emit lines 80 "warning" "Line too long"))
      (:consume p line)
      (if (= :error (:status p))


@@ 18,7 36,7 @@
          (when (> l lines) (set l lines) (set c 1))
          (emit l c "error" (:error p))
          (break))
        (unless com
        (unless nocom
          (def macros @[])
          (def pos (-> (array ;(:where p))
                       (update 0 dec)


@@ 29,18 47,10 @@
            (if (function? cr)
              (match pr
                [(i (importers i))] (cr)
                [(m (= 'defmacro m))]
                (do
                  (array/push macros m)
                  (cr))
                [(m (find |(= $ m) macros)) _] (cr)
                [(m (= 'defmacro m))] (cr)

                [(s (= 'tracev s))] (emit ;pos "warning" "Tracev")
                [(w (= 'when w)) _ (b (< (length b) 2))]
                (emit ;pos "warning" "When with only one form, use if")
                [(u (= 'unless u)) _ (b (< (length b) 2))]
                (emit ;pos "warning"
                      "Unless with only one form, use if not"))
                (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))

A warns.janet => warns.janet +7 -0
@@ 0,0 1,7 @@
(defn a [a]
  (tracev :a)
  (if (when true :a) 0))

(unless false :a)

(print "This ooooooooooooooooooooooooooooooooooooooooooooooooh so long string!")