Skip to content

Commit c6943ca

Browse files
committed
Mark used attribute only based on fst of the payload
Since the snd of payload could be transformed, it is inexact. Also only check the attribute where the location is not ghost
1 parent cac99e6 commit c6943ca

12 files changed

+67
-61
lines changed

jscomp/all.depend

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,9 @@ syntax/bs_syntaxerr.cmx : syntax/bs_syntaxerr.cmi
175175
syntax/ast_compatible.cmx : ext/ext_list.cmx syntax/ast_compatible.cmi
176176
syntax/ast_utf8_string.cmx : ext/ext_utf8.cmx ext/ext_char.cmx \
177177
syntax/ast_utf8_string.cmi
178-
syntax/ast_utf8_string_interp.cmx : ext/literals.cmx ext/ext_utf8.cmx \
179-
ext/ext_string.cmx ext/ext_char.cmx common/bs_loc.cmx \
180-
syntax/ast_utf8_string.cmx syntax/ast_compatible.cmx \
181-
syntax/ast_utf8_string_interp.cmi
178+
syntax/ast_utf8_string_interp.cmx : ext/ext_utf8.cmx ext/ext_string.cmx \
179+
ext/ext_char.cmx common/bs_loc.cmx syntax/ast_utf8_string.cmx \
180+
syntax/ast_compatible.cmx syntax/ast_utf8_string_interp.cmi
182181
syntax/ast_derive_constructor.cmx :
183182
syntax/ast_derive_util.cmx : ext/ext_list.cmx syntax/ast_compatible.cmx \
184183
syntax/ast_derive_util.cmi
@@ -206,9 +205,9 @@ syntax/ast_comb.cmx : ext/ext_list.cmx syntax/ast_literal.cmx \
206205
syntax/ast_core_type.cmx : ext/ext_list.cmx syntax/bs_syntaxerr.cmx \
207206
syntax/ast_literal.cmx syntax/ast_compatible.cmx syntax/ast_comb.cmx \
208207
syntax/ast_core_type.cmi
209-
syntax/bs_ast_invariant.cmx : ext/literals.cmx ext/hash_set_poly.cmx \
210-
ext/ext_string.cmx ext/ext_list.cmx common/bs_warnings.cmx \
211-
syntax/bs_ast_iterator.cmx syntax/ast_core_type.cmx \
208+
syntax/bs_ast_invariant.cmx : ext/hash_set_poly.cmx ext/ext_list.cmx \
209+
common/bs_warnings.cmx syntax/bs_ast_iterator.cmx \
210+
syntax/ast_utf8_string_interp.cmx syntax/ast_core_type.cmx \
212211
syntax/bs_ast_invariant.cmi
213212
syntax/ast_attributes.cmx : ext/ext_string.cmx ext/ext_list.cmx \
214213
syntax/bs_syntaxerr.cmx syntax/bs_ast_invariant.cmx \

jscomp/ext/literals.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ let dash_nostdlib = "-nostdlib"
117117

118118
let reactjs_jsx_ppx_2_exe = "reactjs_jsx_ppx_2.exe"
119119
let reactjs_jsx_ppx_3_exe = "reactjs_jsx_ppx_3.exe"
120-
let unescaped_j_delimiter = "j"
121-
let unescaped_js_delimiter = "js"
122120

123121
let native = "native"
124122
let bytecode = "bytecode"

jscomp/ext/literals.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ val dash_nostdlib : string
118118

119119
val reactjs_jsx_ppx_2_exe : string
120120
val reactjs_jsx_ppx_3_exe : string
121-
val unescaped_j_delimiter : string
122-
123-
124-
val unescaped_js_delimiter : string
125121

126122
val native : string
127123
val bytecode : string

jscomp/syntax/ast_attributes.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ let iter_process_bs_int_as attrs =
282282
) attrs; !st
283283

284284

285-
let iter_process_bs_string_or_int_as attrs =
285+
let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
286286
let st = ref None in
287-
List.iter
287+
Ext_list.iter attrs
288288
(fun
289-
(({txt ; loc}, payload ) as attr : attr) ->
289+
(({txt ; loc}, payload ) as attr) ->
290290
match txt with
291291
| "bs.as"
292292
->
@@ -310,7 +310,7 @@ let iter_process_bs_string_or_int_as attrs =
310310
Bs_syntaxerr.err loc Duplicated_bs_as
311311
| _ -> ()
312312

313-
) attrs;
313+
) ;
314314
!st
315315

316316
let locg = Location.none

