Skip to content

Commit

Permalink
Fix location in parsing comments printing
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed May 25, 2024
1 parent 9b35205 commit ff91945
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 8 deletions.
15 changes: 15 additions & 0 deletions jscomp/syntax/src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,21 @@ and walkExpression expr t comments =
walkList
[Expression parentExpr; Expression memberExpr; Expression targetExpr]
t comments
| Pexp_apply
( {
pexp_desc =
Pexp_ident
{
txt =
Longident.Ldot
(Longident.Ldot (Lident "Js", "Dict"), "fromArray");
};
},
[(Nolabel, keyValues)] )
when Res_parsetree_viewer.isTupleArray keyValues ->
walkList
[Expression keyValues]
t comments
| Pexp_apply (callExpr, arguments) ->
let before, inside, after = partitionByLoc comments callExpr.pexp_loc in
let after =
Expand Down
12 changes: 6 additions & 6 deletions jscomp/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3942,20 +3942,20 @@ and parseListExpr ~startPos p =
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc listExprs)]

and parseDictExpr ~startPos p =
let exprs =
let rows =
parseCommaDelimitedRegion ~grammar:Grammar.DictRows ~closing:Rbrace
~f:parseDictExprRow p
in
let loc = mkLoc startPos p.prevEndPos in
let loc = mkLoc startPos p.endPos in
let toKeyValuePair (recordItem: (Longident.t Location.loc * Parsetree.expression)) =
match recordItem with
| {Location.txt = Longident.Lident key; loc}, valueExpr ->
| {Location.txt = Longident.Lident key; loc = keyLoc}, ({pexp_loc = valueLoc} as valueExpr) ->
Some
(Ast_helper.Exp.tuple ~loc:(mkLoc loc.loc_start valueExpr.pexp_loc.loc_end)
[Ast_helper.Exp.constant ~loc (Pconst_string (key, None)); valueExpr])
(Ast_helper.Exp.tuple ~loc:(mkLoc keyLoc.loc_start valueLoc.loc_end)
[Ast_helper.Exp.constant ~loc:keyLoc (Pconst_string (key, None)); valueExpr])
| _ -> None
in
let keyValuePairs = List.filter_map toKeyValuePair exprs in
let keyValuePairs = List.filter_map toKeyValuePair rows in
Parser.expect Rbrace p;
Ast_helper.Exp.apply ~loc
(Ast_helper.Exp.ident ~loc
Expand Down
6 changes: 4 additions & 2 deletions jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ and printLiteralDictExpr ~state (e : Parsetree.expression) cmtTbl =
{pexp_desc = Pexp_constant (Pconst_string (name, _)); pexp_loc}; value;
];
} ->
Some (Location.mkloc (Longident.Lident name) pexp_loc, value)
Some ((Location.mkloc (Longident.Lident name) pexp_loc, value), e)
| _ -> None
in
let rows =
Expand All @@ -1409,7 +1409,9 @@ and printLiteralDictExpr ~state (e : Parsetree.expression) cmtTbl =
Doc.join
~sep:(Doc.concat [Doc.text ","; Doc.line])
(List.map
(fun row -> printBsObjectRow ~state row cmtTbl)
(fun ((row, e): (Longident.t Location.loc * Parsetree.expression) * Parsetree.expression) ->
let doc = printBsObjectRow ~state row cmtTbl in
printComments doc cmtTbl e.pexp_loc)
rows);
]);
Doc.trailingComma;
Expand Down
38 changes: 38 additions & 0 deletions jscomp/syntax/tests/printer/expr/bsObj.res
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,41 @@ React.jsx(
{"data-foo": (\"data-foo": string)}
}
)

// comments
let x = {/* foo */ "foo": "bar"}
let x = {"foo": /* foo */ "bar"}
let x = {"foo": "bar" /* foo */ }

let x = {
// foo
"foo": "bar",
// bar
"bar": "baz",
// baz
"baz": baz
}

let x = {
"foo": "bar", // foo
"bar": "baz", // bar
"baz": baz // baz
}

let x = {
"foo": /* foo */ "bar",
"bar": /* bar */ "baz",
"baz": /* bar */ baz
}

let x = {
/* foo */ "foo": "bar",
/* bar */ "bar": "baz",
/* bar */ "baz": baz
}

let x = {
"foo": "bar" /* foo */,
"bar": "baz" /* bar */,
"baz": baz /* bar */
}
58 changes: 58 additions & 0 deletions jscomp/syntax/tests/printer/expr/dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,62 @@ let x = Js.Dict.fromArray([
("foo", "bar"),
("bar", "baz"),
("baz", baz)
])

