Merge pull request #112 from nushell/fix_for

Fix the for loop to create vars
This commit is contained in:
JT 2021-10-10 05:24:05 +13:00 committed by GitHub
commit b28f876095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 3 deletions

View File

@ -18,7 +18,7 @@ impl Command for For {
Signature::build("for")
.required(
"var_name",
SyntaxShape::Variable,
SyntaxShape::VarWithOptType,
"name of the looping variable",
)
.required(
@ -34,6 +34,7 @@ impl Command for For {
SyntaxShape::Block(Some(vec![])),
"the block to run",
)
.creates_scope()
}
fn run(

View File

@ -2,6 +2,7 @@ mod alias;
mod def;
mod do_;
mod export_def;
mod for_;
mod help;
mod hide;
mod if_;
@ -14,6 +15,7 @@ pub use alias::Alias;
pub use def::Def;
pub use do_::Do;
pub use export_def::ExportDef;
pub use for_::For;
pub use help::Help;
pub use hide::Hide;
pub use if_::If;

View File

@ -1,5 +1,4 @@
mod each;
mod for_;
mod get;
mod length;
mod lines;
@ -8,7 +7,6 @@ mod where_;
mod wrap;
pub use each::Each;
pub use for_::For;
pub use get::Get;
pub use length::Length;
pub use lines::Lines;

View File

@ -459,6 +459,10 @@ pub fn parse_internal_call(
let signature = working_set.get_decl(decl_id).signature();
if signature.creates_scope {
working_set.enter_scope();
}
// The index into the positional parameter in the definition
let mut positional_idx = 0;
@ -554,6 +558,10 @@ pub fn parse_internal_call(
let err = check_call(command_span, &signature, &call);
error = error.or(err);
if signature.creates_scope {
working_set.exit_scope();
}
// FIXME: type unknown
(Box::new(call), span(spans), error)
}

View File

@ -36,6 +36,7 @@ pub struct Signature {
pub rest_positional: Option<PositionalArg>,
pub named: Vec<Flag>,
pub is_filter: bool,
pub creates_scope: bool,
}
impl PartialEq for Signature {
@ -62,6 +63,7 @@ impl Signature {
rest_positional: None,
named: vec![],
is_filter: false,
creates_scope: false,
}
}
pub fn build(name: impl Into<String>) -> Signature {
@ -189,6 +191,12 @@ impl Signature {
self
}
/// Sets that signature will create a scope as it parses
pub fn creates_scope(mut self) -> Signature {
self.creates_scope = true;
self
}
/// Get list of the short-hand flags
pub fn get_shorts(&self) -> Vec<char> {
self.named.iter().filter_map(|f| f.short).collect()

View File

@ -564,3 +564,8 @@ fn split_column() -> TestResult {
"hello",
)
}
#[test]
fn for_loops() -> TestResult {
run_test(r#"(for x in [1, 2, 3] { $x + 10 }).1"#, "12")
}