From f16401152bb1cdbd1c227dd2486894617e3f721c Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sun, 1 May 2022 09:13:21 +1200 Subject: [PATCH] Make if else more lazy (#5386) --- crates/nu-command/src/core_commands/if_.rs | 25 ++++++++++++++++------ src/tests/test_engine.rs | 8 +++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/src/core_commands/if_.rs b/crates/nu-command/src/core_commands/if_.rs index 45f180b7f7..b934275ae3 100644 --- a/crates/nu-command/src/core_commands/if_.rs +++ b/crates/nu-command/src/core_commands/if_.rs @@ -1,9 +1,8 @@ -use nu_engine::{eval_block, eval_expression, CallExt}; +use nu_engine::{eval_block, eval_expression, eval_expression_with_input, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::{ - Category, Example, FromValue, IntoPipelineData, PipelineData, ShellError, Signature, - SyntaxShape, Value, + Category, Example, FromValue, PipelineData, ShellError, Signature, SyntaxShape, Value, }; #[derive(Clone)] @@ -85,12 +84,24 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are- call.redirect_stderr, ) } else { - eval_expression(engine_state, stack, else_expr) - .map(|x| x.into_pipeline_data()) + eval_expression_with_input( + engine_state, + stack, + else_expr, + input, + call.redirect_stdout, + call.redirect_stderr, + ) } } else { - eval_expression(engine_state, stack, else_case) - .map(|x| x.into_pipeline_data()) + eval_expression_with_input( + engine_state, + stack, + else_case, + input, + call.redirect_stdout, + call.redirect_stderr, + ) } } else { Ok(PipelineData::new(call.head)) diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index b37714cc68..d0553e0cd4 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -43,6 +43,14 @@ fn in_variable_6() -> TestResult { run_test(r#"3 | if $in > 6 { $in - 10 } else { $in * 10 }"#, "30") } +#[test] +fn in_and_if_else() -> TestResult { + run_test( + r#"[1, 2, 3] | if false {} else if true { $in | length }"#, + "3", + ) +} + #[test] fn help_works_with_missing_requirements() -> TestResult { run_test(r#"each --help | lines | length"#, "29")