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:
Solomon
2024-12-05 06:35:15 -07:00
committed by GitHub
parent 3bd45c005b
commit 234484b6f8
4 changed files with 74 additions and 17 deletions

View File

@ -167,7 +167,7 @@ impl Module {
vec![]
} else {
vec![(
final_name.clone(),
normalize_module_name(&final_name),
Value::record(
const_rows
.into_iter()
@ -425,3 +425,32 @@ impl Module {
result
}
}
/// normalize module names for exporting as record constant
fn normalize_module_name(bytes: &[u8]) -> Vec<u8> {
bytes
.iter()
.map(|x| match is_identifier_byte(*x) {
true => *x,
false => b'_',
})
.collect()
}
fn is_identifier_byte(b: u8) -> bool {
b != b'.'
&& b != b'['
&& b != b'('
&& b != b'{'
&& b != b'+'
&& b != b'-'
&& b != b'*'
&& b != b'^'
&& b != b'/'
&& b != b'='
&& b != b'!'
&& b != b'<'
&& b != b'>'
&& b != b'&'
&& b != b'|'
}