Skip to content

Commit 7a48974

Browse files
authored
Merge pull request #4700 from rescript-lang/clean_up_attributes
smooth transition between bs.inline and inline
2 parents 9968179 + 987ab5d commit 7a48974

21 files changed

+421
-943
lines changed

jscomp/ext/literals.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ let fn_method = "fn_method"
6161
let fn_mk = "fn_mk"
6262
(*let js_fn_runmethod = "js_fn_runmethod"*)
6363

64-
let bs_deriving = "bs.deriving"
65-
let bs_deriving_dot = "bs.deriving."
64+
65+
6666

6767

6868
(** nodejs *)

jscomp/main/js_main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ let _ : unit =
538538
exit 2
539539
| x ->
540540
begin
541-
#if undefined BS_RELEASE_BUILD then
541+
#if false (* undefined BS_RELEASE_BUILD *) then
542542
Ext_obj.bt ();
543543
#end
544544
Location.report_exception ppf x;

jscomp/reserved_attributs.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
3+
This is a list of reserved attributes, we may use such information
4+
to detect unused attributes in the future
5+
6+
7+
get
8+
set
9+
this
10+
meth
11+
open -- not decided
12+
inline --
13+
this would conflict exist one or not?
14+
invalid payload in bs.inline
15+
seems that we need loose the check
16+
deriving
17+
string
18+
int
19+
ignore
20+
unwrap
21+
uncurry
22+
as
23+
optional
24+
get_index
25+
return
26+
config
27+
obj
28+
raw -- not needed
29+
re -- not needed
30+
external -- not needed
31+
time -- not needed
32+
node -- not needed
33+
debugger -- not needed
34+
val
35+
module
36+
scope
37+
splice -- deprecated, use variadic
38+
variadic
39+
send
40+
send.pipe -- deprecated, no short-cut
41+
new
42+
set_index
43+
44+
debugger.chrome -- deprecated
45+
46+
keywords is okay `[@open]` is a valid syntax
47+
48+
mark_used_bs_attribute -
49+
may mark attributes including `inline`

jscomp/syntax/ast_attributes.ml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,16 @@ let external_needs_to_be_encoded (attrs : t)=
134134
(fun {txt} ->
135135
Ext_string.starts_with txt "bs." || txt = Literals.gentype_import)
136136

137-
let has_inline_in_stru (attrs : t) : bool =
138-
Ext_list.exists attrs (fun
139-
(({txt;},_) as attr) ->
140-
if txt = "bs.inline" then
141-
(Bs_ast_invariant.mark_used_bs_attribute attr;
142-
true)
143-
else false)
144-
145-
let has_inline_payload_in_sig (attrs : t) =
146-
Ext_list.find_first attrs
147-
(fun (({txt},_) as attr) ->
148-
if txt = "bs.inline" then
149-
begin
150-
Bs_ast_invariant.mark_used_bs_attribute attr;
151-
true
152-
end
153-
else false
154-
)
137+
let is_inline : attr -> bool =
138+
(fun
139+
(({txt;},_)) ->
140+
txt = "bs.inline" || txt = "inline"
141+
)
142+
143+
144+
let has_inline_payload (attrs : t) =
145+
Ext_list.find_first attrs is_inline
146+
155147

156148
type derive_attr = {
157149
bs_deriving : Ast_payload.action list option

jscomp/syntax/ast_attributes.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ val process_bs :
5050
val external_needs_to_be_encoded :
5151
t -> bool
5252

53-
val has_inline_in_stru :
54-
t ->
55-
bool
5653

57-
val has_inline_payload_in_sig :
54+
val has_inline_payload :
5855
t ->
5956
attr option
6057

jscomp/syntax/ast_derive.ml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,7 @@ let gen_signature
6565
(Ast_payload.table_dispatch !derive_table action).signature_gen
6666
tdcls explict_nonrec)
6767

68-
(** used for cases like [%sexp] *)
69-
let gen_expression ({Asttypes.txt ; loc}) typ =
70-
let txt = Ext_string.tail_from txt (String.length Literals.bs_deriving_dot) in
71-
match (Ast_payload.table_dispatch !derive_table
72-
({txt ; loc}, None)).expression_gen with
73-
| None ->
74-
Bs_syntaxerr.err loc (Unregistered txt)
7568

76-
| Some f -> f typ
7769

7870
open Ast_helper
7971
let gen_structure_signature

jscomp/syntax/ast_derive.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ val gen_signature:
5252
Ast_signature.t
5353

5454

55-
val gen_expression :
56-
string Asttypes.loc ->
57-
Parsetree.core_type ->
58-
Parsetree.expression
59-
60-
6155

6256
val gen_structure_signature :
6357
Location.t ->

jscomp/syntax/ast_derive_abstract.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let isAbstract (xs :Ast_payload.action list) =
5050
match txt with
5151
| "abstract" ->
5252
Location.raise_errorf ~loc
53-
"bs.deriving abstract does not work with any other deriving"
53+
"deriving abstract does not work with any other deriving"
5454
| _ -> ()
5555
) ;
5656
Not_abstract

