From 86ae27b0c16b53f5f8a13826abd285f4ff39344a Mon Sep 17 00:00:00 2001 From: mike <98623181+1Kinoti@users.noreply.github.com> Date: Sun, 26 Mar 2023 11:59:47 +0300 Subject: [PATCH] fix unhelpful error message with extra characters in list annotations (#8619) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description with such a line ```nu def err [list: list<>extra] {} ``` this pr changes the error message from ```nu Error: nu::parser::unclosed_delimiter × Unclosed delimiter. ╭─[entry #69:1:1] 1 │ def err [list: list<>extra] {} · ─────┬───── · ╰── unclosed > ╰──── ``` to ```nu × Extra characters in the parameter name. ╭─[entry #69:1:1] 1 │ def err [list: list<>extra] {} · ──┬── · ╰── extra characters ╰──── ``` --- crates/nu-parser/src/parser.rs | 11 +++++++++++ src/tests/test_signatures.rs | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index daf00cc36e..cf6e5060c4 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -3127,6 +3127,17 @@ fn parse_list_shape( // overflows with spans let end = if bytes.ends_with(b">") { span.end - 1 + // extra characters after the > + } else if bytes.contains(&b'>') { + let angle_start = bytes.split(|it| it == &b'>').collect::>()[0].len() + 1; + let span = Span::new(span.start + angle_start, span.end); + + let err = ParseError::LabeledError( + "Extra characters in the parameter name".into(), + "extra characters".into(), + span, + ); + return (SyntaxShape::Any, Some(err)); } else { let err = ParseError::Unclosed(">".into(), span); return (SyntaxShape::List(Box::new(SyntaxShape::Any)), Some(err)); diff --git a/src/tests/test_signatures.rs b/src/tests/test_signatures.rs index dd06f40d63..571d140863 100644 --- a/src/tests/test_signatures.rs +++ b/src/tests/test_signatures.rs @@ -125,3 +125,10 @@ fn list_annotations_with_default_val_2() -> TestResult { let expected = "Default value wrong type"; fail_test(input, expected) } + +#[test] +fn list_annotations_with_extra_characters() -> TestResult { + let input = "def run [list: listextra] {$list | length}; run [1 2 3]"; + let expected = "Extra characters in the parameter name"; + fail_test(input, expected) +}