mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
prevent alias name from being filesize or number (#6595)
* prevent alias name from being filesize or number * add test * fmt
This commit is contained in:
parent
32fbcf39cc
commit
1a3762b905
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2801,6 +2801,7 @@ dependencies = [
|
||||
name = "nu-parser"
|
||||
version = "0.69.2"
|
||||
dependencies = [
|
||||
"bytesize",
|
||||
"chrono",
|
||||
"itertools",
|
||||
"log",
|
||||
|
@ -40,3 +40,25 @@ fn alias_hiding_2() {
|
||||
|
||||
assert_eq!(actual.out, "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_fails_with_invalid_name() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
alias 1234 = echo "test"
|
||||
"#
|
||||
));
|
||||
assert!(actual
|
||||
.err
|
||||
.contains("alias name can't be a number or a filesize"));
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
alias 5gib = echo "test"
|
||||
"#
|
||||
));
|
||||
assert!(actual
|
||||
.err
|
||||
.contains("alias name can't be a number or a filesize"));
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ name = "nu-parser"
|
||||
version = "0.69.2"
|
||||
|
||||
[dependencies]
|
||||
bytesize = "1.1.0"
|
||||
chrono = "0.4.21"
|
||||
itertools = "0.10"
|
||||
miette = "5.1.0"
|
||||
|
@ -112,6 +112,10 @@ pub enum ParseError {
|
||||
#[diagnostic(code(nu::parser::variable_not_valid), url(docsrs))]
|
||||
VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span),
|
||||
|
||||
#[error("Alias name not supported.")]
|
||||
#[diagnostic(code(nu::parser::variable_not_valid), url(docsrs))]
|
||||
AliasNotValid(#[label = "alias name can't be a number or a filesize"] Span),
|
||||
|
||||
#[error("Module not found.")]
|
||||
#[diagnostic(
|
||||
code(nu::parser::module_not_found),
|
||||
@ -344,6 +348,7 @@ impl ParseError {
|
||||
ParseError::MultipleRestParams(s) => *s,
|
||||
ParseError::VariableNotFound(s) => *s,
|
||||
ParseError::VariableNotValid(s) => *s,
|
||||
ParseError::AliasNotValid(s) => *s,
|
||||
ParseError::ModuleNotFound(s) => *s,
|
||||
ParseError::CyclicalModuleImport(_, s) => *s,
|
||||
ParseError::ModuleOrOverlayNotFound(s) => *s,
|
||||
|
@ -544,16 +544,27 @@ pub fn parse_alias(
|
||||
spans: &[Span],
|
||||
expand_aliases_denylist: &[usize],
|
||||
) -> (Pipeline, Option<ParseError>) {
|
||||
let (name_span, split_id) =
|
||||
let (name_span, alias_name, split_id) =
|
||||
if spans.len() > 1 && working_set.get_span_contents(spans[0]) == b"export" {
|
||||
(spans[1], 2)
|
||||
(spans[1], spans.get(2), 2)
|
||||
} else {
|
||||
(spans[0], 1)
|
||||
(spans[0], spans.get(1), 1)
|
||||
};
|
||||
|
||||
let name = working_set.get_span_contents(name_span);
|
||||
|
||||
if name == b"alias" {
|
||||
if let Some(alias_name) = alias_name {
|
||||
let alias_name = String::from_utf8_lossy(working_set.get_span_contents(*alias_name));
|
||||
if alias_name.parse::<bytesize::ByteSize>().is_ok() || alias_name.parse::<f64>().is_ok()
|
||||
{
|
||||
return (
|
||||
Pipeline::from_vec(vec![garbage(name_span)]),
|
||||
Some(ParseError::AliasNotValid(name_span)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((span, err)) = check_name(working_set, spans) {
|
||||
return (Pipeline::from_vec(vec![garbage(*span)]), Some(err));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user