Skip to content

Commit 750d667

Browse files
committed
refactor bs.attributes in arguments
1 parent 8a31d02 commit 750d667

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

jscomp/syntax/ast_attributes.ml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,38 @@ let iter_process_derive_type attrs =
190190
!st
191191

192192

193-
let process_bs_string_int_unwrap_uncurry attrs =
193+
(* duplicated [bs.uncurry] [bs.string] not allowed,
194+
it is worse in bs.uncurry since it will introduce
195+
inconsistency in arity
196+
*)
197+
let iter_process_bs_string_int_unwrap_uncurry attrs =
198+
let st = ref `Nothing in
199+
let assign v (({loc;_}, _ ) as attr : attr) =
200+
if !st = `Nothing then
201+
begin
202+
Bs_ast_invariant.mark_used_bs_attribute attr;
203+
st := v ;
204+
end
205+
else Bs_syntaxerr.err loc Conflict_attributes in
206+
List.iter
207+
(fun (({txt ; loc}, (payload : _ ) ) as attr : attr) ->
208+
match txt with
209+
| "bs.string"
210+
-> assign `String attr
211+
| "bs.int"
212+
-> assign `Int attr
213+
| "bs.ignore"
214+
-> assign `Ignore attr
215+
| "bs.unwrap"
216+
-> assign `Unwrap attr
217+
| "bs.uncurry"
218+
->
219+
assign (`Uncurry (Ast_payload.is_single_int payload)) attr
220+
| _ -> ()
221+
) attrs;
222+
!st
223+
224+
(* let process_bs_string_int_unwrap_uncurry attrs =
194225
List.fold_left
195226
(fun (st,attrs)
196227
(({txt ; loc}, (payload : _ ) ) as attr : attr) ->
@@ -216,7 +247,7 @@ let process_bs_string_int_unwrap_uncurry attrs =
216247
->
217248
Bs_syntaxerr.err loc Conflict_attributes
218249
| _ , _ -> st, (attr :: attrs )
219-
) (`Nothing, []) attrs
250+
) (`Nothing, []) attrs *)
220251

221252

222253
let iter_process_bs_string_as (attrs : t) : string option =

jscomp/syntax/ast_attributes.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ type derive_attr = {
4646
explict_nonrec : bool;
4747
bs_deriving : Ast_payload.action list option
4848
}
49-
val process_bs_string_int_unwrap_uncurry :
50-
t -> [`Nothing | `String | `Int | `Ignore | `Unwrap | `Uncurry of int option ] * t
49+
50+
51+
val iter_process_bs_string_int_unwrap_uncurry :
52+
t ->
53+
[`Nothing | `String | `Int | `Ignore | `Unwrap | `Uncurry of int option ]
5154

5255

5356
val iter_process_bs_string_as :

jscomp/syntax/external_process.ml

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525

2626
[@@@ocaml.warning "+9"]
27+
(* record pattern match complete checker*)
2728

2829

29-
30-
let variant_can_bs_unwrap_fields row_fields =
30+
let variant_can_bs_unwrap_fields (row_fields : Parsetree.row_field list) : bool =
3131
let validity =
3232
List.fold_left
3333
begin fun st row ->
@@ -60,7 +60,8 @@ let variant_can_bs_unwrap_fields row_fields =
6060
]}
6161
The result type would be [ hi:string ]
6262
*)
63-
let get_arg_type ~nolabel optional
63+
let get_arg_type
64+
~nolabel optional
6465
(ptyp : Ast_core_type.t) :
6566
External_arg_spec.attr * Ast_core_type.t =
6667
let ptyp =
@@ -71,12 +72,8 @@ let get_arg_type ~nolabel optional
7172
if optional then
7273
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external
7374
else begin
74-
let ptyp_attrs =
75-
ptyp.Parsetree.ptyp_attributes
76-
in
77-
let result =
78-
Ast_attributes.iter_process_bs_string_or_int_as ptyp_attrs
79-
in
75+
let ptyp_attrs = ptyp.ptyp_attributes in
76+
let result = Ast_attributes.iter_process_bs_string_or_int_as ptyp_attrs in
8077
(* when ppx start dropping attributes
8178
we should warn, there is a trade off whether
8279
we should warn dropped non bs attribute or not
@@ -85,7 +82,6 @@ let get_arg_type ~nolabel optional
8582
match result with
8683
| None ->
8784
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external
88-
8985
| Some (`Int i) ->
9086
Arg_cst(External_arg_spec.cst_int i), Ast_literal.type_int ~loc:ptyp.ptyp_loc ()
9187
| Some (`Str i)->
@@ -97,44 +93,34 @@ let get_arg_type ~nolabel optional
9793
end
9894
else (* ([`a|`b] [@bs.string]) *)
9995
let ptyp_desc = ptyp.ptyp_desc in
100-
match Ast_attributes.process_bs_string_int_unwrap_uncurry ptyp.ptyp_attributes with
101-
| (`String, ptyp_attributes)
102-
->
96+
(match Ast_attributes.iter_process_bs_string_int_unwrap_uncurry ptyp.ptyp_attributes with
97+
| `String ->
10398
begin match ptyp_desc with
10499
| Ptyp_variant ( row_fields, Closed, None)
105-
->
106-
let attr =
107-
Ast_polyvar.map_row_fields_into_strings ptyp.ptyp_loc row_fields in
108-
attr,
109-
{ptyp with
110-
ptyp_attributes
111-
}
100+
->
101+
Ast_polyvar.map_row_fields_into_strings ptyp.ptyp_loc row_fields
112102
| _ ->
113103
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_string_type
114104
end
115-
| (`Ignore, ptyp_attributes) ->
116-
(Ignore, {ptyp with ptyp_attributes})
117-
| (`Int , ptyp_attributes) ->
105+
| `Ignore ->
106+
Ignore
107+
| `Int ->
118108
begin match ptyp_desc with
119109
| Ptyp_variant ( row_fields, Closed, None) ->
120110
let int_lists =
121111
Ast_polyvar.map_row_fields_into_ints ptyp.ptyp_loc row_fields in
122-
Int int_lists ,
123-
{ptyp with
124-
ptyp_attributes
125-
}
112+
Int int_lists
126113
| _ -> Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_int_type
127114
end
128-
| (`Unwrap, ptyp_attributes) ->
129-
115+
| `Unwrap ->
130116
begin match ptyp_desc with
131-
| (Ptyp_variant (row_fields, Closed, _) as ptyp_desc)
117+
| Ptyp_variant (row_fields, Closed, _)
132118
when variant_can_bs_unwrap_fields row_fields ->
133-
Unwrap, {ptyp with ptyp_desc; ptyp_attributes}
119+
Unwrap
134120
| _ ->
135121
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_unwrap_type
136122
end
137-
| (`Uncurry opt_arity, ptyp_attributes) ->
123+
| `Uncurry opt_arity ->
138124
let real_arity = Ast_core_type.get_uncurry_arity ptyp in
139125
(begin match opt_arity, real_arity with
140126
| Some arity, `Not_function ->
@@ -147,9 +133,8 @@ let get_arg_type ~nolabel optional
147133
if n <> arity then
148134
Bs_syntaxerr.err ptyp.ptyp_loc (Inconsistent_arity (arity,n))
149135
else Fn_uncurry_arity arity
150-
151-
end, {ptyp with ptyp_attributes})
152-
| (`Nothing, ptyp_attributes) ->
136+
end)
137+
| `Nothing ->
153138
begin match ptyp_desc with
154139
| Ptyp_constr ({txt = Lident "unit"; _}, [])
155140
-> if nolabel then Extern_unit else Nothing
@@ -160,7 +145,7 @@ let get_arg_type ~nolabel optional
160145
Nothing
161146
| _ ->
162147
Nothing
163-
end, ptyp
148+
end), ptyp
164149

165150

166151

0 commit comments

Comments
 (0)