Skip to content

Commit

Permalink
Remove support for @deriving(jsConverter)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoc committed Mar 24, 2023
1 parent c1bff5e commit 639fcfd
Show file tree
Hide file tree
Showing 13 changed files with 9 additions and 475 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ These are only breaking changes for unformatted code.
- Exponentiation operator `**` is now right-associative. `2. ** 3. ** 2.` now compile to `Math.pow(2, Math.pow(3, 2))` and not anymore `Math.pow(Math.pow(2, 3), 2)`. Parentheses can be used to change precedence.
- Remove unsafe ``` j`$(a)$(b)` ``` interpolation deprecated in compiler version 10 https://github.com/rescript-lang/rescript-compiler/pull/6068
- Remove deprecated module `Printexc`
- `@deriving(jsConverter)` not supported anymore for variant types https://github.com/rescript-lang/rescript-compiler/pull/6088

#### :bug: Bug Fix

Expand Down
1 change: 0 additions & 1 deletion jscomp/build_tests/white space/yy.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
type t =
| Foo
[@@bs.deriving jsConverter]

let u = Xx.sum 3

2 changes: 1 addition & 1 deletion jscomp/ext/warnings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ let message = function
| Bs_polymorphic_comparison ->
"Polymorphic comparison introduced (maybe unsafe)"
| Bs_ffi_warning s -> "FFI warning: " ^ s
| Bs_derive_warning s -> "bs.deriving warning: " ^ s
| Bs_derive_warning s -> "@deriving warning: " ^ s
| Bs_fragile_external s ->
s
^ " : using an empty string as a shorthand to infer the external's name \
Expand Down
145 changes: 6 additions & 139 deletions jscomp/frontend/ast_derive_js_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,49 +127,17 @@ let app1 = Ast_compatible.app1

let app2 = Ast_compatible.app2

let app3 = Ast_compatible.app3

let ( <=~ ) a b = app2 (Exp.ident { loc = noloc; txt = Lident "<=" }) a b

let ( -~ ) a b =
app2 (Exp.ident { loc = noloc; txt = Ldot (Lident "Pervasives", "-") }) a b

let ( +~ ) a b =
app2 (Exp.ident { loc = noloc; txt = Ldot (Lident "Pervasives", "+") }) a b

let ( &&~ ) a b =
app2 (Exp.ident { loc = noloc; txt = Ldot (Lident "Pervasives", "&&") }) a b

let ( ->~ ) a b = Ast_compatible.arrow a b

let jsMapperRt = Longident.Ldot (Lident "Js", "MapperRt")

let fromInt len array exp =
app3
(Exp.ident { loc = noloc; txt = Longident.Ldot (jsMapperRt, "fromInt") })
len array exp

let fromIntAssert len array exp =
app3
(Exp.ident
{ loc = noloc; txt = Longident.Ldot (jsMapperRt, "fromIntAssert") })
len array exp

let raiseWhenNotFound x =
app1
(Exp.ident
{ loc = noloc; txt = Longident.Ldot (jsMapperRt, "raiseWhenNotFound") })
x

let assertExp e = Exp.assert_ e

let derivingName = "jsConverter"

(* let notApplicable loc =
Location.prerr_warning
loc
(Warnings.Bs_derive_warning ( derivingName ^ " not applicable to this type")) *)

