Revert "Make if blocks work like a def-env (#4656)" (#4782)

This reverts commit 477f3be8df.
This commit is contained in:
JT 2022-03-08 08:29:12 -05:00 committed by GitHub
parent 477f3be8df
commit dc0c5a9772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 70 deletions

View file

@ -37,88 +37,50 @@ impl Command for If {
fn run(
&self,
engine_state: &EngineState,
caller_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let cond = &call.positional[0];
let then_block: CaptureBlock = call.req(engine_state, caller_stack, 1)?;
let then_block: CaptureBlock = call.req(engine_state, stack, 1)?;
let else_case = call.positional.get(2);
let result = eval_expression(engine_state, caller_stack, cond)?;
let result = eval_expression(engine_state, stack, cond)?;
match &result {
Value::Bool { val, .. } => {
if *val {
let block = engine_state.get_block(then_block.block_id);
let mut callee_stack = caller_stack.captures_to_stack(&then_block.captures);
let result = eval_block(
let mut stack = stack.captures_to_stack(&then_block.captures);
eval_block(
engine_state,
&mut callee_stack,
&mut stack,
block,
input,
call.redirect_stdout,
call.redirect_stderr,
);
let caller_env_vars = caller_stack.get_env_var_names(engine_state);
// remove env vars that are present in the caller but not in the callee
// (the callee hid them)
for var in caller_env_vars.iter() {
if !callee_stack.has_env_var(engine_state, var) {
caller_stack.remove_env_var(engine_state, var);
}
}
// add new env vars from callee to caller
for env_vars in callee_stack.env_vars {
for (var, value) in env_vars {
caller_stack.add_env_var(var, value);
}
}
result
)
} else if let Some(else_case) = else_case {
if let Some(else_expr) = else_case.as_keyword() {
if let Some(block_id) = else_expr.as_block() {
let result = eval_expression(engine_state, caller_stack, else_expr)?;
let result = eval_expression(engine_state, stack, else_expr)?;
let else_block: CaptureBlock = FromValue::from_value(&result)?;
let mut callee_stack =
caller_stack.captures_to_stack(&else_block.captures);
let mut stack = stack.captures_to_stack(&else_block.captures);
let block = engine_state.get_block(block_id);
let result = eval_block(
eval_block(
engine_state,
&mut callee_stack,
&mut stack,
block,
input,
call.redirect_stdout,
call.redirect_stderr,
);
let caller_env_vars = caller_stack.get_env_var_names(engine_state);
// remove env vars that are present in the caller but not in the callee
// (the callee hid them)
for var in caller_env_vars.iter() {
if !callee_stack.has_env_var(engine_state, var) {
caller_stack.remove_env_var(engine_state, var);
}
}
// add new env vars from callee to caller
for env_vars in callee_stack.env_vars {
for (var, value) in env_vars {
caller_stack.add_env_var(var, value);
}
}
result
)
} else {
eval_expression(engine_state, caller_stack, else_expr)
eval_expression(engine_state, stack, else_expr)
.map(|x| x.into_pipeline_data())
}
} else {
eval_expression(engine_state, caller_stack, else_case)
eval_expression(engine_state, stack, else_case)
.map(|x| x.into_pipeline_data())
}
} else {
@ -150,11 +112,6 @@ impl Command for If {
example: "if 5 < 3 { 'yes!' } else if 4 < 5 { 'no!' } else { 'okay!' }",
result: Some(Value::test_string("no!")),
},
Example {
description: "Update the environment based on a condition",
example: r#"if true { let-env ENV_VAR = "new value" }"#,
result: None,
},
]
}
}

View file

@ -350,16 +350,3 @@ fn default_value11() -> TestResult {
fn default_value12() -> TestResult {
fail_test(r#"def foo [--x:int = "a"] { $x }"#, "default value not int")
}
#[test]
fn def_env_if() -> TestResult {
run_test(r#"if true { let-env FOO = "QUX" }; $env.FOO"#, "QUX")
}
#[test]
fn def_env_if2() -> TestResult {
run_test(
r#"if false { let-env FOO = "QUX" } else { let-env FOO = "QUUX" }; $env.FOO"#,
"QUUX",
)
}