Skip to content

Commit 8fd0fc3

Browse files
authored
Fix to parse object comments when no trailing comma (#8000)
1 parent 9a34af0 commit 8fd0fc3

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

rust/kcl-lib/src/parsing/parser.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ fn property_separator(i: &mut TokenSlice) -> ModalResult<()> {
987987
// Normally you need a comma.
988988
comma_sep,
989989
// But, if the object is ending, no need for a comma.
990-
peek(preceded(opt(whitespace), close_brace)).void(),
990+
peek(preceded((opt(non_code_node), opt(whitespace)), close_brace)).void(),
991991
))
992992
.parse_next(i)
993993
}
@@ -1017,7 +1017,7 @@ pub(crate) fn object(i: &mut TokenSlice) -> ModalResult<Node<ObjectExpression>>
10171017
let properties: Vec<_> = repeat(
10181018
0..,
10191019
alt((
1020-
terminated(non_code_node.map(NonCodeOr::NonCode), whitespace),
1020+
terminated(non_code_node.map(NonCodeOr::NonCode), opt(whitespace)),
10211021
terminated(
10221022
alt((object_property, object_property_same_key_and_val)),
10231023
property_separator,
@@ -4861,6 +4861,26 @@ export fn cos(num: number(rad)): number(_) {}"#;
48614861
let _arr = array_elem_by_elem(&mut tokens.as_slice()).unwrap();
48624862
}
48634863

4864+
#[test]
4865+
fn object_no_trailing_comma_with_comment() {
4866+
let program = r#"{
4867+
x=1, // one
4868+
y=2, // two
4869+
z=3 // three
4870+
}"#;
4871+
let module_id = ModuleId::default();
4872+
let tokens = crate::parsing::token::lex(program, module_id).unwrap();
4873+
let _arr = object(&mut tokens.as_slice()).unwrap();
4874+
}
4875+
4876+
#[test]
4877+
fn object_block_comment_no_whitespace() {
4878+
let program = r#"{x=1/* comment*/}"#;
4879+
let module_id = ModuleId::default();
4880+
let tokens = crate::parsing::token::lex(program, module_id).unwrap();
4881+
let _arr = object(&mut tokens.as_slice()).unwrap();
4882+
}
4883+
48644884
#[test]
48654885
fn basic_if_else() {
48664886
let some_program_string = "if true {

rust/kcl-lib/src/unparser.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,33 @@ fn ghi(part001) {
24762476
);
24772477
}
24782478

2479+
#[test]
2480+
fn test_recast_object_no_trailing_comma_with_comments() {
2481+
let some_program_string = r#"{
2482+
x=1, // one
2483+
y=2, // two
2484+
z=3 // three
2485+
}"#;
2486+
let program = crate::parsing::top_level_parse(some_program_string).unwrap();
2487+
2488+
let recasted = program.recast_top(&Default::default(), 0);
2489+
// TODO: We should probably not add an extra new line after the last
2490+
// comment.
2491+
assert_eq!(
2492+
recasted,
2493+
r#"{
2494+
x = 1,
2495+
// one
2496+
y = 2,
2497+
// two
2498+
z = 3,
2499+
// three
2500+
2501+
}
2502+
"#
2503+
);
2504+
}
2505+
24792506
#[test]
24802507
fn test_recast_negative_var() {
24812508
let some_program_string = r#"w = 20

0 commit comments

Comments
 (0)