Skip to content

GenType: support @deriving(accessors) #6537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 22, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 11.0.0-rc.9 (Unreleased)

#### :rocket: New Feature

- GenType: support `@deriving(accessors)` outputs. https://github.com/rescript-lang/rescript-compiler/pull/6537

# 11.0.0-rc.8

#### :rocket: New Feature
Expand Down
7 changes: 7 additions & 0 deletions jscomp/frontend/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,10 @@ let bs_return_undefined : attr =
pstr_loc = locg;
};
] )

let is_gentype (attr : attr) =
match attr with
| {Location.txt = "genType" | "gentype"; _}, _ -> true
| _ -> false

let gentype : attr = ({txt = "genType"; loc = locg}, Ast_payload.empty)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is not used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh right. I think it's better to use it instead of copying the original attrs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. can you push the changelog commit again? I forgot pull.

4 changes: 4 additions & 0 deletions jscomp/frontend/ast_attributes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ val internal_expansive : attr
val rs_externals : t -> string list -> bool

val process_send_pipe : t -> (Parsetree.core_type * t) option

val is_gentype : attr -> bool

val gentype : attr
6 changes: 3 additions & 3 deletions jscomp/frontend/ast_comb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let to_js_re_type loc = Typ.constr ~loc {txt = re_id; loc} []
let to_undefined_type loc x =
Typ.constr ~loc {txt = Ast_literal.Lid.js_undefined; loc} [x]

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

let single_non_rec_val name ty = Sig.value (Val.mk name ty)
let single_non_rec_val ?(attrs = []) name ty = Sig.value (Val.mk ~attrs name ty)
10 changes: 8 additions & 2 deletions jscomp/frontend/ast_comb.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ val to_undefined_type : Location.t -> Parsetree.core_type -> Parsetree.core_type
val to_js_re_type : Location.t -> Parsetree.core_type

val single_non_rec_value :
Ast_helper.str -> Parsetree.expression -> Parsetree.structure_item
?attrs:Parsetree.attributes ->
Ast_helper.str ->
Parsetree.expression ->
Parsetree.structure_item

val single_non_rec_val :
Ast_helper.str -> Parsetree.core_type -> Parsetree.signature_item
?attrs:Parsetree.attributes ->
Ast_helper.str ->
Parsetree.core_type ->
Parsetree.signature_item
24 changes: 20 additions & 4 deletions jscomp/frontend/ast_derive_projector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ let init () =
let core_type =
Ast_derive_util.core_type_of_type_declaration tdcl
in
let gentype_attrs =
match
Ext_list.exists core_type.ptyp_attributes
Ast_attributes.is_gentype
with
| true -> Some [Ast_attributes.gentype]
| false -> None
in
match tdcl.ptype_kind with
| Ptype_record label_declarations ->
Ext_list.map label_declarations
Expand All @@ -26,7 +34,7 @@ let init () =
Parsetree.label_declaration)
->
let txt = "param" in
Ast_comb.single_non_rec_value pld_name
Ast_comb.single_non_rec_value ?attrs:gentype_attrs pld_name
(Ast_compatible.fun_
(Pat.constraint_ (Pat.var {txt; loc}) core_type)
(Exp.field
Expand Down Expand Up @@ -57,7 +65,7 @@ let init () =
| None -> core_type
| Some x -> x
in
Ast_comb.single_non_rec_value
Ast_comb.single_non_rec_value ?attrs:gentype_attrs
{loc; txt = little_con_name}
(if arity = 0 then
(*TODO: add a prefix, better inter-op with FFI *)
Expand Down Expand Up @@ -99,10 +107,18 @@ let init () =
let core_type =
Ast_derive_util.core_type_of_type_declaration tdcl
in
let gentype_attrs =
match
Ext_list.exists core_type.ptyp_attributes
Ast_attributes.is_gentype
with
| true -> Some [Ast_attributes.gentype]
| false -> None
in
match tdcl.ptype_kind with
| Ptype_record label_declarations ->
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
Ast_comb.single_non_rec_val pld_name
Ast_comb.single_non_rec_val ?attrs:gentype_attrs pld_name
(Ast_compatible.arrow core_type pld_type))
| Ptype_variant constructor_declarations ->
Ext_list.map constructor_declarations
Expand All @@ -124,7 +140,7 @@ let init () =
| Some x -> x
| None -> core_type
in
Ast_comb.single_non_rec_val
Ast_comb.single_non_rec_val ?attrs:gentype_attrs
{loc; txt = Ext_string.uncapitalize_ascii con_name}
(Ext_list.fold_right pcd_args annotate_type (fun x acc ->
Ast_compatible.arrow x acc)))
Expand Down
4 changes: 2 additions & 2 deletions jscomp/frontend/ast_derive_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ open Ast_helper

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

let new_type_of_type_declaration (tdcl : Parsetree.type_declaration) newName =
match tdcl with
Expand Down
20 changes: 20 additions & 0 deletions jscomp/gentype_tests/typescript-react-example/src/Derivings.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* TypeScript file generated from Derivings.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as DerivingsBS from './Derivings.bs';

export type action =
"Click"
| "Cancel"
| { TAG: "Submit"; _0: string };

export const click: action = DerivingsBS.click as any;

export const submit: (_1:string) => action = DerivingsBS.submit as any;

export const cancel: action = DerivingsBS.cancel as any;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@genType @deriving(accessors)
type action =
| Click
| Submit(string)
| Cancel
6 changes: 3 additions & 3 deletions jscomp/runtime/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.res | runtime/caml_bytes.cm
o runtime/caml_bytes.cmi : cc runtime/caml_bytes.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
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
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
o runtime/caml_format.cmi : cc runtime/caml_format.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
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
o runtime/caml_hash.cmi : cc runtime/caml_hash.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
Expand All @@ -37,7 +37,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.res | runtime/caml_array_extern
o runtime/caml_md5.cmi : cc runtime/caml_md5.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.res | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
o runtime/caml_module.cmi : cc runtime/caml_module.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
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
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
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
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
Expand All @@ -54,7 +54,7 @@ o runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exce
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
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
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
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
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
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
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
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
Expand Down