branch: elpa/flymake-collection
commit 0c87b8ff48d8b0ef0eab6417ee9b57f0ca095a0d
Author: Valeriy Litkovskyy <vlr.ltk...@protonmail.com>
Commit: GitHub <nore...@github.com>

    checkers: Add statix checker (#12)
---
 src/checkers/flymake-collection-statix.el | 102 ++++++++++++++++++++++++++++++
 src/flymake-collection-hook.el            |   2 +
 tests/checkers/installers/statix.bash     |  11 ++++
 tests/checkers/test-cases/statix.yml      |  44 +++++++++++++
 4 files changed, 159 insertions(+)

diff --git a/src/checkers/flymake-collection-statix.el 
b/src/checkers/flymake-collection-statix.el
new file mode 100644
index 0000000000..5da4df0d77
--- /dev/null
+++ b/src/checkers/flymake-collection-statix.el
@@ -0,0 +1,102 @@
+;;; flymake-collection-statix.el --- Statix diagnostic function -*- 
lexical-binding: t -*-
+
+;; Copyright (c) 2022 Valeriy Litkovskyy
+
+;; Permission is hereby granted, free of charge, to any person obtaining a copy
+;; of this software and associated documentation files (the "Software"), to 
deal
+;; in the Software without restriction, including without limitation the rights
+;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+;; copies of the Software, and to permit persons to whom the Software is
+;; furnished to do so, subject to the following conditions:
+
+;; The above copyright notice and this permission notice shall be included in 
all
+;; copies or substantial portions of the Software.
+
+;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
+;; SOFTWARE.
+
+;;; Commentary:
+
+;; This file provides a flymake backend that lets the user analyze nix code
+;; using statix.
+
+;;;; Tips
+
+;; + You can customize `flymake-collection-statix-args' to your liking.
+
+;;;; Credits
+
+;; This diagnostic would not have been possible without the statix[1] program.
+
+;;  [1] https://github.com/nerdypepper/statix
+
+;;; Code:
+
+;;;; Requirements
+
+(require 'flymake)
+(require 'flymake-collection)
+
+(eval-when-compile
+  (require 'flymake-collection-define))
+
+;;;; Customization
+
+(defcustom flymake-collection-statix-args nil
+  "Additional statix check arguments."
+  :type '(repeat :tag "Arguments" (string :tag "Argument"))
+  :group 'flymake-collection)
+
+;;;; Functions
+
+;;;;; Public
+
+;;;###autoload (autoload 'flymake-collection-statix 
"flymake-collection-statix")
+(flymake-collection-define-enumerate flymake-collection-statix
+  "A Nix syntax and style checker using statix.
+
+See URL `https://github.com/nerdypepper/statix'."
+  :title "statix"
+  :pre-let ((statix-exec (executable-find "statix")))
+  :pre-check (unless statix-exec
+               (error "Cannot find statix executable"))
+  :command `(,statix-exec "check" "--format" "json" "--stdin"
+                          ,@flymake-collection-statix-args)
+  :generator
+  (let ((data (list (cons 'buf flymake-collection-source))))
+    (mapcan
+     (lambda (report)
+       (let-alist report
+         (let ((data (cons (cons 'severity .severity) data)))
+           (mapcar (apply-partially #'append data) .diagnostics))))
+     (cdr (assq 'report (car (flymake-collection-parse-json
+                              (buffer-substring-no-properties
+                               (point-min) (point-max))))))))
+  :enumerate-parser
+  (let-alist it
+    (list .buf
+          (car (flymake-diag-region .buf .at.from.line .at.from.column))
+          (car (flymake-diag-region .buf .at.to.line .at.to.column))
+          (flymake-collection-statix--make-type it)
+          .message)))
+
+;;;;; Private
+
+(defun flymake-collection-statix--make-type (it)
+  "Make diagnostic type from IT.
+See `flymake-collection-define-enumerate' for IT.  IT must
+contain severity."
+  (let-alist it
+    (pcase-exhaustive .severity
+      ("Warn" :warning)
+      ("Error" :error)
+      ("Hint" :note))))
+
+(provide 'flymake-collection-statix)
+
+;;; flymake-collection-statix.el ends here
diff --git a/src/flymake-collection-hook.el b/src/flymake-collection-hook.el
index e040123666..baf4d90a17 100644
--- a/src/flymake-collection-hook.el
+++ b/src/flymake-collection-hook.el
@@ -62,6 +62,8 @@
     (lua-mode
      flymake-collection-luacheck
      (flymake-collection-lua :disabled t))
+    (nix-mode
+     flymake-collection-statix)
     (sql-mode
      flymake-collection-sql-lint
      (flymake-collection-sqlint :disabled t))
diff --git a/tests/checkers/installers/statix.bash 
b/tests/checkers/installers/statix.bash
new file mode 100755
index 0000000000..c26d36c51f
--- /dev/null
+++ b/tests/checkers/installers/statix.bash
@@ -0,0 +1,11 @@
+# INSTALL RUSTUP
+curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh -s -- -y
+. "$HOME/.cargo/env"
+
+# BUILD STATIX
+git clone "https://github.com/nerdypepper/statix";
+cd statix
+cargo build --release --features json
+
+# INSTALL STATIX
+ln -s "$(pwd)/target/release/statix" /usr/local/bin/statix
diff --git a/tests/checkers/test-cases/statix.yml 
b/tests/checkers/test-cases/statix.yml
new file mode 100644
index 0000000000..e5f5798db4
--- /dev/null
+++ b/tests/checkers/test-cases/statix.yml
@@ -0,0 +1,44 @@
+---
+checker: flymake-collection-statix
+tests:
+  - name: no-lints
+    file: |
+      # test case with no lints
+      {
+        hello = "world";
+        someFn = n: 2 + n;
+      }
+    lints: []
+  - name: notes
+    file: |
+      # test case with some warnings
+      {
+        hello = hello;
+        world = world;
+      }
+    lints:
+      - point: [3, 2]
+        level: warning
+        message: This assignment is better written with `inherit` (statix)
+      - point: [4, 2]
+        level: warning
+        message: This assignment is better written with `inherit` (statix)
+  - name: syntax-error
+    file: |
+      { definitely should not work
+    lints:
+      - point: [1, 0]
+        level: error
+        message: Unexpected end of file (statix)
+      - point: [1, 0]
+        level: error
+        message: Unexpected end of file, wanted any of [TOKEN_SEMICOLON] 
(statix)
+      - point: [1, 0]
+        level: error
+        message: Unexpected end of file (statix)
+      - point: [1, 0]
+        level: error
+        message: Unexpected end of file, wanted any of [TOKEN_ASSIGN] (statix)
+      - point: [1, 13]
+        level: error
+        message: Unexpected TOKEN_IDENT at 13..28, wanted any of 
[TOKEN_ASSIGN] (statix)

Reply via email to