Prevent invalid var names

This commit is contained in:
JT 2021-10-12 18:08:55 +13:00
parent 5f14faf4b4
commit aea8627c30
3 changed files with 16 additions and 0 deletions

View File

@ -73,6 +73,10 @@ pub enum ParseError {
#[diagnostic(code(nu::parser::variable_not_found), url(docsrs))]
VariableNotFound(#[label = "variable not found"] Span),
#[error("Variable name not supported.")]
#[diagnostic(code(nu::parser::variable_not_valid), url(docsrs))]
VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span),
#[error("Module not found.")]
#[diagnostic(code(nu::parser::module_not_found), url(docsrs))]
ModuleNotFound(#[label = "module not found"] Span),

View File

@ -1782,6 +1782,13 @@ pub fn parse_var_with_opt_type(
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(spans[*spans_idx]).to_vec();
if bytes.contains(&b' ') || bytes.contains(&b'"') || bytes.contains(&b'\'') {
return (
garbage(spans[*spans_idx]),
Some(ParseError::VariableNotValid(spans[*spans_idx])),
);
}
if bytes.ends_with(b":") {
// We end with colon, so the next span should be the type
if *spans_idx + 1 < spans.len() {

View File

@ -759,3 +759,8 @@ fn custom_switch4() -> TestResult {
"bar",
)
}
#[test]
fn bad_var_name() -> TestResult {
fail_test(r#"let $"foo bar" = 4"#, "can't contain")
}