use command: Don't create a variable with empty record if it doesn't define any constants (#14051)

# Description
Fixes: #13967

The key changes lays in `nu-protocol/src/module.rs`, when resolving
import pattern, nushell only needs to bring `$module` with a record
value if it defines any constants.

# User-Facing Changes
```nushell
module spam {}
use spam
```
Will no longer create a `$spam` variable with an empty record.

# Tests + Formatting
Adjusted some tests and added some tests.
This commit is contained in:
Wind
2024-10-17 10:25:45 +08:00
committed by sholderbach
parent b0427ca9ff
commit d7014e671d
3 changed files with 49 additions and 26 deletions

View File

@ -161,20 +161,25 @@ impl Module {
}
let span = self.span.unwrap_or(backup_span);
let const_record = Value::record(
const_rows
.into_iter()
.map(|(name, val)| (String::from_utf8_lossy(&name).to_string(), val))
.collect(),
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![(
final_name.clone(),
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)],
vec![(final_name, const_record)],
),
ResolvedImportPattern::new(decls, vec![(final_name.clone(), self_id)], constants),
errors,
);
};