Skip to content

Commit 9e704c5

Browse files
author
Hongbo Zhang
committed
[refactoring] clean up
1 parent 2cb684b commit 9e704c5

File tree

7 files changed

+109
-143
lines changed

7 files changed

+109
-143
lines changed

jscomp/syntax/ast_comb.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,20 @@ let create_local_external loc
5656
pexp_attributes = [];
5757
pexp_loc = loc
5858
})
59+
60+
open Ast_helper
61+
62+
let exp_apply_no_label ?loc ?attrs a b =
63+
Exp.apply ?loc ?attrs a (List.map (fun x -> "", x) b)
64+
65+
let fun_no_label ?loc ?attrs pat body =
66+
Exp.fun_ ?loc ?attrs "" None pat body
67+
68+
let arrow_no_label ?loc ?attrs b c =
69+
Typ.arrow ?loc ?attrs "" b c
70+
71+
let discard_exp_as_unit loc e =
72+
exp_apply_no_label ~loc
73+
(Exp.ident ~loc {txt = Ast_literal.Lid.ignore_id; loc})
74+
[Exp.constraint_ ~loc e
75+
(Ast_literal.type_unit ~loc ())]

jscomp/syntax/ast_comb.mli

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,27 @@ val create_local_external : Location.t ->
2929
?local_module_name:string ->
3030
?local_fun_name:string ->
3131
(Asttypes.label * Parsetree.expression) list -> Parsetree.expression_desc
32+
33+
val exp_apply_no_label :
34+
?loc:Location.t ->
35+
?attrs:Parsetree.attributes ->
36+
Parsetree.expression -> Parsetree.expression list -> Parsetree.expression
37+
38+
val fun_no_label :
39+
?loc:Location.t ->
40+
?attrs:Parsetree.attributes ->
41+
Parsetree.pattern -> Parsetree.expression -> Parsetree.expression
42+
43+
val arrow_no_label :
44+
?loc:Location.t ->
45+
?attrs:Parsetree.attributes ->
46+
Parsetree.core_type -> Parsetree.core_type -> Parsetree.core_type
47+
48+
(* note we first declare its type is [unit],
49+
then [ignore] it, [ignore] is necessary since
50+
the js value maybe not be of type [unit] and
51+
we can use [unit] value (though very little chance)
52+
sometimes
53+
*)
54+
val discard_exp_as_unit :
55+
Location.t -> Parsetree.expression -> Parsetree.expression

jscomp/syntax/ast_literal.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
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+
open Ast_helper
26+
2527
module Lid = struct
2628
type t = Longident.t
2729
let val_unit : t = Lident "()"
@@ -47,11 +49,14 @@ module No_loc = struct
4749
Ast_helper.Typ.mk (Ptyp_constr ({ txt = Lid.type_string; loc}, []))
4850

4951
let type_any = Ast_helper.Typ.any ()
52+
let pat_unit = Pat.construct {txt = Lid.val_unit; loc} None
5053
end
5154

5255
type 'a lit = ?loc: Location.t -> unit -> 'a
5356
type expression_lit = Parsetree.expression lit
5457
type core_type_lit = Parsetree.core_type lit
58+
type pattern_lit = Parsetree.pattern lit
59+
5560
let val_unit ?loc () =
5661
match loc with
5762
| None -> No_loc.val_unit
@@ -76,3 +81,9 @@ let type_any ?loc () =
7681
match loc with
7782
| None -> No_loc.type_any
7883
| Some loc -> Ast_helper.Typ.any ~loc ()
84+
85+
let pat_unit ?loc () =
86+
match loc with
87+
| None -> No_loc.pat_unit
88+
| Some loc ->
89+
Pat.construct ~loc {txt = Lid.val_unit; loc} None

jscomp/syntax/ast_literal.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ end
3737

3838
type expression_lit = Parsetree.expression lit
3939
type core_type_lit = Parsetree.core_type lit
40+
type pattern_lit = Parsetree.pattern lit
4041

4142
val val_unit : expression_lit
4243

@@ -46,4 +47,4 @@ val type_string : core_type_lit
4647

4748
val type_any : core_type_lit
4849

49-
50+
val pat_unit : pattern_lit

jscomp/syntax/ast_payload.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ let as_string_exp (x : t ) =
4848
_}] -> Some e
4949
| _ -> None
5050

51+
let as_empty_structure (x : t ) =
52+
match x with
53+
| PStr ([]) -> true
54+
| PTyp _ | PPat _ | PStr (_ :: _ ) -> false
55+
5156
let is_string_or_strings (x : t) :
5257
[ `None | `Single of string | `Some of string list ] =
5358
let module M = struct exception Not_str end in

jscomp/syntax/ast_payload.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ type t = Parsetree.payload
3131

3232
val is_single_string : t -> string option
3333
val as_string_exp : t -> Parsetree.expression option
34-
34+
val as_empty_structure : t -> bool
3535
val is_string_or_strings :
3636
t -> [ `None | `Single of string | `Some of string list ]

0 commit comments

Comments
 (0)