don't allow break/continue in each and items command (#13398)

# Description
Fixes: #11451

# User-Facing Changes
### Before
```nushell
❯ [1 2 3] | each {|e| break; print $e }
╭────────────╮
│ empty list │
╰────────────╯
```

### After
```
❯ [1 2 3] | each {|e| break; print $e }
Error: nu:🐚:eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #9:1:2]
 1 │ [1 2 3] | each {|e| break; print $e }
   ·  ┬
   ·  ╰── source value
   ╰────

Error:   × Break used outside of loop
   ╭─[entry #9:1:21]
 1 │ [1 2 3] | each {|e| break; print $e }
   ·                     ──┬──
   ·                       ╰── used outside of loop
   ╰────
```

# Tests + Formatting
Removes some tests.
This commit is contained in:
Wind 2024-07-19 15:21:02 +08:00 committed by GitHub
parent f3843a6176
commit e8764de3c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 6 additions and 36 deletions

View File

@ -21,7 +21,9 @@ impl Command for Break {
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
r#"This command is a parser keyword. For details, check: r#"This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nu.html"# https://www.nushell.sh/book/thinking_in_nu.html
break can only be used in while, loop, and for loops. It can not be used with each or other filter commands"#
} }
fn command_type(&self) -> CommandType { fn command_type(&self) -> CommandType {

View File

@ -21,7 +21,9 @@ impl Command for Continue {
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
r#"This command is a parser keyword. For details, check: r#"This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nu.html"# https://www.nushell.sh/book/thinking_in_nu.html
continue can only be used in while, loop, and for loops. It can not be used with each or other filter commands"#
} }
fn command_type(&self) -> CommandType { fn command_type(&self) -> CommandType {

View File

@ -132,8 +132,6 @@ with 'transpose' first."#
Ok(data) => Some(data.into_value(head).unwrap_or_else(|err| { Ok(data) => Some(data.into_value(head).unwrap_or_else(|err| {
Value::error(chain_error_with_input(err, is_error, span), span) Value::error(chain_error_with_input(err, is_error, span), span)
})), })),
Err(ShellError::Continue { span }) => Some(Value::nothing(span)),
Err(ShellError::Break { .. }) => None,
Err(error) => { Err(error) => {
let error = chain_error_with_input(error, is_error, span); let error = chain_error_with_input(error, is_error, span);
Some(Value::error(error, span)) Some(Value::error(error, span))
@ -149,10 +147,6 @@ with 'transpose' first."#
.map_while(move |value| { .map_while(move |value| {
let value = match value { let value = match value {
Ok(value) => value, Ok(value) => value,
Err(ShellError::Continue { span }) => {
return Some(Value::nothing(span))
}
Err(ShellError::Break { .. }) => return None,
Err(err) => return Some(Value::error(err, head)), Err(err) => return Some(Value::error(err, head)),
}; };
@ -163,8 +157,6 @@ with 'transpose' first."#
.and_then(|data| data.into_value(head)) .and_then(|data| data.into_value(head))
{ {
Ok(value) => Some(value), Ok(value) => Some(value),
Err(ShellError::Continue { span }) => Some(Value::nothing(span)),
Err(ShellError::Break { .. }) => None,
Err(error) => { Err(error) => {
let error = chain_error_with_input(error, is_error, span); let error = chain_error_with_input(error, is_error, span);
Some(Value::error(error, span)) Some(Value::error(error, span))

View File

@ -60,7 +60,6 @@ impl Command for Items {
match result { match result {
Ok(value) => Some(value), Ok(value) => Some(value),
Err(ShellError::Break { .. }) => None,
Err(err) => { Err(err) => {
let err = chain_error_with_input(err, false, span); let err = chain_error_with_input(err, false, span);
Some(Value::error(err, head)) Some(Value::error(err, head))

View File

@ -15,12 +15,3 @@ fn break_while_loop() {
assert_eq!(actual.out, "hello"); assert_eq!(actual.out, "hello");
} }
#[test]
fn break_each() {
let actual = nu!("
[1, 2, 3, 4, 5] | each {|x| if $x > 3 { break }; $x} | math sum
");
assert_eq!(actual.out, "6");
}

View File

@ -58,22 +58,6 @@ fn each_while_uses_enumerate_index() {
assert_eq!(actual.out, "[0, 1, 2, 3]"); assert_eq!(actual.out, "[0, 1, 2, 3]");
} }
#[test]
fn each_element_continue_command() {
let actual =
nu!("[1,2,3,4,6,7] | each { |x| if ($x mod 2 == 0) {continue} else { $x }} | to nuon");
assert_eq!(actual.out, "[1, 3, 7]");
}
#[test]
fn each_element_break_command() {
let actual =
nu!("[1,2,5,4,6,7] | each { |x| if ($x mod 3 == 0) {break} else { $x }} | to nuon");
assert_eq!(actual.out, "[1, 2, 5, 4]");
}
#[test] #[test]
fn errors_in_nested_each_show() { fn errors_in_nested_each_show() {
let actual = nu!("[[1,2]] | each {|x| $x | each {|y| error make {msg: \"oh noes\"} } }"); let actual = nu!("[[1,2]] | each {|x| $x | each {|y| error make {msg: \"oh noes\"} } }");