jscomp/syntax/ast_exp_apply.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ let handle_exp_apply
9898
| {pexp_desc = Pexp_apply (fn, args); pexp_loc; pexp_attributes} ->
9999
let fn = self.expr self fn in
100100
let args = Ext_list.map args (fun (lab,exp) -> lab, self.expr self exp ) in
101-
Bs_ast_invariant.warn_unused_attributes pexp_attributes;
101+
Bs_ast_invariant.warn_discarded_unused_attributes pexp_attributes;
102102
{ pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, new_obj_arg) :: args);
103103
pexp_attributes = [];
104104
pexp_loc = pexp_loc}
@@ -117,7 +117,7 @@ let handle_exp_apply
117117
->
118118
let fn = self.expr self fn in
119119
let args = Ext_list.map args (fun (lab,exp) -> lab, self.expr self exp ) in
120-
Bs_ast_invariant.warn_unused_attributes pexp_attributes;
120+
Bs_ast_invariant.warn_discarded_unused_attributes pexp_attributes;
121121
{ Parsetree.pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, bounded_obj_arg) :: args);
122122
pexp_attributes = [];
123123
pexp_loc = pexp_loc}
@@ -171,7 +171,7 @@ let handle_exp_apply
171171
currently the pattern match is written in a top down style.
172172
Another corner case: f##(g a b [@bs])
173173
*)
174-
Bs_ast_invariant.warn_unused_attributes attrs ;
174+
Bs_ast_invariant.warn_discarded_unused_attributes attrs ;
175175
{e with pexp_desc = Ast_util.method_apply loc self obj name args}
176176
| [
177177
#if OCAML_VERSION =~ ">4.03.0" then

jscomp/syntax/ast_polyvar.ml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,32 @@ let map_row_fields_into_strings ptyp_loc
9595
(row_fields : Parsetree.row_field list) : External_arg_spec.attr =
9696
let case, result =
9797
Ext_list.fold_right row_fields (`Nothing, []) (fun tag (nullary, acc) ->
98-
match nullary, tag with
99-
| (`Nothing | `Null),
100-
Rtag (label, attrs, true, [])
101-
->
102-
begin match Ast_attributes.iter_process_bs_string_as attrs with
103-
| Some name ->
104-
`Null, ((hash_label label, name) :: acc )
98+
match nullary, tag with
99+
| (`Nothing | `Null),
100+
Rtag (label, attrs, true, [])
101+
->
102+
begin match Ast_attributes.iter_process_bs_string_as attrs with
103+
| Some name ->
104+
`Null, ((hash_label label, name) :: acc )
105105

106-
| None ->
107-
`Null, ((hash_label label, label_of_name label) :: acc )
108-
end
109-
| (`Nothing | `NonNull), Rtag(label, attrs, false, ([ _ ]))
110-
->
111-
begin match Ast_attributes.iter_process_bs_string_as attrs with
112-
| Some name ->
113-
`NonNull, ((hash_label label, name) :: acc)
114-
| None ->
115-
`NonNull, ((hash_label label, label_of_name label) :: acc)
116-
end
117-
| _ -> Bs_syntaxerr.err ptyp_loc Invalid_bs_string_type
106+
| None ->
107+
`Null, ((hash_label label, label_of_name label) :: acc )
108+
end
109+
| (`Nothing | `NonNull), Rtag(label, attrs, false, ([ _ ]))
110+
->
111+
begin match Ast_attributes.iter_process_bs_string_as attrs with
112+
| Some name ->
113+
`NonNull, ((hash_label label, name) :: acc)
114+
| None ->
115+
`NonNull, ((hash_label label, label_of_name label) :: acc)
116+
end
117+
| _ -> Bs_syntaxerr.err ptyp_loc Invalid_bs_string_type
118118

119-
) in
120-
(match case with
121-
| `Nothing -> Bs_syntaxerr.err ptyp_loc Invalid_bs_string_type
122-
| `Null -> External_arg_spec.NullString result
123-
| `NonNull -> NonNullString result)
119+
) in
120+
match case with
121+
| `Nothing -> Bs_syntaxerr.err ptyp_loc Invalid_bs_string_type
122+
| `Null -> External_arg_spec.NullString result
123+
| `NonNull -> NonNullString result
124124

125125

126126
let is_enum row_fields =

jscomp/syntax/ast_tuple_pattern_flatten.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ let flattern_tuple_pattern_vb
9999
List.for_all is_simple_pattern xs &&
100100
Ext_list.same_length es xs
101101
->
102-
Bs_ast_invariant.warn_unused_attributes tuple_attributes ; (* will be dropped*)
102+
Bs_ast_invariant.warn_discarded_unused_attributes tuple_attributes ; (* will be dropped*)
103103
Ext_list.fold_right2 xs es acc (fun pat exp acc->
104104
{Parsetree.
105105
pvb_pat =

jscomp/syntax/ast_utf8_string_interp.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ let to_string_ident : Longident.t =
385385

386386

387387
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
388+
let unescaped_j_delimiter = "j"
389+
let unescaped_js_delimiter = "js"
388390

389391
let escaped = Some escaped_j_delimiter
390392

@@ -457,7 +459,7 @@ let transform_interp loc s =
457459

458460

459461
let transform (e : Parsetree.expression) s delim : Parsetree.expression =
460-
if Ext_string.equal delim Literals.unescaped_js_delimiter then
462+
if Ext_string.equal delim unescaped_js_delimiter then
461463
let js_str = Ast_utf8_string.transform e.pexp_loc s in
462464
{ e with pexp_desc =
463465
Pexp_constant (
@@ -467,8 +469,12 @@ let transform (e : Parsetree.expression) s delim : Parsetree.expression =
467469
Const_string
468470
#end
469471
(js_str, escaped))}
470-
else if Ext_string.equal delim Literals.unescaped_j_delimiter then
472+
else if Ext_string.equal delim unescaped_j_delimiter then
471473
transform_interp e.pexp_loc s
472474
else e
473475

474-
let is_unicode_string opt = Ext_string.equal opt escaped_j_delimiter
476+
let is_unicode_string opt = Ext_string.equal opt escaped_j_delimiter
477+
478+
let is_unescaped s =
479+
Ext_string.equal s unescaped_j_delimiter
480+
|| Ext_string.equal s unescaped_js_delimiter

jscomp/syntax/ast_utf8_string_interp.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ val transform :
7575
Parsetree.expression
7676

7777
val is_unicode_string :
78+
string ->
79+
bool
80+
81+
val is_unescaped :
7882
string ->
7983
bool

jscomp/syntax/bs_ast_invariant.ml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ let is_bs_attribute txt =
3333
String.unsafe_get txt 2 = '.'
3434
)
3535

36-
let used_attributes : Parsetree.attribute Hash_set_poly.t = Hash_set_poly.create 16
36+
let used_attributes : _ Hash_set_poly.t = Hash_set_poly.create 16
3737

38+
#if false then
3839
let dump_attribute fmt = (fun ( (sloc : string Asttypes.loc),payload) ->
3940
Format.fprintf fmt "@[%s %a@]" sloc.txt (Printast.payload 0 ) payload
4041
)
@@ -43,28 +44,31 @@ let dump_used_attributes fmt =
4344
Format.fprintf fmt "Used attributes Listing Start:@.";
4445
Hash_set_poly.iter (fun attr -> dump_attribute fmt attr) used_attributes;
4546
Format.fprintf fmt "Used attributes Listing End:@."
47+
#end
4648

47-
48-
let mark_used_bs_attribute (x : Parsetree.attribute) =
49-
Hash_set_poly.add used_attributes x
49+
(* only mark non-ghost used bs attribute *)
50+
let mark_used_bs_attribute ((x,_) : Parsetree.attribute) =
51+
if not x.loc.loc_ghost then
52+
Hash_set_poly.add used_attributes x
5053

5154
let dummy_unused_attribute : Warnings.t = (Bs_unused_attribute "")
5255

5356

5457

5558
let warn_unused_attribute
56-
(({txt; loc}, _) as attr : Parsetree.attribute) =
59+
(({txt; loc} as sloc, _) : Parsetree.attribute) =
5760
if is_bs_attribute txt &&
58-
not (Hash_set_poly.mem used_attributes attr) then
61+
not loc.loc_ghost &&
62+
not (Hash_set_poly.mem used_attributes sloc) then
5963
begin
60-
#if BS_DEBUG then (*COMMENT*)
64+
#if false then (*COMMENT*)
6165
dump_used_attributes Format.err_formatter;
6266
dump_attribute Format.err_formatter attr ;
6367
#end
6468
Location.prerr_warning loc (Bs_unused_attribute txt)
6569
end
6670

67-
let warn_unused_attributes (attrs : Parsetree.attributes) =
71+
let warn_discarded_unused_attributes (attrs : Parsetree.attributes) =
6872
if attrs <> [] then
6973
Ext_list.iter attrs warn_unused_attribute
7074

@@ -90,8 +94,7 @@ let emit_external_warnings : iterator=
9094
Const_string
9195
#end
9296
(_, Some s))
93-
when Ext_string.equal s Literals.unescaped_j_delimiter
94-
|| Ext_string.equal s Literals.unescaped_js_delimiter ->
97+
when Ast_utf8_string_interp.is_unescaped s ->
9598
Bs_warnings.error_unescaped_delimiter a.pexp_loc s
9699
| _ -> default_iterator.expr self a
97100
);

jscomp/syntax/bs_ast_invariant.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ type iterator = Bs_ast_iterator.iterator
3030
val mark_used_bs_attribute :
3131
Parsetree.attribute -> unit
3232

33-
(** [warn_unused_attributes discarded]
33+
(** [warn_discarded_unused_attributes discarded]
3434
warn if [discarded] has unused bs attribute
3535
*)
36-
val warn_unused_attributes :
36+
val warn_discarded_unused_attributes :
3737
Parsetree.attributes -> unit
3838
(** Ast invariant checking for detecting errors *)
3939

jscomp/syntax/external_process.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let get_arg_type
8484
we should warn, there is a trade off whether
8585
we should warn dropped non bs attribute or not
8686
*)
87-
Bs_ast_invariant.warn_unused_attributes ptyp_attrs;
87+
Bs_ast_invariant.warn_discarded_unused_attributes ptyp_attrs;
8888
match result with
8989
| None ->
9090
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external

0 commit comments

Comments
 (0)