From 6db5692be4de5c5a0e4b2063389d30e9b99b595d Mon Sep 17 00:00:00 2001 From: Hristo Filaretov Date: Thu, 19 Aug 2021 09:05:36 +0200 Subject: [PATCH] Only allow unaliasing in current scope, add tests (#3936) * unalias only removes aliases in the current scope * Add a test and fix previous ones which did not function as expected --- crates/nu-engine/src/evaluate/scope.rs | 2 +- tests/shell/pipeline/commands/internal.rs | 39 +++++++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/nu-engine/src/evaluate/scope.rs b/crates/nu-engine/src/evaluate/scope.rs index 4b12400903..5da6af9fe7 100644 --- a/crates/nu-engine/src/evaluate/scope.rs +++ b/crates/nu-engine/src/evaluate/scope.rs @@ -408,7 +408,7 @@ impl ParserScope for Scope { } fn remove_alias(&self, name: &str) { - for frame in self.frames.lock().iter_mut().rev() { + if let Some(frame) = self.frames.lock().last_mut() { frame.aliases.remove(name); } } diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 1b982f4846..d07c9b36fc 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -1150,23 +1150,44 @@ fn unalias_shadowing() { let actual = nu!( cwd: ".", pipeline( r#" - alias ll = ls -l - let xyz = { ll -a } - unalias ll - do $xyz + def test-shadowing [] { + alias greet = echo hello; + let xyz = { greet }; + unalias greet; + do $xyz + }; + test-shadowing "#) ); + assert_eq!(actual.out, "hello"); +} - assert_eq!(actual.out, ""); +#[test] +fn unalias_does_not_escape_scope() { + let actual = nu!( + cwd: ".", pipeline( + r#" + def test-alias [] { + alias greet = echo hello; + (unalias greet); + greet + }; + test-alias + "#) + ); + assert_eq!(actual.out, "hello"); } #[test] fn unalias_hides_alias() { let actual = nu!(cwd: ".", pipeline( - r#"alias ll = ls -l - ll - unalias ll - ll + r#" + def test-alias [] { + alias ll = ls -l; + unalias ll; + ll + }; + test-alias "#) );