mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 09:53:43 +01:00
Allow complex types in input/output and let (#10405)
# Description This PR fixes #9702 on the side of parse. I.e. input/output types in signature and type annotations in `let` now should correctly parse with type annotations that contain commas and spaces: ![image](https://github.com/nushell/nushell/assets/17511668/babc0a69-5cb3-46c2-98ef-6da69ee3d3be) # User-Facing Changes Return values and let type annotations now can contain stuff like `table<a: int b: record<c: string d: datetime>>` e.t.c
This commit is contained in:
parent
bc437da5c7
commit
65e2733571
@ -3098,10 +3098,22 @@ pub fn parse_var_with_opt_type(
|
|||||||
if bytes.ends_with(b":") {
|
if bytes.ends_with(b":") {
|
||||||
// We end with colon, so the next span should be the type
|
// We end with colon, so the next span should be the type
|
||||||
if *spans_idx + 1 < spans.len() {
|
if *spans_idx + 1 < spans.len() {
|
||||||
|
let span_beginning = *spans_idx;
|
||||||
*spans_idx += 1;
|
*spans_idx += 1;
|
||||||
let type_bytes = working_set.get_span_contents(spans[*spans_idx]).to_vec();
|
// signature like record<a: int b: int> is broken into multiple spans due to
|
||||||
|
// whitespaces. Collect the rest into one span and work on it
|
||||||
|
let full_span = span(&spans[*spans_idx..]);
|
||||||
|
let type_bytes = working_set.get_span_contents(full_span).to_vec();
|
||||||
|
|
||||||
let ty = parse_type(working_set, &type_bytes, spans[*spans_idx]);
|
let (tokens, parse_error) =
|
||||||
|
lex_signature(&type_bytes, full_span.start, &[b','], &[], true);
|
||||||
|
|
||||||
|
if let Some(parse_error) = parse_error {
|
||||||
|
working_set.parse_errors.push(parse_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ty = parse_type(working_set, &type_bytes, tokens[0].span);
|
||||||
|
*spans_idx += spans.len() - *spans_idx - 1;
|
||||||
|
|
||||||
let var_name = bytes[0..(bytes.len() - 1)].to_vec();
|
let var_name = bytes[0..(bytes.len() - 1)].to_vec();
|
||||||
|
|
||||||
@ -3118,7 +3130,7 @@ pub fn parse_var_with_opt_type(
|
|||||||
(
|
(
|
||||||
Expression {
|
Expression {
|
||||||
expr: Expr::VarDecl(id),
|
expr: Expr::VarDecl(id),
|
||||||
span: span(&spans[*spans_idx - 1..*spans_idx + 1]),
|
span: span(&spans[span_beginning..*spans_idx + 1]),
|
||||||
ty: ty.clone(),
|
ty: ty.clone(),
|
||||||
custom_completion: None,
|
custom_completion: None,
|
||||||
},
|
},
|
||||||
@ -3215,7 +3227,7 @@ pub fn parse_input_output_types(
|
|||||||
full_span.end -= 1;
|
full_span.end -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (tokens, parse_error) = lex(bytes, full_span.start, &[b','], &[], true);
|
let (tokens, parse_error) = lex_signature(bytes, full_span.start, &[b','], &[], true);
|
||||||
|
|
||||||
if let Some(parse_error) = parse_error {
|
if let Some(parse_error) = parse_error {
|
||||||
working_set.parse_errors.push(parse_error);
|
working_set.parse_errors.push(parse_error);
|
||||||
|
@ -1354,6 +1354,38 @@ mod input_types {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Def;
|
||||||
|
|
||||||
|
impl Command for Def {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"def"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Mock def command."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("def")
|
||||||
|
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
||||||
|
.required("def_name", SyntaxShape::String, "definition name")
|
||||||
|
.required("params", SyntaxShape::Signature, "parameters")
|
||||||
|
.required("body", SyntaxShape::Closure(None), "body of the definition")
|
||||||
|
.category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
_engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
_call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct GroupBy;
|
pub struct GroupBy;
|
||||||
|
|
||||||
@ -1608,6 +1640,7 @@ mod input_types {
|
|||||||
let delta = {
|
let delta = {
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
working_set.add_decl(Box::new(Let));
|
working_set.add_decl(Box::new(Let));
|
||||||
|
working_set.add_decl(Box::new(Def));
|
||||||
working_set.add_decl(Box::new(AggCustom));
|
working_set.add_decl(Box::new(AggCustom));
|
||||||
working_set.add_decl(Box::new(GroupByCustom));
|
working_set.add_decl(Box::new(GroupByCustom));
|
||||||
working_set.add_decl(Box::new(GroupBy));
|
working_set.add_decl(Box::new(GroupBy));
|
||||||
@ -1833,4 +1866,52 @@ mod input_types {
|
|||||||
Some(ParseError::VariableNotFound(_, _))
|
Some(ParseError::VariableNotFound(_, _))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case::input_output(b"def q []: int -> int {1}", false)]
|
||||||
|
#[case::input_output(b"def q []: string -> string {'qwe'}", false)]
|
||||||
|
#[case::input_output(b"def q []: nothing -> nothing {null}", false)]
|
||||||
|
#[case::input_output(b"def q []: list<string> -> list<string> {[]}", false)]
|
||||||
|
#[case::input_output(
|
||||||
|
b"def q []: record<a: int b: int> -> record<c: int e: int> {{c: 1 e: 1}}",
|
||||||
|
false
|
||||||
|
)]
|
||||||
|
#[case::input_output(
|
||||||
|
b"def q []: table<a: int b: int> -> table<c: int e: int> {[{c: 1 e: 1}]}",
|
||||||
|
false
|
||||||
|
)]
|
||||||
|
#[case::input_output(
|
||||||
|
b"def q []: nothing -> record<c: record<a: int b: int> e: int> {{c: {a: 1 b: 2} e: 1}}",
|
||||||
|
false
|
||||||
|
)]
|
||||||
|
#[case::input_output(b"def q []: nothing -> list<string {[]}", true)]
|
||||||
|
#[case::input_output(b"def q []: nothing -> record<c: int e: int {{c: 1 e: 1}}", true)]
|
||||||
|
#[case::input_output(b"def q []: record<c: int e: int -> record<a: int> {{a: 1}}", true)]
|
||||||
|
#[case::input_output(b"def q []: nothing -> record<a: record<a: int> {{a: {a: 1}}}", true)]
|
||||||
|
#[case::vardecl(b"let a: int = 1", false)]
|
||||||
|
#[case::vardecl(b"let a: string = 'qwe'", false)]
|
||||||
|
#[case::vardecl(b"let a: nothing = null", false)]
|
||||||
|
#[case::vardecl(b"let a: list<string> = []", false)]
|
||||||
|
#[case::vardecl(b"let a: record<a: int b: int> = {a: 1 b: 1}", false)]
|
||||||
|
#[case::vardecl(
|
||||||
|
b"let a: record<c: record<a: int b: int> e: int> = {c: {a: 1 b: 2} e: 1}",
|
||||||
|
false
|
||||||
|
)]
|
||||||
|
#[case::vardecl(b"let a: table<a: int b: int> = [[a b]; [1 1]]", false)]
|
||||||
|
#[case::vardecl(b"let a: list<string asd> = []", true)]
|
||||||
|
#[case::vardecl(b"let a: record<a: int b: record<a: int> = {a: 1 b: {a: 1}}", true)]
|
||||||
|
fn test_type_annotations(#[case] phrase: &[u8], #[case] expect_errors: bool) {
|
||||||
|
let mut engine_state = EngineState::new();
|
||||||
|
add_declarations(&mut engine_state);
|
||||||
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||||
|
// this should not panic
|
||||||
|
let _block = parse(&mut working_set, None, phrase, false);
|
||||||
|
// check that no parse errors happened
|
||||||
|
assert_eq!(
|
||||||
|
!working_set.parse_errors.is_empty(),
|
||||||
|
expect_errors,
|
||||||
|
"Got errors {:?}",
|
||||||
|
working_set.parse_errors
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
|||||||
use nu_test_support::playground::Playground;
|
use nu_test_support::playground::Playground;
|
||||||
use nu_test_support::{nu, pipeline};
|
use nu_test_support::{nu, pipeline};
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn source_file_relative_to_file() {
|
fn source_file_relative_to_file() {
|
||||||
@ -244,3 +245,29 @@ fn parse_long_duration() {
|
|||||||
|
|
||||||
assert_eq!(actual.out, "1min 18sec 797ms");
|
assert_eq!(actual.out, "1min 18sec 797ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case("def test []: int -> int { 1 }")]
|
||||||
|
#[case("def test []: string -> string { 'qwe' }")]
|
||||||
|
#[case("def test []: nothing -> nothing { null }")]
|
||||||
|
#[case("def test []: list<string> -> list<string> { [] }")]
|
||||||
|
#[case("def test []: record<a: int b: int> -> record<c: int e: int> { {c: 1 e: 1} }")]
|
||||||
|
#[case("def test []: table<a: int b: int> -> table<c: int e: int> { [ {c: 1 e: 1} ] }")]
|
||||||
|
#[case("def test []: nothing -> record<c: int e: int> { {c: 1 e: 1} }")]
|
||||||
|
fn parse_function_signature(#[case] phrase: &str) {
|
||||||
|
let actual = nu!(phrase);
|
||||||
|
assert!(actual.err.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case("let a: int = 1")]
|
||||||
|
#[case("let a: string = 'qwe'")]
|
||||||
|
#[case("let a: nothing = null")]
|
||||||
|
#[case("let a: list<string> = []")]
|
||||||
|
#[case("let a: record<a: int b: int> = {a: 1 b: 1}")]
|
||||||
|
#[case("let a: table<a: int b: int> = [[a b]; [1 2] [3 4]]")]
|
||||||
|
#[case("let a: record<a: record<name: string> b: int> = {a: {name: bob} b: 1}")]
|
||||||
|
fn parse_let_signature(#[case] phrase: &str) {
|
||||||
|
let actual = nu!(phrase);
|
||||||
|
assert!(actual.err.is_empty());
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user