Skip to content

Commit 35f1604

Browse files
committed
add new tests and add is_line_comment boolean to lexer COMMENT
definition
1 parent f9c5a6c commit 35f1604

File tree

6 files changed

+154
-82
lines changed

6 files changed

+154
-82
lines changed
Lines changed: 100 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,60 @@
11
/*
22
* Multiline comment
3+
*/
4+
// Empty comments, or comments with whitespaces:
5+
//
6+
//
7+
//
8+
//a
9+
/*inlinecommentwithnospaces*/
10+
/*
11+
12+
313
*/
414
/*
515
* Multiline comment with a // single line comment
616
*/
717
// Single line comment
8-
// Single line comment with a multiline /* starting
18+
919
let testPostComment = "";
1020

1121
// let commentedCode = "";
12-
// Test inter-code comments
22+
// Test: inter-code comments
1323
let testMultiline a =>
1424
switch a {
1525
// single line comment
16-
| `Thingy x => {
17-
print_string
18-
// multiline comment should be fine
19-
"matched thingy x";
20-
let zz = 10;
21-
// post line single line comment
22-
zz
23-
}
24-
| `Other x => {
25-
// single line comment above
26-
print_string "matched other x";
27-
x
28-
}
29-
};
30-
31-
// single line comment below
32-
// short comment
26+
| `Thingy x =>
27+
print_string
28+
/* multiline comment should be fine */
29+
"matched thingy x";
30+
let zz = 10;
31+
// post line single line comment
32+
zz
33+
| `Other x =>
34+
// single line comment above
35+
print_string "matched other x";
36+
x
37+
}
38+
// single line comment below;
39+
40+
/* short comment */
3341
let x = ["test"];
3442

35-
// short comment
43+
/* short comment */
3644
let x = {
3745
// /* */
3846
let y = "";
3947
()
4048
};
4149

4250
// /* this is a valid nested comment*/ this is a valid comment
43-
// valid /* this is a valid comment
51+
// valid /* this is also a valid nested comment */
4452
let z = 10;
4553

54+
///////////////////////////////////////////////////////////////////////////////////
4655
// The following tests will test the conversion of /* */ to single line
4756
// comments as well as the wrapping of interleaved comments within short sequences.
57+
///////////////////////////////////////////////////////////////////////////////////
4858
/*
4959
* Test wrapping every form of named arguments where various parts are
5060
* commented.
@@ -53,79 +63,120 @@ let a = 10;
5363

5464
let b = 20;
5565

56-
//A
57-
let named /* a::a */ a::a /* b::b */ b::b => /* a + b */ a + b;
58-
59-
//B
60-
let namedAlias /* a::aa */ a::aa /* b::bb */ b::bb => /* aa + bb */ aa + bb;
61-
62-
//C
66+
/*A*/
67+
let named /* a::a */ a::a /* b::b */ b::b =>
68+
/* a + b */
69+
a + b;
70+
71+
/*B*/
72+
let namedAlias
73+
/* a::aa */
74+
a::aa
75+
/* b::bb */
76+
b::bb =>
77+
/* aa + bb */
78+
aa + bb;
79+
80+
/*C*/
6381
let namedAnnot
6482
/* a::(a: option int) */
6583
a::(a: option int)
6684
/* b::(b: option int) */
6785
b::(b: option int) =>
68-
// 20
86+
/* 20 */
6987
20;
7088

71-
//D
89+
/*D*/
7290
let namedAliasAnnot
7391
/* a::(aa: option int) */
7492
a::(aa: option int)
7593
/* b::(bb: option int) */
7694
b::(bb: option int) =>
77-
// 20
95+
/* 20 */
7896
20;
7997

80-
//E
81-
let optional /* a::a=? */ a::a=? /* b::b=? */ b::b=? /* () */ () =>
82-
// 10
98+
/*E*/
99+
let optional
100+
/* a::a=? */
101+
a::a=?
102+
/* b::b=? */
103+
b::b=?
104+
/* () */
105+
() =>
106+
/* 10 */
83107
10;
84108

85-
//F
86-
let optionalAlias /* a::aa */ a::aa=? /* ?b:bb */ b::bb=? /* () */ () =>
87-
// 10
109+
/*F*/
110+
let optionalAlias
111+
/* a::aa */
112+
a::aa=?
113+
/* ?b:bb */
114+
b::bb=?
115+
/* () */
116+
() =>
117+
/* 10 */
88118
10;
89119