// comments
let x = dict{/* foo */ "foo": "bar"}
let x = dict{"foo": /* foo */ "bar"}
let x = dict{"foo": "bar" /* foo */ }

let x = dict{
// foo
"foo": "bar",
// bar
"bar": "baz",
// baz
"baz": baz
}

let x = dict{
"foo": "bar", // foo
"bar": "baz", // bar
"baz": baz // baz
}

let x = dict{
"foo": /* foo */ "bar",
"bar": /* bar */ "baz",
"baz": /* bar */ baz
}

let x = dict{
/* foo */ "foo": "bar",
/* bar */ "bar": "baz",
/* bar */ "baz": baz
}

let x = dict{
"foo": "bar" /* foo */,
"bar": "baz" /* bar */,
"baz": baz /* bar */
}

let x = Js.Dict.fromArray([/* foo */ ("foo", "bar"), /* bar */ ("bar", "baz")])
let x = Js.Dict.fromArray([(/* foo */ "foo", "bar"), (/* bar */"bar", "baz"), (/* baz */ "baz", baz)])
let x = Js.Dict.fromArray([("foo", /* foo */"bar"), ("bar", /* bar */"baz"), ("baz", /* baz */baz)])
let x = Js.Dict.fromArray([("foo", "bar" /* foo */), ("bar", "baz" /* bar */), ("baz", baz /* baz */)])

let x = Js.Dict.fromArray([
// foo
("foo", "bar"),
// bar
("bar", "baz"),
// baz
("baz", baz)
])

let x = Js.Dict.fromArray([
("foo", "bar"), // foo
("bar", "baz"), // bar
("baz", baz) // baz
])
38 changes: 38 additions & 0 deletions jscomp/syntax/tests/printer/expr/expected/bsObj.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,41 @@ React.jsx(
{"data-foo": (\"data-foo": string)}
},
)

// comments
let x = {/* foo */ "foo": "bar"}
let x = {"foo": /* foo */ "bar"}
let x = {"foo": "bar" /* foo */}

let x = {
// foo
"foo": "bar",
// bar
"bar": "baz",
// baz
"baz": baz,
}

let x = {
"foo": "bar", // foo
"bar": "baz", // bar
"baz": baz, // baz
}

let x = {
"foo": /* foo */ "bar",
"bar": /* bar */ "baz",
"baz": /* bar */ baz,
}

let x = {
/* foo */ "foo": "bar",
/* bar */ "bar": "baz",
/* bar */ "baz": baz,
}

let x = {
"foo": "bar" /* foo */,
"bar": "baz" /* bar */,
"baz": baz /* bar */,
}
58 changes: 58 additions & 0 deletions jscomp/syntax/tests/printer/expr/expected/dict.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,61 @@ let x = dict{
"bar": "baz",
"baz": baz,
}

// comments
let x = dict{/* foo */ "foo": "bar"}
let x = dict{"foo": /* foo */ "bar"}
let x = dict{"foo": "bar" /* foo */}

let x = dict{
// foo
"foo": "bar",
// bar
"bar": "baz",
// baz
"baz": baz,
}

let x = dict{
"foo": "bar", // foo
"bar": "baz", // bar
"baz": baz, // baz
}

let x = dict{
"foo": /* foo */ "bar",
"bar": /* bar */ "baz",
"baz": /* bar */ baz,
}

let x = dict{
/* foo */ "foo": "bar",
/* bar */ "bar": "baz",
/* bar */ "baz": baz,
}

let x = dict{
"foo": "bar" /* foo */,
"bar": "baz" /* bar */,
"baz": baz /* bar */,
}

let x = dict{/* foo */ "foo": "bar", /* bar */ "bar": "baz"}
let x = dict{/* foo */ "foo": "bar", /* bar */ "bar": "baz", /* baz */ "baz": baz}
let x = dict{"foo": /* foo */ "bar", "bar": /* bar */ "baz", "baz": /* baz */ baz}
let x = dict{"foo": "bar" /* foo */, "bar": "baz" /* bar */, "baz": baz /* baz */}

let x = dict{
// foo
"foo": "bar",
// bar
"bar": "baz",
// baz
"baz": baz,
}

let x = dict{
"foo": "bar", // foo
"bar": "baz", // bar
"baz": baz, // baz
}

0 comments on commit ff91945

Please sign in to comment.