feat: align example treefmt config with flake treefmt config

Prevent unnecessary reformatting
This commit is contained in:
Brian McGee 2023-12-23 13:06:50 +00:00
parent 0c93d98483
commit 12452b01b6
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0
8 changed files with 215 additions and 209 deletions

View File

@ -55,7 +55,7 @@ func TestConfig(t *testing.T) {
// nix
nix, ok := cfg.Formatters["nix"]
as.True(ok, "nix formatter not found")
as.Equal("nixpkgs-fmt", nix.Command)
as.Equal("alejandra", nix.Command)
as.Nil(nix.Options)
as.Equal([]string{"*.nix"}, nix.Includes)
as.Equal([]string{"examples/nix/sources.nix"}, nix.Excludes)
@ -72,7 +72,7 @@ func TestConfig(t *testing.T) {
prettier, ok := cfg.Formatters["prettier"]
as.True(ok, "prettier formatter not found")
as.Equal("prettier", prettier.Command)
as.Equal([]string{"--write"}, prettier.Options)
as.Equal([]string{"--write", "--tab-width", "4"}, prettier.Options)
as.Equal([]string{
"*.css",
"*.html",
@ -88,12 +88,12 @@ func TestConfig(t *testing.T) {
as.Equal([]string{"CHANGELOG.md"}, prettier.Excludes)
// rust
// rust, ok := cfg.Formatters["rust"]
// as.True(ok, "rust formatter not found")
// as.Equal("rustfmt", rust.Command)
// as.Equal([]string{"--edition", "2018"}, rust.Options)
// as.Equal([]string{"*.rs"}, rust.Includes)
// as.Nil(rust.Excludes)
rust, ok := cfg.Formatters["rust"]
as.True(ok, "rust formatter not found")
as.Equal("rustfmt", rust.Command)
as.Equal([]string{"--edition", "2018"}, rust.Options)
as.Equal([]string{"*.rs"}, rust.Includes)
as.Nil(rust.Excludes)
// shell
shell, ok := cfg.Formatters["shell"]

View File

@ -28,12 +28,11 @@
golangci-lint
# formatters for testing
alejandra
elmPackages.elm-format
haskellPackages.cabal-fmt
haskellPackages.ormolu
mdsh
nixpkgs-fmt
nodePackages.prettier
python3.pkgs.black
rufo

View File

@ -15,7 +15,21 @@
statix.enable = true;
};
settings.formatter.prettier.options = ["--tab-width" "4"];
settings.formatter.prettier = {
options = ["--tab-width" "4"];
includes = [
"*.css"
"*.html"
"*.js"
"*.json"
"*.jsx"
"*.md"
"*.mdx"
"*.scss"
"*.ts"
"*.yaml"
];
};
};
devshells.default = {

View File

@ -46,7 +46,10 @@ const helloFactory = function ({ React }) {
return (
<p>
Hello,&nbsp;
<span style={styles.displayMode} onClick={() => setMode("edit")}>
<span
style={styles.displayMode}
onClick={() => setMode("edit")}
>
{word}!
</span>
<input

View File

@ -3,8 +3,7 @@ let
#
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: name: spec:
let
fetch_file = pkgs: name: spec: let
name' = sanitizeName name + "-src";
in
if spec.builtin or true
@ -20,8 +19,7 @@ let
name = name';
};
fetch_tarball = pkgs: name: spec:
let
fetch_tarball = pkgs: name: spec: let
name' = sanitizeName name + "-src";
in
if spec.builtin or true
@ -37,8 +35,7 @@ let
inherit (spec) url sha256;
};
fetch_git = name: spec:
let
fetch_git = name: spec: let
ref =
spec.ref
or (
@ -85,17 +82,16 @@ let
);
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources: system:
let
mkPkgs = sources: system: let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
import (builtins_fetchTarball {inherit (sources.nixpkgs) url sha256;}) {inherit system;};
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath
then import <nixpkgs> { }
then import <nixpkgs> {}
else
abort
''
@ -123,8 +119,7 @@ let
# If the environment variable NIV_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
replace = name: drv:
let
replace = name: drv: let
saneName =
stringAsChars
(c:
@ -162,7 +157,7 @@ let
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last:
if first > last
then [ ]
then []
else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
@ -177,34 +172,28 @@ let
optionalAttrs = cond: as:
if cond
then as
else { };
else {};
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball =
{ url
, name ? null
, sha256
,
} @ attrs:
let
builtins_fetchTarball = {
url,
name ? null,
} @ attrs: let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12"
then fetchTarball ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
then fetchTarball ({inherit url;} // (optionalAttrs (name != null) {inherit name;}))
else fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl =
{ url
, name ? null
, sha256
,
} @ attrs:
let
builtins_fetchurl = {
url,
name ? null,
} @ attrs: let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12"
then fetchurl ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
then fetchurl ({inherit url;} // (optionalAttrs (name != null) {inherit name;}))
else fetchurl attrs;
# Create the final "sources" from the config
@ -216,21 +205,22 @@ let
then
abort
"The values in sources.json should not have an 'outPath' attribute"
else spec // { outPath = replace name (fetch config.pkgs name spec); }
else spec // {outPath = replace name (fetch config.pkgs name spec);}
)
config.sources;
# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? if builtins.pathExists ./sources.json
mkConfig = {
sourcesFile ?
if builtins.pathExists ./sources.json
then ./sources.json
else null
, sources ? if (sourcesFile == null)
then { }
else builtins.fromJSON (builtins.readFile sourcesFile)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
,
else null,
sources ?
if (sourcesFile == null)
then {}
else builtins.fromJSON (builtins.readFile sourcesFile),
system ? builtins.currentSystem,
pkgs ? mkPkgs sources system,
}: rec {
# The sources, i.e. the attribute set of spec name to spec
inherit sources;
@ -239,4 +229,4 @@ let
inherit pkgs;
};
in
mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); }
mkSources (mkConfig {}) // {__functor = _: settings: mkSources (mkConfig settings);}

View File

@ -27,7 +27,7 @@ includes = ["*.hs"]
excludes = ["examples/haskell/"]
[formatter.nix]
command = "nixpkgs-fmt"
command = "alejandra"
includes = ["*.nix"]
# Act as an example on how to exclude specific files
excludes = ["examples/nix/sources.nix"]
@ -39,7 +39,7 @@ includes = ["*.rb"]
[formatter.prettier]
command = "prettier"
options = ["--write"]
options = ["--write", "--tab-width", "4"]
includes = [
"*.css",
"*.html",