let init () =
Ast_derive.register derivingName (fun (x : Parsetree.expression option) ->
let createType = handle_config x in
Expand All @@ -182,7 +150,6 @@ let init () =
let name = tdcl.ptype_name.txt in
let toJs = name ^ "ToJs" in
let fromJs = name ^ "FromJs" in
let constantArray = "jsMapperConstantArray" in
let loc = tdcl.ptype_loc in
let patToJs = { Asttypes.loc; txt = toJs } in
let patFromJs = { Asttypes.loc; txt = fromJs } in
Expand Down Expand Up @@ -302,95 +269,9 @@ let init () =
| None ->
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[])
| Ptype_variant ctors ->
if Ast_polyvar.is_enum_constructors ctors then
let xs =
Ast_polyvar.map_constructor_declarations_into_ints ctors
in
match xs with
| `New xs ->
let constantArrayExp =
Exp.ident { loc; txt = Lident constantArray }
in
let exp_len =
Ast_compatible.const_exp_int (List.length ctors)
in
let v =
[
unsafeIndexGet;
eraseTypeStr;
Ast_comb.single_non_rec_value
{ loc; txt = constantArray }
(Ast_compatible.const_exp_int_list_as_array xs);
toJsBody
(app2 unsafeIndexGetExp constantArrayExp exp_param);
Ast_comb.single_non_rec_value patFromJs
(Ast_compatible.fun_ (Pat.var pat_param)
(if createType then
fromIntAssert exp_len constantArrayExp
(exp_param +: newType)
+> core_type
else
fromInt exp_len constantArrayExp exp_param
+> Ast_core_type.lift_option_type core_type));
]
in
if createType then newTypeStr :: v else v
| `Offset offset ->
let v =
[
eraseTypeStr;
toJsBody
(coerceResultToNewType
(eraseType exp_param
+~ Ast_compatible.const_exp_int offset));
(let len = List.length ctors in
let range_low =
Ast_compatible.const_exp_int (offset + 0)
in
let range_upper =
Ast_compatible.const_exp_int (offset + len - 1)
in

Ast_comb.single_non_rec_value { loc; txt = fromJs }
(Ast_compatible.fun_ (Pat.var pat_param)
(if createType then
Exp.let_ Nonrecursive
[
Vb.mk (Pat.var pat_param)
(exp_param +: newType);
]
(Exp.sequence
(assertExp
(exp_param <=~ range_upper
&&~ (range_low <=~ exp_param)))
(exp_param
-~ Ast_compatible.const_exp_int offset))
+> core_type
else
Exp.ifthenelse
(exp_param <=~ range_upper
&&~ (range_low <=~ exp_param))
(Exp.construct
{ loc; txt = Ast_literal.predef_some }
(Some
(exp_param
-~ Ast_compatible.const_exp_int
offset)))
(Some
(Exp.construct
{
loc;
txt = Ast_literal.predef_none;
}
None))
+> Ast_core_type.lift_option_type core_type)));
]
in
if createType then newTypeStr :: v else v
else (
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[])
| Ptype_variant _ ->
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[]
| Ptype_open ->
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[]
Expand Down Expand Up @@ -452,23 +333,9 @@ let init () =
| None ->
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[])
| Ptype_variant ctors ->
if Ast_polyvar.is_enum_constructors ctors then
let ty1 =
if createType then newType else Ast_literal.type_int ()
in
let ty2 =
if createType then core_type
else Ast_core_type.lift_option_type core_type
in
newTypeStr
+? [
toJsType ty1;
Ast_comb.single_non_rec_val patFromJs (ty1 ->~ ty2);
]
else (
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[])
| Ptype_variant _ ->
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[]
| Ptype_open ->
U.notApplicable tdcl.Parsetree.ptype_loc derivingName;
[]
Expand Down
22 changes: 0 additions & 22 deletions jscomp/frontend/ast_derive_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,6 @@ let new_type_of_type_declaration (tdcl : Parsetree.type_declaration) newName =
ptype_private = Public;
ptype_manifest = None;
} )

(* let mk_fun ~loc (typ : Parsetree.core_type)
(value : string) body
: Parsetree.expression =
Ast_compatible.fun_
(Pat.constraint_ (Pat.var {txt = value ; loc}) typ)
body
let destruct_label_declarations ~loc
(arg_name : string)
(labels : Parsetree.label_declaration list) :
(Parsetree.core_type * Parsetree.expression) list * string list
=
Ext_list.fold_right labels ([], [])
(fun {pld_name = {txt}; pld_type}
(core_type_exps, labels) ->
((pld_type,
Exp.field (Exp.ident {txt = Lident arg_name ; loc})
{txt = Lident txt ; loc}) :: core_type_exps),
txt :: labels
) *)

let notApplicable loc derivingName =
Location.prerr_warning loc
(Warnings.Bs_derive_warning (derivingName ^ " not applicable to this type"))
Expand Down
13 changes: 0 additions & 13 deletions jscomp/frontend/ast_polyvar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,3 @@ let is_enum_polyvar (ty : Parsetree.type_declaration) =
when is_enum row_fields ->
Some row_fields
| _ -> None

let is_enum_constructors (constructors : Parsetree.constructor_declaration list)
=
List.for_all
(fun (x : Parsetree.constructor_declaration) ->
match x with
| {
pcd_args =
Pcstr_tuple [] (* Note the enum is encoded using [Pcstr_tuple []]*);
} ->
true
| _ -> false)
constructors
2 changes: 0 additions & 2 deletions jscomp/frontend/ast_polyvar.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ val map_row_fields_into_strings :

val is_enum_polyvar :
Parsetree.type_declaration -> Parsetree.row_field list option

val is_enum_constructors : Parsetree.constructor_declaration list -> bool
87 changes: 0 additions & 87 deletions jscomp/test/ast_js_mapper_test.js

This file was deleted.

0 comments on commit 639fcfd

Please sign in to comment.