mirror of
https://github.com/nushell/nushell
synced 2025-01-14 06:04:09 +00:00
Also set $in-variable with input (#856)
* Also set in-variable with input * Fix test * Add more tests
This commit is contained in:
parent
a4421434d9
commit
6f4b7efd3e
2 changed files with 54 additions and 2 deletions
|
@ -3610,7 +3610,8 @@ pub fn parse_block(
|
||||||
let block: Block = lite_block
|
let block: Block = lite_block
|
||||||
.block
|
.block
|
||||||
.iter()
|
.iter()
|
||||||
.map(|pipeline| {
|
.enumerate()
|
||||||
|
.map(|(idx, pipeline)| {
|
||||||
if pipeline.commands.len() > 1 {
|
if pipeline.commands.len() > 1 {
|
||||||
let mut output = pipeline
|
let mut output = pipeline
|
||||||
.commands
|
.commands
|
||||||
|
@ -3636,7 +3637,42 @@ pub fn parse_block(
|
||||||
expressions: output,
|
expressions: output,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let (stmt, err) = parse_statement(working_set, &pipeline.commands[0]);
|
let (mut stmt, err) = parse_statement(working_set, &pipeline.commands[0]);
|
||||||
|
|
||||||
|
if idx == 0 {
|
||||||
|
if let Some(let_decl_id) = working_set.find_decl(b"let") {
|
||||||
|
if let Some(let_env_decl_id) = working_set.find_decl(b"let-env") {
|
||||||
|
if let Statement::Pipeline(pipeline) = &mut stmt {
|
||||||
|
for expr in pipeline.expressions.iter_mut() {
|
||||||
|
if let Expression {
|
||||||
|
expr: Expr::Call(call),
|
||||||
|
..
|
||||||
|
} = expr
|
||||||
|
{
|
||||||
|
if call.decl_id == let_decl_id
|
||||||
|
|| call.decl_id == let_env_decl_id
|
||||||
|
{
|
||||||
|
// Do an expansion
|
||||||
|
if let Some(Expression {
|
||||||
|
expr: Expr::Keyword(_, _, expr),
|
||||||
|
..
|
||||||
|
}) = call.positional.get_mut(1)
|
||||||
|
{
|
||||||
|
if expr.has_in_variable(working_set) {
|
||||||
|
*expr = Box::new(wrap_expr_with_collect(
|
||||||
|
working_set,
|
||||||
|
expr,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if error.is_none() {
|
if error.is_none() {
|
||||||
error = err;
|
error = err;
|
||||||
|
|
|
@ -174,3 +174,19 @@ fn let_sees_input() -> TestResult {
|
||||||
"11",
|
"11",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn let_sees_in_variable() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"def c [] { let x = $in.name; $x | str length }; {name: bob, size: 100 } | c"#,
|
||||||
|
"3",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn let_sees_in_variable2() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"def c [] { let x = ($in | str length); $x }; 'bob' | c"#,
|
||||||
|
"3",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue