Skip to content

Commit d19b60b

Browse files
authored
Merge pull request #4156 from BuckleScript/fix_4152
fix #4152
2 parents 46344bb + 95fa7ce commit d19b60b

37 files changed

+506
-301
lines changed

jscomp/core/classify_function.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
let classify (prog : string) : Js_raw_exp_info.t =
25+
let classify (prog : string) : Js_raw_info.exp =
2626
match Parser_flow.parse_expression
2727
(Parser_env.init_env None prog) false with
2828
| (_, Function {
@@ -45,9 +45,16 @@ let classify (prog : string) : Js_raw_exp_info.t =
4545
|(_, Literal _), [] ->
4646
Js_literal
4747
| _ ->
48-
Js_unknown
48+
Js_exp_unknown
4949
| exception _ ->
50-
Js_unknown
50+
Js_exp_unknown
51+
52+
let classify_stmt (prog : string) : Js_raw_info.stmt =
53+
let result = Parser_flow.parse_program false None prog in
54+
match fst result with
55+
| (_loc, [], _) ->
56+
Js_stmt_comment
57+
| _ -> Js_stmt_unknown
5158
(* we can also analayze throw
5259
x.x pure access
5360
*)

jscomp/core/classify_function.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@
2424

2525

2626
val classify :
27-
string -> Js_raw_exp_info.t
27+
string -> Js_raw_info.exp
28+
29+
val classify_stmt :
30+
string -> Js_raw_info.stmt

jscomp/core/j.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ and tag_info = Js_op.tag_info
6060

6161
and required_modules = Js_op.required_modules
6262

63-
and code_info = Js_op.code_info
63+
6464

6565

6666
(** object literal, if key is ident, in this case, it might be renamed by
@@ -168,7 +168,7 @@ and expression_desc =
168168
*)
169169
| Unicode of string
170170
(* It is escaped string, print delimited by '"'*)
171-
| Raw_js_code of string * code_info
171+
| Raw_js_code of Js_raw_info.t
172172
| Raw_js_function of string * string list
173173
(* literally raw JS code
174174
*)

jscomp/core/js_dump.ml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ let exp_need_paren (e : J.expression) =
169169
(* | Caml_uninitialized_obj _ *)
170170
| Call ({expression_desc = Fun _ | Raw_js_function _ | Raw_js_code _ },_,_) -> true
171171

172-
| Raw_js_code (_, Exp _)
172+
| Raw_js_code {code_info = Exp _}
173173
| Fun _
174174
| Raw_js_function _
175175
| Caml_block (_,_,_, (Blk_record _ | Blk_module _))
176176
| Object _ -> true
177-
| Raw_js_code (_,Stmt)
177+
| Raw_js_code {code_info = Stmt _ }
178178
| Length _
179179
| Call _
180180
| Caml_block_tag _
@@ -646,8 +646,7 @@ and expression_desc cxt ~(level:int) f x : cxt =
646646
P.space f ;
647647
pp_js_function_params_body f s params;
648648
cxt
649-
| Raw_js_code (s,info) ->
650-
let s = String.trim s in
649+
| Raw_js_code {code = s; code_info = info} ->
651650
(match info with
652651
| Exp exp_info ->
653652
let raw_paren =
@@ -656,10 +655,11 @@ and expression_desc cxt ~(level:int) f x : cxt =
656655
P.string f s ;
657656
if raw_paren then P.string f L.rparen;
658657
cxt
659-
| Stmt ->
660-
P.newline f ;
658+
| Stmt stmt_info ->
659+
if stmt_info = Js_stmt_comment then P.string f s
660+
else begin P.newline f ;
661661
P.string f s ;
662-
P.newline f ;
662+
P.newline f end ;
663663
cxt)
664664

665665
| Number v ->
@@ -1051,16 +1051,23 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10511051
cxt
10521052
| Exp {expression_desc = Var _;}
10531053
-> (* Does it make sense to optimize here? *)
1054-
semi f; cxt
1055-
| Exp e ->
1056-
let cxt =
1057-
(
1058-
if exp_need_paren e
1059-
then P.paren_group f 1
1060-
else P.group f 0
1061-
) (fun _ -> expression ~level:0 cxt f e ) in
1062-
semi f;
1054+
(* semi f; *)
10631055
cxt
1056+
| Exp e ->
1057+
(
1058+
match e.expression_desc with
1059+
| Raw_js_code {code = s; code_info = Stmt (Js_stmt_comment)} ->
1060+
P.string f s;
1061+
cxt
1062+
| _ ->
1063+
let cxt =
1064+
(
1065+
if exp_need_paren e
1066+
then P.paren_group f 1
1067+
else P.group f 0
1068+
) (fun _ -> expression ~level:0 cxt f e ) in
1069+
semi f;
1070+
cxt)
10641071
| Block b -> (* No braces needed here *)
10651072
ipp_comment f L.start_block;
10661073
let cxt = statement_list top cxt f b in

jscomp/core/js_dump_program.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ let deps_program_is_empty (x : J.deps_program) =
4848
} -> program_is_empty program
4949
| _ -> false
5050

51+
let rec extract_block_comments acc (x : J.block) =
52+
match x with
53+
| {statement_desc = Exp {expression_desc = Raw_js_code {code ; code_info = Stmt (Js_stmt_comment)}} } :: rest
54+
-> extract_block_comments (code :: acc) rest
55+
| _ -> (acc ,x)
56+
57+
58+
let extract_file_comments (x : J.deps_program) =
59+
let comments, new_block = extract_block_comments [] x.program.block in
60+
comments , {x with program = {x.program with block = new_block}}
61+
62+
63+
64+
65+
5166
let program f cxt ( x : J.program ) =
5267
P.force_newline f;
5368
let cxt = Js_dump.statement_list true cxt f x.block in
@@ -107,7 +122,7 @@ let es6_program ~output_dir fmt f ( x : J.deps_program) =
107122
*)
108123

