diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-23 14:55:08 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-23 22:55:08 +0800 |
commit | d68d155f93a355a9f6f0451150186b7fad8c58b8 (patch) | |
tree | e3cba7df7cd9d2221ff2b97ea30d8c9a07e7bba1 /test/compress | |
parent | e535f1918915251681df6ffe80a56d56672685ea (diff) | |
download | tracifyjs-d68d155f93a355a9f6f0451150186b7fad8c58b8.tar.gz tracifyjs-d68d155f93a355a9f6f0451150186b7fad8c58b8.zip |
support `class` literals (#4658)
Diffstat (limited to 'test/compress')
-rw-r--r-- | test/compress/classes.js | 574 | ||||
-rw-r--r-- | test/compress/conditionals.js | 36 | ||||
-rw-r--r-- | test/compress/exports.js | 44 | ||||
-rw-r--r-- | test/compress/hoist_props.js | 26 | ||||
-rw-r--r-- | test/compress/imports.js | 21 | ||||
-rw-r--r-- | test/compress/objects.js | 66 | ||||
-rw-r--r-- | test/compress/properties.js | 22 |
7 files changed, 783 insertions, 6 deletions
diff --git a/test/compress/classes.js b/test/compress/classes.js new file mode 100644 index 00000000..8c5575c0 --- /dev/null +++ b/test/compress/classes.js @@ -0,0 +1,574 @@ +constructor_1: { + input: { + "use strict"; + console.log(new class { + constructor(a) { + this.a = a; + } + }("PASS").a); + } + expect_exact: '"use strict";console.log(new class{constructor(a){this.a=a}}("PASS").a);' + expect_stdout: "PASS" + node_version: ">=4" +} + +constructor_2: { + input: { + "use strict"; + console.log(new class { + "constructor"(a) { + this.a = a; + } + }("PASS").a); + } + expect_exact: '"use strict";console.log(new class{constructor(a){this.a=a}}("PASS").a);' + expect_stdout: "PASS" + node_version: ">=4" +} + +constructor_3: { + input: { + "use strict"; + console.log(new class { + ["constructor"](a) { + this.a = a; + } + }("FAIL").a || "PASS"); + } + expect_exact: '"use strict";console.log(new class{["constructor"](a){this.a=a}}("FAIL").a||"PASS");' + expect_stdout: "PASS" + node_version: ">=4" +} + +constructor_4: { + input: { + "use strict"; + class A { + static constructor(a) { + console.log(a); + } + } + A.constructor("PASS"); + } + expect_exact: '"use strict";class A{static constructor(a){console.log(a)}}A.constructor("PASS");' + expect_stdout: "PASS" + node_version: ">=4" +} + +fields: { + input: { + var o = new class A { + "#p"; + static #p = "PASS"; + async + get + q() { + return A.#p; + } + ; + [6 * 7] = console ? "foo" : "bar" + }; + for (var k in o) + console.log(k, o[k]); + console.log(o.q); + } + expect_exact: 'var o=new class A{"#p";static #p="PASS";async;get q(){return A.#p}[6*7]=console?"foo":"bar"};for(var k in o)console.log(k,o[k]);console.log(o.q);' + expect_stdout: [ + "42 foo", + "#p undefined", + "async undefined", + "PASS", + ] + node_version: ">=12" +} + +methods: { + input: { + "use strict"; + class A { + static f() { + return "foo"; + } + *g() { + yield A.f(); + yield "bar"; + } + } + for (var a of new A().g()) + console.log(a); + } + expect_exact: '"use strict";class A{static f(){return"foo"}*g(){yield A.f();yield"bar"}}for(var a of(new A).g())console.log(a);' + expect_stdout: [ + "foo", + "bar", + ] + node_version: ">=4" +} + +private_methods: { + input: { + new class A { + static *#f() { + yield A.#p * 3; + } + async #g() { + for (var a of A.#f()) + return a * await 2; + } + static get #p() { + return 7; + } + get q() { + return this.#g(); + } + }().q.then(console.log); + } + expect_exact: "(new class A{static*#f(){yield 3*A.#p}async #g(){for(var a of A.#f())return a*await 2}static get #p(){return 7}get q(){return this.#g()}}).q.then(console.log);" + expect_stdout: "42" + node_version: ">=14" +} + +await: { + input: { + var await = "PASS"; + (async function() { + return await new class extends (await function() {}) { [await 42] = await }; + })().then(function(o) { + console.log(o[42]); + }); + } + expect_exact: 'var await="PASS";(async function(){return await new class extends(await function(){}){[await 42]=await}})().then(function(o){console.log(o[42])});' + expect_stdout: "PASS" + node_version: ">=12" +} + +yield: { + input: { + var a = function*() { + yield new class { [yield "foo"] = "bar" }; + }(); + console.log(a.next().value); + console.log(a.next(42).value[42]); + } + expect_exact: 'var a=function*(){yield new class{[yield"foo"]="bar"}}();console.log(a.next().value);console.log(a.next(42).value[42]);' + expect_stdout: [ + "foo", + "bar", + ] + node_version: ">=12" +} + +conditional_parenthesis: { + options = { + conditionals: true, + } + input: { + "use strict"; + if (class {}) + console.log("PASS"); + } + expect_exact: '"use strict";(class{})&&console.log("PASS");' + expect_stdout: "PASS" + node_version: ">=4" +} + +class_super: { + input: { + "use strict"; + class A { + static get p() { + return "Foo"; + } + static get q() { + return super.p || 42; + } + constructor() { + console.log("a.p", super.p, this.p); + console.log("a.q", super.q, this.q); + } + get p() { + return "foo"; + } + get q() { + return super.p || null; + } + } + class B extends A { + static get p() { + return "Bar"; + } + static get q() { + return super.p; + } + constructor() { + super(); + console.log("b.p", super.p, this.p); + console.log("b.q", super.q, this.q); + } + get p() { + return "bar"; + } + get q() { + return super.p; + } + } + console.log("A", A.p, A.q); + console.log("B", B.p, B.q); + new B(); + } + expect_exact: '"use strict";class A{static get p(){return"Foo"}static get q(){return super.p||42}constructor(){console.log("a.p",super.p,this.p);console.log("a.q",super.q,this.q)}get p(){return"foo"}get q(){return super.p||null}}class B extends A{static get p(){return"Bar"}static get q(){return super.p}constructor(){super();console.log("b.p",super.p,this.p);console.log("b.q",super.q,this.q)}get p(){return"bar"}get q(){return super.p}}console.log("A",A.p,A.q);console.log("B",B.p,B.q);new B;' + expect_stdout: [ + "A Foo 42", + "B Bar Foo", + "a.p undefined bar", + "a.q undefined foo", + "b.p foo bar", + "b.q null foo", + ] + node_version: ">=4" +} + +block_scoped: { + options = { + evaluate: true, + dead_code: true, + loops: true, + } + input: { + "use strict"; + while (0) { + class A {} + } + if (console) { + class B {} + } + console.log(typeof A, typeof B); + } + expect: { + "use strict"; + 0; + if (console) { + class B {} + } + console.log(typeof A, typeof B); + } + expect_stdout: "undefined undefined" + node_version: ">=4" +} + +keep_extends: { + options = { + toplevel: true, + unused: true, + } + input: { + "use strict"; + try { + class A extends 42 {} + } catch (e) { + console.log("PASS"); + } + } + expect: { + "use strict"; + try { + (class extends 42 {}); + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" + node_version: ">=4" +} + +drop_name: { + options = { + unused: true, + } + input: { + "use strict"; + try { + console.log(class A extends 42 {}) + } catch (e) { + console.log("PASS"); + } + } + expect: { + "use strict"; + try { + console.log(class extends 42 {}) + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" + node_version: ">=4" +} + +separate_name: { + options = { + merge_vars: true, + toplevel: true, + } + input: { + "use strict"; + class A { + constructor(v) { + this.p = v; + } + } + var a = new A("PASS"); + console.log(a.p); + } + expect: { + "use strict"; + class A { + constructor(v) { + this.p = v; + } + } + var a = new A("PASS"); + console.log(a.p); + } + expect_stdout: "PASS" + node_version: ">=4" +} + +static_side_effects: { + options = { + inline: true, + toplevel: true, + unused: true, + } + input: { + var a = "FAIL 1"; + class A { + static p = a = "PASS"; + q = a = "FAIL 2"; + } + console.log(a); + } + expect: { + var a = "FAIL 1"; + a = "PASS"; + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=12" +} + +single_use: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + "use strict"; + class A {} + console.log(typeof new A()); + } + expect: { + "use strict"; + console.log(typeof new class {}()); + } + expect_stdout: "object" + node_version: ">=4" +} + +collapse_non_strict: { + options = { + collapse_vars: true, + toplevel: true, + } + input: { + var a = 42..p++; + new class extends (a || function() { + console.log("PASS"); + }) {} + } + expect: { + var a = 42..p++; + new class extends (a || function() { + console.log("PASS"); + }) {} + } + expect_stdout: "PASS" + node_version: ">=6" +} + +collapse_rhs: { + options = { + collapse_vars: true, + } + input: { + "use strict"; + var a = "FAIL"; + a = "PASS"; + class A { + p = "PASS"; + } + console.log(a); + } + expect: { + "use strict"; + var a = "FAIL"; + a = "PASS"; + class A { + p = "PASS"; + } + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=12" +} + +collapse_rhs_static: { + options = { + collapse_vars: true, + } + input: { + "use strict"; + var a = "FAIL"; + a = "PASS"; + class A { + static p = "PASS"; + } + console.log(a); + } + expect: { + "use strict"; + var a = "FAIL"; + class A { + static p = a = "PASS"; + } + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=12" +} + +property_side_effects: { + options = { + inline: true, + keep_fargs: false, + unused: true, + } + input: { + "use strict"; + (function f(a, b) { + class A { + [a.log("PASS")]() { + b.log("FAIL"); + } + } + })(console, console); + } + expect: { + "use strict"; + (function(a) { + a.log("PASS"); + })(console, console); + } + expect_stdout: "PASS" + node_version: ">=4" +} + +property_side_effects_static: { + options = { + inline: true, + keep_fargs: false, + unused: true, + } + input: { + "use strict"; + (function f(a, b) { + class A { + static [a.log("PASS")]() { + b.log("FAIL"); + } + } + })(console, console); + } + expect: { + "use strict"; + (function(a) { + a.log("PASS"); + })(console, console); + } + expect_stdout: "PASS" + node_version: ">=4" +} + +unused_await: { + options = { + inline: true, + unused: true, + } + input: { + var await = "PASS"; + (async function() { + class A { + static p = console.log(await); + } + })(); + } + expect: { + var await = "PASS"; + (async function() { + (() => console.log(await))(); + })(); + } + expect_stdout: "PASS" + node_version: ">=12" +} + +computed_key_side_effects: { + options = { + evaluate: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + "use strict"; + var a = 0; + class A { + [(a++, 0)]() {} + } + console.log(a); + } + expect: { + "use strict"; + console.log(1); + } + expect_stdout: "1" + node_version: ">=4" +} + +computed_key_generator: { + options = { + unused: true, + } + input: { + "use strict"; + var a = function*() { + class A { + static [console.log(yield)]() {} + } + }(); + a.next("FAIL"); + a.next("PASS"); + } + expect: { + "use strict"; + var a = function*() { + console.log(yield); + }(); + a.next("FAIL"); + a.next("PASS"); + } + expect_stdout: "PASS" + node_version: ">=4" +} diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index 64088eb2..4187c152 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -1861,3 +1861,39 @@ issue_3808_2: { } expect_stdout: " PASS" } + +object_super: { + options = { + conditionals: true, + } + input: { + Object.setPrototypeOf({ + f(a) { + a ? this.g("FAIL") : super.g("FAIL"); + }, + g(b) { + console.log(b); + }, + }, { + g() { + console.log("PASS"); + }, + }).f(); + } + expect: { + Object.setPrototypeOf({ + f(a) { + a ? this.g("FAIL") : super.g("FAIL"); + }, + g(b) { + console.log(b); + }, + }, { + g() { + console.log("PASS"); + }, + }).f(); + } + expect_stdout: "PASS" + node_version: ">=4" +} diff --git a/test/compress/exports.js b/test/compress/exports.js index 24c1d8c0..d3a4ffce 100644 --- a/test/compress/exports.js +++ b/test/compress/exports.js @@ -17,22 +17,25 @@ var_defs: { defuns: { input: { + export class A {} export function e() {} export function* f(a) {} export async function g(b, c) {} export async function* h({}, ...[]) {} } - expect_exact: "export function e(){}export function*f(a){}export async function g(b,c){}export async function*h({},...[]){}" + expect_exact: "export class A{}export function e(){}export function*f(a){}export async function g(b,c){}export async function*h({},...[]){}" } defaults: { input: { export default 42; + export default async; export default (x, y) => x * x; + export default class {}; export default function*(a, b) {}; export default async function f({ c }, ...[ d ]) {}; } - expect_exact: "export default 42;export default(x,y)=>x*x;export default function*(a,b){}export default async function f({c:c},...[d]){}" + expect_exact: "export default 42;export default async;export default(x,y)=>x*x;export default class{}export default function*(a,b){}export default async function f({c:c},...[d]){}" } defaults_parenthesis_1: { @@ -88,18 +91,22 @@ drop_unused: { input: { export default 42; export default (x, y) => x * x; - export default function*(a, b) {}; - export default async function f({ c }, ...[ d ]) {}; + export default class A extends B { get p() { h() } } + export default function*(a, b) {} + export default async function f({ c }, ...[ d ]) {} export var e; export function g(x, [ y ], ...z) {} + function h() {} } expect: { export default 42; export default (x, y) => x * x; - export default function*(a, b) {}; - export default async function({}) {}; + export default class extends B { get p() { h() } } + export default function*(a, b) {} + export default async function({}) {} export var e; export function g(x, []) {} + function h() {} } } @@ -195,3 +202,28 @@ hoist_exports: { export { f as bbb, o as ccc, c as fff }; } } + +keep_return_values: { + options = { + booleans: true, + evaluate: true, + reduce_vars: true, + toplevel: true, + } + input: { + export default function() { + return []; + } + export default function f() { + return null; + } + } + expect: { + export default function() { + return []; + } + export default function f() { + return null; + } + } +} diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js index dbee9b42..597db4bc 100644 --- a/test/compress/hoist_props.js +++ b/test/compress/hoist_props.js @@ -1068,3 +1068,29 @@ issue_4023: { } expect_stdout: "true" } + +object_super: { + options = { + hoist_props: true, + reduce_vars: true, + toplevel: true, + } + input: { + var o = { + f(a) { + return a ? console.log("PASS") : super.log("PASS"); + }, + }; + o.f(42); + } + expect: { + var o = { + f(a) { + return a ? console.log("PASS") : super.log("PASS"); + }, + }; + o.f(42); + } + expect_stdout: "PASS" + node_version: ">=4" +} diff --git a/test/compress/imports.js b/test/compress/imports.js index f8313b06..558dd6a8 100644 --- a/test/compress/imports.js +++ b/test/compress/imports.js @@ -144,3 +144,24 @@ keep_ref: { foo(); } } + +forbid_merge: { + options = { + merge_vars: true, + toplevel: true, + } + input: { + import A from "foo"; + export default class extends A {} + var f = () => () => {}; + f(); + f(); + } + expect: { + import A from "foo"; + export default class extends A {} + var f = () => () => {}; + f(); + f(); + } +} diff --git a/test/compress/objects.js b/test/compress/objects.js index 67acefd6..3c7f2d18 100644 --- a/test/compress/objects.js +++ b/test/compress/objects.js @@ -280,6 +280,72 @@ shorthand_keywords: { node_version: ">=6" } +object_super: { + input: { + var o = { + f() { + return super.p; + }, + p: "FAIL", + }; + Object.setPrototypeOf(o, { p: "PASS" }); + console.log(o.f()); + } + expect_exact: 'var o={f(){return super.p},p:"FAIL"};Object.setPrototypeOf(o,{p:"PASS"});console.log(o.f());' + expect_stdout: "PASS" + node_version: ">=4" +} + +object_super_async: { + input: { + var o = { + async f() { + return super.p; + }, + p: "FAIL", + }; + Object.setPrototypeOf(o, { p: "PASS" }); + o.f().then(console.log); + } + expect_exact: 'var o={async f(){return super.p},p:"FAIL"};Object.setPrototypeOf(o,{p:"PASS"});o.f().then(console.log);' + expect_stdout: "PASS" + node_version: ">=8" +} + +object_super_generator: { + input: { + var o = { + *f() { + yield super.p; + }, + p: "FAIL", + }; + Object.setPrototypeOf(o, { p: "PASS" }); + console.log(o.f().next().value); + } + expect_exact: 'var o={*f(){yield super.p},p:"FAIL"};Object.setPrototypeOf(o,{p:"PASS"});console.log(o.f().next().value);' + expect_stdout: "PASS" + node_version: ">=4" +} + +object_super_async_generator: { + input: { + var o = { + async *f() { + return super.p; + }, + p: "FAIL", + }; + Object.setPrototypeOf(o, { p: "PASS" }); + o.f().next().then(function(v) { + console.log(v.value, v.done); + }); + } + expect_exact: 'var o={async*f(){return super.p},p:"FAIL"};Object.setPrototypeOf(o,{p:"PASS"});o.f().next().then(function(v){console.log(v.value,v.done)});' + expect_stdout: "PASS true" + node_version: ">=10" +} + issue_4269_1: { options = { evaluate: true, diff --git a/test/compress/properties.js b/test/compress/properties.js index 43f08b42..eb47624a 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -1378,3 +1378,25 @@ issue_3389: { } expect_stdout: "PASS" } + +object_super: { + options = { + properties: true, + } + input: { + ({ + f(a) { + return a ? console.log("PASS") : super.log("PASS"); + }, + }).f(console); + } + expect: { + ({ + f(a) { + return a ? console.log("PASS") : super.log("PASS"); + }, + }).f(console); + } + expect_stdout: "PASS" + node_version: ">=4" +} |