Require that values that look like numbers parse as numberlike (#8635)

# Description

Require that any value that looks like it might be a number (starts with
a digit, or a '-' + digit, or a '+' + digits, or a special form float
like `-inf`, `inf`, or `NaN`) must now be treated as a number-like
value. Number-like syntax can only parse into number-like values.
Number-like values include: durations, ints, floats, ranges, filesizes,
binary data, etc.

# User-Facing Changes

BREAKING CHANGE
BREAKING CHANGE
BREAKING CHANGE
BREAKING CHANGE
BREAKING CHANGE
BREAKING CHANGE
BREAKING CHANGE
BREAKING CHANGE

Just making sure we see this for release notes 😅 

This breaks any and all numberlike values that were treated as strings
before. Example, we used to allow `3,` as a bare word. Anything like
this would now require quotes or backticks to be treated as a string or
bare word, respectively.

# 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

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```

# 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.
This commit is contained in:
JT
2023-03-28 19:31:38 +13:00
committed by GitHub
parent 7ec5f2f2eb
commit 90b65018b6
20 changed files with 154 additions and 86 deletions

View File

@ -90,7 +90,7 @@ impl Command for SubCommand {
Example {
description:
"Apply logical negation to a list of numbers, treat input as 2 bytes number",
example: "[4 3 2] | bits not -n 2",
example: "[4 3 2] | bits not -n '2'",
result: Some(Value::List {
vals: vec![
Value::test_int(65531),

View File

@ -85,7 +85,7 @@ impl Command for SubCommand {
},
Example {
description: "Rotate right a list of numbers of one byte",
example: "[15 33 92] | bits ror 2 -n 1",
example: "[15 33 92] | bits ror 2 -n '1'",
result: Some(Value::List {
vals: vec![
Value::test_int(195),

View File

@ -85,7 +85,7 @@ impl Command for SubCommand {
},
Example {
description: "Shift left a number with 1 byte by 7 bits",
example: "2 | bits shl 7 -n 1",
example: "2 | bits shl 7 -n '1'",
result: Some(Value::test_int(0)),
},
Example {

View File

@ -105,7 +105,7 @@ impl Command for Fill {
Example {
description:
"Fill a number on the left side to a width of 5 with the character '0'",
example: "1 | fill --alignment right --character 0 --width 5",
example: "1 | fill --alignment right --character '0' --width 5",
result: Some(Value::String {
val: "00001".into(),
span: Span::test_data(),
@ -113,7 +113,7 @@ impl Command for Fill {
},
Example {
description: "Fill a number on both sides to a width of 5 with the character '0'",
example: "1.1 | fill --alignment center --character 0 --width 5",
example: "1.1 | fill --alignment center --character '0' --width 5",
result: Some(Value::String {
val: "01.10".into(),
span: Span::test_data(),
@ -122,7 +122,7 @@ impl Command for Fill {
Example {
description:
"Fill a filesize on the left side to a width of 5 with the character '0'",
example: "1kib | fill --alignment middle --character 0 --width 10",
example: "1kib | fill --alignment middle --character '0' --width 10",
result: Some(Value::String {
val: "0001024000".into(),
span: Span::test_data(),

View File

@ -76,7 +76,8 @@ impl Command for Debug {
},
Example {
description: "Debug print a table",
example: "[[version patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | debug",
example:
"[[version patch]; ['0.1.0' false] ['0.1.1' true] ['0.2.0' false]] | debug",
result: Some(Value::List {
vals: vec![
Value::test_string("{version: 0.1.0, patch: false}"),

View File

@ -119,7 +119,7 @@ impl Command for Find {
},
Example {
description: "Find value in records",
example: r#"[[version name]; [0.1.0 nushell] [0.1.1 fish] [0.2.0 zsh]] | find -r "nu""#,
example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find -r "nu""#,
result: Some(Value::List {
vals: vec![Value::test_record(
vec!["version", "name"],

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
/// Source a file for environment variables.
@ -23,7 +23,7 @@ impl Command for GotoShell {
])
.optional(
"shell_number",
SyntaxShape::String,
SyntaxShape::OneOf(vec![SyntaxShape::Int, SyntaxShape::String]),
"shell number to change to",
)
.category(Category::Shells)
@ -40,23 +40,32 @@ impl Command for GotoShell {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let new_shell: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
let new_shell: Option<Value> = call.opt(engine_state, stack, 0)?;
match new_shell {
Some(shell_span) => {
if shell_span.item == "-" {
switch_shell(engine_state, stack, call, shell_span.span, SwitchTo::Last)
} else {
let n = shell_span
.item
.parse::<usize>()
.map_err(|_| ShellError::NotFound {
span: shell_span.span,
})?;
switch_shell(engine_state, stack, call, shell_span.span, SwitchTo::Nth(n))
Some(shell_span) => match &shell_span {
Value::String { val, span } => {
if val == "-" {
switch_shell(engine_state, stack, call, *span, SwitchTo::Last)
} else {
Err(ShellError::TypeMismatch {
err_message: "int or '-'".into(),
span: *span,
})
}
}
}
Value::Int { val, span } => switch_shell(
engine_state,
stack,
call,
*span,
SwitchTo::Nth(*val as usize),
),
_ => Err(ShellError::TypeMismatch {
err_message: "int or '-'".into(),
span: call.head,
}),
},
None => list_shells(engine_state, stack, call.head),
}
}

View File

@ -197,7 +197,7 @@ impl Command for Char {
},
Example {
description: "Output Unicode character",
example: r#"char -u 1f378"#,
example: r#"char -u '1f378'"#,
result: Some(Value::test_string("\u{1f378}")),
},
Example {
@ -207,7 +207,7 @@ impl Command for Char {
},
Example {
description: "Output multi-byte Unicode character",
example: r#"char -u 1F468 200D 1F466 200D 1F466"#,
example: r#"char -u '1F468' '200D' '1F466' '200D' '1F466'"#,
result: Some(Value::test_string(
"\u{1F468}\u{200D}\u{1F466}\u{200D}\u{1F466}",
)),

View File

@ -160,7 +160,7 @@ prints out the list properly."#
},
Example {
description: "Render a table with 'name' column in it to a grid",
example: "[[name patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | grid",
example: "[[name patch]; ['0.1.0' false] ['0.1.1' true] ['0.2.0' false]] | grid",
result: Some(Value::test_string("0.1.0 │ 0.1.1 │ 0.2.0\n")),
},
]