Skip to content

Commit

Permalink
fix(parser): prevent panic in object item parser (#285)
Browse files Browse the repository at this point in the history
Fixes #284
  • Loading branch information
martinohmann committed Aug 24, 2023
1 parent a791b42 commit 8502d2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/hcl-edit/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,12 @@ fn object_items<'i, 's>(

// Associate the trailing comment with the item value, updating the span if it
// already has a decor suffix.
let suffix_start = match decor.suffix() {
Some(suffix) => suffix.span().unwrap().start,
None => comment_span.start,
let suffix_span = match decor.suffix().and_then(RawString::span) {
Some(span) => span.start..comment_span.end,
None => comment_span,
};

decor.set_suffix(RawString::from_span(suffix_start..comment_span.end));
decor.set_suffix(RawString::from_span(suffix_span));

line_ending
.value(ObjectValueTerminator::Newline)
Expand Down
15 changes: 15 additions & 0 deletions crates/hcl-edit/tests/regressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ fn issue_270() {
body.set_prefer_omit_trailing_newline(true);
assert_eq!(body.to_string(), no_trailing_newline);
}

#[test]
fn issue_284() {
let input = r#"
locals {
test = {
a = b// this comment breaks the parser
c = d // but this one doesn't
}
}
"#;

let res: Result<Body, _> = input.parse();
assert!(res.is_ok());
}

0 comments on commit 8502d2f

Please sign in to comment.