Skip to content

Commit dbb0780

Browse files
authored
Merge pull request #4097 from BuckleScript/raw_checker
error checking for raw/re, fix #4089
2 parents 566b1a1 + 59a671d commit dbb0780

29 files changed

+298432
-19186
lines changed

jscomp/core/js_raw_exp_info.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ type t =
3737
| RegExp of RegExp.t
3838
]}
3939
*)
40-
| Js_unknown
40+
| Js_unknown
41+
42+
43+
type raw_kind =
44+
| Raw_re
45+
| Raw_exp
46+
| Raw_program
47+

jscomp/snapshot.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ build $SNAP/whole_compiler.ml: bspack | ./bin/bspack.exe
2222

2323

2424
build $SNAP/bsppx.ml: bspack | ./bin/bspack.exe
25-
flags = ${releaseMode} -bs-MD -module-alias Config=Config_whole_compiler -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I stubs -I common -I ext -I syntax -I core -I main
25+
flags = ${releaseMode} -bs-MD -module-alias Config=Config_whole_compiler -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I stubs -I common -I ext -I syntax -I core -I js_parser -I main
2626
main = Bsppx_main
2727

2828

jscomp/syntax/ast_compatible.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ type arg_label = Asttypes.arg_label =
3434
| Nolabel
3535
| Labelled of string
3636
| Optional of string
37-
let no_label : arg_label = Nolabel
37+
3838
let is_arg_label_simple (s : arg_label) = s = (Nolabel : arg_label)
3939
type label = arg_label
4040

4141

4242

4343
let arrow ?(loc=default_loc) ?(attrs = []) a b =
44-
Ast_helper.Typ.arrow ~loc ~attrs no_label a b
44+
Ast_helper.Typ.arrow ~loc ~attrs Nolabel a b
4545

4646
let apply_simple
4747
?(loc = default_loc)
@@ -52,7 +52,7 @@ let apply_simple
5252
pexp_desc =
5353
Pexp_apply(
5454
fn,
55-
(Ext_list.map args (fun x -> no_label, x) ) ) }
55+
(Ext_list.map args (fun x -> Nolabel, x) ) ) }
5656

5757
let app1
5858
?(loc = default_loc)
@@ -63,7 +63,7 @@ let app1
6363
pexp_desc =
6464
Pexp_apply(
6565
fn,
66-
[no_label, arg1]
66+
[Nolabel, arg1]
6767
) }
6868

6969
let app2
@@ -76,8 +76,8 @@ let app2
7676
Pexp_apply(
7777
fn,
7878
[
79-
no_label, arg1;
80-
no_label, arg2 ]
79+
Nolabel, arg1;
80+
Nolabel, arg2 ]
8181
) }
8282

8383
let app3
@@ -90,9 +90,9 @@ let app3
9090
Pexp_apply(
9191
fn,
9292
[
93-
no_label, arg1;
94-
no_label, arg2;
95-
no_label, arg3
93+
Nolabel, arg1;
94+
Nolabel, arg2;
95+
Nolabel, arg3
9696
]
9797
) }
9898

@@ -104,7 +104,7 @@ let fun_
104104
{
105105
pexp_loc = loc;
106106
pexp_attributes = attrs;
107-
pexp_desc = Pexp_fun(no_label,None, pat, exp)
107+
pexp_desc = Pexp_fun(Nolabel,None, pat, exp)
108108
}
109109

110110
let opt_label s =

jscomp/syntax/ast_compatible.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type label = arg_label
3030

3131

3232

33-
val no_label: arg_label
33+
3434

3535
type loc = Location.t
3636
type attrs = Parsetree.attribute list

jscomp/syntax/ast_core_type_class_type.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let handle_class_type_field self
106106
private_flag,
107107
virtual_flag,
108108
Ast_util.to_method_type
109-
loc self Ast_compatible.no_label ty
109+
loc self Nolabel ty
110110
(Ast_literal.type_unit ~loc ())
111111
);
112112
pctf_attributes} in
@@ -190,7 +190,7 @@ let typ_mapper
190190
| Meth_callback attr, attrs ->
191191
attrs, attr +> ty
192192
in
193-
Ast_compatible.object_field name attrs (Ast_util.to_method_type loc self Ast_compatible.no_label core_type
193+
Ast_compatible.object_field name attrs (Ast_util.to_method_type loc self Nolabel core_type
194194
(Ast_literal.type_unit ~loc ())) in
195195
let not_getter_setter ty =
196196
let attrs, core_type =

jscomp/syntax/ast_exp_apply.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let app_exp_mapper
109109
begin match fn with
110110
| {pexp_desc = Pexp_apply (fn, args); pexp_loc; pexp_attributes} ->
111111
Bs_ast_invariant.warn_discarded_unused_attributes pexp_attributes;
112-
{ pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, new_obj_arg) :: args);
112+
{ pexp_desc = Pexp_apply(fn, (Nolabel, new_obj_arg) :: args);
113113
pexp_attributes = [];
114114
pexp_loc = pexp_loc}
115115
| {pexp_desc = Pexp_variant(label,None); pexp_loc; pexp_attributes} ->
@@ -128,7 +128,7 @@ let app_exp_mapper
128128
| {pexp_desc = Pexp_apply (fn,args); pexp_loc; pexp_attributes }
129129
->
130130
Bs_ast_invariant.warn_discarded_unused_attributes pexp_attributes;
131-
{ Parsetree.pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, bounded_obj_arg) :: args);
131+
{ Parsetree.pexp_desc = Pexp_apply(fn, (Nolabel, bounded_obj_arg) :: args);
132132
pexp_attributes = [];
133133
pexp_loc = pexp_loc}
134134
| {pexp_desc = Pexp_construct(ctor,None); pexp_loc; pexp_attributes}
@@ -144,7 +144,7 @@ let app_exp_mapper
144144
let fn = Ast_open_cxt.restore_exp e wholes in
145145
let args = Ext_list.map args (fun (lab,exp) -> lab, Ast_open_cxt.restore_exp exp wholes) in
146146
Bs_ast_invariant.warn_discarded_unused_attributes pexp_attributes;
147-
{ pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, new_obj_arg) :: args);
147+
{ pexp_desc = Pexp_apply(fn, (Nolabel, new_obj_arg) :: args);
148148
pexp_attributes = [];
149149
pexp_loc = loc}
150150
| _ -> Ast_compatible.app1 ~loc fn new_obj_arg

