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

@ -85,7 +85,7 @@ fn works_with_1_param_blocks() {
fn works_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | all { print $in | true }"#
r#"[1 2 3] | all {|| print $in | true }"#
));
assert_eq!(actual.out, "123true");
@ -105,7 +105,7 @@ fn early_exits_with_1_param_blocks() {
fn early_exits_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | all { print $in | false }"#
r#"[1 2 3] | all {|| print $in | false }"#
));
assert_eq!(actual.out, "1false");
@ -125,7 +125,7 @@ fn all_uses_enumerate_index() {
fn unique_env_each_iteration() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"[1 2] | all { print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
"[1 2] | all {|| print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
);
assert_eq!(actual.out, "truetruetrue");

View File

@ -61,7 +61,7 @@ fn works_with_1_param_blocks() {
fn works_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | any { print $in | false }"#
r#"[1 2 3] | any {|| print $in | false }"#
));
assert_eq!(actual.out, "123false");
@ -81,7 +81,7 @@ fn early_exits_with_1_param_blocks() {
fn early_exits_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | any { print $in | true }"#
r#"[1 2 3] | any {|| print $in | true }"#
));
assert_eq!(actual.out, "1true");
@ -101,7 +101,7 @@ fn any_uses_enumerate_index() {
fn unique_env_each_iteration() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"[1 2] | any { print ($env.PWD | str ends-with 'formats') | cd '/' | false } | to nuon"
"[1 2] | any {|| print ($env.PWD | str ends-with 'formats') | cd '/' | false } | to nuon"
);
assert_eq!(actual.out, "truetruefalse");

View File

@ -79,7 +79,7 @@ fn long_stream_binary_overflow() {
let actual = nu!(
cwd: ".",
r#"
nu --testbin repeater (0x[01]) 32768 | bytes starts-with (0..32768 | each { 0x[01] } | bytes collect)
nu --testbin repeater (0x[01]) 32768 | bytes starts-with (0..32768 | each {|| 0x[01] } | bytes collect)
"#
);
@ -92,7 +92,7 @@ fn long_stream_binary_exact() {
let actual = nu!(
cwd: ".",
r#"
nu --testbin repeater (0x[01020304]) 8192 | bytes starts-with (0..<8192 | each { 0x[01020304] } | bytes collect)
nu --testbin repeater (0x[01020304]) 8192 | bytes starts-with (0..<8192 | each {|| 0x[01020304] } | bytes collect)
"#
);
@ -105,7 +105,7 @@ fn long_stream_string_exact() {
let actual = nu!(
cwd: ".",
r#"
nu --testbin repeater hell 8192 | bytes starts-with (0..<8192 | each { "hell" | into binary } | bytes collect)
nu --testbin repeater hell 8192 | bytes starts-with (0..<8192 | each {|| "hell" | into binary } | bytes collect)
"#
);
@ -118,8 +118,8 @@ fn long_stream_mixed_exact() {
let actual = nu!(
cwd: ".",
r#"
let binseg = (0..<2048 | each { 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each { "hell" | into binary } | bytes collect)
let binseg = (0..<2048 | each {|| 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each {|| "hell" | into binary } | bytes collect)
nu --testbin repeat_bytes 003d9fbf 2048 68656c6c 2048 | bytes starts-with (bytes build $binseg $strseg)
"#
@ -138,8 +138,8 @@ fn long_stream_mixed_overflow() {
let actual = nu!(
cwd: ".",
r#"
let binseg = (0..<2048 | each { 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each { "hell" | into binary } | bytes collect)
let binseg = (0..<2048 | each {|| 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each {|| "hell" | into binary } | bytes collect)
nu --testbin repeat_bytes 003d9fbf 2048 68656c6c 2048 | bytes starts-with (bytes build $binseg $strseg 0x[01])
"#

View File

@ -6,7 +6,7 @@ fn reports_emptiness() {
cwd: ".", pipeline(
r#"
[[] '' {} null]
| all {
| all {||
is-empty
}
"#
@ -21,7 +21,7 @@ fn reports_nonemptiness() {
cwd: ".", pipeline(
r#"
[[1] ' ' {a:1} 0]
| any {
| any {||
is-empty
}
"#
@ -36,7 +36,7 @@ fn reports_emptiness_by_columns() {
cwd: ".", pipeline(
r#"
[{a:1 b:null c:null} {a:2 b:null c:null}]
| any {
| any {||
is-empty b c
}
"#
@ -51,7 +51,7 @@ fn reports_nonemptiness_by_columns() {
cwd: ".", pipeline(
r#"
[{a:1 b:null c:3} {a:null b:5 c:2}]
| any {
| any {||
is-empty a b
}
"#

View File

@ -61,7 +61,7 @@ fn errors_if_given_unknown_column_name() {
cwd: dirs.test(), pipeline(
r#"
open los_tres_caballeros.json
| group-by { get nu.releases.version }
| group-by {|| get nu.releases.version }
"#
));

View File

@ -548,7 +548,7 @@ fn list_ignores_ansi() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls | find .txt | each { ls $in.name }
ls | find .txt | each {|| ls $in.name }
"#
));

View File

@ -44,7 +44,7 @@ fn sets_the_column_from_a_block_full_stream_output() {
cwd: "tests/fixtures/formats", pipeline(
r#"
{content: null}
| update content { open --raw cargo_sample.toml | lines | first 5 }
| update content {|| open --raw cargo_sample.toml | lines | first 5 }
| get content.1
| str contains "nu"
"#

View File

@ -44,7 +44,7 @@ fn sets_the_column_from_a_block_full_stream_output() {
cwd: "tests/fixtures/formats", pipeline(
r#"
{content: null}
| upsert content { open --raw cargo_sample.toml | lines | first 5 }
| upsert content {|| open --raw cargo_sample.toml | lines | first 5 }
| get content.1
| str contains "nu"
"#

View File

@ -35,7 +35,7 @@ fn where_inside_block_works() {
fn filters_with_0_arity_block() {
let actual = nu!(
cwd: ".",
"[1 2 3 4] | where { $in < 3 } | to nuon"
"[1 2 3 4] | where {|| $in < 3 } | to nuon"
);
assert_eq!(actual.out, "[1, 2]");
@ -55,7 +55,7 @@ fn filters_with_1_arity_block() {
fn unique_env_each_iteration() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"[1 2] | where { print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
"[1 2] | where {|| print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
);
assert_eq!(actual.out, "truetrue[1, 2]");