Make if else more lazy (#5386)

This commit is contained in:
JT 2022-05-01 09:13:21 +12:00 committed by GitHub
parent 3df03e2e6d
commit f16401152b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

View file

@ -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::ast::Call;
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoPipelineData, PipelineData, ShellError, Signature, Category, Example, FromValue, PipelineData, ShellError, Signature, SyntaxShape, Value,
SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -85,12 +84,24 @@ https://www.nushell.sh/book/thinking_in_nushell.html#parsing-and-evaluation-are-
call.redirect_stderr, call.redirect_stderr,
) )
} else { } else {
eval_expression(engine_state, stack, else_expr) eval_expression_with_input(
.map(|x| x.into_pipeline_data()) engine_state,
stack,
else_expr,
input,
call.redirect_stdout,
call.redirect_stderr,
)
} }
} else { } else {
eval_expression(engine_state, stack, else_case) eval_expression_with_input(
.map(|x| x.into_pipeline_data()) engine_state,
stack,
else_case,
input,
call.redirect_stdout,
call.redirect_stderr,
)
} }
} else { } else {
Ok(PipelineData::new(call.head)) Ok(PipelineData::new(call.head))

View file

@ -43,6 +43,14 @@ fn in_variable_6() -> TestResult {
run_test(r#"3 | if $in > 6 { $in - 10 } else { $in * 10 }"#, "30") 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] #[test]
fn help_works_with_missing_requirements() -> TestResult { fn help_works_with_missing_requirements() -> TestResult {
run_test(r#"each --help | lines | length"#, "29") run_test(r#"each --help | lines | length"#, "29")