Skip to content

Commit 74ef2e2

Browse files
bloodyowlcknitt
authored andcommitted
Fix location in parsing comments printing
1 parent bdbc51e commit 74ef2e2

File tree

7 files changed

+221
-8
lines changed

7 files changed

+221
-8
lines changed

jscomp/syntax/src/res_comments_table.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,19 @@ and walk_expression expr t comments =
13451345
walk_list
13461346
[Expression parent_expr; Expression member_expr; Expression target_expr]
13471347
t comments
1348+
| Pexp_apply
1349+
( {
1350+
pexp_desc =
1351+
Pexp_ident
1352+
{
1353+
txt =
1354+
Longident.Ldot
1355+
(Longident.Ldot (Lident "Js", "Dict"), "fromArray");
1356+
};
1357+
},
1358+
[(Nolabel, key_values)] )
1359+
when Res_parsetree_viewer.is_tuple_array key_values ->
1360+
walk_list [Expression key_values] t comments
13481361
| Pexp_apply (call_expr, arguments) ->
13491362
let before, inside, after = partition_by_loc comments call_expr.pexp_loc in
13501363
let after =

jscomp/syntax/src/res_core.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,22 +3922,26 @@ and parse_list_expr ~start_pos p =
39223922
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc list_exprs)]
39233923

39243924
and parse_dict_expr ~start_pos p =
3925-
let exprs =
3925+
let rows =
39263926
parse_comma_delimited_region ~grammar:Grammar.DictRows ~closing:Rbrace
39273927
~f:parse_dict_expr_row p
39283928
in
3929-
let loc = mk_loc start_pos p.prev_end_pos in
3929+
let loc = mk_loc start_pos p.end_pos in
39303930
let to_key_value_pair
39313931
(record_item : Longident.t Location.loc * Parsetree.expression) =
39323932
match record_item with
3933-
| {Location.txt = Longident.Lident key; loc}, valueExpr ->
3933+
| ( {Location.txt = Longident.Lident key; loc = keyLoc},
3934+
({pexp_loc = value_loc} as value_expr) ) ->
39343935
Some
39353936
(Ast_helper.Exp.tuple
3936-
~loc:(mk_loc loc.loc_start valueExpr.pexp_loc.loc_end)
3937-
[Ast_helper.Exp.constant ~loc (Pconst_string (key, None)); valueExpr])
3937+
~loc:(mk_loc keyLoc.loc_start value_loc.loc_end)
3938+
[
3939+
Ast_helper.Exp.constant ~loc:keyLoc (Pconst_string (key, None));
3940+
value_expr;
3941+
])
39383942
| _ -> None
39393943
in
3940-
let key_value_pairs = List.filter_map to_key_value_pair exprs in
3944+
let key_value_pairs = List.filter_map to_key_value_pair rows in
39413945
Parser.expect Rbrace p;
39423946
Ast_helper.Exp.apply ~loc
39433947
(Ast_helper.Exp.ident ~loc

jscomp/syntax/src/res_printer.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl =
14191419
{pexp_desc = Pexp_constant (Pconst_string (name, _)); pexp_loc}; value;
14201420
];
14211421
} ->
1422-
Some (Location.mkloc (Longident.Lident name) pexp_loc, value)
1422+
Some ((Location.mkloc (Longident.Lident name) pexp_loc, value), e)
14231423
| _ -> None
14241424
in
14251425
let rows =
@@ -1438,7 +1438,11 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl =
14381438
Doc.join
14391439
~sep:(Doc.concat [Doc.text ","; Doc.line])
14401440
(List.map
1441-
(fun row -> print_bs_object_row ~state row cmt_tbl)
1441+
(fun ((row, e) :
1442+
(Longident.t Location.loc * Parsetree.expression)
1443+
* Parsetree.expression) ->
1444+
let doc = print_bs_object_row ~state row cmt_tbl in
1445+
print_comments doc cmt_tbl e.pexp_loc)
14421446
rows);
14431447
]);
14441448
Doc.trailing_comma;

jscomp/syntax/tests/printer/expr/bsObj.res

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,41 @@ React.jsx(
5656
{"data-foo": (\"data-foo": string)}
5757
}
5858
)
59+
60+
// comments
61+
let x = {/* foo */ "foo": "bar"}
62+
let x = {"foo": /* foo */ "bar"}
63+
let x = {"foo": "bar" /* foo */ }
64+
65+
let x = {
66+
// foo
67+
"foo": "bar",
68+
// bar
69+
"bar": "baz",
70+
// baz
71+
"baz": baz
72+
}
73+
74+
let x = {
75+
"foo": "bar", // foo
76+
"bar": "baz", // bar
77+
"baz": baz // baz
78+
}
79+
80+
let x = {
81+
"foo": /* foo */ "bar",
82+
"bar": /* bar */ "baz",
83+
"baz": /* bar */ baz
84+
}
85+
86+
let x = {
87+
/* foo */ "foo": "bar",
88+
/* bar */ "bar": "baz",
89+
/* bar */ "baz": baz
90+
}
91+
92+
let x = {
93+
"foo": "bar" /* foo */,
94+
"bar": "baz" /* bar */,
95+
"baz": baz /* bar */
96+
}

