diff --git a/crates/nu-engine/src/evaluate/evaluator.rs b/crates/nu-engine/src/evaluate/evaluator.rs index b10ab05c20..9272f39748 100644 --- a/crates/nu-engine/src/evaluate/evaluator.rs +++ b/crates/nu-engine/src/evaluate/evaluator.rs @@ -172,8 +172,8 @@ pub async fn evaluate_baseline_expr( let next = item.get_data_by_member(member); match next { - Err(err) => { - if let UnspannedPathMember::String(_name) = &member.unspanned { + Err(err) => match &member.unspanned { + UnspannedPathMember::String(_name) => { let possible_matches = did_you_mean(&item, member.as_string()); match possible_matches { @@ -187,7 +187,14 @@ pub async fn evaluate_baseline_expr( None => return Err(err), } } - } + UnspannedPathMember::Int(_row) => { + return Err(ShellError::labeled_error( + "Unknown row", + "unknown row", + &member.span, + )); + } + }, Ok(next) => { item = next.clone().value.into_value(&tag); } diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 5b5e58c58f..4cb2e98e16 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -558,6 +558,54 @@ fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() { assert_eq!(actual.out, "nushell"); } +#[test] +fn index_out_of_bounds() { + let actual = nu!( + cwd: ".", + r#" + let foo = [1, 2, 3]; echo $foo.5 + "# + ); + + assert!(actual.err.contains("unknown row")); +} + +#[test] +fn index_row() { + let actual = nu!( + cwd: ".", + r#" + let foo = [[name]; [joe] [bob]]; echo $foo.1 | to json + "# + ); + + assert_eq!(actual.out, r#"{"name":"bob"}"#); +} + +#[test] +fn index_cell() { + let actual = nu!( + cwd: ".", + r#" + let foo = [[name]; [joe] [bob]]; echo $foo.name.1 + "# + ); + + assert_eq!(actual.out, "bob"); +} + +#[test] +fn index_cell_alt() { + let actual = nu!( + cwd: ".", + r#" + let foo = [[name]; [joe] [bob]]; echo $foo.1.name + "# + ); + + assert_eq!(actual.out, "bob"); +} + #[test] fn echoing_ranges() { let actual = nu!(