From d3bfc615245b8c7ef4e3dc10b32673e2c14be834 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Mon, 10 Jan 2022 13:52:01 +1100 Subject: [PATCH] Don't panic on alias errors (#713) --- crates/nu-parser/src/parser.rs | 27 +++++++++++++++++++++------ src/tests/test_parser.rs | 9 +++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 59ff117b1b..72dd36f22f 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -748,6 +748,7 @@ pub fn parse_call( let mut pos = 0; let cmd_start = pos; let mut name_spans = vec![]; + let mut name = vec![]; for word_span in spans[cmd_start..].iter() { // Find the longest group of words that could form a command @@ -759,11 +760,17 @@ pub fn parse_call( name_spans.push(*word_span); - let name = working_set.get_span_contents(span(&name_spans)); + let name_part = working_set.get_span_contents(*word_span); + if name.is_empty() { + name.extend(name_part); + } else { + name.push(b' '); + name.extend(name_part); + } if expand_aliases { // If the word is an alias, expand it and re-parse the expression - if let Some(expansion) = working_set.find_alias(name) { + if let Some(expansion) = working_set.find_alias(&name) { trace!("expanding alias"); let orig_span = spans[pos]; @@ -801,8 +808,7 @@ pub fn parse_call( pos += 1; } - let name = working_set.get_span_contents(span(&name_spans)); - let mut maybe_decl_id = working_set.find_decl(name); + let mut maybe_decl_id = working_set.find_decl(&name); while maybe_decl_id.is_none() { // Find the longest command match @@ -814,8 +820,17 @@ pub fn parse_call( name_spans.pop(); pos -= 1; - let name = working_set.get_span_contents(span(&name_spans)); - maybe_decl_id = working_set.find_decl(name); + let mut name = vec![]; + for name_span in &name_spans { + let name_part = working_set.get_span_contents(*name_span); + if name.is_empty() { + name.extend(name_part); + } else { + name.push(b' '); + name.extend(name_part); + } + } + maybe_decl_id = working_set.find_decl(&name); } if let Some(decl_id) = maybe_decl_id { diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index ac27807666..64fa65a66c 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -139,3 +139,12 @@ fn multiline_pipe_in_block() -> TestResult { fn bad_short_flag() -> TestResult { fail_test(r#"def foo3 [-l?:int] { $l }"#, "short flag") } + +#[test] +fn alias_with_error_doesnt_panic() -> TestResult { + fail_test( + r#"alias s = shells + s ."#, + "extra positional", + ) +}