90-
//G
120+
/*G*/
91121
let optionalAnnot
92122
/* a::(a: option int)=? */
93123
a::(a: option int)=?
94124
/* ?b:(b: option int) */
95125
b::(b: option int)=?
96126
/* () */
97127
() =>
98-
// 10
128+
/* 10 */
99129
10;
100130

101-
//H
131+
/*H*/
102132
let optionalAliasAnnot
103133
/* a::(aa: option int)=? */
104134
a::(aa: option int)=?
105135
/* b::(bb: option int)=? */
106136
b::(bb: option int)=?
107137
/* () => */
108138
() =>
109-
// 10
139+
/* 10 */
110140
10;
111141

112-
//I: This one is really annoying? Where's the visual label?
113-
let defOptional /* a::a=10 */ a::a=10 /* b::b=10 */ b::b=10 /* () => */ () =>
114-
// 10
142+
/*I: This one is really annoying? Where's the visual label?*/
143+
let defOptional
144+
/* a::a=10 */
145+
a::a=10
146+
/* b::b=10 */
147+
b::b=10
148+
/* () => */
149+
() =>
150+
/* 10 */
115151
10;
116152

117-
//J
118-
let defOptionalAlias /* a::aa=10 */ a::aa=10 /* b::bb=10 */ b::bb=10 /* () => */ () =>
119-
// 10;
153+
/*J*/
154+
let defOptionalAlias
155+
/* a::aa=10 */
156+
a::aa=10
157+
/* b::bb=10 */
158+
b::bb=10
159+
/* () => */
160+
() =>
161+
/* 10; */
120162
10;
121163

122-
//K
164+
/*K*/
123165
let defOptionalAnnot
124166
/* a::(a:int)=10 */
125167
a::(a: int)=10
126168
/* b::(b:int)=10 */
127169
b::(b: int)=10
128170
/* () => */
129171
() =>
130-
// 10;
172+
/* 10; */
131173
10;
174+
175+
// This tests a short inline comment that should retain it's inline properties when formatted
176+
let x = [/* sh */ "te"];
177+
178+
// This tests an empty /* */ nested comment
179+
// This tests a line comment that should be converted to an inline comment when formatted
180+
let x = [/* sh*/ "te"];
181+
182+
// File ends with a comment

