Allow modules to use other modules (#6162)

* Allow private imports inside modules

Can call `use ...` inside modules now.

* Add more tests

* Add a leak test

* Refactor exportables; Prepare for 'export use'

* Fix description

* Implement 'export use' command

This allows re-exporting module's commands and aliases from another
module.

* Add more tests; Fix import pattern list strings

The import pattern strings didn't trim the surrounding quotes.

* Add ignored test
This commit is contained in:
Jakub Žádník
2022-07-29 11:57:10 +03:00
committed by GitHub
parent cf2e9cf481
commit 2cffff0c1b
10 changed files with 620 additions and 117 deletions

View File

@ -18,7 +18,7 @@ impl Command for ExportCommand {
}
fn usage(&self) -> &str {
"Export custom commands or environment variables from a module."
"Export definitions or environment variables from a module."
}
fn extra_usage(&self) -> &str {

View File

@ -0,0 +1,56 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct ExportUse;
impl Command for ExportUse {
fn name(&self) -> &str {
"export use"
}
fn usage(&self) -> &str {
"Use definitions from a module and export them from this module"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("export use")
.required("pattern", SyntaxShape::ImportPattern, "import pattern")
.category(Category::Core)
}
fn extra_usage(&self) -> &str {
r#"This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nushell.html"#
}
fn is_parser_keyword(&self) -> bool {
true
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Re-export a command from another module",
example: r#"module spam { export def foo [] { "foo" } }
module eggs { export use spam foo }
use eggs foo
foo
"#,
result: Some(Value::String {
val: "foo".to_string(),
span: Span::test_data(),
}),
}]
}
}

View File

@ -12,6 +12,7 @@ mod export_def;
mod export_def_env;
mod export_env;
mod export_extern;
mod export_use;
mod extern_;
mod for_;
pub mod help;
@ -40,6 +41,7 @@ pub use export_def::ExportDef;
pub use export_def_env::ExportDefEnv;
pub use export_env::ExportEnv;
pub use export_extern::ExportExtern;
pub use export_use::ExportUse;
pub use extern_::Extern;
pub use for_::For;
pub use help::Help;