Skip to content

Commit 9f5c415

Browse files
committed
Fix location in parsing comments printing
1 parent 9b35205 commit 9f5c415

File tree

7 files changed

+224
-9
lines changed

7 files changed

+224
-9
lines changed

Diff for: jscomp/syntax/src/res_comments_table.ml

+13
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,19 @@ and walkExpression expr t comments =
13141314
walkList
13151315
[Expression parentExpr; Expression memberExpr; Expression targetExpr]
13161316
t comments
1317+
| Pexp_apply
1318+
( {
1319+
pexp_desc =
1320+
Pexp_ident
1321+
{
1322+
txt =
1323+
Longident.Ldot
1324+
(Longident.Ldot (Lident "Js", "Dict"), "fromArray");
1325+
};
1326+
},
1327+
[(Nolabel, keyValues)] )
1328+
when Res_parsetree_viewer.isTupleArray keyValues ->
1329+
walkList [Expression keyValues] t comments
13171330
| Pexp_apply (callExpr, arguments) ->
13181331
let before, inside, after = partitionByLoc comments callExpr.pexp_loc in
13191332
let after =

Diff for: jscomp/syntax/src/res_core.ml

+13-7
Original file line numberDiff line numberDiff line change
@@ -3942,20 +3942,26 @@ and parseListExpr ~startPos p =
39423942
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc listExprs)]
39433943

39443944
and parseDictExpr ~startPos p =
3945-
let exprs =
3945+
let rows =
39463946
parseCommaDelimitedRegion ~grammar:Grammar.DictRows ~closing:Rbrace
39473947
~f:parseDictExprRow p
39483948
in
3949-
let loc = mkLoc startPos p.prevEndPos in
3950-
let toKeyValuePair (recordItem: (Longident.t Location.loc * Parsetree.expression)) =
3949+
let loc = mkLoc startPos p.endPos in
3950+
let toKeyValuePair
3951+
(recordItem : Longident.t Location.loc * Parsetree.expression) =
39513952
match recordItem with
3952-
| {Location.txt = Longident.Lident key; loc}, valueExpr ->
3953+
| ( {Location.txt = Longident.Lident key; loc = keyLoc},
3954+
({pexp_loc = valueLoc} as valueExpr) ) ->
39533955
Some
3954-
(Ast_helper.Exp.tuple ~loc:(mkLoc loc.loc_start valueExpr.pexp_loc.loc_end)
3955-
[Ast_helper.Exp.constant ~loc (Pconst_string (key, None)); valueExpr])
3956+
(Ast_helper.Exp.tuple
3957+
~loc:(mkLoc keyLoc.loc_start valueLoc.loc_end)
3958+
[
3959+
Ast_helper.Exp.constant ~loc:keyLoc (Pconst_string (key, None));
3960+
valueExpr;
3961+
])
39563962
| _ -> None
39573963
in
3958-
let keyValuePairs = List.filter_map toKeyValuePair exprs in
3964+
let keyValuePairs = List.filter_map toKeyValuePair rows in
39593965
Parser.expect Rbrace p;
39603966
Ast_helper.Exp.apply ~loc
39613967
(Ast_helper.Exp.ident ~loc

Diff for: jscomp/syntax/src/res_printer.ml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ and printLiteralDictExpr ~state (e : Parsetree.expression) cmtTbl =
13901390
{pexp_desc = Pexp_constant (Pconst_string (name, _)); pexp_loc}; value;
13911391
];
13921392
} ->
1393-
Some (Location.mkloc (Longident.Lident name) pexp_loc, value)
1393+
Some ((Location.mkloc (Longident.Lident name) pexp_loc, value), e)
13941394
| _ -> None
13951395
in
13961396
let rows =
@@ -1409,7 +1409,11 @@ and printLiteralDictExpr ~state (e : Parsetree.expression) cmtTbl =
14091409
Doc.join
14101410
~sep:(Doc.concat [Doc.text ","; Doc.line])
14111411
(List.map
1412-
(fun row -> printBsObjectRow ~state row cmtTbl)
1412+
(fun ((row, e) :
1413+
(Longident.t Location.loc * Parsetree.expression)
1414+
* Parsetree.expression) ->
1415+
let doc = printBsObjectRow ~state row cmtTbl in
1416+
printComments doc cmtTbl e.pexp_loc)
14131417
rows);
14141418
]);
14151419
Doc.trailingComma;

Diff for: jscomp/syntax/tests/printer/expr/bsObj.res

+38
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+
}

Diff for: jscomp/syntax/tests/printer/expr/dict.res

+58
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
])

Diff for: jscomp/syntax/tests/printer/expr/expected/bsObj.res.txt

+38
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+
}

Diff for: jscomp/syntax/tests/printer/expr/expected/dict.res.txt

+58
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)