Repository: nushell/nushell
File: crates/nu-protocol/src/module.rs (Rust)

Code before:
                    new_name.push(b' ');
                    new_name.extend(sub_name);

                    decls.push((new_name, sub_decl_id));
                }

                const_rows.extend(sub_results.constants);
            }

            decls.extend(self.decls_with_head(&final_name));

            for (name, var_id) in self.consts() {
                match working_set.get_constant(var_id) {
                    Ok(const_val) => const_rows.push((name, const_val.clone())),
                    Err(err) => errors.push(err),
                }
            }

            let span = self.span.unwrap_or(backup_span);

            // only needs to bring `$module` with a record value if it defines any constants.
            let constants = if const_rows.is_empty() {
                vec![]
            } else {
                vec![(
                    name_override.unwrap_or(&self.const_name).to_vec(),
                    Value::record(
                        const_rows
                            .into_iter()
                            .map(|(name, val)| (String::from_utf8_lossy(&name).to_string(), val))
                            .collect(),
                        span,
                    ),
                )]
            };

            return (
                ResolvedImportPattern::new(decls, vec![(final_name.clone(), self_id)], constants),
                errors,
            );
        };

        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(),
                    )]

Review comment (suggestion):
I don't think it's necessary to save `const_name` to a module.
We can just `normalize_variable_name` for final_name.

Code after:
                }

                const_rows.extend(sub_results.constants);
            }

            decls.extend(self.decls_with_head(&final_name));

            for (name, var_id) in self.consts() {
                match working_set.get_constant(var_id) {
                    Ok(const_val) => const_rows.push((name, const_val.clone())),
                    Err(err) => errors.push(err),
                }
            }

            let span = self.span.unwrap_or(backup_span);

            // only needs to bring `$module` with a record value if it defines any constants.
            let constants = if const_rows.is_empty() {
                vec![]
            } else {
                vec![(
                    normalize_module_name(&final_name),
                    Value::record(
                        const_rows
                            .into_iter()
                            .map(|(name, val)| (String::from_utf8_lossy(&name).to_string(), val))
                            .collect(),
                        span,
                    ),
                )]
            };

            return (
                ResolvedImportPattern::new(decls, vec![(final_name.clone(), self_id)], constants),
                errors,
            );
        };

        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![]
                };