Don't panic on alias errors (#713)

This commit is contained in:
JT 2022-01-10 13:52:01 +11:00 committed by GitHub
parent 733b2836f1
commit d3bfc61524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -748,6 +748,7 @@ pub fn parse_call(
let mut pos = 0; let mut pos = 0;
let cmd_start = pos; let cmd_start = pos;
let mut name_spans = vec![]; let mut name_spans = vec![];
let mut name = vec![];
for word_span in spans[cmd_start..].iter() { for word_span in spans[cmd_start..].iter() {
// Find the longest group of words that could form a command // Find the longest group of words that could form a command
@ -759,11 +760,17 @@ pub fn parse_call(
name_spans.push(*word_span); 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 expand_aliases {
// If the word is an alias, expand it and re-parse the expression // 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"); trace!("expanding alias");
let orig_span = spans[pos]; let orig_span = spans[pos];
@ -801,8 +808,7 @@ pub fn parse_call(
pos += 1; 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() { while maybe_decl_id.is_none() {
// Find the longest command match // Find the longest command match
@ -814,8 +820,17 @@ pub fn parse_call(
name_spans.pop(); name_spans.pop();
pos -= 1; pos -= 1;
let name = working_set.get_span_contents(span(&name_spans)); let mut name = vec![];
maybe_decl_id = working_set.find_decl(name); 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 { if let Some(decl_id) = maybe_decl_id {

View File

@ -139,3 +139,12 @@ fn multiline_pipe_in_block() -> TestResult {
fn bad_short_flag() -> TestResult { fn bad_short_flag() -> TestResult {
fail_test(r#"def foo3 [-l?:int] { $l }"#, "short flag") 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",
)
}