mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
allow define it as a variable inside closure (#12888)
# Description Fixes: #12690 The issue is happened after https://github.com/nushell/nushell/pull/12056 is merged. It will raise error if user doesn't supply required parameter when run closure with do. And parser adds a `$it` parameter when parsing closure or block expression. I believe the previous behavior is because we allow such syntax on previous version(0.44): ```nushell let x = { print $it } ``` But it's no longer allowed after 0.60. So I think they can be removed. # User-Facing Changes ```nushell let tmp = { let it = 42 print $it } do -c $tmp ``` should be possible again. # Tests + Formatting Added 1 test
This commit is contained in:
parent
6891267b53
commit
8adf3406e5
@ -66,3 +66,10 @@ fn ignore_error_works_with_list_stream() {
|
|||||||
let actual = nu!(r#"do -i { ["a", null, "b"] | ansi strip }"#);
|
let actual = nu!(r#"do -i { ["a", null, "b"] | ansi strip }"#);
|
||||||
assert!(actual.err.is_empty());
|
assert!(actual.err.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_closure_with_it_using() {
|
||||||
|
let actual = nu!(r#"let x = {let it = 3; $it}; do $x"#);
|
||||||
|
assert!(actual.err.is_empty());
|
||||||
|
assert_eq!(actual.out, "3");
|
||||||
|
}
|
||||||
|
@ -4177,20 +4177,6 @@ pub fn parse_block_expression(working_set: &mut StateWorkingSet, span: Span) ->
|
|||||||
|
|
||||||
if let Some(signature) = signature {
|
if let Some(signature) = signature {
|
||||||
output.signature = signature.0;
|
output.signature = signature.0;
|
||||||
} else if let Some(last) = working_set.delta.scope.last() {
|
|
||||||
// FIXME: this only supports the top $it. Is this sufficient?
|
|
||||||
|
|
||||||
if let Some(var_id) = last.get_var(b"$it") {
|
|
||||||
let mut signature = Signature::new("");
|
|
||||||
signature.required_positional.push(PositionalArg {
|
|
||||||
var_id: Some(*var_id),
|
|
||||||
name: "$it".into(),
|
|
||||||
desc: String::new(),
|
|
||||||
shape: SyntaxShape::Any,
|
|
||||||
default_value: None,
|
|
||||||
});
|
|
||||||
output.signature = Box::new(signature);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
output.span = Some(span);
|
output.span = Some(span);
|
||||||
@ -4518,20 +4504,6 @@ pub fn parse_closure_expression(
|
|||||||
|
|
||||||
if let Some(signature) = signature {
|
if let Some(signature) = signature {
|
||||||
output.signature = signature.0;
|
output.signature = signature.0;
|
||||||
} else if let Some(last) = working_set.delta.scope.last() {
|
|
||||||
// FIXME: this only supports the top $it. Is this sufficient?
|
|
||||||
|
|
||||||
if let Some(var_id) = last.get_var(b"$it") {
|
|
||||||
let mut signature = Signature::new("");
|
|
||||||
signature.required_positional.push(PositionalArg {
|
|
||||||
var_id: Some(*var_id),
|
|
||||||
name: "$it".into(),
|
|
||||||
desc: String::new(),
|
|
||||||
shape: SyntaxShape::Any,
|
|
||||||
default_value: None,
|
|
||||||
});
|
|
||||||
output.signature = Box::new(signature);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
output.span = Some(span);
|
output.span = Some(span);
|
||||||
|
Loading…
Reference in New Issue
Block a user