mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
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:
parent
b0427ca9ff
commit
d7014e671d
@ -161,20 +161,25 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let span = self.span.unwrap_or(backup_span);
|
let span = self.span.unwrap_or(backup_span);
|
||||||
let const_record = Value::record(
|
|
||||||
|
// 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
|
const_rows
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(name, val)| (String::from_utf8_lossy(&name).to_string(), val))
|
.map(|(name, val)| (String::from_utf8_lossy(&name).to_string(), val))
|
||||||
.collect(),
|
.collect(),
|
||||||
span,
|
span,
|
||||||
);
|
),
|
||||||
|
)]
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
ResolvedImportPattern::new(
|
ResolvedImportPattern::new(decls, vec![(final_name.clone(), self_id)], constants),
|
||||||
decls,
|
|
||||||
vec![(final_name.clone(), self_id)],
|
|
||||||
vec![(final_name, const_record)],
|
|
||||||
),
|
|
||||||
errors,
|
errors,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -238,13 +238,30 @@ fn complex_const_export() {
|
|||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "eats");
|
assert_eq!(actual.out, "eats");
|
||||||
|
|
||||||
let inp = &[
|
let inp = &[MODULE_SETUP, "use spam", "'none' in $spam.eggs.bacon"];
|
||||||
MODULE_SETUP,
|
|
||||||
"use spam",
|
|
||||||
"($spam.eggs.bacon.none | is-empty)",
|
|
||||||
];
|
|
||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "true");
|
assert_eq!(actual.out, "false");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn only_nested_module_have_const() {
|
||||||
|
let setup = r#"
|
||||||
|
module spam {
|
||||||
|
export module eggs {
|
||||||
|
export module bacon {
|
||||||
|
export const viking = 'eats'
|
||||||
|
export module none {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
let inp = &[setup, "use spam", "$spam.eggs.bacon.viking"];
|
||||||
|
let actual = nu!(&inp.join("; "));
|
||||||
|
assert_eq!(actual.out, "eats");
|
||||||
|
|
||||||
|
let inp = &[setup, "use spam", "'none' in $spam.eggs.bacon"];
|
||||||
|
let actual = nu!(&inp.join("; "));
|
||||||
|
assert_eq!(actual.out, "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -261,20 +278,16 @@ fn complex_const_glob_export() {
|
|||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "eats");
|
assert_eq!(actual.out, "eats");
|
||||||
|
|
||||||
let inp = &[MODULE_SETUP, "use spam *", "($eggs.bacon.none | is-empty)"];
|
let inp = &[MODULE_SETUP, "use spam *", "'none' in $eggs.bacon"];
|
||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "true");
|
assert_eq!(actual.out, "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn complex_const_drill_export() {
|
fn complex_const_drill_export() {
|
||||||
let inp = &[
|
let inp = &[MODULE_SETUP, "use spam eggs bacon none", "$none"];
|
||||||
MODULE_SETUP,
|
|
||||||
"use spam eggs bacon none",
|
|
||||||
"($none | is-empty)",
|
|
||||||
];
|
|
||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "true");
|
assert!(actual.err.contains("variable not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -120,6 +120,11 @@ fn export_consts() -> TestResult {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dont_export_module_name_as_a_variable() -> TestResult {
|
||||||
|
fail_test(r#"module spam { }; use spam; $spam"#, "variable not found")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn func_use_consts() -> TestResult {
|
fn func_use_consts() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
|
Loading…
Reference in New Issue
Block a user