Skip to content

Commit 86226f8

Browse files
committed
test(minify): cover the private-method, spread and proto gaps
Gated on private methods and optional chaining, which Node 14.6 is the first to parse when the harness executes the bundle.
1 parent dcd545c commit 86226f8

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Counter {
2+
#used() {
3+
return 1;
4+
}
5+
6+
#unusedMethod() {
7+
return 2;
8+
}
9+
10+
get #unusedGetter() {
11+
return 3;
12+
}
13+
14+
static #unusedStatic() {
15+
return 4;
16+
}
17+
18+
read() {
19+
return this.#used();
20+
}
21+
}
22+
23+
function counted(log) {
24+
return {
25+
*[Symbol.iterator]() {
26+
log.push("next");
27+
yield 1;
28+
}
29+
};
30+
}
31+
32+
function spreadOnly(log) {
33+
(() => {})(...counted(log));
34+
(function () {})(log.push("arg"), ...counted(log));
35+
return log;
36+
}
37+
38+
function sequenceTail(g) {
39+
return [(g(), {})?.b, (g(), [1])?.length];
40+
}
41+
42+
function protoPattern(o) {
43+
const { ["__proto__"]: read } = o;
44+
const literal = { ["__proto__"]: null };
45+
return [read === Object.prototype, Object.getPrototypeOf(literal) === Object.prototype];
46+
}
47+
48+
it("should drop only the private members nothing reads", () => {
49+
expect(new Counter().read()).toBe(1);
50+
});
51+
52+
it("should keep the iteration an empty function's spread arguments run", () => {
53+
expect(spreadOnly([])).toEqual(["next", "arg", "next"]);
54+
});
55+
56+
it("should read a sequence's last value without the needless optional link", () => {
57+
const calls = [];
58+
expect(sequenceTail(() => calls.push(1))).toEqual([undefined, 1]);
59+
expect(calls).toEqual([1, 1]);
60+
});
61+
62+
it("should read `__proto__` in a pattern as the computed key does", () => {
63+
expect(protoPattern({})).toEqual([true, true]);
64+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
module.exports = {
7+
findBundle() {
8+
return "bundle0.js";
9+
},
10+
afterExecute(options) {
11+
const code = fs.readFileSync(
12+
path.join(options.output.path, "bundle0.js"),
13+
"utf8"
14+
);
15+
expect(code).not.toContain("#unused");
16+
expect(code).toContain("#used");
17+
expect(code).toContain("{__proto__:");
18+
expect(code).toContain('{["__proto__"]:');
19+
expect(code).not.toContain("=>{})(...");
20+
}
21+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
// The fixture uses private methods and optional chaining, which Node 14.6
4+
// is the first to parse when the harness executes the bundle.
5+
module.exports = function filter() {
6+
try {
7+
eval("class A { #m() {} static #s() {} get #g() { return a?.b; } }");
8+
return true;
9+
} catch (_err) {
10+
return false;
11+
}
12+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "production",
6+
target: ["web", "es2022"],
7+
experiments: { futureDefaults: true },
8+
output: { filename: "bundle0.js", module: false },
9+
optimization: {
10+
minimizer: ["..."],
11+
minimize: {
12+
javascript: {
13+
compress: { passes: 2 },
14+
mangle: false,
15+
keep_fnames: true,
16+
keep_classnames: true
17+
}
18+
}
19+
}
20+
};

0 commit comments

Comments
 (0)