Skip to content

Commit 6c29596

Browse files
committed
GenType: support @deriving(accessors)
1 parent e9fa3ed commit 6c29596

File tree

10 files changed

+81
-14
lines changed

10 files changed

+81
-14
lines changed

jscomp/frontend/ast_attributes.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,10 @@ let bs_return_undefined : attr =
373373
pstr_loc = locg;
374374
};
375375
] )
376+
377+
let is_gentype (attr : attr) =
378+
match attr with
379+
| {Location.txt = "genType" | "gentype"; _}, _ -> true
380+
| _ -> false
381+
382+
let gentype : attr = ({txt = "genType"; loc = locg}, Ast_payload.empty)

jscomp/frontend/ast_attributes.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,7 @@ val internal_expansive : attr
9090
val rs_externals : t -> string list -> bool
9191

9292
val process_send_pipe : t -> (Parsetree.core_type * t) option
93+
94+
val is_gentype : attr -> bool
95+
96+
val gentype : attr

jscomp/frontend/ast_comb.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ let to_js_re_type loc = Typ.constr ~loc {txt = re_id; loc} []
6262
let to_undefined_type loc x =
6363
Typ.constr ~loc {txt = Ast_literal.Lid.js_undefined; loc} [x]
6464

65-
let single_non_rec_value name exp =
66-
Str.value Nonrecursive [Vb.mk (Pat.var name) exp]
65+
let single_non_rec_value ?(attrs = []) name exp =
66+
Str.value Nonrecursive [Vb.mk ~attrs (Pat.var name) exp]
6767

68-
let single_non_rec_val name ty = Sig.value (Val.mk name ty)
68+
let single_non_rec_val ?(attrs = []) name ty = Sig.value (Val.mk ~attrs name ty)

jscomp/frontend/ast_comb.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ val to_undefined_type : Location.t -> Parsetree.core_type -> Parsetree.core_type
4242
val to_js_re_type : Location.t -> Parsetree.core_type
4343

4444
val single_non_rec_value :
45-
Ast_helper.str -> Parsetree.expression -> Parsetree.structure_item
45+
?attrs:Parsetree.attributes ->
46+
Ast_helper.str ->
47+
Parsetree.expression ->
48+
Parsetree.structure_item
4649

4750
val single_non_rec_val :
48-
Ast_helper.str -> Parsetree.core_type -> Parsetree.signature_item
51+
?attrs:Parsetree.attributes ->
52+
Ast_helper.str ->
53+
Parsetree.core_type ->
54+
Parsetree.signature_item

