From cf0a18be51eddbb95dff04172e2a72e9dc7c432f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Fri, 2 Dec 2022 01:11:03 +0200 Subject: [PATCH] Fix where -b flag (#7313) # Description The `where -b` flag was broken. The following now works: ``` let cond = {|x| $x.name !~ 'foo'}; ls | where -b $cond ``` This is just a quick fix. Ultimately, the `where` command shouldn't need the flag and `where $cond` should work. The first step, however, is to move `where` away from shape-directed parsing and make it a parser keyword. # User-Facing Changes `where -b` is not broken # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- crates/nu-command/src/example_test.rs | 5 +++-- crates/nu-command/src/filters/where_.rs | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index 15228941a2..1900d5aec4 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -9,8 +9,8 @@ pub fn test_examples(cmd: impl Command + 'static) { #[cfg(test)] mod test_examples { use super::super::{ - Ansi, Date, Echo, From, If, Into, LetEnv, Math, MathEuler, MathPi, MathRound, Path, Random, - Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Url, Wrap, + Ansi, Date, Echo, From, If, Into, Let, LetEnv, Math, MathEuler, MathPi, MathRound, Path, + Random, Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Url, Wrap, }; use crate::{Break, Mut, To}; use itertools::Itertools; @@ -60,6 +60,7 @@ mod test_examples { // Base functions that are needed for testing // Try to keep this working set small to keep tests running as fast as possible let mut working_set = StateWorkingSet::new(&*engine_state); + working_set.add_decl(Box::new(Let)); working_set.add_decl(Box::new(Str)); working_set.add_decl(Box::new(StrJoin)); working_set.add_decl(Box::new(StrLength)); diff --git a/crates/nu-command/src/filters/where_.rs b/crates/nu-command/src/filters/where_.rs index 19e91117ce..66f1bd69f6 100644 --- a/crates/nu-command/src/filters/where_.rs +++ b/crates/nu-command/src/filters/where_.rs @@ -49,7 +49,7 @@ impl Command for Where { call: &Call, input: PipelineData, ) -> Result { - if let Ok(Some(capture_block)) = call.get_flag::(engine_state, stack, "block") { + if let Ok(Some(capture_block)) = call.get_flag::(engine_state, stack, "closure") { let metadata = input.metadata(); let ctrlc = engine_state.ctrlc.clone(); let engine_state = engine_state.clone(); @@ -303,6 +303,14 @@ impl Command for Where { span: Span::test_data(), }), }, + Example { + description: "Filter items of a list according to a stored condition", + example: "let cond = {|x| $x > 1}; [1 2] | where -b $cond", + result: Some(Value::List { + vals: vec![Value::test_int(2)], + span: Span::test_data(), + }), + }, Example { description: "List all files in the current directory with sizes greater than 2kb", example: "ls | where size > 2kb",