From e54b867e8e2e95a01b845f2ef8b79f4c549c997c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Fri, 7 Apr 2023 01:12:21 +0300 Subject: [PATCH] Remove parser keywords label from commands that do not need it (#8780) --- crates/nu-cmd-lang/src/core_commands/break_.rs | 9 --------- crates/nu-cmd-lang/src/core_commands/continue_.rs | 9 --------- crates/nu-cmd-lang/src/core_commands/if_.rs | 9 --------- crates/nu-cmd-lang/src/core_commands/loop_.rs | 9 --------- crates/nu-cmd-lang/src/core_commands/match_.rs | 9 --------- crates/nu-cmd-lang/src/core_commands/try_.rs | 9 --------- crates/nu-cmd-lang/src/core_commands/while_.rs | 9 --------- crates/nu-command/tests/commands/alias.rs | 12 ++++++++++++ crates/nu-parser/src/parse_keywords.rs | 10 +++++++++- 9 files changed, 21 insertions(+), 64 deletions(-) diff --git a/crates/nu-cmd-lang/src/core_commands/break_.rs b/crates/nu-cmd-lang/src/core_commands/break_.rs index 5bc6d44d10..0fb4e12133 100644 --- a/crates/nu-cmd-lang/src/core_commands/break_.rs +++ b/crates/nu-cmd-lang/src/core_commands/break_.rs @@ -20,15 +20,6 @@ impl Command for Break { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, _engine_state: &EngineState, diff --git a/crates/nu-cmd-lang/src/core_commands/continue_.rs b/crates/nu-cmd-lang/src/core_commands/continue_.rs index 19c11ab8d3..308033be1b 100644 --- a/crates/nu-cmd-lang/src/core_commands/continue_.rs +++ b/crates/nu-cmd-lang/src/core_commands/continue_.rs @@ -20,15 +20,6 @@ impl Command for Continue { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, _engine_state: &EngineState, diff --git a/crates/nu-cmd-lang/src/core_commands/if_.rs b/crates/nu-cmd-lang/src/core_commands/if_.rs index d6a2beb82b..129db3b37f 100644 --- a/crates/nu-cmd-lang/src/core_commands/if_.rs +++ b/crates/nu-cmd-lang/src/core_commands/if_.rs @@ -40,15 +40,6 @@ impl Command for If { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, engine_state: &EngineState, diff --git a/crates/nu-cmd-lang/src/core_commands/loop_.rs b/crates/nu-cmd-lang/src/core_commands/loop_.rs index 89bd0e88e9..39db174e05 100644 --- a/crates/nu-cmd-lang/src/core_commands/loop_.rs +++ b/crates/nu-cmd-lang/src/core_commands/loop_.rs @@ -25,15 +25,6 @@ impl Command for Loop { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, engine_state: &EngineState, diff --git a/crates/nu-cmd-lang/src/core_commands/match_.rs b/crates/nu-cmd-lang/src/core_commands/match_.rs index eea098ef16..b270ec7983 100644 --- a/crates/nu-cmd-lang/src/core_commands/match_.rs +++ b/crates/nu-cmd-lang/src/core_commands/match_.rs @@ -29,15 +29,6 @@ impl Command for Match { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, engine_state: &EngineState, diff --git a/crates/nu-cmd-lang/src/core_commands/try_.rs b/crates/nu-cmd-lang/src/core_commands/try_.rs index 37feac1224..b272769c8b 100644 --- a/crates/nu-cmd-lang/src/core_commands/try_.rs +++ b/crates/nu-cmd-lang/src/core_commands/try_.rs @@ -36,15 +36,6 @@ impl Command for Try { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, engine_state: &EngineState, diff --git a/crates/nu-cmd-lang/src/core_commands/while_.rs b/crates/nu-cmd-lang/src/core_commands/while_.rs index 168b2b146f..e375f0d0a6 100644 --- a/crates/nu-cmd-lang/src/core_commands/while_.rs +++ b/crates/nu-cmd-lang/src/core_commands/while_.rs @@ -30,15 +30,6 @@ impl Command for While { .category(Category::Core) } - fn extra_usage(&self) -> &str { - r#"This command is a parser keyword. For details, check: - https://www.nushell.sh/book/thinking_in_nu.html"# - } - - fn is_parser_keyword(&self) -> bool { - true - } - fn run( &self, engine_state: &EngineState, diff --git a/crates/nu-command/tests/commands/alias.rs b/crates/nu-command/tests/commands/alias.rs index 645c3f6227..5cd4fd9adc 100644 --- a/crates/nu-command/tests/commands/alias.rs +++ b/crates/nu-command/tests/commands/alias.rs @@ -126,6 +126,18 @@ fn alias_invalid_expression() { assert!(actual.err.contains("cant_alias_expression")); } +#[test] +fn alias_if() { + let actual = nu!(r#" alias spam = if true { 'spam' } else { 'eggs' }; spam "#); + assert_eq!(actual.out, "spam"); +} + +#[test] +fn alias_match() { + let actual = nu!(r#" alias spam = match 3 { 1..10 => 'yes!' }; spam "#); + assert_eq!(actual.out, "yes!"); +} + // Issue https://github.com/nushell/nushell/issues/8103 #[test] fn alias_multiword_name() { diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 28908b7286..f259106f89 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -798,8 +798,16 @@ pub fn parse_alias( let _equals = working_set.get_span_contents(spans[split_id + 1]); let replacement_spans = &spans[(split_id + 2)..]; + let first_bytes = working_set.get_span_contents(replacement_spans[0]); - if is_math_expression_like(working_set, replacement_spans[0], expand_aliases_denylist) { + if first_bytes != b"if" + && first_bytes != b"match" + && is_math_expression_like( + working_set, + replacement_spans[0], + expand_aliases_denylist, + ) + { // TODO: Maybe we need to implement a Display trait for Expression? let (expr, _) = parse_expression( working_set,