jscomp/syntax/tests/printer/expr/dict.res

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,62 @@ let x = Js.Dict.fromArray([
2424
("foo", "bar"),
2525
("bar", "baz"),
2626
("baz", baz)
27+
])
28+
29+
// comments
30+
let x = dict{/* foo */ "foo": "bar"}
31+
let x = dict{"foo": /* foo */ "bar"}
32+
let x = dict{"foo": "bar" /* foo */ }
33+
34+
let x = dict{
35+
// foo
36+
"foo": "bar",
37+
// bar
38+
"bar": "baz",
39+
// baz
40+
"baz": baz
41+
}
42+
43+
let x = dict{
44+
"foo": "bar", // foo
45+
"bar": "baz", // bar
46+
"baz": baz // baz
47+
}
48+
49+
let x = dict{
50+
"foo": /* foo */ "bar",
51+
"bar": /* bar */ "baz",
52+
"baz": /* bar */ baz
53+
}
54+
55+
let x = dict{
56+
/* foo */ "foo": "bar",
57+
/* bar */ "bar": "baz",
58+
/* bar */ "baz": baz
59+
}
60+
61+
let x = dict{
62+
"foo": "bar" /* foo */,
63+
"bar": "baz" /* bar */,
64+
"baz": baz /* bar */
65+
}
66+
67+
let x = Js.Dict.fromArray([/* foo */ ("foo", "bar"), /* bar */ ("bar", "baz")])
68+
let x = Js.Dict.fromArray([(/* foo */ "foo", "bar"), (/* bar */"bar", "baz"), (/* baz */ "baz", baz)])
69+
let x = Js.Dict.fromArray([("foo", /* foo */"bar"), ("bar", /* bar */"baz"), ("baz", /* baz */baz)])
70+
let x = Js.Dict.fromArray([("foo", "bar" /* foo */), ("bar", "baz" /* bar */), ("baz", baz /* baz */)])
71+
72+
let x = Js.Dict.fromArray([
73+
// foo
74+
("foo", "bar"),
75+
// bar
76+
("bar", "baz"),
77+
// baz
78+
("baz", baz)
79+
])
80+
81+
let x = Js.Dict.fromArray([
82+
("foo", "bar"), // foo
83+
("bar", "baz"), // bar
84+
("baz", baz) // baz
2785
])

jscomp/syntax/tests/printer/expr/expected/bsObj.res.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,41 @@ React.jsx(
6060
{"data-foo": (\"data-foo": string)}
6161
},
6262
)
63+
64+
// comments
65+
let x = {/* foo */ "foo": "bar"}
66+
let x = {"foo": /* foo */ "bar"}
67+
let x = {"foo": "bar" /* foo */}
68+
69+
let x = {
70+
// foo
71+
"foo": "bar",
72+
// bar
73+
"bar": "baz",
74+
// baz
75+
"baz": baz,
76+
}
77+
78+
let x = {
79+
"foo": "bar", // foo
80+
"bar": "baz", // bar
81+
"baz": baz, // baz
82+
}
83+
84+
let x = {
85+
"foo": /* foo */ "bar",
86+
"bar": /* bar */ "baz",
87+
"baz": /* bar */ baz,
88+
}
89+
90+
let x = {
91+
/* foo */ "foo": "bar",
92+
/* bar */ "bar": "baz",
93+
/* bar */ "baz": baz,
94+
}
95+
96+
let x = {
97+
"foo": "bar" /* foo */,
98+
"bar": "baz" /* bar */,
99+
"baz": baz /* bar */,
100+
}

jscomp/syntax/tests/printer/expr/expected/dict.res.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,61 @@ let x = dict{
2525
"bar": "baz",
2626
"baz": baz,
2727
}
28+
29+
// comments
30+
let x = dict{/* foo */ "foo": "bar"}
31+
let x = dict{"foo": /* foo */ "bar"}
32+
let x = dict{"foo": "bar" /* foo */}
33+
34+
let x = dict{
35+
// foo
36+
"foo": "bar",
37+
// bar
38+
"bar": "baz",
39+
// baz
40+
"baz": baz,
41+
}
42+
43+
let x = dict{
44+
"foo": "bar", // foo
45+
"bar": "baz", // bar
46+
"baz": baz, // baz
47+
}
48+
49+
let x = dict{
50+
"foo": /* foo */ "bar",
51+
"bar": /* bar */ "baz",
52+
"baz": /* bar */ baz,
53+
}
54+
55+
let x = dict{
56+
/* foo */ "foo": "bar",
57+
/* bar */ "bar": "baz",
58+
/* bar */ "baz": baz,
59+
}
60+
61+
let x = dict{
62+
"foo": "bar" /* foo */,
63+
"bar": "baz" /* bar */,
64+
"baz": baz /* bar */,
65+
}
66+
67+
let x = dict{/* foo */ "foo": "bar", /* bar */ "bar": "baz"}
68+
let x = dict{/* foo */ "foo": "bar", /* bar */ "bar": "baz", /* baz */ "baz": baz}
69+
let x = dict{"foo": /* foo */ "bar", "bar": /* bar */ "baz", "baz": /* baz */ baz}
70+
let x = dict{"foo": "bar" /* foo */, "bar": "baz" /* bar */, "baz": baz /* baz */}
71+
72+
let x = dict{
73+
// foo
74+
"foo": "bar",
75+
// bar
76+
"bar": "baz",
77+
// baz
78+
"baz": baz,
79+
}
80+
81+
let x = dict{
82+
"foo": "bar", // foo
83+
"bar": "baz", // bar
84+
"baz": baz, // baz
85+
}

0 commit comments

Comments
 (0)