add statix-nano jq script for linting nix files in nano

This commit is contained in:
chaos 2024-03-19 14:54:30 +00:00
parent 3fffad500a
commit 696d5439da
No known key found for this signature in database
13 changed files with 212 additions and 4 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
result result
*.qcow2 *.qcow2
*~x

View file

@ -0,0 +1,12 @@
{pkgs, ...}: {
script = pkgs.writeShellScript "nano-lint-nix.sh" ''
#!/usr/bin/env bash
set -uxeo pipefail
env SCRIPT_LIB_DIR=${builtins.path {
path = ./.;
filter = path: type:
path == toString ./statix-to-nano-linter.jq;
}} bash ${./statix-nano-lint.sh} $@
'';
}

View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -uxe
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [ ! -z "${DEBUG:-}" ]; then
JQ_ARGS="--debug-trace"
else
JQ_ARGS=""
fi
statix check -o json "$@" | jq ${JQ_ARGS:-} -rnesM -L $SCRIPT_DIR -L "${SCRIPT_LIB_DIR:-}" 'include "statix-to-nano-linter"; main'

View file

@ -0,0 +1,70 @@
def lpad($len; $fill):
tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
# Severity Enum: https://github.com/nerdypepper/statix/blob/d324490e45bcda1b664f28b6f88da8580deda76b/lib/src/lib.rs#L21
# Warn/Error/Hint
def severity_or_unknown($severity):
if
(["Warn","Error","Hint"] | any(index($severity)))
then
$severity
else "Unknown" end;
def severity_or_unknown: severity_or_unknown(.);
# Emulating how the fancy stderr displays messages
# https://github.com/nerdypepper/statix/blob/d324490e45bcda1b664f28b6f88da8580deda76b/bin/src/traits.rs#L112
def severity_to_inital($severity):
{"Warn": "W", "Error": "E", "Hint": "I"} as $mapping |
($mapping).[$severity]
// debug("severity '\($severity)' could not be be matched to an initial");
def severity_to_inital: severity_to_inital(.);
# Emulating how fancy stderr via ariadne & how errfmt displays
# https://github.com/nerdypepper/statix/blob/d324490e45bcda1b664f28b6f88da8580deda76b/bin/src/traits.rs#L62
def severity_to_name($severity):
{
"Warn": "Warning",
"Error",
"Hint": "Advice",
"Unknown"
} as $mapping | ($mapping).[$severity];
def severity_to_tag($severity; $code):
($code | lpad(2; "0")) as $code |
if $severity != "Unknown"
then "[\(severity_to_inital($severity))\($code)]"
else "[\($code)?]"
end;
def print_diagnostic($filename; $report; $issue; $diagnostic):
$diagnostic.at.from as $from |
$from.line as $line |
$from.column as $column |
severity_or_unknown($issue.severity) as $severity |
severity_to_name($severity) as $severity_name |
severity_to_tag($severity; $issue.code) as $severity_tag |
"\($filename):\($line):\($column): \($severity_tag) \($severity_name): \($issue.note)";
def process_input:
. as $root |
.file as $filename |
.report as $report |
$report[] as $issue |
$issue.diagnostics[] as $diagnostic |
print_diagnostic($filename; $report; $issue; $diagnostic);
def process_inputs:
. as $inputs |
if length == 0 or (. == null) then halt else
[$inputs | .[] | process_input] | join("\n") | .+"\n" | halt_error(1)
end;
def main:
[try input catch infinite] | .[0] | if isinfinite then halt else ., inputs | process_inputs end;
#|
#if
# isempty(.) or (. == null)
#then debug("x") else (. | process_input) end;

View file

@ -0,0 +1,39 @@
# severity_or_unknown tests
## All Valid Severities
include "statix-to-nano-linter"; . | severity_or_unknown
"Warn"
"Warn"
include "statix-to-nano-linter"; severity_or_unknown
"Error"
"Error"
include "statix-to-nano-linter"; severity_or_unknown
"Hint"
"Hint"
## Invalid but used elsewhere in other code
include "statix-to-nano-linter"; severity_or_unknown
"Info"
"Unknown"
include "statix-to-nano-linter"; severity_or_unknown
"Advice"
"Unknown"
## Fake
include "statix-to-nano-linter"; severity_or_unknown
"beep"
"Unknown"
## Technically True
include "statix-to-nano-linter"; severity_or_unknown
"Unknown"
"Unknown"
# severity_to_inital tests
# Valid
include "statix-to-nano-linter"; severity_to_inital
"Warn"
"W"

View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -x
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
jq -L $SCRIPT_DIR --run-tests $SCRIPT_DIR/statix-to-nano-linter.test.jq

View file

@ -1 +1,5 @@
{pkgs, ...}: {home.packages = with pkgs; [nano];} {tree, ...}: {
imports = with tree; [
home.programming.editors.nano
];
}

View file

@ -10,6 +10,7 @@
python3 python3
binutils # for strings binutils # for strings
qrencode qrencode
dos2unix
# This saves a rebuild of already cached busybox # This saves a rebuild of already cached busybox
(pkgs.runCommand "busybox-no-applets" {} '' (pkgs.runCommand "busybox-no-applets" {} ''

View file

@ -0,0 +1,54 @@
{
self,
pkgs,
config,
...
} @ inputs: let
package =
if inputs ? "nixosConfig"
then inputs.nixosConfig.programs.nano.package
else pkgs.nano;
in {
home.packages = with pkgs; [package deadnix statix ];
systemd.user.tmpfiles.rules = [
"d ${config.xdg.cacheHome}/nano - ${config.home.username} users"
];
xdg.configFile."nano/nanorc".text = ''
set softwrap
set tabsize 2
set autoindent
set backup
set backupdir "${config.xdg.cacheHome}/nano"
set indicator
set magic
unset mouse
set nonewlines
set constantshow
set positionlog
set multibuffer
unset minibar
bind ^I formatter all
bind M-I formatter all
bind Sh-M-I formatter all
bind ^L linter all
bind M-L linter all
bind Sh-M-L linter all
include "${package}/share/nano/*.nanorc"
include "${package}/share/nano/extra/*.nanorc"
include "${pkgs.nanorc}/share/*.nanorc"
extendsyntax rust formatter rustfmt
extendsyntax nix formatter alejandra -t1
extendsyntax nix linter ${pkgs.writeShellScript "nano-lint-nix.sh" (let
statix-nano = import "${self}/extras/statix-nano/statix-nano-lint.nix" inputs;
in ''
#!/usr/bin/env bash
set -uxeo pipefail
deadnix $@ -o json | jq -r '.file as $filename | .results | .[] | $filename + ":" + (.line|tostring) + ":" + (.column|tostring) + ": " + .message'
'')}
'';
}

View file

@ -0,0 +1,3 @@
{...}: {
programs.nano.syntaxHighlight = false;
}

View file

@ -1,3 +1,4 @@
disabled = [ disabled = [
"empty_pattern" "empty_pattern",
"repeated_keys"
] ]