mirror of
https://github.com/nushell/nushell
synced 2024-11-11 15:37:07 +00:00
Properly evaluate dynamic blocks (#3339)
This commit is contained in:
parent
ce35689d2e
commit
02d5729941
2 changed files with 32 additions and 37 deletions
|
@ -137,21 +137,17 @@ fn run_pipeline(
|
|||
}
|
||||
}
|
||||
|
||||
match &call.head.expr {
|
||||
Expression::Block(block) => {
|
||||
ctx.scope.enter_scope();
|
||||
for (param, value) in block.params.positional.iter().zip(args.iter()) {
|
||||
ctx.scope.add_var(param.0.name(), value[0].clone());
|
||||
}
|
||||
let result = run_block(&block, ctx, input);
|
||||
ctx.scope.exit_scope();
|
||||
let block = run_expression_block(&call.head, ctx)?.into_vec();
|
||||
|
||||
let result = result?;
|
||||
return Ok(result);
|
||||
if block.len() != 1 {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Dynamic commands must start with a block",
|
||||
"needs to be a block",
|
||||
call.head.span,
|
||||
));
|
||||
}
|
||||
Expression::Variable(v, span) => {
|
||||
if let Some(value) = ctx.scope.get_var(v) {
|
||||
match &value.value {
|
||||
|
||||
match &block[0].value {
|
||||
UntaggedValue::Block(captured_block) => {
|
||||
ctx.scope.enter_scope();
|
||||
ctx.scope.add_vars(&captured_block.captured.entries);
|
||||
|
@ -174,18 +170,6 @@ fn run_pipeline(
|
|||
return Err(ShellError::labeled_error("Dynamic commands must start with a block (or variable pointing to a block)", "needs to be a block", call.head.span));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Variable not found",
|
||||
"variable not found",
|
||||
span,
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error("Dynamic commands must start with a block (or variable pointing to a block)", "needs to be a block", call.head.span));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ClassifiedCommand::Expr(expr) => run_expression_block(&*expr, ctx)?,
|
||||
|
|
|
@ -391,6 +391,17 @@ fn proper_shadow_set_aliases() {
|
|||
assert_eq!(actual.out, "falsetruefalse");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_dynamic_blocks() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
let block = { echo "holaaaa" }; $block
|
||||
"#
|
||||
);
|
||||
assert_eq!(actual.out, "holaaaa");
|
||||
}
|
||||
|
||||
#[cfg(feature = "which")]
|
||||
#[test]
|
||||
fn argument_invocation_reports_errors() {
|
||||
|
|
Loading…
Reference in a new issue