jscomp/syntax/ast_exp_extension.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ open Ast_helper
2828

2929

3030
let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
31-
(({txt ; loc} as lid , payload) : Parsetree.extension) =
31+
(({txt ; loc} , payload) : Parsetree.extension) =
3232
begin match txt with
3333
| "bs.raw" | "raw" ->
3434
Ast_exp_handle_external.handle_raw ~kind:Raw_exp loc payload
@@ -141,10 +141,6 @@ let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
141141
| _ -> Location.raise_errorf ~loc "Expect an expression here"
142142
end
143143
| _ ->
144-
match payload with
145-
| PTyp typ when Ext_string.starts_with txt Literals.bs_deriving_dot ->
146-
self.expr self (Ast_derive.gen_expression lid typ)
147-
| _ ->
148144
e (* For an unknown extension, we don't really need to process further*)
149145
(* Exp.extension ~loc ~attrs:e.pexp_attributes (
150146
self.extension self extension) *)

jscomp/syntax/bs_builtin_ppx.ml

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ let () =
6060
Ast_derive_js_mapper.init ()
6161

6262

63-
63+
let succeed attr attrs =
64+
match attrs with
65+
| [ _ ] -> ()
66+
| _ ->
67+
Bs_ast_invariant.mark_used_bs_attribute attr;
68+
Bs_ast_invariant.warn_discarded_unused_attributes attrs
6469

6570
type mapper = Bs_ast_mapper.mapper
6671
let default_mapper = Bs_ast_mapper.default_mapper
@@ -243,14 +248,14 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
243248
Ast_external.handleExternalInSig self value_desc sigi
244249
else
245250
(match
246-
Ast_attributes.has_inline_payload_in_sig
251+
Ast_attributes.has_inline_payload
247252
pval_attributes with
248-
| Some ({loc},PStr [{pstr_desc = Pstr_eval ({pexp_desc },_)}]) ->
253+
| Some ((_,PStr [{pstr_desc = Pstr_eval ({pexp_desc },_)}]) as attr) ->
249254
begin match pexp_desc with
250255
| Pexp_constant (
251256
Pconst_string
252257
(s,dec)) ->
253-
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
258+
succeed attr pval_attributes;
254259
{ sigi with
255260
psig_desc = Psig_value
256261
{
@@ -260,8 +265,8 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
260265
}}
261266
| Pexp_constant(
262267
Pconst_integer (s,None)
263-
) ->
264-
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
268+
) ->
269+
succeed attr pval_attributes;
265270
let s = Int32.of_string s in
266271
{ sigi with
267272
psig_desc = Psig_value
@@ -273,22 +278,22 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
273278
| Pexp_constant(Pconst_integer (s, Some 'L'))
274279
->
275280
let s = Int64.of_string s in
276-
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
281+
succeed attr pval_attributes;
277282
{sigi with psig_desc = Psig_value {
278283
value_desc with
279284
pval_prim = External_ffi_types.inline_int64_primitive s;
280285
pval_attributes = [];
281286
} }
282287
| Pexp_constant (Pconst_float(s,None)) ->
283-
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
288+
succeed attr pval_attributes;
284289
{sigi with psig_desc = Psig_value {
285290
value_desc with
286291
pval_prim = External_ffi_types.inline_float_primitive s;
287292
pval_attributes = [];
288293
} }
289294
| Pexp_construct ({txt = Lident ("true" | "false" as txt)}, None)
290295
->
291-
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
296+
succeed attr pval_attributes;
292297
{ sigi with
293298
psig_desc = Psig_value
294299
{
@@ -297,12 +302,11 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
297302
pval_attributes = []
298303
}}
299304
| _ ->
300-
Location.raise_errorf ~loc "invalid payload in bs.inline"
305+
default_mapper.signature_item self sigi
301306
end
302-
| Some ({loc}, _) ->
303-
Location.raise_errorf ~loc "invalid payload in bs.inline"
307+
| Some _
304308
| None ->
305-
default_mapper.signature_item self sigi
309+
default_mapper.signature_item self sigi
306310
)
307311
| _ -> default_mapper.signature_item self sigi
308312

@@ -327,66 +331,64 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
327331
->
328332
let pvb_expr = self.expr self pvb_expr in
329333
let pvb_attributes = self.attributes self pvb_attributes in
330-
let has_inline_property = Ast_attributes.has_inline_in_stru pvb_attributes in
331-
if has_inline_property then
332-
begin match pvb_expr.pexp_desc with
333-
| Pexp_constant(
334-
Pconst_string
335-
(s,dec))
336-
->
337-
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
338-
{str with pstr_desc = Pstr_primitive {
339-
pval_name = pval_name ;
340-
pval_type = Ast_literal.type_string ();
341-
pval_loc = pvb_loc;
342-
pval_attributes = [];
343-
pval_prim = External_ffi_types.inline_string_primitive s dec
344-
} }
345-
| Pexp_constant(Pconst_integer (s,None))
346-
->
347-
let s = Int32.of_string s in
348-
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
349-
{str with pstr_desc = Pstr_primitive {
350-
pval_name = pval_name ;
351-
pval_type = Ast_literal.type_int ();
352-
pval_loc = pvb_loc;
353-
pval_attributes = [];
354-
pval_prim = External_ffi_types.inline_int_primitive s
355-
} }
356-
| Pexp_constant(Pconst_integer (s, Some 'L'))
357-
->
358-
let s = Int64.of_string s in
359-
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
360-
{str with pstr_desc = Pstr_primitive {
361-
pval_name = pval_name ;
362-
pval_type = Ast_literal.type_int64;
363-
pval_loc = pvb_loc;
364-
pval_attributes = [];
365-
pval_prim = External_ffi_types.inline_int64_primitive s
366-
} }
367-
| Pexp_constant(Pconst_float (s, None))
368-
->
369-
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
370-
{str with pstr_desc = Pstr_primitive {
371-
pval_name = pval_name ;
372-
pval_type = Ast_literal.type_float;
373-
pval_loc = pvb_loc;
374-
pval_attributes = [];
375-
pval_prim = External_ffi_types.inline_float_primitive s
376-
} }
377-
| Pexp_construct ({txt = Lident ("true" | "false" as txt) },None) ->
378-
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
379-
{str with pstr_desc = Pstr_primitive {
380-
pval_name = pval_name ;
381-
pval_type = Ast_literal.type_bool ();
382-
pval_loc = pvb_loc;
383-
pval_attributes = [];
384-
pval_prim = External_ffi_types.inline_bool_primitive (txt = "true")
385-
} }
386-
| _ -> Location.raise_errorf ~loc:pvb_loc "invalid payload in bs.inline"
387-
end
388-
else
389-
{ str with pstr_desc = Pstr_value(Nonrecursive, [{pvb_pat ; pvb_expr; pvb_attributes; pvb_loc}])}
334+
let has_inline_property = Ast_attributes.has_inline_payload pvb_attributes in
335+
begin match has_inline_property, pvb_expr.pexp_desc with
336+
| Some attr, Pexp_constant(
337+
Pconst_string
338+
(s,dec))
339+
->
340+
succeed attr pvb_attributes;
341+
{str with pstr_desc = Pstr_primitive {
342+
pval_name = pval_name ;
343+
pval_type = Ast_literal.type_string ();
344+
pval_loc = pvb_loc;
345+
pval_attributes = [];
346+
pval_prim = External_ffi_types.inline_string_primitive s dec
347+
} }
348+
| Some attr, Pexp_constant(Pconst_integer (s,None))
349+
->
350+
let s = Int32.of_string s in
351+
succeed attr pvb_attributes;
352+
{str with pstr_desc = Pstr_primitive {
353+
pval_name = pval_name ;
354+
pval_type = Ast_literal.type_int ();
355+
pval_loc = pvb_loc;
356+
pval_attributes = [];
357+
pval_prim = External_ffi_types.inline_int_primitive s
358+
} }
359+
| Some attr, Pexp_constant(Pconst_integer (s, Some 'L'))
360+
->
361+
let s = Int64.of_string s in
362+
succeed attr pvb_attributes;
363+
{str with pstr_desc = Pstr_primitive {
364+
pval_name = pval_name ;
365+
pval_type = Ast_literal.type_int64;
366+
pval_loc = pvb_loc;
367+
pval_attributes = [];
368+
pval_prim = External_ffi_types.inline_int64_primitive s
369+
} }
370+
| Some attr, Pexp_constant(Pconst_float (s, None))
371+
->
372+
succeed attr pvb_attributes;
373+
{str with pstr_desc = Pstr_primitive {
374+
pval_name = pval_name ;
375+
pval_type = Ast_literal.type_float;
376+
pval_loc = pvb_loc;
377+
pval_attributes = [];
378+
pval_prim = External_ffi_types.inline_float_primitive s
379+
} }
380+
| Some attr, Pexp_construct ({txt = Lident ("true" | "false" as txt) },None) ->
381+
succeed attr pvb_attributes;
382+
{str with pstr_desc = Pstr_primitive {
383+
pval_name = pval_name ;
384+
pval_type = Ast_literal.type_bool ();
385+
pval_loc = pvb_loc;
386+
pval_attributes = [];
387+
pval_prim = External_ffi_types.inline_bool_primitive (txt = "true")
388+
} }
389+
| _ ->
390+
{ str with pstr_desc = Pstr_value(Nonrecursive, [{pvb_pat ; pvb_expr; pvb_attributes; pvb_loc}])}
391+
end
390392
| Pstr_attribute({txt = "bs.config" },_) -> str
391393
| _ -> default_mapper.structure_item self str
392394

@@ -431,10 +433,6 @@ let rec
431433
| [] -> []
432434
| item::rest ->
433435
match item.pstr_desc with
434-
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, _),_)
435-
->
436-
Location.prerr_warning loc (Preprocessor "this extension can be safely removed");
437-
structure_mapper self rest
438436
| Pstr_extension ( ({txt = ("bs.raw"| "raw") ; loc}, payload), _attrs)
439437
->
440438
Ast_exp_handle_external.handle_raw_structure loc payload :: structure_mapper self rest

0 commit comments

Comments
 (0)