Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 110d9c6

Browse files
Fix printing of constrained expr rhs js object. (#240)
* Fix printing of constrained expr rhs js object. Constrained expressions would not contain parens, and result into an invalid parse after printing. **input** ```rescript {"name": (name: string)} ``` **before** ```rescript {"name": name: string} // invalid syntax ``` **after** ```rescript {"name": (name: string)} ``` * changelog entry
1 parent 451b99d commit 110d9c6

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
* Fix printing of constrained expressions in rhs of js objects [#240](https://github.com/rescript-lang/syntax/pull/240)
34
* Improve printing of trailing comments under lhs of "pipe" expression in [#329](https://github.com/rescript-lang/syntax/pull/239/files)
45
* Improve printing of jsx children and props with leading line comment in [#236](https://github.com/rescript-lang/syntax/pull/236)
56
* Improve conversion of quoted strings from Reason in [#238](https://github.com/rescript-lang/syntax/pull/238)

src/res_printer.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -4660,7 +4660,11 @@ and printBsObjectRow (lbl, expr) cmtTbl =
46604660
let doc = Doc.concat [
46614661
lblDoc;
46624662
Doc.text ": ";
4663-
printExpressionWithComments expr cmtTbl
4663+
(let doc = printExpressionWithComments expr cmtTbl in
4664+
match Parens.expr expr with
4665+
| Parens.Parenthesized -> addParens doc
4666+
| Braced braces -> printBraces doc expr braces
4667+
| Nothing -> doc);
46644668
] in
46654669
printComments doc cmtTbl cmtLoc
46664670

tests/printer/expr/__snapshots__/render.spec.js.snap

+60
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,54 @@ let person = {
11931193
},
11941194
\\"age\\": 32,
11951195
}
1196+
1197+
// print parens around constrained expr in rhs
1198+
let user = {\\"name\\": (ceo.name: string)}
1199+
// braces should be preserved on rhs
1200+
let user = {\\"name\\": {ceo.name}}
1201+
let user = {
1202+
\\"name\\": {
1203+
ceo.name
1204+
},
1205+
}
1206+
// braced + constrained expr
1207+
let user = {\\"name\\": {(ceo.name: string)}}
1208+
1209+
React.jsx(
1210+
ReactDOM.stringToComponent(\\"div\\"),
1211+
{
1212+
let ariaCurrent = #page
1213+
{
1214+
\\"aria-current\\": (
1215+
ariaCurrent: [
1216+
| #page
1217+
| #step
1218+
| #location
1219+
| #date
1220+
| #time
1221+
| #\\"true\\"
1222+
| #\\"false\\"
1223+
]
1224+
),
1225+
}
1226+
},
1227+
)
1228+
1229+
React.jsx(
1230+
ReactDOM.stringToComponent(\\"div\\"),
1231+
{
1232+
let children = {msg->React.string}
1233+
{\\"children\\": (children: React.element)}
1234+
},
1235+
)
1236+
1237+
(@warning(\\"-3\\") React.jsx)(
1238+
ReactDOM.stringToComponent(\\"div\\"),
1239+
{
1240+
let \\\\\\"data-foo\\" = \\"payload\\"
1241+
{\\"data-foo\\": (\\\\\\"data-foo\\": string)}
1242+
},
1243+
)
11961244
"
11971245
`;
11981246
@@ -3343,6 +3391,18 @@ let x =
33433391
superLongName: 5,
33443392
superLongName: 20,
33453393
}
3394+
3395+
// print parens around constrained expr in rhs
3396+
let user = {name: (ceo.name: string)}
3397+
// braces should be preserved on rhs
3398+
let user = {name: {ceo.name}}
3399+
let user = {
3400+
name: {
3401+
ceo.name
3402+
},
3403+
}
3404+
// braced + constrained expr
3405+
let user = {name: {(ceo.name: string)}}
33463406
"
33473407
`;
33483408

tests/printer/expr/bsObj.js

+44
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,47 @@ let person = {
1212
},
1313
"age": 32
1414
}
15+
16+
// print parens around constrained expr in rhs
17+
let user = {"name": (ceo.name: string)}
18+
// braces should be preserved on rhs
19+
let user = {"name": {ceo.name}}
20+
let user = {"name": {
21+
ceo.name
22+
}}
23+
// braced + constrained expr
24+
let user = {"name": {(ceo.name: string)}}
25+
26+
React.jsx(
27+
ReactDOM.stringToComponent("div"),
28+
{
29+
let ariaCurrent = #page
30+
{
31+
"aria-current": (ariaCurrent : [
32+
| #page
33+
| #step
34+
| #location
35+
| #date
36+
| #time
37+
| #"true"
38+
| #"false"
39+
])
40+
}
41+
}
42+
)
43+
44+
React.jsx(
45+
ReactDOM.stringToComponent("div"),
46+
{
47+
let children = {msg->React.string}
48+
{"children": (children: React.element)}
49+
}
50+
)
51+
52+
(@warning("-3") React.jsx)(
53+
ReactDOM.stringToComponent("div"),
54+
{
55+
let \"data-foo" = "payload"
56+
{"data-foo": (\"data-foo": string)}
57+
}
58+
)

tests/printer/expr/record.js

+10
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ let withSpreadAndNaturalBreak = {...fields, firstField: superLongIdentiiiiiiiiff
1818
let x = @attr {x: 1, y: 2}
1919
let x = @attr {...initialState, superLongName: 1, superLongName: 2, superLongName: 5}
2020
let x = @attr {...initialState, superLongName: 1, superLongName: 2, superLongName: 5, superLongName: 20}
21+
22+
// print parens around constrained expr in rhs
23+
let user = {name: (ceo.name: string)}
24+
// braces should be preserved on rhs
25+
let user = {name: {ceo.name}}
26+
let user = {name: {
27+
ceo.name
28+
}}
29+
// braced + constrained expr
30+
let user = {name: {(ceo.name: string)}}

0 commit comments

Comments
 (0)