diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index ed061ad36a..d2adb4344f 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -221,6 +221,9 @@ static NEEDS_QUOTES_REGEX: Lazy = Lazy::new(|| { }); fn needs_quotes(string: &str) -> bool { + if string.is_empty() { + return true; + } // These are case-sensitive keywords match string { // `true`/`false`/`null` are active keywords in JSON and NUON diff --git a/crates/nu-command/tests/format_conversions/nuon.rs b/crates/nu-command/tests/format_conversions/nuon.rs index 8226a3f0f2..d011a975db 100644 --- a/crates/nu-command/tests/format_conversions/nuon.rs +++ b/crates/nu-command/tests/format_conversions/nuon.rs @@ -296,6 +296,41 @@ fn to_nuon_converts_columns_with_spaces() { assert!(actual.err.is_empty()); } +#[test] +fn to_nuon_quotes_empty_string() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + let test = ""; $test | to nuon + "# + )); + assert!(actual.err.is_empty()); + assert_eq!(actual.out, r#""""#) +} + +#[test] +fn to_nuon_quotes_empty_string_in_list() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + let test = [""]; $test | to nuon | from nuon | $in == [""] + "# + )); + assert!(actual.err.is_empty()); + assert_eq!(actual.out, "true") +} + +#[test] +fn to_nuon_quotes_empty_string_in_table() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + let test = [[a, b]; ['', la] [le lu]]; $test | to nuon | from nuon + "# + )); + assert!(actual.err.is_empty()); +} + #[test] fn does_not_quote_strings_unnecessarily() { let actual = nu!( @@ -331,7 +366,7 @@ fn quotes_some_strings_necessarily() { '-11.0..<-15.0', '11.0..<-15.0', '-11.0..<15.0', '-11.0..', '11.0..', '..15.0', '..-15.0', '..<15.0', '..<-15.0', '2000-01-01', '2022-02-02T14:30:00', '2022-02-02T14:30:00+05:00', - ',', + ',','' '&&' ] | to nuon | from nuon | describe "#