add support of set to use pipe as lhs

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-20 13:31:10 +03:00
parent 3abddcaf77
commit 00364dee14
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 15 additions and 6 deletions

11
lib.nix
View file

@ -121,15 +121,20 @@ let
or' = op kw_or;
not = expr: spaceBetween [ kw_not expr ];
local = expr: spaceBetween [ kw_local expr ];
validSetVariable = expr:
if isJoin expr && expr.sep == "." then expr
else if isRaw expr then expr
else raw (validString expr);
set = var: val: join " = " [ (validSetVariable var) val ];
# Type: validBlockBody :: a -> [b]
validBlockBody = body:
if isList body then body
else if isAttrs body then [ body ]
else validationError body "is not valid block body";
local = expr: spaceBetween [ kw_local expr ];
set = variable: value: join " = " [ (raw variable) value ];
funcParams' = params: wrapParen (join ", " (map raw params));
func = fnName: params: body:

View file

@ -177,15 +177,19 @@ with nix2lua; pkgs.lib.runTests {
]);
expected = "require(\"nvim-tree.api\").config.mappings.default_on_attach(bufnr)";
};
"test returns lua with setting value to the variable" = {
"test returns a simple var definition" = {
expr = toLua (set "parser_config.d2" { });
expected = "parser_config.d2 = { }";
};
"test returns lua with setting value to the local variable" = {
"test returns a var definition with pipe in the name part" = {
expr = toLua (set (pipe1 "parser_config" "d2") { });
expected = "parser_config.d2 = { }";
};
"test returns a local var definition" = {
expr = toLua (local (set "parser_config.d2" { }));
expected = "local parser_config.d2 = { }";
};
"test returns a short variant of setting a value to the local variable" = {
"test returns a short variant of local var definitions" = {
expr = toLua (lset "parser_config.d2" { });
expected = "local parser_config.d2 = { }";
};