Skip to content

Commit 015124c

Browse files
nix(hlint): apply hlint's suggestions inplace
`postgrest-lint` now applies hlint's suggestions inplace to all haskell files. Hlint supports this via another package `apply-refact` which is marked broken in nixpkgs because it doesn't build with GHC 9.10, so we are using the GHC 9.12 pinned version. Co-authored-by: Wolfgang Walther <walther@technowledgy.de> Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
1 parent 3d12aa5 commit 015124c

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

nix/treefmt.nix

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ lib, pkgs, ... }:
1+
{ config, lib, pkgs, ... }:
22
let
33
hlintConfig = pkgs.writeText "hlintConfig.yml" ''
44
@@ -27,9 +27,38 @@ in
2727
programs.ruff-check.enable = true;
2828
programs.stylish-haskell.enable = true;
2929

30-
settings.formatter.hlint.options = [
31-
"--hint=${hlintConfig}"
32-
];
30+
settings.formatter.hlint = {
31+
command = pkgs.writeShellApplication {
32+
name = "hlint-wrapper";
33+
runtimeInputs = with pkgs; [
34+
jq
35+
# The "apply-refact" package is marked broken in nixpkgs because it
36+
# doesn't build with GHC 9.10, so we are using the GHC 9.12 pinned version.
37+
# https://github.com/mpickering/apply-refact#ghc-version-compatibility
38+
haskell.packages.ghc912.apply-refact
39+
];
40+
# hlint with --refactor flag does not support more than a single file at a time,
41+
# so loop through these files manually.
42+
# Unfortuantely, hlint is painfully slow doing so, so we run it on all provided files first
43+
# and then loop through the files which require changes only.
44+
text = ''
45+
mapfile -t filenames < <(
46+
${lib.getExe config.programs.hlint.package} --hint=${hlintConfig} --json "$@" \
47+
| jq -r '.[].file' \
48+
| sort -u \
49+
| grep -v '^$'
50+
)
51+
52+
for file in "''${filenames[@]}"; do
53+
${lib.getExe config.programs.hlint.package} \
54+
--hint="${hlintConfig}" \
55+
--refactor \
56+
--refactor-options=--inplace \
57+
"$file"
58+
done
59+
'';
60+
};
61+
};
3362

3463
# ruff has gaps in scanning for unused code, so we use vulture
3564
settings.formatter.vulture = {

0 commit comments

Comments
 (0)