Restrict closure expression to be something like {|| ...} (#8290)

# Description

As title, closes: #7921 closes: #8273

# User-Facing Changes

when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe

  × Missing || inside closure
   ╭─[entry #2:1:1]
 1 │ let x = {ss ss}
   ·         ───┬───
   ·            ╰── Parsing as a closure, but || is missing
   ╰────
  help: Try add || to the beginning of closure
```

`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe

  × Missing || inside closure
   ╭─[entry #4:1:1]
 1 │ {major:2, minor:1, patch:4} | values | each { into string }
   ·                                             ───────┬───────
   ·                                                    ╰── Parsing as a closure, but || is missing
   ╰────
  help: Try add || to the beginning of closure
```

`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```

Just realize that It's a big breaking change, we need to update config
and scripts...

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
WindSoilder
2023-03-17 20:36:28 +08:00
committed by GitHub
parent 400a9d3b1e
commit a8eef9af33
24 changed files with 108 additions and 71 deletions

View File

@ -166,7 +166,7 @@ fn env_change_define_alias() {
#[test]
fn env_change_simple_block_preserve_env_var() {
let inp = &[
&env_change_hook("FOO", r#"{ let-env SPAM = "spam" }"#),
&env_change_hook("FOO", r#"{|| let-env SPAM = "spam" }"#),
"let-env FOO = 1",
"$env.SPAM",
];
@ -183,8 +183,8 @@ fn env_change_simple_block_list_shadow_env_var() {
&env_change_hook(
"FOO",
r#"[
{ let-env SPAM = "foo" }
{ let-env SPAM = "spam" }
{|| let-env SPAM = "foo" }
{|| let-env SPAM = "spam" }
]"#,
),
"let-env FOO = 1",
@ -200,7 +200,7 @@ fn env_change_simple_block_list_shadow_env_var() {
#[test]
fn env_change_block_preserve_env_var() {
let inp = &[
&env_change_hook_code("FOO", r#"{ let-env SPAM = "spam" }"#),
&env_change_hook_code("FOO", r#"{|| let-env SPAM = "spam" }"#),
"let-env FOO = 1",
"$env.SPAM",
];
@ -227,7 +227,7 @@ fn pre_prompt_define_command() {
#[test]
fn pre_prompt_simple_block_preserve_env_var() {
let inp = &[
&pre_prompt_hook(r#"{ let-env SPAM = "spam" }"#),
&pre_prompt_hook(r#"{|| let-env SPAM = "spam" }"#),
"$env.SPAM",
];
@ -242,8 +242,8 @@ fn pre_prompt_simple_block_list_shadow_env_var() {
let inp = &[
&pre_prompt_hook(
r#"[
{ let-env SPAM = "foo" }
{ let-env SPAM = "spam" }
{|| let-env SPAM = "foo" }
{|| let-env SPAM = "spam" }
]"#,
),
"$env.SPAM",
@ -258,7 +258,7 @@ fn pre_prompt_simple_block_list_shadow_env_var() {
#[test]
fn pre_prompt_block_preserve_env_var() {
let inp = &[
&pre_prompt_hook_code(r#"{ let-env SPAM = "spam" }"#),
&pre_prompt_hook_code(r#"{|| let-env SPAM = "spam" }"#),
"$env.SPAM",
];
@ -284,7 +284,7 @@ fn pre_execution_define_command() {
#[test]
fn pre_execution_simple_block_preserve_env_var() {
let inp = &[
&pre_execution_hook(r#"{ let-env SPAM = "spam" }"#),
&pre_execution_hook(r#"{|| let-env SPAM = "spam" }"#),
"$env.SPAM",
];
@ -299,8 +299,8 @@ fn pre_execution_simple_block_list_shadow_env_var() {
let inp = &[
&pre_execution_hook(
r#"[
{ let-env SPAM = "foo" }
{ let-env SPAM = "spam" }
{|| let-env SPAM = "foo" }
{|| let-env SPAM = "spam" }
]"#,
),
"$env.SPAM",
@ -315,7 +315,7 @@ fn pre_execution_simple_block_list_shadow_env_var() {
#[test]
fn pre_execution_block_preserve_env_var() {
let inp = &[
&pre_execution_hook_code(r#"{ let-env SPAM = "spam" }"#),
&pre_execution_hook_code(r#"{|| let-env SPAM = "spam" }"#),
"$env.SPAM",
];
@ -328,7 +328,7 @@ fn pre_execution_block_preserve_env_var() {
#[test]
fn pre_execution_commandline() {
let inp = &[
&pre_execution_hook_code(r#"{ let-env repl_commandline = (commandline) }"#),
&pre_execution_hook_code(r#"{|| let-env repl_commandline = (commandline) }"#),
"$env.repl_commandline",
];
@ -361,7 +361,7 @@ fn env_change_shadow_command() {
#[test]
fn env_change_block_dont_preserve_command() {
let inp = &[
&env_change_hook_code("FOO", r#"{ def foo [] { "foo" } }"#),
&env_change_hook_code("FOO", r#"{|| def foo [] { "foo" } }"#),
"let-env FOO = 1",
"foo",
];
@ -493,7 +493,7 @@ fn err_hook_non_boolean_condition_output() {
hooks: {
env_change: {
FOO : {
condition: { "foo" }
condition: {|| "foo" }
code: "print spam"
}
}