Error on use path item1 item2, if item1 is not a module (#11183)

# Description
Fixes: #11143

# User-Facing Changes
Take the following as example:
```nushell
module foo { export def bar [] {}; export def baz [] {} }
```

`use foo bar baz` will be error:
```
❯ use foo c d
Error: nu::parser::wrong_import_pattern

  × Wrong import pattern structure.
   ╭─[entry #2:1:1]
 1 │ use foo c d
   ·           ┬
   ·           ╰── Trying to import something but the parent `c` is not a module, maybe you want to try `use <module> [<name1>, <name2>]`
   ╰────
```

# Tests + Formatting
Done
This commit is contained in:
WindSoilder
2023-12-05 18:38:45 +08:00
committed by GitHub
parent 2ffe30ecf0
commit fb3350ebc3
4 changed files with 47 additions and 13 deletions

View File

@ -157,6 +157,17 @@ impl Module {
match head {
ImportPatternMember::Name { name, span } => {
// raise errors if user wants to do something like this:
// `use a b c`: but b is not a sub-module of a.
let errors = if !rest.is_empty() && self.submodules.get(name).is_none() {
vec![ParseError::WrongImportPattern(
format!("Trying to import something but the parent `{}` is not a module, maybe you want to try `use <module> [<name1>, <name2>]`", String::from_utf8_lossy(name)),
rest[0].span(),
)]
} else {
vec![]
};
if name == b"main" {
if let Some(main_decl_id) = self.main {
(
@ -165,7 +176,7 @@ impl Module {
vec![],
vec![],
),
vec![],
errors,
)
} else {
(
@ -176,7 +187,7 @@ impl Module {
} else if let Some(decl_id) = self.decls.get(name) {
(
ResolvedImportPattern::new(vec![(name.clone(), *decl_id)], vec![], vec![]),
vec![],
errors,
)
} else if let Some(var_id) = self.constants.get(name) {
match working_set.get_constant(*var_id) {
@ -186,7 +197,7 @@ impl Module {
vec![],
vec![(name.clone(), const_val.clone())],
),
vec![],
errors,
),
Err(err) => (
ResolvedImportPattern::new(vec![], vec![], vec![]),