Skip to content

Commit

Permalink
Fix absorbtion of empty attrsets with newline
Browse files Browse the repository at this point in the history
Fixes #253
  • Loading branch information
piegamesde committed Dec 2, 2024
1 parent 64d3540 commit 622ea8c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ isAbsorbable (Path _) = True
-- Non-empty sets and lists
isAbsorbable (Set _ _ (Items (_ : _)) _) = True
isAbsorbable (List _ (Items (_ : _)) _) = True
-- Empty sets and lists if they have a line break
-- https://github.com/NixOS/nixfmt/issues/253
isAbsorbable (Set _ (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2}))
| line1 /= line2 = True
isAbsorbable (List (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2}))
| line1 /= line2 = True
isAbsorbable (Parenthesized (LoneAnn _) (Term t) _) = isAbsorbable t
isAbsorbable _ = False

Expand Down
17 changes: 17 additions & 0 deletions test/diff/attr_set/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,21 @@
pkgs.xorg.fontadobe75dpi
];
}
# Regression https://github.com/NixOS/nixfmt/issues/253
{
foo1 = {
};
foo2 = bar {
};
foo3 = bar {
} {
};
foo4 = [
];
foo5 = bar [
];
foo6 = bar [
] [
];
}
]
9 changes: 9 additions & 0 deletions test/diff/attr_set/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,13 @@
pkgs.xorg.fontadobe75dpi
];
}
# Regression https://github.com/NixOS/nixfmt/issues/253
{
foo1 = { };
foo2 = bar { };
foo3 = bar { } { };
foo4 = [ ];
foo5 = bar [ ];
foo6 = bar [ ] [ ];
}
]
23 changes: 23 additions & 0 deletions test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,27 @@
pkgs.xorg.fontadobe75dpi
];
}
# Regression https://github.com/NixOS/nixfmt/issues/253
{
foo1 = {
};
foo2 = bar {
};
foo3 =
bar
{
}
{
};
foo4 = [
];
foo5 = bar [
];
foo6 =
bar
[
]
[
];
}
]

0 comments on commit 622ea8c

Please sign in to comment.