Skip to content

Commit d38633b

Browse files
committed
improve code quality using early return
1 parent 4ce154a commit d38633b

File tree

226 files changed

+23320
-24050
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+23320
-24050
lines changed

jscomp/core/js_stmt_make.ml

+17-9
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ let string_switch
168168
{ statement_desc = String_switch (e,clauses, default); comment}]
169169
| None -> { statement_desc = String_switch (e,clauses, default); comment}
170170

171+
let rec block_last_is_return_throw_or_continue (x : J.block) =
172+
match x with
173+
| [] -> false
174+
| [x ] ->
175+
(match x.statement_desc with
176+
| Return _ | Throw _ | Continue _ ->
177+
true
178+
| _ -> false)
179+
| _ :: rest -> block_last_is_return_throw_or_continue rest
171180

172181
(* TODO: it also make sense to extract some common statements
173182
between those two branches, it does happen since in OCaml you
@@ -223,22 +232,21 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t
223232
| [], _ ->
224233
aux ?comment (E.not e) ifnot [] (*Make sure no infinite loop*)
225234
| [ {statement_desc = Return {return_value = ret_ifso; _}; _}],
226-
[ {statement_desc = Return {return_value = ret_ifnot; _}; _} as _ifnot_stmt]
235+
[ {statement_desc = Return {return_value = ret_ifnot; _}; _}]
227236
->
228237
return_stmt (E.econd e ret_ifso ret_ifnot )
229-
| [{statement_desc = Return _ | Throw _}],_
230-
-> block ({ statement_desc =
231-
If (e,
232-
ifso,
233-
[]);
234-
comment } :: ifnot )
235-
| _, [{statement_desc = Return _ | Throw _}]
238+
| _, [{statement_desc = Return _ }]
236239
-> block ({ statement_desc =
237240
If (E.not e,
238241
ifnot,
239242
[]);
240243
comment } :: ifso )
241-
244+
| _ ,_ when block_last_is_return_throw_or_continue ifso
245+
-> block ({ statement_desc =
246+
If (e,
247+
ifso,
248+
[]);
249+
comment } :: ifnot )
242250
| [ {statement_desc =
243251
Exp
244252
{expression_desc = Bin(Eq, ({expression_desc = Var (Id var_ifso); _} as lhs_ifso), rhs_ifso); _};

jscomp/test/adt_optimize_test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ function f11(x) {
154154
if (typeof x === "number") {
155155
return 2;
156156
}
157-
if (x.tag) {
158-
throw [
159-
Caml_builtin_exceptions.assert_failure,
160-
/* tuple */[
161-
"adt_optimize_test.ml",
162-
191,
163-
9
164-
]
165-
];
157+
if (!x.tag) {
158+
return 1;
166159
}
167-
return 1;
160+
throw [
161+
Caml_builtin_exceptions.assert_failure,
162+
/* tuple */[
163+
"adt_optimize_test.ml",
164+
191,
165+
9
166+
]
167+
];
168168
}
169169

170170
exports.f = f;

jscomp/test/arity_infer.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js")
55

66
function f0(x) {
77
var tmp;
8-
if (x <= 3) {
8+
if (x > 3) {
9+
tmp = (function (x) {
10+
return x + 1 | 0;
11+
});
12+
} else {
913
throw Caml_builtin_exceptions.not_found;
1014
}
11-
tmp = (function (x) {
12-
return x + 1 | 0;
13-
});
1415
return tmp(3);
1516
}
1617

jscomp/test/array_safe_get.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ try {
1616
}
1717
catch (raw_exn){
1818
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
19-
if (exn[0] !== Caml_builtin_exceptions.invalid_argument) {
19+
if (exn[0] === Caml_builtin_exceptions.invalid_argument) {
20+
console.log(exn[1]);
21+
y = 0;
22+
} else {
2023
throw exn;
2124
}
22-
console.log(exn[1]);
23-
y = 0;
2425
}
2526

2627
exports.x = x;

jscomp/test/bal_set_mini.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,27 @@ function bal(l, v, r) {
3737
} else {
3838
return /* Empty */0;
3939
}
40+
}
41+
if (hr <= (hl + 2 | 0)) {
42+
return /* Node */[
43+
l,
44+
v,
45+
r,
46+
hl >= hr ? hl + 1 | 0 : hr + 1 | 0
47+
];
48+
}
49+
if (!r) {
50+
return /* Empty */0;
51+
}
52+
var rr = r[2];
53+
var rv = r[1];
54+
var rl = r[0];
55+
if (height(rr) >= height(rl)) {
56+
return create(create(l, v, rl), rv, rr);
57+
} else if (rl) {
58+
return create(create(l, v, rl[0]), rl[1], create(rl[2], rv, rr));
4059
} else {
41-
if (hr <= (hl + 2 | 0)) {
42-
return /* Node */[
43-
l,
44-
v,
45-
r,
46-
hl >= hr ? hl + 1 | 0 : hr + 1 | 0
47-
];
48-
}
49-
if (!r) {
50-
return /* Empty */0;
51-
}
52-
var rr = r[2];
53-
var rv = r[1];
54-
var rl = r[0];
55-
if (height(rr) >= height(rl)) {
56-
return create(create(l, v, rl), rv, rr);
57-
} else if (rl) {
58-
return create(create(l, v, rl[0]), rl[1], create(rl[2], rv, rr));
59-
} else {
60-
return /* Empty */0;
61-
}
60+
return /* Empty */0;
6261
}
6362
}
6463

jscomp/test/bdd.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ function $$eval(_bdd, vars) {
1313
if (Caml_array.caml_array_get(vars, bdd[1])) {
1414
_bdd = bdd[3];
1515
continue ;
16-
} else {
17-
_bdd = bdd[0];
18-
continue ;
1916
}
17+
_bdd = bdd[0];
18+
continue ;
2019
};
2120
}
2221

@@ -98,14 +97,13 @@ function insert(idl, idh, v, ind, bucket, newNode) {
9897
]);
9998
n_items.contents = n_items.contents + 1 | 0;
10099
return ;
101-
} else {
102-
resize((sz_1.contents + sz_1.contents | 0) + 2 | 0);
103-
var ind$1 = hashVal(idl, idh, v) & sz_1.contents;
104-
return Caml_array.caml_array_set(htab.contents, ind$1, /* :: */[
105-
newNode,
106-
Caml_array.caml_array_get(htab.contents, ind$1)
107-
]);
108100
}
101+
resize((sz_1.contents + sz_1.contents | 0) + 2 | 0);
102+
var ind$1 = hashVal(idl, idh, v) & sz_1.contents;
103+
return Caml_array.caml_array_set(htab.contents, ind$1, /* :: */[
104+
newNode,
105+
Caml_array.caml_array_get(htab.contents, ind$1)
106+
]);
109107
}
110108