109124
let pp_deps_program
110-
~output_prefix
125+
~(output_prefix : string)
111126
(kind : Js_packages_info.module_system )
112127
(program : J.deps_program) (f : Ext_pp.t) =
113128
if not !Js_config.no_version_header then
@@ -119,6 +134,8 @@ let pp_deps_program
119134
P.string f empty_explanation
120135
(* This is empty module, it won't be referred anywhere *)
121136
else
137+
let comments, program = extract_file_comments program in
138+
Ext_list.rev_iter comments (fun comment -> P.string f comment; P.newline f) ;
122139
let output_dir = Filename.dirname output_prefix in
123140
begin
124141
ignore (match kind with

jscomp/core/js_exp_make.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ let unicode ?comment s : t =
125125
{expression_desc = Unicode (s); comment}
126126

127127
let raw_js_code ?comment info s : t =
128-
{expression_desc = Raw_js_code (s,info) ; comment }
128+
{expression_desc = Raw_js_code {code = String.trim s; code_info = info} ; comment }
129129

130130
let raw_js_function ?comment arg block : t =
131131
{expression_desc = Raw_js_function(arg,block); comment}

jscomp/core/js_exp_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ val dummy_obj :
339339
(** convert a block to expresion by using IIFE *)
340340
val of_block : ?comment:string -> ?e:J.expression -> J.statement list -> t
341341

342-
val raw_js_code : ?comment:string -> J.code_info -> string -> t
342+
val raw_js_code : ?comment:string -> Js_raw_info.code_info -> string -> t
343343
val raw_js_function : ?comment:string -> string -> string list -> t
344344
val nil : t
345345
val is_null : ?comment:string -> t -> t

jscomp/core/js_fold.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ class virtual fold =
356356
let o = o#block _x_i2 in let o = o#unknown _x_i3 in o
357357
| Str (_x, _x_i1) -> let o = o#bool _x in let o = o#string _x_i1 in o
358358
| Unicode _x -> let o = o#string _x in o
359-
| Raw_js_code (_x, _x_i1) ->
360-
let o = o#string _x in let o = o#code_info _x_i1 in o
359+
| Raw_js_code _x -> let o = o#unknown _x in o
361360
| Raw_js_function (_x, _x_i1) ->
362361
let o = o#string _x in
363362
let o = o#list (fun o -> o#string) _x_i1 in o
@@ -386,7 +385,6 @@ class virtual fold =
386385
let o = o#program _x in
387386
let o = o#required_modules _x_i1 in
388387
let o = o#option (fun o -> o#string) _x_i2 in o
389-
method code_info : code_info -> 'self_type = o#unknown
390388
method case_clause :
391389
(* since in ocaml, it's expression oriented langauge, [return] in
392390
general has no jumps, it only happens when we do

jscomp/core/js_map.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,7 @@ class virtual map =
392392
| Str (_x, _x_i1) ->
393393
let _x = o#bool _x in let _x_i1 = o#string _x_i1 in Str (_x, _x_i1)
394394
| Unicode _x -> let _x = o#string _x in Unicode _x
395-
| Raw_js_code (_x, _x_i1) ->
396-
let _x = o#string _x in
397-
let _x_i1 = o#code_info _x_i1 in Raw_js_code (_x, _x_i1)
395+
| Raw_js_code _x -> let _x = o#unknown _x in Raw_js_code _x
398396
| Raw_js_function (_x, _x_i1) ->
399397
let _x = o#string _x in
400398
let _x_i1 = o#list (fun o -> o#string) _x_i1
@@ -429,7 +427,6 @@ class virtual map =
429427
let _x_i1 = o#required_modules _x_i1 in
430428
let _x_i2 = o#option (fun o -> o#string) _x_i2
431429
in { program = _x; modules = _x_i1; side_effect = _x_i2; }
432-
method code_info : code_info -> code_info = o#unknown
433430
method case_clause :
434431
(* since in ocaml, it's expression oriented langauge, [return] in
435432
general has no jumps, it only happens when we do

jscomp/core/js_op.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ type length_object =
242242
| Function
243243
| Caml_block
244244

245-
type code_info =
246-
| Exp of Js_raw_exp_info.t
247-
| Stmt
248245
(** TODO: define constant - for better constant folding *)
249246
(* type constant = *)
250247
(* | Const_int of int *)

0 commit comments

Comments
 (0)