jscomp/syntax/ast_exp_extension.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
9393
(Exp.ident ~loc {txt = Ast_raw.raw_function_id;loc})
9494
(Ast_compatible.const_exp_string ~loc (toString {args = List.rev acc ; block }))
9595
end
96-
| _ -> Ast_util.handle_raw ~check_js_regex:false loc payload
96+
| _ -> Ast_util.handle_raw ~kind:Raw_exp loc payload
9797
end
9898
| "bs.re" | "re" ->
9999
Exp.constraint_ ~loc
100-
(Ast_util.handle_raw ~check_js_regex:true loc payload)
100+
(Ast_util.handle_raw ~kind:Raw_re loc payload)
101101
(Ast_comb.to_js_re_type loc)
102102
| "bs.external" | "external" ->
103103
begin match Ast_payload.as_ident payload with

jscomp/syntax/ast_external_process.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ let handle_attributes
875875
| _ ->
876876
(* more error checking *)
877877
[External_arg_spec.empty_kind arg_type],
878-
[{label = Ast_compatible.no_label;
878+
[{label = Nolabel;
879879
ty = new_ty;
880880
attr = [];
881881
loc = obj.ptyp_loc} ],

jscomp/syntax/ast_payload.ml

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,65 @@ let is_single_int (x : t ) : int option =
6565
_}] -> Some (int_of_string name)
6666
| _ -> None
6767

68-
type rtn = Not_String_Lteral | JS_Regex_Check_Failed | Correct of Parsetree.expression
6968

70-
let as_string_exp ~check_js_regex (x : t ) =
69+
let offset_pos
70+
({pos_lnum; pos_bol; pos_cnum} as loc : Lexing.position)
71+
({line; column} : Loc.position) first_line_offset : Lexing.position =
72+
if line = 1 then
73+
{loc with pos_cnum = pos_cnum + column + first_line_offset }
74+
else {
75+
loc with
76+
pos_lnum = pos_lnum + line - 1;
77+
pos_cnum = pos_bol + column
78+
}
79+
80+
81+
(* Here the loc is the payload loc *)
82+
let check_flow_errors ~(loc : Location.t)
83+
deli (errors : (Loc.t * Parse_error.t) list) =
84+
match errors with
85+
| [] -> ()
86+
| ({start ;
87+
_end },first_error) :: _ ->
88+
let offset =
89+
(match deli with
90+
| None -> 1 (* length of '"'*)
91+
| Some deli ->
92+
String.length deli + 2 (* length of "{|"*)
93+
) in
94+
Location.raise_errorf ~loc:{loc with
95+
loc_start = offset_pos loc.loc_start start
96+
offset ;
97+
loc_end = offset_pos loc.loc_start _end
98+
offset } "%s"
99+
(Parse_error.PP.error first_error)
100+
;;
101+
let raw_as_string_exp_exn
102+
~(kind: Js_raw_exp_info.raw_kind)
103+
(x : t ) : _ option =
71104
match x with (** TODO also need detect empty phrase case *)
72105
| PStr [ {
73106
pstr_desc =
74107
Pstr_eval (
75108
{pexp_desc =
76109
Pexp_constant
77-
(Pconst_string (str,_))
110+
(Pconst_string (str,deli))
78111
;
79-
_} as e ,_);
80-
_}] -> if check_js_regex then (if Ext_js_regex.js_regex_checker str then Correct e else JS_Regex_Check_Failed) else Correct e
81-
| _ -> Not_String_Lteral
112+
pexp_loc = loc} as e ,_);
113+
_}] ->
114+
check_flow_errors ~loc deli (match kind with
115+
| Raw_re ->
116+
let l s =
117+
Flow_lexer.regexp (Lex_env.new_lex_env None (Sedlexing.Utf8.from_string s) ~enable_types_in_comments:false)
118+
in
119+
(snd (l str)).lex_errors
120+
| Raw_exp ->
121+
snd (Parser_flow.parse_expression (Parser_env.init_env None str) false)
122+
| Raw_program ->
123+
snd (Parser_flow.parse_program false None str)
124+
);
125+
Some e
126+
| _ -> None
82127

83128
let as_core_type loc x =
84129
match x with

jscomp/syntax/ast_payload.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ val is_single_string_as_ast :
4040

4141
val is_single_int : t -> int option
4242

43-
type rtn = Not_String_Lteral | JS_Regex_Check_Failed | Correct of Parsetree.expression
44-
val as_string_exp : check_js_regex: bool -> t -> rtn
43+
44+
45+
(** Convert %raw into expression *)
46+
val raw_as_string_exp_exn :
47+
kind: Js_raw_exp_info.raw_kind ->
48+
t ->
49+
Parsetree.expression option
50+
4551
val as_core_type : Location.t -> t -> Parsetree.core_type
4652
(* val as_empty_structure : t -> bool *)
4753
val as_ident : t -> Longident.t Asttypes.loc option

0 commit comments

Comments
 (0)