Skip to content

Commit

Permalink
Remove --flag: bool support (#11541)
Browse files Browse the repository at this point in the history
# Description
This is a follow up to: #11365

After this pr, `--flag: bool` is no longer allowed.

I think `ParseWarning::Deprecated` is useful when we want to deprecated
something at syntax level, so I just leave it there for now.

# User-Facing Changes
## Before
```
❯ def foo [--b: bool] {}
Error:   × Deprecated: --flag: bool
   ╭─[entry #15:1:1]
 1 │ def foo [--b: bool] {}
   ·               ──┬─
   ·                 ╰── `--flag: bool` is deprecated and will be removed in 0.90. Please use `--flag` instead, more info: https://www.nushell.sh/book/custom_commands.html
   ╰────
```

## After
```
❯ def foo [--b: bool] {}
Error:   × Type annotations are not allowed for boolean switches.
   ╭─[entry #2:1:1]
 1 │ def foo [--b: bool] {}
   ·               ──┬─
   ·                 ╰── Remove the `: bool` type annotation.
   ╰────
```
# Tests + Formatting
Done
  • Loading branch information
WindSoilder committed Jan 25, 2024
1 parent f286286 commit a4809d2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
13 changes: 0 additions & 13 deletions crates/nu-command/tests/commands/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,6 @@ fn def_default_value_should_restrict_implicit_type() {
assert!(actual2.err.contains("expected int"));
}

#[test]
fn def_boolean_flags() {
let actual = nu!("def foo [--x: bool] { $x }; foo --x");
assert!(actual.err.contains("flag missing bool argument"));
let actual = nu!("def foo [--x: bool = false] { $x }; foo");
assert_eq!(actual.out, "false");
let actual = nu!("def foo [--x: bool = false] { $x }; foo --x");
assert!(actual.err.contains("flag missing bool argument"));
// boolean flags' default value should be null
let actual = nu!("def foo [--x: bool] { $x == null }; foo");
assert_eq!(actual.out, "true");
}

#[test]
fn def_wrapped_with_block() {
let actual = nu!(
Expand Down
10 changes: 5 additions & 5 deletions crates/nu-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use nu_protocol::{
},
engine::StateWorkingSet,
eval_const::eval_constant,
span, BlockId, DidYouMean, Flag, ParseError, ParseWarning, PositionalArg, Signature, Span,
Spanned, SyntaxShape, Type, Unit, VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID,
span, BlockId, DidYouMean, Flag, ParseError, PositionalArg, Signature, Span, Spanned,
SyntaxShape, Type, Unit, VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID,
};

use crate::parse_keywords::{
Expand Down Expand Up @@ -3603,9 +3603,9 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) ->
} => {
working_set.set_variable_type(var_id.expect("internal error: all custom parameters must have var_ids"), syntax_shape.to_type());
if syntax_shape == SyntaxShape::Boolean {
working_set.warning(ParseWarning::DeprecatedWarning(
"--flag: bool".to_string(),
"--flag".to_string(),
working_set.error(ParseError::LabeledError(
"Type annotations are not allowed for boolean switches.".to_string(),
"Remove the `: bool` type annotation.".to_string(),
span,
));
}
Expand Down
3 changes: 1 addition & 2 deletions src/tests/test_custom_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ fn custom_flag2() -> TestResult {
#[test]
fn deprecated_boolean_flag() {
let actual = nu!(r#"def florb [--dry-run: bool, --another-flag] { "aaa" }; florb"#);
assert!(actual.err.contains("Deprecated"));
assert_eq!(actual.out, "aaa");
assert!(actual.err.contains("not allowed"));
}

#[test]
Expand Down

0 comments on commit a4809d2

Please sign in to comment.