From 0adf2accddb355421ef872f8ec3409060169de64 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 3 Oct 2020 09:18:23 +1300 Subject: [PATCH] Fix defaulting alias var values (#2631) --- crates/nu-cli/src/commands/run_alias.rs | 14 +++++++++++++- crates/nu-cli/tests/commands/alias.rs | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/commands/run_alias.rs b/crates/nu-cli/src/commands/run_alias.rs index efbf288478..79b4cf8612 100644 --- a/crates/nu-cli/src/commands/run_alias.rs +++ b/crates/nu-cli/src/commands/run_alias.rs @@ -4,7 +4,7 @@ use crate::prelude::*; use derive_new::new; use nu_errors::ShellError; -use nu_protocol::{hir::Block, Scope, Signature, SyntaxShape}; +use nu_protocol::{hir::Block, Scope, Signature, SyntaxShape, UntaggedValue}; #[derive(new, Clone)] pub struct AliasCommand { @@ -51,12 +51,24 @@ impl WholeStreamCommand for AliasCommand { let evaluated = call_info.evaluate(®istry).await?; let mut vars = IndexMap::new(); + + let mut num_positionals = 0; if let Some(positional) = &evaluated.args.positional { + num_positionals = positional.len(); for (pos, arg) in positional.iter().enumerate() { vars.insert(alias_command.args[pos].0.to_string(), arg.clone()); } } + if alias_command.args.len() > num_positionals { + for idx in 0..(alias_command.args.len() - num_positionals) { + vars.insert( + alias_command.args[idx + num_positionals].0.to_string(), + UntaggedValue::nothing().into_untagged_value(), + ); + } + } + let scope = Scope::append_vars(scope, vars); // FIXME: we need to patch up the spans to point at the top-level error diff --git a/crates/nu-cli/tests/commands/alias.rs b/crates/nu-cli/tests/commands/alias.rs index b3cc0ab7e2..0597acd6b1 100644 --- a/crates/nu-cli/tests/commands/alias.rs +++ b/crates/nu-cli/tests/commands/alias.rs @@ -16,6 +16,21 @@ fn alias_args_work() { }) } +#[test] +fn alias_missing_args_work() { + Playground::setup("append_test_1", |dirs, _| { + let actual = nu!( + cwd: dirs.root(), + r#" + alias double_echo [a b] {^echo $a $b} + double_echo bob + "# + ); + + assert_eq!(actual.out, "bob"); + }) +} + #[test] #[cfg(not(windows))] fn alias_parses_path_tilde() {