diff --git a/crates/nu-command/tests/commands/mut_.rs b/crates/nu-command/tests/commands/mut_.rs index cb8f12d6a..72437a1f8 100644 --- a/crates/nu-command/tests/commands/mut_.rs +++ b/crates/nu-command/tests/commands/mut_.rs @@ -177,3 +177,15 @@ fn mut_records_update_properly() { let actual = nu!(pipeline("mut a = {}; $a.b.c = 100; $a.b.c")); assert_eq!(actual.out, "100"); } + +#[test] +fn mut_value_with_if() { + let actual = nu!("mut a = 3; $a = if 3 == 3 { 10 }; echo $a"); + assert_eq!(actual.out, "10"); +} + +#[test] +fn mut_value_with_match() { + let actual = nu!("mut a = 3; $a = match 3 { 1 => { 'yes!' }, _ => { 'no!' } }; echo $a"); + assert_eq!(actual.out, "no!"); +} diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index a693230ff..f9c5864c3 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -4870,6 +4870,14 @@ pub fn parse_math_expression( break; } + let content = working_set.get_span_contents(spans[idx]); + // allow `if` to be a special value for assignment. + if content == b"if" || content == b"match" { + let rhs = parse_call(working_set, &spans[idx..], spans[0], false); + expr_stack.push(op); + expr_stack.push(rhs); + break; + } let rhs = parse_value(working_set, spans[idx], &SyntaxShape::Any); while op_prec <= last_prec && expr_stack.len() > 1 { diff --git a/tests/shell/environment/env.rs b/tests/shell/environment/env.rs index 6513a05bf..f46b828f5 100644 --- a/tests/shell/environment/env.rs +++ b/tests/shell/environment/env.rs @@ -85,6 +85,18 @@ fn env_assignment() { assert_eq!(actual.out, "barbaz"); } +#[test] +fn env_assignment_with_if() { + let actual = nu!(r#"$env.FOOBAR = if 3 == 4 { "bar" } else { "baz" }; $env.FOOBAR"#); + assert_eq!(actual.out, "baz"); +} + +#[test] +fn env_assignment_with_match() { + let actual = nu!(r#"$env.FOOBAR = match 1 { 1 => { 'yes!' }, _ => { 'no!' } }; $env.FOOBAR"#); + assert_eq!(actual.out, "yes!"); +} + #[test] fn mutate_env_file_pwd_env_var_fails() { let actual = nu!("$env.FILE_PWD = 'foo'");