From 4cdfcf6e9ec3b55a6b09ee9298c45b612a3b55b1 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 21 Oct 2024 13:06:49 -0400 Subject: [PATCH] fix(cst): improve insert for single line json object (#48) --- src/cst/mod.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/cst/mod.rs b/src/cst/mod.rs index 542a1a3..80a006b 100644 --- a/src/cst/mod.rs +++ b/src/cst/mod.rs @@ -1631,6 +1631,7 @@ impl CstObject { } fn insert_or_append(&self, index: Option, prop_name: &str, value: CstInputValue) -> CstObjectProp { + self.ensure_multiline(); insert_or_append_to_container( &CstContainerNode::Object(self.clone()), self.properties().into_iter().map(|c| c.into()).collect(), @@ -3235,6 +3236,30 @@ value3: true r#"{ "value": 4, "propName": true, +}"#, + ); + + // insert when is on a single line + run_test( + 1, + "propName", + json!(true), + r#"{ "value": 4 }"#, + r#"{ + "value": 4, + "propName": true +}"#, + ); + + // insert when is on a single line with trailing comma + run_test( + 1, + "propName", + json!(true), + r#"{ "value": 4, }"#, + r#"{ + "value": 4, + "propName": true, }"#, ); }