Skip to content

Commit 6d0227e

Browse files
Merge pull request #1032 from JLHwung/fix-1028
2 parents f051377 + 335cb88 commit 6d0227e

File tree

12 files changed

+1053
-805
lines changed

12 files changed

+1053
-805
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"watch": "gulp watch"
2020
},
2121
"devDependencies": {
22-
"@babel/core": "^7.9.0",
23-
"@babel/plugin-transform-block-scoping": "^7.8.3",
24-
"@babel/preset-env": "^7.9.0",
22+
"@babel/core": "^7.17.0",
23+
"@babel/plugin-transform-block-scoping": "^7.16.7",
24+
"@babel/preset-env": "^7.17.10",
2525
"babel-jest": "^24.9.0",
2626
"butternut": "^0.4.6",
2727
"bytes": "^3.0.0",

packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/fn-params/expected.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ function foo() {
2525
set a(b) {}
2626

2727
};
28-
29-
class A {
28+
return new class {
3029
set c(d) {
3130
x.a = 5;
3231
}
3332

34-
}
35-
36-
return new A();
33+
}();
3734
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Foo {
2+
get all() {
3+
return false;
4+
}
5+
constructor() {
6+
var _this = this;
7+
(function () {
8+
return _this.all != null;
9+
}).call(document);
10+
}
11+
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Foo {
2+
get all() {
3+
return false;
4+
}
5+
6+
constructor() {
7+
var _this = this;
8+
9+
(function () {
10+
return _this.all != null;
11+
}).call(document);
12+
}
13+
14+
}

packages/babel-plugin-minify-dead-code-elimination/src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,29 @@ module.exports = ({ types: t, traverse }) => {
371371
refPath.scope.getBinding(replacement.name) === binding &&
372372
binding.constantViolations.length === 0
373373
);
374+
} else if (replacementPath.isThisExpression()) {
375+
bail = true;
374376
} else {
375377
replacementPath.traverse({
376378
Function(path) {
377379
path.skip();
378380
},
379381

382+
ThisExpression(path) {
383+
bail = true;
384+
path.stop();
385+
},
386+
380387
ReferencedIdentifier({ node }) {
381-
if (bail) {
382-
return;
383-
}
384388
const binding = scope.getBinding(node.name);
385389
if (
386390
binding &&
387391
refPath.scope.getBinding(node.name) === binding
388392
) {
389393
bail = binding.constantViolations.length > 0;
394+
if (bail) {
395+
path.stop();
396+
}
390397
}
391398
}
392399
});

packages/babel-plugin-minify-mangle-names/__tests__/fixtures/constant-violations-2/expected.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
!function () {
2-
var b = 1;
3-
b--;
4-
var b = 10;
5-
a(b);
2+
var a = 1;
3+
a--;
4+
var a = 10;
5+
b(a);
66

7-
function a() {
7+
function b() {
88
var a = 10;
99
a++;
1010
var a = 20;
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
function d3_svg_line(a) {
2-
var c = d3_geom_pointX,
3-
d = d3_geom_pointY,
2+
var b = d3_geom_pointX,
3+
c = d3_geom_pointY,
44
e = d3_true,
55
f = d3_svg_lineLinear,
66
g = f.key,
77
h = 0.7;
88

9-
function b(b) {
10-
var j = [],
11-
k = [],
12-
l = -1,
13-
m = b.length,
14-
n,
15-
o = d3_functor(c);
9+
function i(c) {
10+
var g = [],
11+
j = [],
12+
k = -1,
13+
l = c.length,
14+
m,
15+
n = d3_functor(b);
1616

17-
function g() {
18-
j.push("M", f(a(k), h));
17+
function o() {
18+
g.push("M", f(a(j), h));
1919
}
2020

21-
while (++l < m) {
22-
if (e.call(this, n = b[l], l)) {
23-
k.push([+o.call(this, n, l)]);
24-
} else if (k.length) {
25-
g();
26-
k = [];
21+
while (++k < l) {
22+
if (e.call(this, m = c[k], k)) {
23+
j.push([+n.call(this, m, k)]);
24+
} else if (j.length) {
25+
o();
26+
j = [];
2727
}
2828
}
2929

30-
if (k.length) g();
31-
return j.length ? j.join("") : null;
30+
if (j.length) o();
31+
return g.length ? g.join("") : null;
3232
}
3333

34-
return b;
34+
return i;
3535
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(function () {
22
class Foo {}
33

4-
const b = class Bar extends Foo {};
5-
var c = class Baz {};
4+
const a = class Bar extends Foo {};
5+
var b = class Baz {};
66

7-
function a() {
8-
new c();
7+
function c() {
8+
new b();
99
}
1010

11-
a();
11+
c();
1212
})();
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
function Foo() {
2-
var ba, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
2+
var aa, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
33
var z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y;
44
var Z, $;
5-
ba, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
5+
aa, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
66
z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y;
77
Z, $;
88

9-
function aa() {
10-
var aa, a, b, c, d, e, f, g, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
9+
function _() {
10+
var h, a, b, c, d, e, f, g, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
1111
var z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y;
1212
var Z, $;
13-
aa, a, b, c, d, e, f, g, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
13+
h, a, b, c, d, e, f, g, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
1414
z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y;
1515
Z, $;
1616

17-
function h() {
17+
function _() {
1818
var aa, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y;
1919
var z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y;
2020
var Z, $;
@@ -23,8 +23,8 @@ function Foo() {
2323
Z, $;
2424
}
2525

26-
h();
26+
_();
2727
}
2828

29-
aa();
29+
_();
3030
}

packages/babel-preset-minify/__tests__/minify-env-tests.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ describe("preset along with env", () => {
5050
}
5151
`,
5252
`
53+
function _defineProperties(a, b) { for (var c, d = 0; d < b.length; d++) c = b[d], c.enumerable = c.enumerable || !1, c.configurable = !0, "value" in c && (c.writable = !0), Object.defineProperty(a, c.key, c); }
54+
55+
function _createClass(a, b, c) { return b && _defineProperties(a.prototype, b), c && _defineProperties(a, c), Object.defineProperty(a, "prototype", { writable: !1 }), a; }
56+
5357
function _classCallCheck(a, b) { if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); }
5458
5559
function a() {
56-
return function a() {
60+
var a = /*#__PURE__*/_createClass(function a() {
5761
"use strict";
5862
5963
_classCallCheck(this, a);
60-
};
64+
});
65+
66+
return a;
6167
}
6268
`
6369
);
@@ -237,16 +243,20 @@ describe("preset along with env", () => {
237243
}
238244
`,
239245
`
246+
function _defineProperties(a, b) { for (var c, d = 0; d < b.length; d++) c = b[d], c.enumerable = c.enumerable || !1, c.configurable = !0, "value" in c && (c.writable = !0), Object.defineProperty(a, c.key, c); }
247+
248+
function _createClass(a, b, c) { return b && _defineProperties(a.prototype, b), c && _defineProperties(a, c), Object.defineProperty(a, "prototype", { writable: !1 }), a; }
249+
240250
function _classCallCheck(a, b) { if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); }
241251
242252
function bar() {
243253
var c = console;
244254
return {
245-
a: function b(a) {
255+
a: /*#__PURE__*/_createClass(function d(b) {
246256
"use strict";
247257
248-
_classCallCheck(this, b), c.log(a);
249-
}
258+
_classCallCheck(this, d), c.log(b);
259+
})
250260
};
251261
}
252262
`
@@ -261,13 +271,17 @@ describe("preset along with env", () => {
261271
exports.A = A;
262272
`,
263273
`
274+
function _defineProperties(a, b) { for (var c, d = 0; d < b.length; d++) c = b[d], c.enumerable = c.enumerable || !1, c.configurable = !0, "value" in c && (c.writable = !0), Object.defineProperty(a, c.key, c); }
275+
276+
function _createClass(a, b, c) { return b && _defineProperties(a.prototype, b), c && _defineProperties(a, c), Object.defineProperty(a, "prototype", { writable: !1 }), a; }
277+
264278
function _classCallCheck(a, b) { if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); }
265279
266-
var A = function a() {
280+
var A = /*#__PURE__*/_createClass(function a() {
267281
"use strict";
268282
269283
_classCallCheck(this, a);
270-
};
284+
});
271285
272286
A.B = {}, exports.A = A;
273287
`

0 commit comments

Comments
 (0)