Skip to content

Commit 0471d19

Browse files
committed
napkin: revert the rest of upstream PR#291
rescript-lang/syntax#291
1 parent 3ce16ca commit 0471d19

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

jscomp/napkin/res_ast_conversion.ml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,6 @@ let normalize =
359359
| _ ->
360360
default_mapper.pat mapper p
361361
end;
362-
typ = (fun mapper typ ->
363-
match typ.ptyp_desc with
364-
| Ptyp_constr({txt = Longident.Ldot(Longident.Lident "Js", "t")}, [arg]) ->
365-
(* Js.t({"a": b}) -> {"a": b}
366-
Since compiler >9.0.1 objects don't need Js.t wrapping anymore *)
367-
mapper.typ mapper arg
368-
| _ -> default_mapper.typ mapper typ
369-
);
370362
expr = (fun mapper expr ->
371363
match expr.pexp_desc with
372364
| Pexp_constant (Pconst_string (txt, None)) ->

jscomp/napkin/res_core.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ let makeListPattern loc seq ext_opt =
375375
Ast_helper.Pat.mk ~loc (Ppat_construct(Location.mkloc (Longident.Lident "::") loc, Some arg))
376376
in
377377
handle_seq seq
378+
(* {"foo": bar} -> Js.t({. foo: bar})
379+
* {.. "foo": bar} -> Js.t({.. foo: bar})
380+
* {..} -> Js.t({..}) *)
381+
let makeBsObjType ~attrs ~loc ~closed rows =
382+
let obj = Ast_helper.Typ.object_ ~loc rows closed in
383+
let jsDotTCtor =
384+
Location.mkloc (Longident.Ldot (Longident.Lident "Js", "t")) loc
385+
in
386+
Ast_helper.Typ.constr ~loc ~attrs jsDotTCtor [obj]
378387

379388
(* TODO: diagnostic reporting *)
380389
let lidentOfPath longident =
@@ -3806,7 +3815,7 @@ and parseRecordOrBsObjectType ~attrs p =
38063815
in
38073816
Parser.expect Rbrace p;
38083817
let loc = mkLoc startPos p.prevEndPos in
3809-
Ast_helper.Typ.object_ ~loc ~attrs fields closedFlag
3818+
makeBsObjType ~attrs ~loc ~closed:closedFlag fields
38103819

38113820
(* TODO: check associativity in combination with attributes *)
38123821
and parseTypeAlias p typ =
@@ -4208,7 +4217,7 @@ and parseConstrDeclArgs p =
42084217
in
42094218
Parser.expect Rbrace p;
42104219
let loc = mkLoc startPos p.prevEndPos in
4211-
let typ = Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag in
4220+
let typ = makeBsObjType ~attrs:[] ~loc ~closed:closedFlag fields in
42124221
Parser.optional p Comma |> ignore;
42134222
let moreArgs =
42144223
parseCommaDelimitedRegion
@@ -4259,7 +4268,7 @@ and parseConstrDeclArgs p =
42594268
) in
42604269
Parser.expect Rbrace p;
42614270
let loc = mkLoc startPos p.prevEndPos in
4262-
let typ = Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag in
4271+
let typ = makeBsObjType ~attrs:[] ~loc ~closed:closedFlag fields in
42634272
Parser.optional p Comma |> ignore;
42644273
let moreArgs =
42654274
parseCommaDelimitedRegion
@@ -4591,7 +4600,7 @@ and parseRecordOrBsObjectDecl p =
45914600
Parser.expect Rbrace p;
45924601
let loc = mkLoc startPos p.prevEndPos in
45934602
let typ =
4594-
Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag
4603+
makeBsObjType ~attrs:[] ~loc ~closed:closedFlag fields
45954604
|> parseTypeAlias p
45964605
in
45974606
let typ = parseArrowTypeRest ~es6Arrow:true ~startPos typ p in
@@ -4638,7 +4647,7 @@ and parseRecordOrBsObjectDecl p =
46384647
Parser.expect Rbrace p;
46394648
let loc = mkLoc startPos p.prevEndPos in
46404649
let typ =
4641-
Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag |> parseTypeAlias p
4650+
makeBsObjType ~attrs:[] ~loc ~closed:closedFlag fields |> parseTypeAlias p
46424651
in
46434652
let typ = parseArrowTypeRest ~es6Arrow:true ~startPos typ p in
46444653
(Some typ, Asttypes.Public, Parsetree.Ptype_abstract)

jscomp/napkin/res_printer.ml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,9 +1468,17 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
14681468
in
14691469
Doc.concat [typ; Doc.text " as "; Doc.concat [Doc.text "'"; printIdentLike alias]]
14701470

1471+
| Ptyp_constr({txt = Longident.Ldot(Longident.Lident "Js", "t")}, [{ptyp_desc = Ptyp_object (_fields, _openFlag)} as typ]) ->
1472+
let bsObject = printTypExpr typ cmtTbl in
1473+
begin match typExpr.ptyp_attributes with
1474+
| [] -> bsObject
1475+
| attrs ->
1476+
Doc.concat [
1477+
printAttributes ~inline:true attrs cmtTbl;
1478+
printTypExpr typ cmtTbl;
1479+
]
1480+
end
14711481
(* object printings *)
1472-
| Ptyp_object (fields, openFlag) ->
1473-
printObject ~inline:false fields openFlag cmtTbl
14741482
| Ptyp_constr(longidentLoc, [{ptyp_desc = Ptyp_object (fields, openFlag)}]) ->
14751483
(* for foo<{"a": b}>, when the object is long and needs a line break, we
14761484
want the <{ and }> to stay hugged together *)
@@ -1496,6 +1504,17 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
14961504
let constrName = printLidentPath longidentLoc cmtTbl in
14971505
begin match constrArgs with
14981506
| [] -> constrName
1507+
| [{
1508+
Parsetree.ptyp_desc =
1509+
Ptyp_constr({txt = Longident.Ldot(Longident.Lident "Js", "t")},
1510+
[{ptyp_desc = Ptyp_object (fields, openFlag)}])
1511+
}] ->
1512+
Doc.concat([
1513+
constrName;
1514+
Doc.lessThan;
1515+
printObject ~inline:true fields openFlag cmtTbl;
1516+
Doc.greaterThan;
1517+
])
14991518
| _args -> Doc.group(
15001519
Doc.concat([
15011520
constrName;
@@ -1599,6 +1618,8 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
15991618
)
16001619
end
16011620
| Ptyp_tuple types -> printTupleType ~inline:false types cmtTbl
1621+
| Ptyp_object (fields, openFlag) ->
1622+
printObject ~inline:false fields openFlag cmtTbl
16021623
| Ptyp_poly([], typ) ->
16031624
printTypExpr typ cmtTbl
16041625
| Ptyp_poly(stringLocs, typ) ->

0 commit comments

Comments
 (0)