mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
normalize special characters in module names to allow variable access (#14353)
Fixes #14252 # User-Facing Changes - Special characters in module names are replaced with underscores when importing constants, preventing "expected valid variable name": ```nushell > module foo-bar { export const baz = 1 } > use foo-bar > $foo_bar.baz ``` - "expected valid variable name" errors now include a suggestion list: ```nushell > module foo-bar { export const baz = 1 } > use foo-bar > $foo-bar Error: nu::parser::parse_mismatch_with_did_you_mean × Parse mismatch during operation. ╭─[entry #1:1:1] 1 │ $foo-bar; · ────┬─── · ╰── expected valid variable name. Did you mean '$foo_bar'? ╰──── ```
This commit is contained in:
@ -2122,7 +2122,21 @@ pub fn parse_variable_expr(working_set: &mut StateWorkingSet, span: Span) -> Exp
|
||||
String::from_utf8_lossy(contents).to_string()
|
||||
};
|
||||
|
||||
if let Some(id) = parse_variable(working_set, span) {
|
||||
let bytes = working_set.get_span_contents(span);
|
||||
let suggestion = || {
|
||||
DidYouMean::new(
|
||||
&working_set.list_variables(),
|
||||
working_set.get_span_contents(span),
|
||||
)
|
||||
};
|
||||
if !is_variable(bytes) {
|
||||
working_set.error(ParseError::ExpectedWithDidYouMean(
|
||||
"valid variable name",
|
||||
suggestion(),
|
||||
span,
|
||||
));
|
||||
garbage(working_set, span)
|
||||
} else if let Some(id) = working_set.find_variable(bytes) {
|
||||
Expression::new(
|
||||
working_set,
|
||||
Expr::Var(id),
|
||||
@ -2133,9 +2147,7 @@ pub fn parse_variable_expr(working_set: &mut StateWorkingSet, span: Span) -> Exp
|
||||
working_set.error(ParseError::EnvVarNotVar(name, span));
|
||||
garbage(working_set, span)
|
||||
} else {
|
||||
let ws = &*working_set;
|
||||
let suggestion = DidYouMean::new(&ws.list_variables(), ws.get_span_contents(span));
|
||||
working_set.error(ParseError::VariableNotFound(suggestion, span));
|
||||
working_set.error(ParseError::VariableNotFound(suggestion(), span));
|
||||
garbage(working_set, span)
|
||||
}
|
||||
}
|
||||
@ -5612,18 +5624,6 @@ pub fn parse_expression(working_set: &mut StateWorkingSet, spans: &[Span]) -> Ex
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_variable(working_set: &mut StateWorkingSet, span: Span) -> Option<VarId> {
|
||||
let bytes = working_set.get_span_contents(span);
|
||||
|
||||
if is_variable(bytes) {
|
||||
working_set.find_variable(bytes)
|
||||
} else {
|
||||
working_set.error(ParseError::Expected("valid variable name", span));
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_builtin_commands(
|
||||
working_set: &mut StateWorkingSet,
|
||||
lite_command: &LiteCommand,
|
||||
|
Reference in New Issue
Block a user