/
babel-plugin-transform-remove-undefined-test.js
215 lines (194 loc) · 5.24 KB
/
babel-plugin-transform-remove-undefined-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
jest.autoMockOff();
const babel = require("babel-core");
const plugin = require("../src/index");
const unpad = require("../../../utils/unpad");
function transform(code) {
return babel.transform(code, {
plugins: [plugin],
}).code;
}
describe("transform-remove-undefined-plugin", () => {
it("should remove multiple undefined assignments in 1 statement", () => {
const source = "let a = undefined, b = 3, c = undefined, d;";
const expected = "let a,\n b = 3,\n c,\n d;";
expect(transform(source)).toBe(expected);
});
it("should remove let-assignments to undefined", () => {
const source = "let a = undefined;";
const expected = "let a;";
expect(transform(source)).toBe(expected);
});
it("should remove let-assignments to void 0", () => {
const source = "let a = void 0;";
const expected = "let a;";
expect(transform(source)).toBe(expected);
});
it("should not remove const-assignments to undefined", () => {
const source = "const a = undefined;";
expect(transform(source)).toBe(source);
});
it("should remove undefined return value", () => {
const source = unpad(`
function foo() {
return undefined;
}`);
const expected = unpad(`
function foo() {
return;
}`);
expect(transform(source)).toBe(expected);
});
it("should remove var declarations in functions", () => {
const source = unpad(`
function foo() {
var a = undefined;
}`);
const expected = unpad(`
function foo() {
var a;
}`);
expect(transform(source)).toBe(expected);
});
it("should remove let-assignments in inner blocks", () => {
const source = unpad(`
let a = 1;
{
let a = undefined;
}`);
const expected = unpad(`
let a = 1;
{
let a;
}`);
expect(transform(source)).toBe(expected);
});
it("should remove var-assignments in loops if no modify", () => {
const source = unpad(`
for (var a = undefined;;) {
var b = undefined;
}`);
const expected = unpad(`
for (var a;;) {
var b;
}`);
expect(transform(source)).toBe(expected);
});
it("should not remove var-assignments in loops if modify", () => {
const source = unpad(`
for (var a;;) {
var b = undefined;
console.log(b);
b = 3;
}`);
expect(transform(source)).toBe(source);
});
it("should not remove var-assignments if referenced before", () => {
const source = unpad(`
function foo() {
a = 3;
var a = undefined;
}`);
expect(transform(source)).toBe(source);
});
it("should not remove nested var-assignments if referenced before", () => {
const source = unpad(`
function foo() {
aa = 3;
var { a: aa, b: bb } = undefined;
}`);
expect(transform(source)).toBe(source);
});
it("should not remove if lval is reference before via a function", () => {
const source = unpad(`
function foo() {
bar();
var x = undefined;
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(source);
});
it("should remove if not referenced in any way before", () => {
const source = unpad(`
function foo() {
var x = undefined;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
const expected = unpad(`
function foo() {
var x;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(expected);
});
it("should not remove on mutually recursive function", () => {
const source = unpad(`
function foo() {
a();
var c = undefined;
function a() {
b();
}
function b() {
a();
c = 3;
}
}`);
expect(transform(source)).toBe(source);
});
it("should not remove if rval has side effects", () => {
const source = unpad(`
function foo() {
var a = void b();
return void bar();
}`);
expect(transform(source)).toBe(source);
});
it("should remove from sequence expressions", () => {
const source = unpad(`
a = b, void 0, b = c, d.e.f(), void 0, hello.world();
`);
const expected = unpad(`
a = b, b = c, d.e.f(), hello.world();
`);
expect(transform(source)).toBe(expected);
});
it("should NOT remove last undefined from sequence expressions", () => {
const source = unpad(`
if (foo.bar(), void 0) {
foo.baz();
}
function bar() {
return a.b(), void 0;
}
`);
expect(transform(source)).toBe(source);
});
it("should remove last undefined from sequence expressions if safe", () => {
const source = unpad(`
a = b, void 0, b = c, d.e.f(), void 0, hello.world(), void 0;
`);
const expected = unpad(`
a = b, b = c, d.e.f(), hello.world();
`);
expect(transform(source)).toBe(expected);
});
it("should remove not remove local undefined", () => {
const source = unpad(`
function foo(undefined) {
a = b, undefined;
return undefined;
}`);
expect(transform(source)).toBe(source);
});
});