Makes a more helpful error for let in pipeline (#5632)

* a more helpful error for let in pipeline

* a more helpful error for let in pipeline fmt

* changed help message

* type-o

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
pwygab 2022-05-26 08:13:14 +08:00 committed by GitHub
parent a42d419b66
commit 3d62528d8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View file

@ -78,6 +78,16 @@ pub enum ParseError {
)]
BuiltinCommandInPipeline(String, #[label("not allowed in pipeline")] Span),
#[error("Let statement used in pipeline.")]
#[diagnostic(
code(nu::parser::unexpected_keyword),
url(docsrs),
help(
"Assigning '{0}' to '{1}' does not produce a value to be piped. If the pipeline is meant to apply to '{0}' by itself, use 'let {1} = ({0} | ...)'."
)
)]
LetInPipeline(String, String, #[label("let in pipeline")] Span),
#[error("Incorrect value")]
#[diagnostic(code(nu::parser::incorrect_value), url(docsrs), help("{2}"))]
IncorrectValue(String, #[label("unexpected {0}")] Span, String),
@ -300,6 +310,7 @@ impl ParseError {
ParseError::ExpectedKeyword(_, s) => *s,
ParseError::UnexpectedKeyword(_, s) => *s,
ParseError::BuiltinCommandInPipeline(_, s) => *s,
ParseError::LetInPipeline(_, _, s) => *s,
ParseError::IncorrectValue(_, s, _) => *s,
ParseError::MultipleRestParams(s) => *s,
ParseError::VariableNotFound(s) => *s,

View file

@ -4371,7 +4371,12 @@ pub fn parse_expression(
expand_aliases_denylist,
)
.0,
Some(ParseError::BuiltinCommandInPipeline("let".into(), spans[0])),
Some(ParseError::LetInPipeline(
String::from_utf8_lossy(working_set.get_span_contents(spans[spans.len() - 1]))
.to_string(),
String::from_utf8_lossy(working_set.get_span_contents(spans[1])).to_string(),
spans[0],
)),
),
b"alias" => (
parse_call(