jscomp/frontend/ast_derive_projector.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ let init () =
1818
let core_type =
1919
Ast_derive_util.core_type_of_type_declaration tdcl
2020
in
21+
let gentype_attrs =
22+
Ext_list.filter core_type.ptyp_attributes
23+
Ast_attributes.is_gentype
24+
in
2125
match tdcl.ptype_kind with
2226
| Ptype_record label_declarations ->
2327
Ext_list.map label_declarations
@@ -26,7 +30,7 @@ let init () =
2630
Parsetree.label_declaration)
2731
->
2832
let txt = "param" in
29-
Ast_comb.single_non_rec_value pld_name
33+
Ast_comb.single_non_rec_value ~attrs:gentype_attrs pld_name
3034
(Ast_compatible.fun_
3135
(Pat.constraint_ (Pat.var {txt; loc}) core_type)
3236
(Exp.field
@@ -57,7 +61,7 @@ let init () =
5761
| None -> core_type
5862
| Some x -> x
5963
in
60-
Ast_comb.single_non_rec_value
64+
Ast_comb.single_non_rec_value ~attrs:gentype_attrs
6165
{loc; txt = little_con_name}
6266
(if arity = 0 then
6367
(*TODO: add a prefix, better inter-op with FFI *)
@@ -99,10 +103,14 @@ let init () =
99103
let core_type =
100104
Ast_derive_util.core_type_of_type_declaration tdcl
101105
in
106+
let gentype_attrs =
107+
Ext_list.filter core_type.ptyp_attributes
108+
Ast_attributes.is_gentype
109+
in
102110
match tdcl.ptype_kind with
103111
| Ptype_record label_declarations ->
104112
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
105-
Ast_comb.single_non_rec_val pld_name
113+
Ast_comb.single_non_rec_val ~attrs:gentype_attrs pld_name
106114
(Ast_compatible.arrow core_type pld_type))
107115
| Ptype_variant constructor_declarations ->
108116
Ext_list.map constructor_declarations
@@ -124,7 +132,7 @@ let init () =
124132
| Some x -> x
125133
| None -> core_type
126134
in
127-
Ast_comb.single_non_rec_val
135+
Ast_comb.single_non_rec_val ~attrs:gentype_attrs
128136
{loc; txt = Ext_string.uncapitalize_ascii con_name}
129137
(Ext_list.fold_right pcd_args annotate_type (fun x acc ->
130138
Ast_compatible.arrow x acc)))

jscomp/frontend/ast_derive_util.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ open Ast_helper
2626

2727
let core_type_of_type_declaration (tdcl : Parsetree.type_declaration) =
2828
match tdcl with
29-
| {ptype_name = {txt; loc}; ptype_params} ->
30-
Typ.constr {txt = Lident txt; loc} (Ext_list.map ptype_params fst)
29+
| {ptype_name = {txt; loc}; ptype_params; ptype_attributes = attrs} ->
30+
Typ.constr ~attrs {txt = Lident txt; loc} (Ext_list.map ptype_params fst)
3131

3232
let new_type_of_type_declaration (tdcl : Parsetree.type_declaration) newName =
3333
match tdcl with

jscomp/gentype_tests/typescript-react-example/src/Derivings.bs.js

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* TypeScript file generated from Derivings.res by genType. */
2+
3+
/* eslint-disable */
4+
/* tslint:disable */
5+
6+
import * as DerivingsBS from './Derivings.bs';
7+
8+
export type action =
9+
"Click"
10+
| "Cancel"
11+
| { TAG: "Submit"; _0: string };
12+
13+
export const click: action = DerivingsBS.click as any;
14+
15+
export const submit: (_1:string) => action = DerivingsBS.submit as any;
16+
17+
export const cancel: action = DerivingsBS.cancel as any;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@genType @deriving(accessors)
2+
type action =
3+
| Click
4+
| Submit(string)
5+
| Cancel

jscomp/runtime/release.ninja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.res | runtime/caml_bytes.cm
2121
o runtime/caml_bytes.cmi : cc runtime/caml_bytes.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2222
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2323
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
24-
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
24+
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
2525
o runtime/caml_format.cmi : cc runtime/caml_format.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2626
o runtime/caml_hash.cmj : cc_cmi runtime/caml_hash.res | runtime/caml_hash.cmi runtime/caml_hash_primitive.cmj runtime/caml_nativeint_extern.cmj runtime/js.cmj
2727
o runtime/caml_hash.cmi : cc runtime/caml_hash.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -37,7 +37,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.res | runtime/caml_array_extern
3737
o runtime/caml_md5.cmi : cc runtime/caml_md5.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3838
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.res | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
3939
o runtime/caml_module.cmi : cc runtime/caml_module.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
40-
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
40+
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml.cmj runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
4141
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
4242
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
4343
o runtime/caml_option.cmi : cc runtime/caml_option.resi | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
@@ -54,7 +54,7 @@ o runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exce
5454
o runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5555
o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5656
o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
57-
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/js.cmi runtime/js.cmj
57+
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/caml_option.cmj runtime/js.cmi runtime/js.cmj
5858
o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runtime/caml_nativeint_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5959
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6060
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj

0 commit comments

Comments
 (0)