From 435abadd8a0bec185bff1a31de86a2b4a9c5d8ae Mon Sep 17 00:00:00 2001 From: Andrej Kolchin Date: Wed, 8 Nov 2023 19:35:40 +0000 Subject: [PATCH] Add special error case for `alias` (#10975) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a special error, which is triggered by `alias foo=bar` style commands. It adds a help string which recommends adding spaces. Resolve #10958 --------- Co-authored-by: Jakub Žádník --- crates/nu-parser/src/parse_keywords.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 267938f6a8..02607f1929 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -1005,7 +1005,28 @@ pub fn parse_alias( working_set.add_decl(Box::new(decl)); } - if spans.len() < 4 { + // special case for `alias foo=bar` + if spans.len() == 2 && working_set.get_span_contents(spans[1]).contains(&b'=') { + let arg = String::from_utf8_lossy(working_set.get_span_contents(spans[1])); + + // split at '='. Note that the output must never be None, the + // `unwrap` is just to avoid the possibility of panic, if the + // invariant is broken. + let (name, initial_value) = arg.split_once('=').unwrap_or((&arg, "")); + + let name = if name.is_empty() { "{name}" } else { name }; + let initial_value = if initial_value.is_empty() { + "{initial_value}" + } else { + initial_value + }; + + working_set.error(ParseError::IncorrectValue( + "alias argument".into(), + spans[1], + format!("Make sure to put spaces around '=': alias {name} = {initial_value}"), + )) + } else if spans.len() < 4 { working_set.error(ParseError::IncorrectValue( "Incomplete alias".into(), span(&spans[..split_id]),