Allow multi-word aliases (#8777)

This commit is contained in:
Jakub Žádník 2023-04-07 00:05:09 +03:00 committed by GitHub
parent a29b61bd4f
commit 87ddba0193
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 23 deletions

View file

@ -113,3 +113,16 @@ fn alias_wont_recurse2() {
assert!(actual.err.is_empty()); assert!(actual.err.is_empty());
}) })
} }
// Isuue https://github.com/nushell/nushell/issues/8103
#[test]
fn alias_multiword_name() {
let actual = nu!(r#"alias `foo bar` = echo 'test'; foo bar"#);
assert_eq!(actual.out, "test");
let actual = nu!(r#"alias 'foo bar' = echo 'test'; foo bar"#);
assert_eq!(actual.out, "test");
let actual = nu!(r#"alias "foo bar" = echo 'test'; foo bar"#);
assert_eq!(actual.out, "test");
}

View file

@ -731,7 +731,7 @@ pub fn parse_alias(
let has_help_flag = alias_call.has_flag("help"); let has_help_flag = alias_call.has_flag("help");
let alias_pipeline = Pipeline::from_vec(vec![Expression { let alias_pipeline = Pipeline::from_vec(vec![Expression {
expr: Expr::Call(alias_call), expr: Expr::Call(alias_call.clone()),
span: span(spans), span: span(spans),
ty: output, ty: output,
custom_completion: None, custom_completion: None,
@ -741,43 +741,52 @@ pub fn parse_alias(
return (alias_pipeline, None); return (alias_pipeline, None);
} }
if spans.len() >= split_id + 3 { let alias_name_expr = if let Some(expr) = alias_call.positional_nth(0) {
let alias_name = working_set.get_span_contents(spans[split_id]); expr
} else {
return (
garbage_pipeline(spans),
Some(ParseError::UnknownState(
"Missing positional after call check".to_string(),
span(spans),
)),
);
};
let alias_name = if alias_name.starts_with(b"\"") let alias_name = if let Some(name) = alias_name_expr.as_string() {
&& alias_name.ends_with(b"\"") if name.contains('#')
&& alias_name.len() > 1 || name.contains('^')
{ || name.parse::<bytesize::ByteSize>().is_ok()
alias_name[1..(alias_name.len() - 1)].to_vec() || name.parse::<f64>().is_ok()
} else {
alias_name.to_vec()
};
let checked_name = String::from_utf8_lossy(&alias_name).to_string();
if checked_name.contains('#')
|| checked_name.contains('^')
|| checked_name.parse::<bytesize::ByteSize>().is_ok()
|| checked_name.parse::<f64>().is_ok()
{ {
return ( return (
Pipeline::from_vec(vec![garbage(name_span)]), garbage_pipeline(spans),
Some(ParseError::AliasNotValid(name_span)), Some(ParseError::AliasNotValid(alias_name_expr.span)),
); );
} else {
name
} }
} else {
return (
garbage_pipeline(spans),
Some(ParseError::AliasNotValid(alias_name_expr.span)),
);
};
if spans.len() >= split_id + 3 {
if let Some(mod_name) = module_name { if let Some(mod_name) = module_name {
if checked_name.as_bytes() == mod_name { if alias_name.as_bytes() == mod_name {
return ( return (
alias_pipeline, alias_pipeline,
Some(ParseError::NamedAsModule( Some(ParseError::NamedAsModule(
"alias".to_string(), "alias".to_string(),
checked_name, alias_name,
spans[split_id], spans[split_id],
)), )),
); );
} }
if checked_name == "main" { if alias_name == "main" {
return ( return (
alias_pipeline, alias_pipeline,
Some(ParseError::ExportMainAliasNotAllowed(spans[split_id])), Some(ParseError::ExportMainAliasNotAllowed(spans[split_id])),
@ -846,7 +855,7 @@ pub fn parse_alias(
}; };
let decl = Alias { let decl = Alias {
name: checked_name, name: alias_name,
command, command,
wrapped_call, wrapped_call,
}; };