Skip to content

Commit d355ccd

Browse files
committed
refine purity analysis over raw object literals
1 parent 3dee062 commit d355ccd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

jscomp/core/classify_function.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
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+
26+
let rec is_obj_literal ( x : _ Flow_ast.Expression.t) : bool =
27+
match snd x with
28+
| Literal _ -> true
29+
| Object {properties} ->
30+
Ext_list.for_all properties is_literal_kv
31+
| Array {elements} ->
32+
Ext_list.for_all elements (fun x ->
33+
match x with
34+
| None -> true
35+
| Some (Expression x) -> is_obj_literal x
36+
| Some _ -> false
37+
)
38+
| _ -> false
39+
and is_literal_kv (x : _ Flow_ast.Expression.Object.property) =
40+
match x with
41+
| Property (_ , Init {value}) -> is_obj_literal value
42+
| _ -> false
43+
44+
2545
let classify (prog : string) : Js_raw_info.exp =
2646
match Parser_flow.parse_expression
2747
(Parser_env.init_env None prog) false with
@@ -51,6 +71,8 @@ let classify (prog : string) : Js_raw_info.exp =
5171
| Some _ -> None
5272
in
5373
Js_literal {comment}
74+
| (_,Object _) as exp , _ ->
75+
if is_obj_literal exp then Js_literal {comment = None} else Js_exp_unknown
5476
| _ ->
5577
Js_exp_unknown
5678
| exception _ ->

0 commit comments

Comments
 (0)