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

Commit 4257e1b

Browse files
IwanKaramazowIwan
and
Iwan
authored
Update object method parsing and printer for ReScript 9 (#348)
Fixes #311 `obj["say"]` was currently parsed as `obj##say` (Pexp_apply with ##) We now parse this straight into `obj#say`(Pexp_send) Co-authored-by: Iwan <[email protected]>
1 parent 82628aa commit 4257e1b

File tree

11 files changed

+92
-46
lines changed

11 files changed

+92
-46
lines changed

src/res_ast_conversion.ml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ let normalize =
421421
pexp_attributes = [];
422422
pexp_desc = Pexp_ident (Location.mknoloc (Longident.Lident "x"))
423423
},
424-
(default_mapper.cases mapper cases)
424+
(mapper.cases mapper cases)
425425
)
426426

427427
}
@@ -435,21 +435,19 @@ let normalize =
435435
{
436436
pexp_loc = expr.pexp_loc;
437437
pexp_attributes = expr.pexp_attributes;
438-
pexp_desc = Pexp_field (operand, (Location.mknoloc (Longident.Lident "contents")))
438+
pexp_desc = Pexp_field (mapper.expr mapper operand, (Location.mknoloc (Longident.Lident "contents")))
439439
}
440440
| Pexp_apply (
441-
{pexp_desc = Pexp_ident {txt = Longident.Lident "##"}} as op,
442-
[Asttypes.Nolabel, lhs; Nolabel, ({pexp_desc = Pexp_constant (Pconst_string (txt, None))} as stringExpr)]
441+
{pexp_desc = Pexp_ident {txt = Longident.Lident "##"}},
442+
[
443+
Asttypes.Nolabel, lhs; Nolabel,
444+
({pexp_desc = Pexp_constant (Pconst_string (txt, None)) | (Pexp_ident ({txt = Longident.Lident txt})); pexp_loc = labelLoc})]
443445
) ->
444-
let ident = {
445-
Parsetree.pexp_loc = stringExpr.pexp_loc;
446-
pexp_attributes = [];
447-
pexp_desc = Pexp_ident (Location.mkloc (Longident.Lident txt) stringExpr.pexp_loc)
448-
} in
446+
let label = Location.mkloc txt labelLoc in
449447
{
450448
pexp_loc = expr.pexp_loc;
451449
pexp_attributes = expr.pexp_attributes;
452-
pexp_desc = Pexp_apply (op, [Asttypes.Nolabel, lhs; Nolabel, ident])
450+
pexp_desc = Pexp_send (mapper.expr mapper lhs, label)
453451
}
454452
| Pexp_match (
455453
condition,
@@ -461,9 +459,9 @@ let normalize =
461459
let ternaryMarker = (Location.mknoloc "ns.ternary", Parsetree.PStr []) in
462460
{Parsetree.pexp_loc = expr.pexp_loc;
463461
pexp_desc = Pexp_ifthenelse (
464-
default_mapper.expr mapper condition,
465-
default_mapper.expr mapper thenExpr,
466-
(Some (default_mapper.expr mapper elseExpr))
462+
mapper.expr mapper condition,
463+
mapper.expr mapper thenExpr,
464+
(Some (mapper.expr mapper elseExpr))
467465
);
468466
pexp_attributes = ternaryMarker::expr.pexp_attributes;
469467
}

src/res_core.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,9 +1955,7 @@ and parseBracketAccess p expr startPos =
19551955
let e =
19561956
let identLoc = mkLoc stringStart stringEnd in
19571957
let loc = mkLoc lbracket rbracket in
1958-
Ast_helper.Exp.apply ~loc
1959-
(Ast_helper.Exp.ident ~loc (Location.mkloc (Longident.Lident "##") loc))
1960-
[Nolabel, expr; Nolabel, (Ast_helper.Exp.ident ~loc:identLoc (Location.mkloc (Longident.Lident s) identLoc))]
1958+
Ast_helper.Exp.send ~loc expr (Location.mkloc s identLoc)
19611959
in
19621960
let e = parsePrimaryExpr ~operand:e p in
19631961
let equalStart = p.startPos in

src/res_printer.ml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,8 +3137,26 @@ and printExpression (e : Parsetree.expression) cmtTbl =
31373137
Doc.concat [Doc.text ": "; printTypExpr typ1 cmtTbl]
31383138
in
31393139
Doc.concat [Doc.lparen; docExpr; ofType; Doc.text " :> "; docTyp; Doc.rparen]
3140-
| Pexp_send _ ->
3141-
Doc.text "Pexp_send not impemented in printer"
3140+
| Pexp_send (parentExpr, label) ->
3141+
let parentDoc =
3142+
let doc = printExpressionWithComments parentExpr cmtTbl in
3143+
match Parens.unaryExprOperand parentExpr with
3144+
| Parens.Parenthesized -> addParens doc
3145+
| Braced braces -> printBraces doc parentExpr braces
3146+
| Nothing -> doc
3147+
in
3148+
let member =
3149+
let memberDoc = printComments (Doc.text label.txt) cmtTbl label.loc in
3150+
Doc.concat [Doc.text "\""; memberDoc; Doc.text "\""]
3151+
in
3152+
Doc.group (
3153+
Doc.concat [
3154+
parentDoc;
3155+
Doc.lbracket;
3156+
member;
3157+
Doc.rbracket;
3158+
]
3159+
)
31423160
| Pexp_new _ ->
31433161
Doc.text "Pexp_new not impemented in printer"
31443162
| Pexp_setinstvar _ ->

tests/conversion/reason/__snapshots__/render.spec.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,17 @@ type propField<'a> = Js.nullable<{..} as 'a>
12851285
type propField<'a> = {\\"a\\": b}
12861286
type propField<'a> = {..\\"a\\": b}
12871287
type propField<'a> = {\\"a\\": {\\"b\\": c}}
1288+
1289+
user[\\"address\\"]
1290+
user[\\"address\\"][\\"street\\"]
1291+
user[\\"address\\"][\\"street\\"][\\"log\\"]
1292+
1293+
user[\\"address\\"] = \\"Avenue 1\\"
1294+
user[\\"address\\"][\\"street\\"] = \\"Avenue\\"
1295+
user[\\"address\\"][\\"street\\"][\\"number\\"] = \\"1\\"
1296+
1297+
school[\\"print\\"](direction[\\"name\\"], studentHead[\\"name\\"])
1298+
city[\\"getSchool\\"]()[\\"print\\"](direction[\\"name\\"], studentHead[\\"name\\"])
12881299
"
12891300
`;
12901301

tests/conversion/reason/expected/jsObject.re.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ type propField<'a> = Js.nullable<{..} as 'a>
1313
type propField<'a> = {"a": b}
1414
type propField<'a> = {.."a": b}
1515
type propField<'a> = {"a": {"b": c}}
16+
17+
user["address"]
18+
user["address"]["street"]
19+
user["address"]["street"]["log"]
20+
21+
user["address"] = "Avenue 1"
22+
user["address"]["street"] = "Avenue"
23+
user["address"]["street"]["number"] = "1"
24+
25+
school["print"](direction["name"], studentHead["name"])
26+
city["getSchool"]()["print"](direction["name"], studentHead["name"])

tests/conversion/reason/jsObject.re

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ type propField('a) = Js.nullable(Js.t({..} as 'a))
1313
type propField('a) = {. "a": b}
1414
type propField('a) = {.. "a": b}
1515
type propField('a) = Js.t(Js.t({. "a": Js.t({. "b": c})}))
16+
17+
user##address;
18+
user##address##street;
19+
user##address##street##log;
20+
21+
user##address #= "Avenue 1";
22+
user##address##street #= "Avenue" ;
23+
user##address##street##number #= "1";
24+
25+
school##print(direction##name, studentHead##name);
26+
(city##getSchool())##print(direction##name, studentHead##name);
27+

tests/parsing/errors/other/__snapshots__/parse.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`breadcrumbs170.res 1`] = `
44
"=====Parsetree==========================================
55
let l = (Some [1; 2; 3]) |> Obj.magic
6-
module M = struct ;;match l with | None -> [] | Some l -> l ## prop end
6+
module M = struct ;;match l with | None -> [] | Some l -> l#prop end
77
;;from
88
;;now
99
;;on

tests/parsing/errors/other/expected/breadcrumbs170.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
I'm not sure what to parse here when looking at "}".
1212

1313
let l = (Some [1; 2; 3]) |> Obj.magic
14-
module M = struct ;;match l with | None -> [] | Some l -> l ## prop end
14+
module M = struct ;;match l with | None -> [] | Some l -> l#prop end
1515
;;from
1616
;;now
1717
;;on

tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,10 @@ let icon =
778778
[@JSX ])
779779
let _ =
780780
((MessengerSharedPhotosAlbumViewPhotoReact.createElement
781-
?ref:((if (foo ## bar) == baz
781+
?ref:((if foo#bar == baz
782782
then Some (foooooooooooooooooooooooo setRefChild)
783783
else None)[@ns.namedArgLoc ][@ns.ternary ])
784-
~key:((node ## legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
784+
~key:((node#legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
785785
[@JSX ])
786786
let _ = ((Foo.createElement ~bar:((bar)[@ns.namedArgLoc ]) ~children:[] ())
787787
[@JSX ])
@@ -792,7 +792,7 @@ let _ =
792792
[@JSX ])
793793
let x = ((div ~children:[] ())[@JSX ])
794794
let _ = ((div ~asd:((1)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
795-
;;(foo ## bar) #= ((bar ~children:[] ())[@JSX ])
795+
;;foo#bar #= ((bar ~children:[] ())[@JSX ])
796796
;;foo #= ((bar ~children:[] ())[@JSX ])
797797
;;foo #= ((bar ~children:[] ())[@JSX ])
798798
let x = [|((div ~children:[] ())[@JSX ])|]
@@ -1078,12 +1078,11 @@ let _ =
10781078
[@ns.braces ])] ())
10791079
[@JSX ])
10801080
let _ =
1081-
((View.createElement ~style:((styles ## backgroundImageWrapper)
1081+
((View.createElement ~style:((styles#backgroundImageWrapper)
10821082
[@ns.namedArgLoc ])
10831083
~children:[(((let uri = \\"/images/header-background.png\\" in
10841084
((Image.createElement ~resizeMode:((Contain)
1085-
[@ns.namedArgLoc ])
1086-
~style:((styles ## backgroundImage)
1085+
[@ns.namedArgLoc ]) ~style:((styles#backgroundImage)
10871086
[@ns.namedArgLoc ]) ~uri:((uri)[@ns.namedArgLoc ])
10881087
~children:[] ())
10891088
[@JSX ])))
@@ -1215,13 +1214,13 @@ let x = (arr.((x : int))).((y : int))
12151214
[@ns.namedArgLoc ]) ~b:((bArg)[@ns.namedArgLoc ]) ?c:((c)
12161215
[@ns.namedArgLoc ]) ?d:((expr)[@ns.namedArgLoc ])
12171216
;;f ~a:(((x : int))[@ns.namedArgLoc ]) ?b:(((y : int))[@ns.namedArgLoc ])
1218-
;;connection ## platformId
1219-
;;((connection ## left) ## account) ## accountName
1220-
;;(john ## age) #= 99
1221-
;;((john ## son) ## age) #= ((steve ## age) - 5)
1222-
;;(dict ##
1223-
) #= abc
1224-
;;(dict ## \\") #= (dict2 ## \\")"
1217+
;;connection#platformId
1218+
;;((connection#left)#account)#accountName
1219+
;;john#age #= 99
1220+
;;(john#son)#age #= (steve#age - 5)
1221+
;;dict#
1222+
#= abc
1223+
;;dict#\\" #= dict2#\\""
12251224
`;
12261225
12271226
exports[`record.res 1`] = `

tests/parsing/grammar/expressions/expected/jsx.res.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ let icon =
220220
[@JSX ])
221221
let _ =
222222
((MessengerSharedPhotosAlbumViewPhotoReact.createElement
223-
?ref:((if (foo ## bar) == baz
223+
?ref:((if foo#bar == baz
224224
then Some (foooooooooooooooooooooooo setRefChild)
225225
else None)[@ns.namedArgLoc ][@ns.ternary ])
226-
~key:((node ## legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
226+
~key:((node#legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
227227
[@JSX ])
228228
let _ = ((Foo.createElement ~bar:((bar)[@ns.namedArgLoc ]) ~children:[] ())
229229
[@JSX ])
@@ -234,7 +234,7 @@ let _ =
234234
[@JSX ])
235235
let x = ((div ~children:[] ())[@JSX ])
236236
let _ = ((div ~asd:((1)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
237-
;;(foo ## bar) #= ((bar ~children:[] ())[@JSX ])
237+
;;foo#bar #= ((bar ~children:[] ())[@JSX ])
238238
;;foo #= ((bar ~children:[] ())[@JSX ])
239239
;;foo #= ((bar ~children:[] ())[@JSX ])
240240
let x = [|((div ~children:[] ())[@JSX ])|]
@@ -520,12 +520,11 @@ let _ =
520520
[@ns.braces ])] ())
521521
[@JSX ])
522522
let _ =
523-
((View.createElement ~style:((styles ## backgroundImageWrapper)
523+
((View.createElement ~style:((styles#backgroundImageWrapper)
524524
[@ns.namedArgLoc ])
525525
~children:[(((let uri = "/images/header-background.png" in
526526
((Image.createElement ~resizeMode:((Contain)
527-
[@ns.namedArgLoc ])
528-
~style:((styles ## backgroundImage)
527+
[@ns.namedArgLoc ]) ~style:((styles#backgroundImage)
529528
[@ns.namedArgLoc ]) ~uri:((uri)[@ns.namedArgLoc ])
530529
~children:[] ())
531530
[@JSX ])))

tests/parsing/grammar/expressions/expected/primary.res.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ let x = (arr.((x : int))).((y : int))
2727
[@ns.namedArgLoc ]) ~b:((bArg)[@ns.namedArgLoc ]) ?c:((c)
2828
[@ns.namedArgLoc ]) ?d:((expr)[@ns.namedArgLoc ])
2929
;;f ~a:(((x : int))[@ns.namedArgLoc ]) ?b:(((y : int))[@ns.namedArgLoc ])
30-
;;connection ## platformId
31-
;;((connection ## left) ## account) ## accountName
32-
;;(john ## age) #= 99
33-
;;((john ## son) ## age) #= ((steve ## age) - 5)
34-
;;(dict ##
35-
) #= abc
36-
;;(dict ## ") #= (dict2 ## ")
30+
;;connection#platformId
31+
;;((connection#left)#account)#accountName
32+
;;john#age #= 99
33+
;;(john#son)#age #= (steve#age - 5)
34+
;;dict#
35+
#= abc
36+
;;dict#" #= dict2#"

0 commit comments

Comments
 (0)