Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80728 views
1
ifs_1: {
2
options = {
3
conditionals: true
4
};
5
input: {
6
if (foo) bar();
7
if (!foo); else bar();
8
if (foo); else bar();
9
if (foo); else;
10
}
11
expect: {
12
foo&&bar();
13
foo&&bar();
14
foo||bar();
15
foo;
16
}
17
}
18
19
ifs_2: {
20
options = {
21
conditionals: true
22
};
23
input: {
24
if (foo) {
25
x();
26
} else if (bar) {
27
y();
28
} else if (baz) {
29
z();
30
}
31
32
if (foo) {
33
x();
34
} else if (bar) {
35
y();
36
} else if (baz) {
37
z();
38
} else {
39
t();
40
}
41
}
42
expect: {
43
foo ? x() : bar ? y() : baz && z();
44
foo ? x() : bar ? y() : baz ? z() : t();
45
}
46
}
47
48
ifs_3_should_warn: {
49
options = {
50
conditionals : true,
51
dead_code : true,
52
evaluate : true,
53
booleans : true
54
};
55
input: {
56
if (x && !(x + "1") && y) { // 1
57
var qq;
58
foo();
59
} else {
60
bar();
61
}
62
63
if (x || !!(x + "1") || y) { // 2
64
foo();
65
} else {
66
var jj;
67
bar();
68
}
69
}
70
expect: {
71
var qq; bar(); // 1
72
var jj; foo(); // 2
73
}
74
}
75
76
ifs_4: {
77
options = {
78
conditionals: true
79
};
80
input: {
81
if (foo && bar) {
82
x(foo)[10].bar.baz = something();
83
} else
84
x(foo)[10].bar.baz = something_else();
85
}
86
expect: {
87
x(foo)[10].bar.baz = (foo && bar) ? something() : something_else();
88
}
89
}
90
91
ifs_5: {
92
options = {
93
if_return: true,
94
conditionals: true,
95
comparisons: true,
96
};
97
input: {
98
function f() {
99
if (foo) return;
100
bar();
101
baz();
102
}
103
function g() {
104
if (foo) return;
105
if (bar) return;
106
if (baz) return;
107
if (baa) return;
108
a();
109
b();
110
}
111
}
112
expect: {
113
function f() {
114
if (!foo) {
115
bar();
116
baz();
117
}
118
}
119
function g() {
120
if (!(foo || bar || baz || baa)) {
121
a();
122
b();
123
}
124
}
125
}
126
}
127
128
ifs_6: {
129
options = {
130
conditionals: true,
131
comparisons: true
132
};
133
input: {
134
if (!foo && !bar && !baz && !boo) {
135
x = 10;
136
} else {
137
x = 20;
138
}
139
}
140
expect: {
141
x = foo || bar || baz || boo ? 20 : 10;
142
}
143
}
144
145