Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/cst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2776,7 +2776,7 @@ fn insert_or_append_to_container(
Some(&mut insert_index),
vec![
CstNewline::new(style_info.newline_kind).into(),
CstStringLit::new(child_indents.current_indent.clone()).into(),
CstWhitespace::new(child_indents.current_indent.clone()).into(),
],
);
container.raw_insert_value_with_internal_indent(Some(&mut insert_index), value, &style_info, &child_indents);
Expand Down Expand Up @@ -2804,7 +2804,7 @@ fn insert_or_append_to_container(
Some(&mut insert_index),
vec![
CstNewline::new(style_info.newline_kind).into(),
CstStringLit::new(child_indents.current_indent.clone()).into(),
CstWhitespace::new(child_indents.current_indent.clone()).into(),
],
);
container.raw_insert_value_with_internal_indent(Some(&mut insert_index), value, &style_info, &child_indents);
Expand All @@ -2816,7 +2816,7 @@ fn insert_or_append_to_container(
Some(&mut insert_index),
vec![
CstNewline::new(style_info.newline_kind).into(),
CstStringLit::new(indents.current_indent.clone()).into(),
CstWhitespace::new(indents.current_indent.clone()).into(),
],
);
}
Expand All @@ -2836,7 +2836,7 @@ fn insert_or_append_to_container(
Some(&mut insert_index),
vec![
CstNewline::new(style_info.newline_kind).into(),
CstStringLit::new(indents.current_indent.clone()).into(),
CstWhitespace::new(indents.current_indent.clone()).into(),
],
);
}
Expand Down Expand Up @@ -3596,6 +3596,35 @@ value3: true
);
}

#[test]
fn append_to_multiline_array_does_not_expose_phantom_string_lit() {
// regression test for https://github.com/dprint/jsonc-parser/issues/78
let cst = build_cst(
r#"{
"servers": [
{"name": "linear"},
{"name": "supabase"}
]
}"#,
);
let arr = cst.object_value_or_create().unwrap().array_value("servers").unwrap();
arr.append(CstInputValue::Object(vec![(
"name".to_string(),
CstInputValue::String("github".to_string()),
)]));

let elements = arr.elements();
assert_eq!(elements.len(), 3);
for el in &elements {
assert!(
el.as_string_lit().is_none(),
"element should not be a string lit: {:?}",
el
);
assert!(el.as_object().is_some(), "element should be an object: {:?}", el);
}
}

#[test]
fn insert_array_element_trailing_commas() {
let cst = build_cst(
Expand Down Expand Up @@ -4256,10 +4285,7 @@ value3: true
let prop = root_obj.get("key").unwrap();
// String containing a backslash: /.github/workflows/lint\.yaml$/
prop.set_value(json!("/.github/workflows/lint\\.yaml$/"));
assert_eq!(
cst.to_string(),
r#"{"key": "/.github/workflows/lint\\.yaml$/"}"#,
);
assert_eq!(cst.to_string(), r#"{"key": "/.github/workflows/lint\\.yaml$/"}"#,);
// Verify decoded value roundtrips correctly
let decoded = root_obj
.get("key")
Expand Down Expand Up @@ -4348,7 +4374,11 @@ value3: true
arr.append(json!("line1\nline2"));

let text = cst.to_string();
assert!(text.contains(r#""path\\to\\file""#), "backslash in array element: {}", text);
assert!(
text.contains(r#""path\\to\\file""#),
"backslash in array element: {}",
text
);
assert!(text.contains(r#""line1\nline2""#), "newline in array element: {}", text);
}

Expand Down
Loading