Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

feat: enforce empty set/list style #280

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Unreleased / YYYY-MM-DD
=======================

### Formatting changes

* Empty sets: `{ }`
and lists: `[ ]`
are now formatted as: `{}`
and: `[]` respectively (#264)

1.2.0 / 2021-03-29
==================
Expand Down
12 changes: 6 additions & 6 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ pub(crate) fn spacing() -> SpacingDsl {
.test("[1 2 3]", "[ 1 2 3 ]")
.inside(NODE_LIST).after(T!["["]).single_space_or_newline()
.inside(NODE_LIST).before(T!["]"]).single_space_or_newline()
.test("[]", "[]")
.inside(NODE_LIST).between(T!["["], T!["]"]).no_space()
.inside(NODE_LIST).after(T!["["]).when(inline_with_attr_set).no_space()
.inside(NODE_LIST).before(T!["]"]).when(inline_with_attr_set).no_space()
.test("[]", "[ ]")
.inside(NODE_LIST).between(T!["["], T!["]"]).single_space_or_optional_newline()
.inside(NODE_LIST).between(VALUES, VALUES).single_space_or_newline()
.inside(NODE_LIST).between(VALUES, TOKEN_COMMENT).single_space_or_optional_newline()
.inside(NODE_LIST).between(TOKEN_COMMENT, VALUES).single_space_or_newline()
Expand All @@ -72,8 +72,8 @@ pub(crate) fn spacing() -> SpacingDsl {
.test("{foo = 92;}", "{ foo = 92; }")
.inside(NODE_ATTR_SET).after(T!["{"]).single_space_or_newline()
.inside(NODE_ATTR_SET).before(T!["}"]).single_space_or_newline()
.test("{}", "{ }")
.inside(NODE_ATTR_SET).between(T!["{"], T!["}"]).single_space()
.test("{}", "{}")
.inside(NODE_ATTR_SET).between(T!["{"], T!["}"]).no_space()
.inside(NODE_ATTR_SET).before(NODE_KEY_VALUE).single_space_or_optional_newline()
.inside(NODE_ATTR_SET).between(NODE_KEY_VALUE, NODE_KEY_VALUE).single_space_or_newline()
.inside(NODE_ATTR_SET).between(NODE_INHERIT, [NODE_INHERIT, TOKEN_COMMENT]).single_space_or_optional_newline()
Expand Down Expand Up @@ -120,7 +120,7 @@ pub(crate) fn spacing() -> SpacingDsl {
.inside(NODE_IF_ELSE).around([T![else],T![then]]).single_space_or_optional_newline()
.inside(NODE_IF_ELSE).after(T![then]).when(has_expression_node).single_space_or_newline()
.inside(NODE_IF_ELSE).after(T![else]).when(has_expression_node).single_space_or_newline()

// special-case to force a linebreak before `=` in
//
// ```nix
Expand Down Expand Up @@ -434,7 +434,7 @@ pub(crate) fn indentation() -> IndentDsl {
92
)
"#)

.rule("Indent attribute set content")
.inside(NODE_ATTR_SET)
.not_matching([T!["{"], T!["}"]])
Expand Down
2 changes: 1 addition & 1 deletion test_data/indent_or_default.good.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
catAttrs = builtins.catAttrs or
(attr: l: concatLists (map (s: if s ? ${attr} then [ s.${attr} ] else [ ]) l));
(attr: l: concatLists (map (s: if s ? ${attr} then [ s.${attr} ] else []) l));
}
2 changes: 1 addition & 1 deletion test_data/indent_paren.good.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
a
(attrNames n)
)
{ }
{}
list_of_attrs;
bar = fun "arg" (callPackage ./. {
inherit foo;
Expand Down
4 changes: 2 additions & 2 deletions test_data/indented_lambda.bad.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
else "--${k}"
, mkOption ? k: v:
if v == null
then [ ]
else [ (mkOptionName k) (lib.generators.mkValueStringDefault { } v) ]
then []
else [ (mkOptionName k) (lib.generators.mkValueStringDefault {} v) ]
}:
{ toINI = {
# parameter comment
Expand Down
6 changes: 3 additions & 3 deletions test_data/indented_lambda.good.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
else "--${k}"
, mkOption ? k: v:
if v == null
then [ ]
else [ (mkOptionName k) (lib.generators.mkValueStringDefault { } v) ]
then []
else [ (mkOptionName k) (lib.generators.mkValueStringDefault {} v) ]
zimbatm marked this conversation as resolved.
Show resolved Hide resolved
}:
{
toINI =
{
# parameter comment
mkSectionName ? (name: libStr.escape [ "[" "]" ] name)
, mkKeyValue ? mkKeyValueDefault { } "="
, mkKeyValue ? mkKeyValueDefault {} "="
}: attrsOfAttrs:
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
}
5 changes: 2 additions & 3 deletions test_data/issue-132.good.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
buildInputs = [
] ++ stdenv.lib.optionals enableGui (with qt5; [ qtbase qtwebkit ])
++ stdenv.lib.optionals enableJupyter [ boost jsoncpp openssl zmqpp ]
buildInputs = [] ++ stdenv.lib.optionals enableGui (with qt5; [ qtbase qtwebkit ])
++ stdenv.lib.optionals enableJupyter [ boost jsoncpp openssl zmqpp ]
;
}
2 changes: 1 addition & 1 deletion test_data/issue-185.bad.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
attrs = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attrs = {};
attrs = { };

lists = [ ];
lists = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lists = [];
lists = [ ];

}
4 changes: 2 additions & 2 deletions test_data/issue-185.good.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
attrs = { };
lists = [ ];
attrs = {};
lists = [];
}
2 changes: 1 addition & 1 deletion test_data/issue-199.good.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ let


in
{ }
{}
2 changes: 1 addition & 1 deletion test_data/issue-205.bad.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ let
inherit foo;bar = baz;
};
in
{ }
{}
2 changes: 1 addition & 1 deletion test_data/issue-205.good.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ let
inherit foo; bar = baz;
};
in
{ }
{}
2 changes: 1 addition & 1 deletion test_data/list_elements.good.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
1
[ ]
[]
{ a = 92; }
"hello"
foo.bar
Expand Down
2 changes: 1 addition & 1 deletion test_data/nested_indent.bad.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
pkgs ? import ./nix/nixpkgs.nix { },
pkgs ? import ./nix/nixpkgs.nix {},
src ? builtins.fetchGit {
url = ./.;
ref = "HEAD";
Expand Down
4 changes: 2 additions & 2 deletions test_data/nested_indent.good.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ pkgs ? import ./nix/nixpkgs.nix { }
{ pkgs ? import ./nix/nixpkgs.nix {}
, src ? builtins.fetchGit {
url = ./.;
ref = "HEAD";
}
}:
pkgs.example rec { }
pkgs.example rec {}
4 changes: 2 additions & 2 deletions test_data/nixpkgs_repository/doc_shellnix.good.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ pkgs ? import ../. { } }:
(import ./default.nix { }).overrideAttrs (x: {
{ pkgs ? import ../. {} }:
(import ./default.nix {}).overrideAttrs (x: {
buildInputs = x.buildInputs ++ [ pkgs.xmloscopy pkgs.ruby ];

})
8 changes: 4 additions & 4 deletions test_data/nixpkgs_repository/lib_customisation.bad.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
newDrv = derivation (drv.drvAttrs // (f drv));
in lib.flip (extendDerivation true) newDrv (
{
meta = drv.meta or { };
passthru = if drv ? passthru then drv.passthru else { };
meta = drv.meta or {};
passthru = if drv ? passthru then drv.passthru else {};
}
//
(drv.passthru or { })
(drv.passthru or {})
//
(if (drv ? crossDrv && drv ? nativeDrv)
then {
crossDrv = overrideDerivation drv.crossDrv f;
nativeDrv = overrideDerivation drv.nativeDrv f;
}
else { }));
else {}));
}
8 changes: 4 additions & 4 deletions test_data/nixpkgs_repository/lib_customisation.good.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
in
lib.flip (extendDerivation true) newDrv (
{
meta = drv.meta or { };
passthru = if drv ? passthru then drv.passthru else { };
meta = drv.meta or {};
passthru = if drv ? passthru then drv.passthru else {};
}
//
(drv.passthru or { })
(drv.passthru or {})
//
(if (drv ? crossDrv && drv ? nativeDrv)
then {
crossDrv = overrideDerivation drv.crossDrv f;
nativeDrv = overrideDerivation drv.nativeDrv f;
}
else { })
else {})
);
}
6 changes: 3 additions & 3 deletions test_data/nixpkgs_repository/lib_deprecated.good.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
innerClosePropagation = acc: xs:
if xs == [ ]
if xs == []
then acc
else
let
Expand All @@ -14,8 +14,8 @@
in innerClosePropagation
acc'
(uniqList {
inputList = (maybeAttrNullable "propagatedBuildInputs" [ ] y)
++ (maybeAttrNullable "propagatedNativeBuildInputs" [ ] y)
inputList = (maybeAttrNullable "propagatedBuildInputs" [] y)
++ (maybeAttrNullable "propagatedNativeBuildInputs" [] y)
++ ys;
acc = acc';
});
Expand Down
2 changes: 1 addition & 1 deletion test_data/nixpkgs_repository/nixpkgs_idempotent.good.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Operations on attribute sets.
rec {
filterAttrs = pred: set:
listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [ (nameValuePair name v) ] else [ ]) (attrNames set));
listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [ (nameValuePair name v) ] else []) (attrNames set));

inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor;
}
2 changes: 1 addition & 1 deletion test_data/syntax_errors/issue-93.good.nix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ } =
{} =
2 changes: 1 addition & 1 deletion test_data/top_level_with2.good.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import <nixpkgs> { };
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "rnix";
buildInputs = [ cargo ];
Expand Down