nushell/crates/nu-command/src/math
Artemiy 1867bb1a88
Fix incorrect handling of boolean flags for builtin commands (#11492)
# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6))
method did not evaluate and take into consideration expression used with
flag.

To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.

# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

![image](https://github.com/nushell/nushell/assets/17511668/f9fbabb2-3cfd-43f9-ba9e-ece76d80043c)
After:

![image](https://github.com/nushell/nushell/assets/17511668/21867596-2075-437f-9c85-45563ac70083)

Another example:
Before:

![image](https://github.com/nushell/nushell/assets/17511668/efdbc5ca-5227-45a4-ac5b-532cdc2bbf5f)
After:

![image](https://github.com/nushell/nushell/assets/17511668/2907d5c5-aa93-404d-af1c-21cdc3d44646)


# Tests + Formatting
Added test reproducing some variants of original issue.
2024-01-11 17:19:48 +02:00
..
abs.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
avg.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
ceil.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
floor.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
log.rs Enforce required, optional, and rest positional arguments start with an uppercase and end with a period. (#11285) 2023-12-15 14:32:37 +08:00
math_.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
max.rs Convert more examples and tests to record! macro (#10840) 2023-10-28 14:52:31 +02:00
median.rs Convert ShellError::UnsupportedInput to named fields (#10971) 2023-11-07 23:25:32 +01:00
min.rs Convert more examples and tests to record! macro (#10840) 2023-10-28 14:52:31 +02:00
mod.rs cratification: part III of the math commands to nu-cmd-extra (#9674) 2023-07-13 09:11:26 -07:00
mode.rs Convert ShellError::UnsupportedInput to named fields (#10971) 2023-11-07 23:25:32 +01:00
product.rs Replace &Span with Span since Span is Copy (#9770) 2023-07-31 21:47:46 +02:00
reducers.rs Apply nightly clippy fixes (#11083) 2023-11-17 09:15:55 -06:00
round.rs Add long options for generators and math (#10752) 2023-10-19 18:17:42 +02:00
sqrt.rs Convert ShellError::UnsupportedInput to named fields (#10971) 2023-11-07 23:25:32 +01:00
stddev.rs Fix incorrect handling of boolean flags for builtin commands (#11492) 2024-01-11 17:19:48 +02:00
sum.rs Replace &Span with Span since Span is Copy (#9770) 2023-07-31 21:47:46 +02:00
utils.rs Move more commands to opaque Record type (#11122) 2023-11-22 23:48:48 +01:00
variance.rs Fix incorrect handling of boolean flags for builtin commands (#11492) 2024-01-11 17:19:48 +02:00