formatTest/unit_tests/input/comments.re

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/*
22
* Multiline comment
33
*/
4+
5+
// Empty comments, or comments with whitespaces:
6+
//
7+
//
8+
//
9+
//a
10+
/*inlinecommentwithnospaces*/
11+
412
/*
513
614
@@ -185,5 +193,4 @@ let x = [
185193
];
186194

187195

188-
189-
196+
// File ends with a comment

src/reason_lexer.mll

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ rule token = parse
431431
let end_loc = comment lexbuf in
432432
let s = get_stored_string () in
433433
reset_string_buffer ();
434-
COMMENT (s, { start_loc with
434+
COMMENT (s, true, { start_loc with
435435
Location.loc_end = end_loc.Location.loc_end })
436436
}
437437
| "/*"
@@ -441,7 +441,7 @@ rule token = parse
441441
let end_loc = comment lexbuf in
442442
let s = get_stored_string () in
443443
reset_string_buffer ();
444-
COMMENT (s, { start_loc with
444+
COMMENT (s, false, { start_loc with
445445
Location.loc_end = end_loc.Location.loc_end })
446446
}
447447
| "/*/"
@@ -453,7 +453,7 @@ rule token = parse
453453
let end_loc = comment lexbuf in
454454
let s = get_stored_string () in
455455
reset_string_buffer ();
456-
COMMENT (s, { loc with Location.loc_end = end_loc.Location.loc_end })
456+
COMMENT (s, false, { loc with Location.loc_end = end_loc.Location.loc_end })
457457
}
458458
| "*/"
459459
{ let loc = Location.curr lexbuf in
@@ -646,7 +646,20 @@ and comment = parse
646646
{ store_lexeme lexbuf; comment lexbuf }
647647
| eof
648648
{ match !comment_start_loc with
649-
| [] -> assert false
649+
| [] ->
650+
(* if the file ends with a single line comment then *)
651+
if !single_line_comment then (
652+
single_line_comment := false;
653+
match (!comment_start_loc, !line_comment_start_loc) with
654+
| ([], []) ->
655+
assert false
656+
| ([], _) ->
657+
line_comment_start_loc := []; Location.curr lexbuf
658+
| (_, _) ->
659+
let start = List.hd (List.rev !comment_start_loc) in
660+
comment_start_loc := [];
661+
raise (Error (Unmatched_nested_comment start, Location.curr lexbuf))
662+
) else assert false
650663
| loc :: _ ->
651664
let start = List.hd (List.rev !comment_start_loc) in
652665
comment_start_loc := [];
@@ -764,8 +777,8 @@ and skip_sharp_bang = parse
764777
let last_comments = ref []
765778
let rec token lexbuf =
766779
match token_with_comments lexbuf with
767-
COMMENT (s, comment_loc) ->
768-
last_comments := (s, comment_loc) :: !last_comments;
780+
COMMENT (s, is_line_comment, comment_loc) ->
781+
last_comments := (s, is_line_comment, comment_loc) :: !last_comments;
769782
token lexbuf
770783
| tok -> tok
771784
let comments () = List.rev !last_comments

src/reason_parser.mly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ let built_in_explicit_arity_constructors = ["Some"; "Assert_failure"; "Match_fai
915915
%token WHEN
916916
%token WHILE
917917
%token WITH
918-
%token <string * Location.t> COMMENT
918+
%token <string * bool * Location.t> COMMENT
919919

920920
%token EOL
921921

src/reason_pprint_ast.ml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,16 +1221,14 @@ let rec extractComments comments tester =
12211221
let open Lexing in
12221222
(* There might be an issue here - we shouldn't have to split "up to and including".
12231223
Up to should be sufficient. Comments' end position might be off by one (too large) *)
1224-
comments |> List.partition (fun (str, attLoc, physLoc) ->
1224+
comments |> List.partition (fun (str, isLineComment, attLoc, physLoc) ->
12251225
let oneGreaterThanAttachmentLocEnd = attLoc.loc_end.pos_cnum in
12261226
let attachmentLocLastChar = oneGreaterThanAttachmentLocEnd - 1 in
12271227
let oneGreaterThanPhysLocEnd = physLoc.loc_end.pos_cnum in
12281228
let physLastChar = oneGreaterThanPhysLocEnd - 1 in
12291229
tester attLoc.loc_start.pos_cnum attachmentLocLastChar physLoc.loc_start.pos_cnum physLastChar
12301230
)
12311231

1232-
1233-
12341232
let space = " "
12351233
(* Can't you tell the difference? *)
12361234
let tab = " "
@@ -1270,17 +1268,15 @@ let smallestLeadingSpaces strs =
12701268
is part of a list that could potentially break to prevent inline
12711269
comments from being rendered as line comments
12721270
*)
1273-
let formatItemComment ?(forceMultiline=false) (str, commLoc, physCommLoc) =
1271+
let formatItemComment ?(forceMultiline=false) (str, is_line_comment, commLoc, physCommLoc) =
12741272
let commLines = Str.split_delim (Str.regexp "\n") ("/*" ^ str ^ "*/") in
1275-
match commLines with
1276-
| [] -> easyAtom ""
1277-
| [hd] ->
1278-
if forceMultiline then
1279-
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom hd]
1280-
(* caveat: single line comment . *)
1281-
else
1282-
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom ("//" ^ str)]
1283-
| zero::one::tl ->
1273+
match (commLines, is_line_comment, forceMultiline) with
1274+
| (_, true, false) ->
1275+
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom ("//" ^ str)]
1276+
| ([], _, _) -> easyAtom ""
1277+
| ([hd], _, _) ->
1278+
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom hd]
1279+
| (zero::one::tl, _, _) ->
12841280
let lineZero = List.nth commLines 0 in
12851281
let lineOne = List.nth commLines 1 in
12861282
let hasMeaningfulContentOnLineZero = lineZeroHasMeaningfulContent lineZero in
@@ -1321,7 +1317,7 @@ let removeSepFromListConfig listSettings =
13211317
1. Before-line items break in unison with list items.
13221318
2. End of line comments are placed *after* separators. *)
13231319
let rec interleaveComments ?endMaxChar listConfig layoutListItems comments =
1324-
let isDocComment (c, _, _) = String.length c > 0 && c.[0] == '*' in
1320+
let isDocComment (c, _, _, _) = String.length c > 0 && c.[0] == '*' in
13251321
match (layoutListItems, endMaxChar) with
13261322
| ([], None)-> ([], comments)
13271323
| ([], Some endMax)->

0 commit comments

Comments
 (0)