Skip to content

Fix inline comment before spread syntax in record #6615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2964,15 +2964,27 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
let spread =
match spreadExpr with
| None -> Doc.nil
| Some expr ->
| Some ({pexp_desc} as expr) ->
let doc =
match pexp_desc with
| Pexp_constant (Pconst_string _) ->
printExpression ~state expr cmtTbl
| Pexp_ident {txt = expr} -> printLident expr
| _ -> Doc.nil
in
let docWithSpread =
Doc.concat
[
Doc.dotdotdot;
(match Parens.expr expr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc expr braces
| Nothing -> doc);
]
in
Doc.concat
[
Doc.dotdotdot;
(let doc = printExpressionWithComments ~state expr cmtTbl in
match Parens.expr expr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc expr braces
| Nothing -> doc);
printComments docWithSpread cmtTbl expr.Parsetree.pexp_loc;
Doc.comma;
Doc.line;
]
Expand Down
42 changes: 41 additions & 1 deletion jscomp/syntax/tests/printer/comments/expected/expr.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ let user = /* before */ {
} // after

let spreadUser = {
.../* before */ user1 /* after */,
/* before */ ...user1 /* after */,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place where the formatting will change. The spread (...) operator and the ident are matched together. I think it makes sense than allowing comment to be placed in between them.

/* c0 */ age /* c1 */: /* c2 */ 32 /* c3 */,
}

Expand Down Expand Up @@ -284,3 +284,43 @@ Doc.concat(list{
rows,
/* a */
})

// More record spread test
type b = {
// spread a
...a,
// spread c
...c,
// no spread
b: string,
}

type a = {
// spread from different module
...M.a,
// spread c
...c,
// no spread
b: string,
}

let b = {
// exotic
...\"let",
// foo
bar: "foo",
}

let b = {
// quote
..."let",
// foo
bar: "foo",
}

let c = {
// from different module
...M.a,
// foo
bar: "foo",
}
40 changes: 40 additions & 0 deletions jscomp/syntax/tests/printer/comments/expr.res
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,43 @@ Doc.concat(list{
rows,
/* a */
});

// More record spread test
type b = {
// spread a
...a,
// spread c
...c,
// no spread
b: string
}

type a = {
// spread from different module
...M.a,
// spread c
...c,
// no spread
b: string
}

let b = {
// exotic
...\"let",
// foo
bar: "foo"
}

let b = {
// quote
..."let",
// foo
bar: "foo"
}

let c = {
// from different module
...M.a,
// foo
bar: "foo"
}