mirror of
https://github.com/nushell/nushell.git
synced 2025-04-10 14:08:40 +02:00
feat(overlay): expose constants with overlay use
(#15081)
# Description `overlay use` now imports constants exported from modules, just like `use`. ```nushell # foo.nu export const a = 1 export const b = 2 ``` - `overlay use foo.nu` being equivalent to `use foo.nu *` and exposing constants `$a = 1` and `$b = 2` - `overlay use foo.nu -p` being equivalent to `use foo.nu` and exposing the constant `$foo = {a: 1, b: 2}` # User-Facing Changes `overlay use` now imports constants just like `use`. # Tests + Formatting - 🟢 toolkit fmt - 🟢 toolkit clippy - 🟢 toolkit test - 🟢 toolkit test stdlib # After Submitting N/A
This commit is contained in:
parent
62e56d3581
commit
4ac4f71a37
@ -2977,8 +2977,7 @@ pub fn parse_overlay_new(working_set: &mut StateWorkingSet, call: Box<Call>) ->
|
|||||||
working_set.add_overlay(
|
working_set.add_overlay(
|
||||||
overlay_name.as_bytes().to_vec(),
|
overlay_name.as_bytes().to_vec(),
|
||||||
module_id,
|
module_id,
|
||||||
vec![],
|
ResolvedImportPattern::new(vec![], vec![], vec![], vec![]),
|
||||||
vec![],
|
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -3192,8 +3191,7 @@ pub fn parse_overlay_use(working_set: &mut StateWorkingSet, call: Box<Call>) ->
|
|||||||
working_set.add_overlay(
|
working_set.add_overlay(
|
||||||
final_overlay_name.as_bytes().to_vec(),
|
final_overlay_name.as_bytes().to_vec(),
|
||||||
origin_module_id,
|
origin_module_id,
|
||||||
definitions.decls,
|
definitions,
|
||||||
definitions.modules,
|
|
||||||
has_prefix,
|
has_prefix,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,7 +5,8 @@ use crate::{
|
|||||||
StateDelta, Variable, VirtualPath, Visibility,
|
StateDelta, Variable, VirtualPath, Visibility,
|
||||||
},
|
},
|
||||||
BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, OverlayId,
|
BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, OverlayId,
|
||||||
ParseError, ParseWarning, Signature, Span, SpanId, Type, Value, VarId, VirtualPathId,
|
ParseError, ParseWarning, ResolvedImportPattern, Signature, Span, SpanId, Type, Value, VarId,
|
||||||
|
VirtualPathId,
|
||||||
};
|
};
|
||||||
use core::panic;
|
use core::panic;
|
||||||
use std::{
|
use std::{
|
||||||
@ -915,7 +916,12 @@ impl<'a> StateWorkingSet<'a> {
|
|||||||
let name = self.last_overlay_name().to_vec();
|
let name = self.last_overlay_name().to_vec();
|
||||||
let origin = overlay_frame.origin;
|
let origin = overlay_frame.origin;
|
||||||
let prefixed = overlay_frame.prefixed;
|
let prefixed = overlay_frame.prefixed;
|
||||||
self.add_overlay(name, origin, vec![], vec![], prefixed);
|
self.add_overlay(
|
||||||
|
name,
|
||||||
|
origin,
|
||||||
|
ResolvedImportPattern::new(vec![], vec![], vec![], vec![]),
|
||||||
|
prefixed,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.delta
|
self.delta
|
||||||
@ -952,8 +958,7 @@ impl<'a> StateWorkingSet<'a> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
name: Vec<u8>,
|
name: Vec<u8>,
|
||||||
origin: ModuleId,
|
origin: ModuleId,
|
||||||
decls: Vec<(Vec<u8>, DeclId)>,
|
definitions: ResolvedImportPattern,
|
||||||
modules: Vec<(Vec<u8>, ModuleId)>,
|
|
||||||
prefixed: bool,
|
prefixed: bool,
|
||||||
) {
|
) {
|
||||||
let last_scope_frame = self.delta.last_scope_frame_mut();
|
let last_scope_frame = self.delta.last_scope_frame_mut();
|
||||||
@ -980,8 +985,22 @@ impl<'a> StateWorkingSet<'a> {
|
|||||||
|
|
||||||
self.move_predecls_to_overlay();
|
self.move_predecls_to_overlay();
|
||||||
|
|
||||||
self.use_decls(decls);
|
self.use_decls(definitions.decls);
|
||||||
self.use_modules(modules);
|
self.use_modules(definitions.modules);
|
||||||
|
|
||||||
|
let mut constants = vec![];
|
||||||
|
|
||||||
|
for (name, const_vid) in definitions.constants {
|
||||||
|
constants.push((name, const_vid));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (name, const_val) in definitions.constant_values {
|
||||||
|
let const_var_id =
|
||||||
|
self.add_variable(name.clone(), Span::unknown(), const_val.get_type(), false);
|
||||||
|
self.set_variable_const_val(const_var_id, const_val);
|
||||||
|
constants.push((name, const_var_id));
|
||||||
|
}
|
||||||
|
self.use_variables(constants);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_overlay(&mut self, name: &[u8], keep_custom: bool) {
|
pub fn remove_overlay(&mut self, name: &[u8], keep_custom: bool) {
|
||||||
|
@ -347,9 +347,8 @@ fn const_captures_in_closures_work() {
|
|||||||
assert_eq!(actual.out, "hello world");
|
assert_eq!(actual.out, "hello world");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ignore = "TODO: Need to fix `overlay hide` to hide the constants brought by `overlay use`"]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn complex_const_overlay_use_hide() {
|
fn complex_const_overlay_use() {
|
||||||
let inp = &[MODULE_SETUP, "overlay use spam", "$X"];
|
let inp = &[MODULE_SETUP, "overlay use spam", "$X"];
|
||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "x");
|
assert_eq!(actual.out, "x");
|
||||||
@ -365,11 +364,15 @@ fn complex_const_overlay_use_hide() {
|
|||||||
let inp = &[
|
let inp = &[
|
||||||
MODULE_SETUP,
|
MODULE_SETUP,
|
||||||
"overlay use spam",
|
"overlay use spam",
|
||||||
"($eggs.bacon.none | is-empty)",
|
"($eggs.bacon not-has 'none')",
|
||||||
];
|
];
|
||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert_eq!(actual.out, "true");
|
assert_eq!(actual.out, "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[ignore = "TODO: `overlay hide` should be possible to use after `overlay use` in the same source unit."]
|
||||||
|
#[test]
|
||||||
|
fn overlay_use_hide_in_single_source_unit() {
|
||||||
let inp = &[MODULE_SETUP, "overlay use spam", "overlay hide", "$eggs"];
|
let inp = &[MODULE_SETUP, "overlay use spam", "overlay hide", "$eggs"];
|
||||||
let actual = nu!(&inp.join("; "));
|
let actual = nu!(&inp.join("; "));
|
||||||
assert!(actual.err.contains("nu::parser::variable_not_found"));
|
assert!(actual.err.contains("nu::parser::variable_not_found"));
|
||||||
|
Loading…
Reference in New Issue
Block a user