Skip to content

Commit

Permalink
fix(grainfmt): Handle source files with no code, only comments (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusroberts committed Aug 8, 2022
1 parent 3c0d4d2 commit a435d60
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
28 changes: 17 additions & 11 deletions compiler/src/formatting/format.re
Original file line number Diff line number Diff line change
Expand Up @@ -4199,18 +4199,24 @@ let format_ast =
print_attributes(attributes);
};

let top_level_stmts =
block_item_iterator(
~previous=TopOfFile,
~get_loc,
~print_item,
~comments=cleaned_comments,
~print_attribute=get_attributes,
~original_source,
parsed_program.statements,
);
// special case where we have no code, we still format the comments

let final_doc = Doc.concat([top_level_stmts, Doc.hardLine]);
let final_doc =
switch (parsed_program.statements) {
| [] => Comment_utils.new_comments_to_docs(cleaned_comments)
| _ =>
let top_level_stmts =
block_item_iterator(
~previous=TopOfFile,
~get_loc,
~print_item,
~comments=cleaned_comments,
~print_attribute=get_attributes,
~original_source,
parsed_program.statements,
);
Doc.concat([top_level_stmts, Doc.hardLine]);
};

Doc.toString(~width=80, ~eol, final_doc);
};
10 changes: 10 additions & 0 deletions compiler/test/formatter_inputs/only_comments.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@




/* this is just a comment */




// and this too
3 changes: 3 additions & 0 deletions compiler/test/formatter_outputs/only_comments.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* this is just a comment */

// and this too
1 change: 1 addition & 0 deletions compiler/test/suites/formatter.re
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ describe("formatter", ({test, testSkip}) => {
assertFormatOutput("patterns", "patterns");
assertFormatOutput("rationals", "rationals");
assertFormatOutput("constraints", "constraints");
assertFormatOutput("only_comments", "only_comments");
});

0 comments on commit a435d60

Please sign in to comment.