111109
function resetUnique(param) {
@@ -144,17 +142,16 @@ function mkNode(low, v, high) {
144142
}
145143
_b = b[1];
146144
continue ;
147-
} else {
148-
var n_002 = (nodeC.contents = nodeC.contents + 1 | 0, nodeC.contents);
149-
var n$1 = /* Node */[
150-
low,
151-
v,
152-
n_002,
153-
high
154-
];
155-
insert(getId(low), getId(high), v, ind, bucket, n$1);
156-
return n$1;
157145
}
146+
var n_002 = (nodeC.contents = nodeC.contents + 1 | 0, nodeC.contents);
147+
var n$1 = /* Node */[
148+
low,
149+
v,
150+
n_002,
151+
high
152+
];
153+
insert(getId(low), getId(high), v, ind, bucket, n$1);
154+
return n$1;
158155
};
159156
}
160157

jscomp/test/caml_sys_poly_fill_test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ try {
5252
tmp = Caml_sys.caml_sys_getenv("Caml_sys_poly_fill_test");
5353
}
5454
catch (exn){
55-
if (exn !== Caml_builtin_exceptions.not_found) {
55+
if (exn === Caml_builtin_exceptions.not_found) {
56+
tmp = "Z";
57+
} else {
5658
throw exn;
5759
}
58-
tmp = "Z";
5960
}
6061

6162
eq("File \"caml_sys_poly_fill_test.ml\", line 23, characters 5-12", "Z", tmp);

jscomp/test/class6_test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ var q = Curry._3(colored_point[0], void 0, 4, "blue");
111111
function lookup_obj(obj, _param) {
112112
while(true) {
113113
var param = _param;
114-
if (!param) {
115-
throw Caml_builtin_exceptions.not_found;
114+
if (param) {
115+
var obj$prime = param[0];
116+
if (Caml_obj.caml_equal(obj, obj$prime)) {
117+
return obj$prime;
118+
}
119+
_param = param[1];
120+
continue ;
116121
}
117-
var obj$prime = param[0];
118-
if (Caml_obj.caml_equal(obj, obj$prime)) {
119-
return obj$prime;
120-
}
121-
_param = param[1];
122-
continue ;
122+
throw Caml_builtin_exceptions.not_found;
123123
};
124124
}
125125

jscomp/test/class_fib_open_recursion_test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ function memo_fib_init($$class) {
6363
return Hashtbl.find(self$2[cache], x);
6464
}
6565
catch (exn){
66-
if (exn !== Caml_builtin_exceptions.not_found) {
67-
throw exn;
66+
if (exn === Caml_builtin_exceptions.not_found) {
67+
var v = Curry._2(calc$1, self$2, x);
68+
Hashtbl.add(self$2[cache], x, v);
69+
return v;
6870
}
69-
var v = Curry._2(calc$1, self$2, x);
70-
Hashtbl.add(self$2[cache], x, v);
71-
return v;
71+
throw exn;
7272
}
7373
}));
7474
return (function (env, self) {

jscomp/test/custom_error_test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ function test_js_error(param) {
1111
}
1212
catch (raw_exn){
1313
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
14-
if (exn[0] !== Js_exn.$$Error) {
15-
throw exn;
14+
if (exn[0] === Js_exn.$$Error) {
15+
console.log(exn[1].stack);
16+
return ;
1617
}
17-
console.log(exn[1].stack);
18-
return ;
18+
throw exn;
1919
}
2020
return Caml_option.some(e);
2121
}
@@ -26,10 +26,10 @@ function test_js_error2(param) {
2626
}
2727
catch (raw_e){
2828
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
29-
if (e[0] !== Js_exn.$$Error) {
29+
if (e[0] === Js_exn.$$Error) {
30+
console.log(e[1].stack);
3031
throw e;
3132
}
32-
console.log(e[1].stack);
3333
throw e;
3434
}
3535
}
@@ -41,11 +41,11 @@ function example1(param) {
4141
}
4242
catch (raw_exn){
4343
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
44-
if (exn[0] !== Js_exn.$$Error) {
45-
throw exn;
44+
if (exn[0] === Js_exn.$$Error) {
45+
console.log(exn[1].stack);
46+
return ;
4647
}
47-
console.log(exn[1].stack);
48-
return ;
48+
throw exn;
4949
}
5050
return Caml_option.some(v);
5151
}

jscomp/test/defunctor_make_test.js

+25-26
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,37 @@ function bal(l, x, d, r) {
4040
var hl = l ? l[4] : 0;
4141
var hr = r ? r[4] : 0;
4242
if (hl > (hr + 2 | 0)) {
43-
if (!l) {
43+
if (l) {
44+
var lr = l[3];
45+
var ld = l[2];
46+
var lv = l[1];
47+
var ll = l[0];
48+
if (height(ll) >= height(lr)) {
49+
return create(ll, lv, ld, create(lr, x, d, r));
50+
}
51+
if (lr) {
52+
return create(create(ll, lv, ld, lr[0]), lr[1], lr[2], create(lr[3], x, d, r));
53+
}
4454
throw [
4555
Caml_builtin_exceptions.invalid_argument,
4656
"Map.bal"
4757
];
4858
}
49-
var lr = l[3];
50-
var ld = l[2];
51-
var lv = l[1];
52-
var ll = l[0];
53-
if (height(ll) >= height(lr)) {
54-
return create(ll, lv, ld, create(lr, x, d, r));
55-
}
56-
if (lr) {
57-
return create(create(ll, lv, ld, lr[0]), lr[1], lr[2], create(lr[3], x, d, r));
58-
}
5959
throw [
6060
Caml_builtin_exceptions.invalid_argument,
6161
"Map.bal"
6262
];
63-
} else {
64-
if (hr <= (hl + 2 | 0)) {
65-
return /* Node */[
66-
l,
67-
x,
68-
d,
69-
r,
70-
hl >= hr ? hl + 1 | 0 : hr + 1 | 0
71-
];
72-
}
73-
if (!r) {
74-
throw [
75-
Caml_builtin_exceptions.invalid_argument,
76-
"Map.bal"
63+
}
64+
if (hr <= (hl + 2 | 0)) {
65+
return /* Node */[
66+
l,
67+
x,
68+
d,
69+
r,
70+
hl >= hr ? hl + 1 | 0 : hr + 1 | 0
7771
];
78-
}
72+
}
73+
if (r) {
7974
var rr = r[3];
8075
var rd = r[2];
8176
var rv = r[1];
@@ -91,6 +86,10 @@ function bal(l, x, d, r) {
9186
"Map.bal"
9287
];
9388
}
89+
throw [
90+
Caml_builtin_exceptions.invalid_argument,
91+
"Map.bal"
92+
];
9493
}
9594

9695
function add(x, data, compare, param) {

0 commit comments

Comments
 (0)