Skip to content

Commit 00cb645

Browse files
authored
GenType: support @deriving(accessors) (#6537)
* GenType: support `@deriving(accessors)` outputs * [skip ci] changelog * changed to not recycle loc unintentinonlly
1 parent dbe3a58 commit 00cb645

File tree

11 files changed

+93
-14
lines changed

11 files changed

+93
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 11.0.0-rc.9 (Unreleased)
1414

15+
#### :rocket: New Feature
16+
17+
- GenType: support `@deriving(accessors)` outputs. https://github.com/rescript-lang/rescript-compiler/pull/6537
18+
1519
# 11.0.0-rc.8
1620

1721
#### :rocket: New Feature

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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ let init () =
1818
let core_type =
1919
Ast_derive_util.core_type_of_type_declaration tdcl
2020
in
21+
let gentype_attrs =
22+
match
23+
Ext_list.exists core_type.ptyp_attributes
24+
Ast_attributes.is_gentype
25+
with
26+
| true -> Some [Ast_attributes.gentype]
27+
| false -> None
28+
in
2129
match tdcl.ptype_kind with
2230
| Ptype_record label_declarations ->
2331
Ext_list.map label_declarations
@@ -26,7 +34,7 @@ let init () =
2634
Parsetree.label_declaration)
2735
->
2836
let txt = "param" in
29-
Ast_comb.single_non_rec_value pld_name
37+
Ast_comb.single_non_rec_value ?attrs:gentype_attrs pld_name
3038
(Ast_compatible.fun_
3139
(Pat.constraint_ (Pat.var {txt; loc}) core_type)
3240
(Exp.field
@@ -57,7 +65,7 @@ let init () =
5765
| None -> core_type
5866
| Some x -> x
5967
in
60-
Ast_comb.single_non_rec_value
68+
Ast_comb.single_non_rec_value ?attrs:gentype_attrs
6169
{loc; txt = little_con_name}
6270
(if arity = 0 then
6371
(*TODO: add a prefix, better inter-op with FFI *)
@@ -99,10 +107,18 @@ let init () =
99107
let core_type =
100108
Ast_derive_util.core_type_of_type_declaration tdcl
101109
in
110+
let gentype_attrs =
111+
match
112+
Ext_list.exists core_type.ptyp_attributes
113+
Ast_attributes.is_gentype
114+
with
115+
| true -> Some [Ast_attributes.gentype]
116+
| false -> None
117+
in
102118
match tdcl.ptype_kind with
103119
| Ptype_record label_declarations ->
104120
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
105-
Ast_comb.single_non_rec_val pld_name
121+
Ast_comb.single_non_rec_val ?attrs:gentype_attrs pld_name
106122
(Ast_compatible.arrow core_type pld_type))
107123
| Ptype_variant constructor_declarations ->
108124
Ext_list.map constructor_declarations
@@ -124,7 +140,7 @@ let init () =
124140
| Some x -> x
125141
| None -> core_type
126142
in
127-
Ast_comb.single_non_rec_val
143+
Ast_comb.single_non_rec_val ?attrs:gentype_attrs
128144
{loc; txt = Ext_string.uncapitalize_ascii con_name}
129145
(Ext_list.fold_right pcd_args annotate_type (fun x acc ->
130146
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)