mirror of
https://github.com/nushell/nushell
synced 2025-01-02 16:29:00 +00:00
Merge pull request #112 from nushell/fix_for
Fix the for loop to create vars
This commit is contained in:
commit
b28f876095
6 changed files with 25 additions and 3 deletions
|
@ -18,7 +18,7 @@ impl Command for For {
|
||||||
Signature::build("for")
|
Signature::build("for")
|
||||||
.required(
|
.required(
|
||||||
"var_name",
|
"var_name",
|
||||||
SyntaxShape::Variable,
|
SyntaxShape::VarWithOptType,
|
||||||
"name of the looping variable",
|
"name of the looping variable",
|
||||||
)
|
)
|
||||||
.required(
|
.required(
|
||||||
|
@ -34,6 +34,7 @@ impl Command for For {
|
||||||
SyntaxShape::Block(Some(vec![])),
|
SyntaxShape::Block(Some(vec![])),
|
||||||
"the block to run",
|
"the block to run",
|
||||||
)
|
)
|
||||||
|
.creates_scope()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
|
@ -2,6 +2,7 @@ mod alias;
|
||||||
mod def;
|
mod def;
|
||||||
mod do_;
|
mod do_;
|
||||||
mod export_def;
|
mod export_def;
|
||||||
|
mod for_;
|
||||||
mod help;
|
mod help;
|
||||||
mod hide;
|
mod hide;
|
||||||
mod if_;
|
mod if_;
|
||||||
|
@ -14,6 +15,7 @@ pub use alias::Alias;
|
||||||
pub use def::Def;
|
pub use def::Def;
|
||||||
pub use do_::Do;
|
pub use do_::Do;
|
||||||
pub use export_def::ExportDef;
|
pub use export_def::ExportDef;
|
||||||
|
pub use for_::For;
|
||||||
pub use help::Help;
|
pub use help::Help;
|
||||||
pub use hide::Hide;
|
pub use hide::Hide;
|
||||||
pub use if_::If;
|
pub use if_::If;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
mod each;
|
mod each;
|
||||||
mod for_;
|
|
||||||
mod get;
|
mod get;
|
||||||
mod length;
|
mod length;
|
||||||
mod lines;
|
mod lines;
|
||||||
|
@ -8,7 +7,6 @@ mod where_;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
|
|
||||||
pub use each::Each;
|
pub use each::Each;
|
||||||
pub use for_::For;
|
|
||||||
pub use get::Get;
|
pub use get::Get;
|
||||||
pub use length::Length;
|
pub use length::Length;
|
||||||
pub use lines::Lines;
|
pub use lines::Lines;
|
||||||
|
|
|
@ -459,6 +459,10 @@ pub fn parse_internal_call(
|
||||||
|
|
||||||
let signature = working_set.get_decl(decl_id).signature();
|
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
|
// The index into the positional parameter in the definition
|
||||||
let mut positional_idx = 0;
|
let mut positional_idx = 0;
|
||||||
|
|
||||||
|
@ -554,6 +558,10 @@ pub fn parse_internal_call(
|
||||||
let err = check_call(command_span, &signature, &call);
|
let err = check_call(command_span, &signature, &call);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
|
if signature.creates_scope {
|
||||||
|
working_set.exit_scope();
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: type unknown
|
// FIXME: type unknown
|
||||||
(Box::new(call), span(spans), error)
|
(Box::new(call), span(spans), error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ pub struct Signature {
|
||||||
pub rest_positional: Option<PositionalArg>,
|
pub rest_positional: Option<PositionalArg>,
|
||||||
pub named: Vec<Flag>,
|
pub named: Vec<Flag>,
|
||||||
pub is_filter: bool,
|
pub is_filter: bool,
|
||||||
|
pub creates_scope: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Signature {
|
impl PartialEq for Signature {
|
||||||
|
@ -62,6 +63,7 @@ impl Signature {
|
||||||
rest_positional: None,
|
rest_positional: None,
|
||||||
named: vec![],
|
named: vec![],
|
||||||
is_filter: false,
|
is_filter: false,
|
||||||
|
creates_scope: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn build(name: impl Into<String>) -> Signature {
|
pub fn build(name: impl Into<String>) -> Signature {
|
||||||
|
@ -189,6 +191,12 @@ impl Signature {
|
||||||
self
|
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
|
/// Get list of the short-hand flags
|
||||||
pub fn get_shorts(&self) -> Vec<char> {
|
pub fn get_shorts(&self) -> Vec<char> {
|
||||||
self.named.iter().filter_map(|f| f.short).collect()
|
self.named.iter().filter_map(|f| f.short).collect()
|
||||||
|
|
|
@ -564,3 +564,8 @@ fn split_column() -> TestResult {
|
||||||
"hello",
|
"hello",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_loops() -> TestResult {
|
||||||
|
run_test(r#"(for x in [1, 2, 3] { $x + 10 }).1"#, "12")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue