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

Update object method parsing and printer for ReScript 9 #348

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 11 additions & 13 deletions src/res_ast_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ let normalize =
pexp_attributes = [];
pexp_desc = Pexp_ident (Location.mknoloc (Longident.Lident "x"))
},
(default_mapper.cases mapper cases)
(mapper.cases mapper cases)
)

}
Expand All @@ -435,21 +435,19 @@ let normalize =
{
pexp_loc = expr.pexp_loc;
pexp_attributes = expr.pexp_attributes;
pexp_desc = Pexp_field (operand, (Location.mknoloc (Longident.Lident "contents")))
pexp_desc = Pexp_field (mapper.expr mapper operand, (Location.mknoloc (Longident.Lident "contents")))
}
| Pexp_apply (
{pexp_desc = Pexp_ident {txt = Longident.Lident "##"}} as op,
[Asttypes.Nolabel, lhs; Nolabel, ({pexp_desc = Pexp_constant (Pconst_string (txt, None))} as stringExpr)]
{pexp_desc = Pexp_ident {txt = Longident.Lident "##"}},
[
Asttypes.Nolabel, lhs; Nolabel,
({pexp_desc = Pexp_constant (Pconst_string (txt, None)) | (Pexp_ident ({txt = Longident.Lident txt})); pexp_loc = labelLoc})]
) ->
let ident = {
Parsetree.pexp_loc = stringExpr.pexp_loc;
pexp_attributes = [];
pexp_desc = Pexp_ident (Location.mkloc (Longident.Lident txt) stringExpr.pexp_loc)
} in
let label = Location.mkloc txt labelLoc in
{
pexp_loc = expr.pexp_loc;
pexp_attributes = expr.pexp_attributes;
pexp_desc = Pexp_apply (op, [Asttypes.Nolabel, lhs; Nolabel, ident])
pexp_desc = Pexp_send (mapper.expr mapper lhs, label)
}
| Pexp_match (
condition,
Expand All @@ -461,9 +459,9 @@ let normalize =
let ternaryMarker = (Location.mknoloc "ns.ternary", Parsetree.PStr []) in
{Parsetree.pexp_loc = expr.pexp_loc;
pexp_desc = Pexp_ifthenelse (
default_mapper.expr mapper condition,
default_mapper.expr mapper thenExpr,
(Some (default_mapper.expr mapper elseExpr))
mapper.expr mapper condition,
mapper.expr mapper thenExpr,
(Some (mapper.expr mapper elseExpr))
);
pexp_attributes = ternaryMarker::expr.pexp_attributes;
}
Expand Down
4 changes: 1 addition & 3 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1955,9 +1955,7 @@ and parseBracketAccess p expr startPos =
let e =
let identLoc = mkLoc stringStart stringEnd in
let loc = mkLoc lbracket rbracket in
Ast_helper.Exp.apply ~loc
(Ast_helper.Exp.ident ~loc (Location.mkloc (Longident.Lident "##") loc))
[Nolabel, expr; Nolabel, (Ast_helper.Exp.ident ~loc:identLoc (Location.mkloc (Longident.Lident s) identLoc))]
Ast_helper.Exp.send ~loc expr (Location.mkloc s identLoc)
in
let e = parsePrimaryExpr ~operand:e p in
let equalStart = p.startPos in
Expand Down
22 changes: 20 additions & 2 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3137,8 +3137,26 @@ and printExpression (e : Parsetree.expression) cmtTbl =
Doc.concat [Doc.text ": "; printTypExpr typ1 cmtTbl]
in
Doc.concat [Doc.lparen; docExpr; ofType; Doc.text " :> "; docTyp; Doc.rparen]
| Pexp_send _ ->
Doc.text "Pexp_send not impemented in printer"
| Pexp_send (parentExpr, label) ->
let parentDoc =
let doc = printExpressionWithComments parentExpr cmtTbl in
match Parens.unaryExprOperand parentExpr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc parentExpr braces
| Nothing -> doc
in
let member =
let memberDoc = printComments (Doc.text label.txt) cmtTbl label.loc in
Doc.concat [Doc.text "\""; memberDoc; Doc.text "\""]
in
Doc.group (
Doc.concat [
parentDoc;
Doc.lbracket;
member;
Doc.rbracket;
]
)
| Pexp_new _ ->
Doc.text "Pexp_new not impemented in printer"
| Pexp_setinstvar _ ->
Expand Down
11 changes: 11 additions & 0 deletions tests/conversion/reason/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,17 @@ type propField<'a> = Js.nullable<{..} as 'a>
type propField<'a> = {\\"a\\": b}
type propField<'a> = {..\\"a\\": b}
type propField<'a> = {\\"a\\": {\\"b\\": c}}

user[\\"address\\"]
user[\\"address\\"][\\"street\\"]
user[\\"address\\"][\\"street\\"][\\"log\\"]

user[\\"address\\"] = \\"Avenue 1\\"
user[\\"address\\"][\\"street\\"] = \\"Avenue\\"
user[\\"address\\"][\\"street\\"][\\"number\\"] = \\"1\\"

school[\\"print\\"](direction[\\"name\\"], studentHead[\\"name\\"])
city[\\"getSchool\\"]()[\\"print\\"](direction[\\"name\\"], studentHead[\\"name\\"])
"
`;

Expand Down
11 changes: 11 additions & 0 deletions tests/conversion/reason/expected/jsObject.re.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ type propField<'a> = Js.nullable<{..} as 'a>
type propField<'a> = {"a": b}
type propField<'a> = {.."a": b}
type propField<'a> = {"a": {"b": c}}

user["address"]
user["address"]["street"]
user["address"]["street"]["log"]

user["address"] = "Avenue 1"
user["address"]["street"] = "Avenue"
user["address"]["street"]["number"] = "1"

school["print"](direction["name"], studentHead["name"])
city["getSchool"]()["print"](direction["name"], studentHead["name"])
12 changes: 12 additions & 0 deletions tests/conversion/reason/jsObject.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ type propField('a) = Js.nullable(Js.t({..} as 'a))
type propField('a) = {. "a": b}
type propField('a) = {.. "a": b}
type propField('a) = Js.t(Js.t({. "a": Js.t({. "b": c})}))

user##address;
user##address##street;
user##address##street##log;

user##address #= "Avenue 1";
user##address##street #= "Avenue" ;
user##address##street##number #= "1";

school##print(direction##name, studentHead##name);
(city##getSchool())##print(direction##name, studentHead##name);

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`breadcrumbs170.res 1`] = `
"=====Parsetree==========================================
let l = (Some [1; 2; 3]) |> Obj.magic
module M = struct ;;match l with | None -> [] | Some l -> l ## prop end
module M = struct ;;match l with | None -> [] | Some l -> l#prop end
;;from
;;now
;;on
Expand Down
2 changes: 1 addition & 1 deletion tests/parsing/errors/other/expected/breadcrumbs170.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
I'm not sure what to parse here when looking at "}".

let l = (Some [1; 2; 3]) |> Obj.magic
module M = struct ;;match l with | None -> [] | Some l -> l ## prop end
module M = struct ;;match l with | None -> [] | Some l -> l#prop end
;;from
;;now
;;on
Expand Down
25 changes: 12 additions & 13 deletions tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -778,10 +778,10 @@ let icon =
[@JSX ])
let _ =
((MessengerSharedPhotosAlbumViewPhotoReact.createElement
?ref:((if (foo ## bar) == baz
?ref:((if foo#bar == baz
then Some (foooooooooooooooooooooooo setRefChild)
else None)[@ns.namedArgLoc ][@ns.ternary ])
~key:((node ## legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
~key:((node#legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
[@JSX ])
let _ = ((Foo.createElement ~bar:((bar)[@ns.namedArgLoc ]) ~children:[] ())
[@JSX ])
Expand All @@ -792,7 +792,7 @@ let _ =
[@JSX ])
let x = ((div ~children:[] ())[@JSX ])
let _ = ((div ~asd:((1)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
;;(foo ## bar) #= ((bar ~children:[] ())[@JSX ])
;;foo#bar #= ((bar ~children:[] ())[@JSX ])
;;foo #= ((bar ~children:[] ())[@JSX ])
;;foo #= ((bar ~children:[] ())[@JSX ])
let x = [|((div ~children:[] ())[@JSX ])|]
Expand Down Expand Up @@ -1078,12 +1078,11 @@ let _ =
[@ns.braces ])] ())
[@JSX ])
let _ =
((View.createElement ~style:((styles ## backgroundImageWrapper)
((View.createElement ~style:((styles#backgroundImageWrapper)
[@ns.namedArgLoc ])
~children:[(((let uri = \\"/images/header-background.png\\" in
((Image.createElement ~resizeMode:((Contain)
[@ns.namedArgLoc ])
~style:((styles ## backgroundImage)
[@ns.namedArgLoc ]) ~style:((styles#backgroundImage)
[@ns.namedArgLoc ]) ~uri:((uri)[@ns.namedArgLoc ])
~children:[] ())
[@JSX ])))
Expand Down Expand Up @@ -1215,13 +1214,13 @@ let x = (arr.((x : int))).((y : int))
[@ns.namedArgLoc ]) ~b:((bArg)[@ns.namedArgLoc ]) ?c:((c)
[@ns.namedArgLoc ]) ?d:((expr)[@ns.namedArgLoc ])
;;f ~a:(((x : int))[@ns.namedArgLoc ]) ?b:(((y : int))[@ns.namedArgLoc ])
;;connection ## platformId
;;((connection ## left) ## account) ## accountName
;;(john ## age) #= 99
;;((john ## son) ## age) #= ((steve ## age) - 5)
;;(dict ##
) #= abc
;;(dict ## \\") #= (dict2 ## \\")"
;;connection#platformId
;;((connection#left)#account)#accountName
;;john#age #= 99
;;(john#son)#age #= (steve#age - 5)
;;dict#
#= abc
;;dict#\\" #= dict2#\\""
`;

exports[`record.res 1`] = `
Expand Down
11 changes: 5 additions & 6 deletions tests/parsing/grammar/expressions/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ let icon =
[@JSX ])
let _ =
((MessengerSharedPhotosAlbumViewPhotoReact.createElement
?ref:((if (foo ## bar) == baz
?ref:((if foo#bar == baz
then Some (foooooooooooooooooooooooo setRefChild)
else None)[@ns.namedArgLoc ][@ns.ternary ])
~key:((node ## legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
~key:((node#legacy_attachment_id)[@ns.namedArgLoc ]) ~children:[] ())
[@JSX ])
let _ = ((Foo.createElement ~bar:((bar)[@ns.namedArgLoc ]) ~children:[] ())
[@JSX ])
Expand All @@ -234,7 +234,7 @@ let _ =
[@JSX ])
let x = ((div ~children:[] ())[@JSX ])
let _ = ((div ~asd:((1)[@ns.namedArgLoc ]) ~children:[] ())[@JSX ])
;;(foo ## bar) #= ((bar ~children:[] ())[@JSX ])
;;foo#bar #= ((bar ~children:[] ())[@JSX ])
;;foo #= ((bar ~children:[] ())[@JSX ])
;;foo #= ((bar ~children:[] ())[@JSX ])
let x = [|((div ~children:[] ())[@JSX ])|]
Expand Down Expand Up @@ -520,12 +520,11 @@ let _ =
[@ns.braces ])] ())
[@JSX ])
let _ =
((View.createElement ~style:((styles ## backgroundImageWrapper)
((View.createElement ~style:((styles#backgroundImageWrapper)
[@ns.namedArgLoc ])
~children:[(((let uri = "/images/header-background.png" in
((Image.createElement ~resizeMode:((Contain)
[@ns.namedArgLoc ])
~style:((styles ## backgroundImage)
[@ns.namedArgLoc ]) ~style:((styles#backgroundImage)
[@ns.namedArgLoc ]) ~uri:((uri)[@ns.namedArgLoc ])
~children:[] ())
[@JSX ])))
Expand Down
14 changes: 7 additions & 7 deletions tests/parsing/grammar/expressions/expected/primary.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ let x = (arr.((x : int))).((y : int))
[@ns.namedArgLoc ]) ~b:((bArg)[@ns.namedArgLoc ]) ?c:((c)
[@ns.namedArgLoc ]) ?d:((expr)[@ns.namedArgLoc ])
;;f ~a:(((x : int))[@ns.namedArgLoc ]) ?b:(((y : int))[@ns.namedArgLoc ])
;;connection ## platformId
;;((connection ## left) ## account) ## accountName
;;(john ## age) #= 99
;;((john ## son) ## age) #= ((steve ## age) - 5)
;;(dict ##
) #= abc
;;(dict ## ") #= (dict2 ## ")
;;connection#platformId
;;((connection#left)#account)#accountName
;;john#age #= 99
;;(john#son)#age #= (steve#age - 5)
;;dict#
#= abc
;;dict#" #= dict2#"