Skip to content

Commit

Permalink
Properly evaluate dynamic blocks (nushell#3339)
Browse files Browse the repository at this point in the history
  • Loading branch information
JT committed Apr 21, 2021
1 parent ce35689 commit 02d5729
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
58 changes: 21 additions & 37 deletions crates/nu-engine/src/evaluate/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,51 +137,35 @@ fn run_pipeline(
}
}

match &call.head.expr {
Expression::Block(block) => {
let block = run_expression_block(&call.head, ctx)?.into_vec();

if block.len() != 1 {
return Err(ShellError::labeled_error(
"Dynamic commands must start with a block",
"needs to be a block",
call.head.span,
));
}

match &block[0].value {
UntaggedValue::Block(captured_block) => {
ctx.scope.enter_scope();
for (param, value) in block.params.positional.iter().zip(args.iter()) {
ctx.scope.add_vars(&captured_block.captured.entries);
for (param, value) in captured_block
.block
.params
.positional
.iter()
.zip(args.iter())
{
ctx.scope.add_var(param.0.name(), value[0].clone());
}
let result = run_block(&block, ctx, input);
let result = run_block(&captured_block.block, ctx, input);
ctx.scope.exit_scope();

let result = result?;
return Ok(result);
}
Expression::Variable(v, span) => {
if let Some(value) = ctx.scope.get_var(v) {
match &value.value {
UntaggedValue::Block(captured_block) => {
ctx.scope.enter_scope();
ctx.scope.add_vars(&captured_block.captured.entries);
for (param, value) in captured_block
.block
.params
.positional
.iter()
.zip(args.iter())
{
ctx.scope.add_var(param.0.name(), value[0].clone());
}
let result = run_block(&captured_block.block, ctx, input);
ctx.scope.exit_scope();

let result = result?;
return Ok(result);
}
_ => {
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));
}
Expand Down
11 changes: 11 additions & 0 deletions tests/shell/pipeline/commands/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 02d5729

Please sign in to comment.