From 956d234b14c1c4e78f141eaa7006831d0eb8373d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 18 Oct 2022 17:42:51 -0400 Subject: [PATCH 1/2] rename test cases --- .../exec.js | 3 +++ .../fixtures/2022-03-ordering/{decorators => field}/exec.js | 0 .../test/fixtures/2022-03-ordering/initializers/exec.js | 3 +++ 3 files changed, 6 insertions(+) rename packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/{accessor-initializers => accessor-static-accessor-initializers}/exec.js (92%) rename packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/{decorators => field}/exec.js (100%) diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-accessor-initializers/exec.js similarity index 92% rename from packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers/exec.js rename to packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-accessor-initializers/exec.js index 892ced55301d..448854862950 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-accessor-initializers/exec.js @@ -42,6 +42,9 @@ class A { accessor #d; } +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + new A(); var nums = Array.from({ length: 38 }, (_, i) => i); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/decorators/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/field/exec.js similarity index 100% rename from packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/decorators/exec.js rename to packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/field/exec.js diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/initializers/exec.js index 35e07dd009a1..2a43cb651ee9 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/initializers/exec.js @@ -47,6 +47,9 @@ class A { accessor #h; } +var nums = Array.from({ length: 46 }, (_, i) => i); +expect(log).toEqual(nums); + new A(); var nums = Array.from({ length: 54 }, (_, i) => i); From 4bf20185d8367e968d426c1bdc9409a7737fbc99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 18 Oct 2022 19:18:13 -0400 Subject: [PATCH 2/2] fix: protoInit call should not be injected to static field initializer --- .../src/transformer-2022-03.ts | 7 +- .../accessor-initializers-fields/exec.js | 67 ++++++++++++++++ .../accessor-method-initializers/exec.js | 75 ++++++++++++++++++ .../exec.js | 3 + .../exec.js | 77 +++++++++++++++++++ .../decorators/exec.js | 31 -------- .../2022-03-ordering--to-es2015/field/exec.js | 31 ++++++++ .../initializers/exec.js | 3 + .../exec.js | 74 ++++++++++++++++++ .../accessor-initializers-fields/exec.js | 67 ++++++++++++++++ .../accessor-method-initializers/exec.js | 75 ++++++++++++++++++ .../exec.js | 77 +++++++++++++++++++ .../exec.js | 74 ++++++++++++++++++ 13 files changed, 629 insertions(+), 32 deletions(-) create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers-fields/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-method-initializers/exec.js rename packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/{accessor-initializers => accessor-static-accessor-initializers}/exec.js (92%) create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-method-initializers/exec.js delete mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/decorators/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/field/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/static-accessor-method-initializers/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers-fields/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-method-initializers/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-method-initializers/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/static-accessor-method-initializers/exec.js diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2022-03.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2022-03.ts index ec961bf53366..d5df7d579185 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2022-03.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2022-03.ts @@ -524,6 +524,7 @@ function transformClass( const elementDecoratorInfo: (DecoratorInfo | ComputedPropInfo)[] = []; + // The initializer of the first non-static field will be injected with the protoInit call let firstFieldPath: | NodePath | undefined; @@ -767,7 +768,11 @@ function transformClass( element.node.decorators = null; } - if (!firstFieldPath && (kind === FIELD || kind === ACCESSOR)) { + if ( + !firstFieldPath && + !isStatic && + (kind === FIELD || kind === ACCESSOR) + ) { firstFieldPath = element as NodePath< t.ClassProperty | t.ClassPrivateProperty >; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers-fields/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers-fields/exec.js new file mode 100644 index 000000000000..a59b51cc2314 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers-fields/exec.js @@ -0,0 +1,67 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logFieldDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 27) + @logAccessorDecoratorRun(3, 10, 22, 26) + accessor a; + + @logFieldDecoratorRun(4, 15) + @logFieldDecoratorRun(5, 14) + b; + + @logFieldDecoratorRun(6, 17) + @logFieldDecoratorRun(7, 16) + #c; + + @logAccessorDecoratorRun(8, 13, 25, 29) + @logAccessorDecoratorRun(9, 12, 24, 28) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-method-initializers/exec.js new file mode 100644 index 000000000000..a3efd8e40a6d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-method-initializers/exec.js @@ -0,0 +1,75 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 31) + @logAccessorDecoratorRun(3, 10, 22, 30) + accessor a; + + @logMethodDecoratorRun(4, 13, 25, 35) + @logMethodDecoratorRun(5, 12, 24, 34) + b() {}; + + @logMethodDecoratorRun(6, 15, 27, 37) + @logMethodDecoratorRun(7, 14, 26, 36) + #c() {}; + + @logAccessorDecoratorRun(8, 17, 29, 33) + @logAccessorDecoratorRun(9, 16, 28, 32) + accessor #d; + + constructor() { + this.b(); + this.#c(); + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-accessor-initializers/exec.js similarity index 92% rename from packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers/exec.js rename to packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-accessor-initializers/exec.js index 892ced55301d..448854862950 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-accessor-initializers/exec.js @@ -42,6 +42,9 @@ class A { accessor #d; } +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + new A(); var nums = Array.from({ length: 38 }, (_, i) => i); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-method-initializers/exec.js new file mode 100644 index 000000000000..d940e8c8f379 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/accessor-static-method-initializers/exec.js @@ -0,0 +1,77 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + static { + A.b(), A.#c(); + } + + @logAccessorDecoratorRun(2, 15, 31, 35) + @logAccessorDecoratorRun(3, 14, 30, 34) + accessor a; + + @logMethodDecoratorRun(4, 11, 21, 25) + @logMethodDecoratorRun(5, 10, 20, 24) + static b() {}; + + @logMethodDecoratorRun(6, 13, 23, 27) + @logMethodDecoratorRun(7, 12, 22, 26) + static #c() {}; + + @logAccessorDecoratorRun(8, 17, 33, 37) + @logAccessorDecoratorRun(9, 16, 32, 36) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/decorators/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/decorators/exec.js deleted file mode 100644 index f44656aaee7c..000000000000 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/decorators/exec.js +++ /dev/null @@ -1,31 +0,0 @@ -var log = []; - -function push(x) { log.push(x); return x; } - -function logDecoratorRun(a, b) { - push(a); - return function (el) { push(b); return el; }; -} - -@logDecoratorRun(0, 19) -@logDecoratorRun(1, 18) -class A { - @logDecoratorRun(2, 15) - @logDecoratorRun(3, 14) - a; - - @logDecoratorRun(4, 11) - @logDecoratorRun(5, 10) - static b; - - @logDecoratorRun(6, 13) - @logDecoratorRun(7, 12) - static #c; - - @logDecoratorRun(8, 17) - @logDecoratorRun(9, 16) - #d; -} - -var nums = Array.from({ length: 20 }, (_, i) => i); -expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/field/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/field/exec.js new file mode 100644 index 000000000000..7a51e8b93b7e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/field/exec.js @@ -0,0 +1,31 @@ +var log = []; + +function push(x) { log.push(x); return x; } + +function logDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logDecoratorRun(0, 21) +@logDecoratorRun(1, 20) +class A { + @logDecoratorRun(2, 17) + @logDecoratorRun(3, 16) + [push(4)]; + + @logDecoratorRun(5, 13) + @logDecoratorRun(6, 12) + static [push(7)]; + + @logDecoratorRun(8, 15) + @logDecoratorRun(9, 14) + static #c; + + @logDecoratorRun(10, 19) + @logDecoratorRun(11, 18) + #d; +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/initializers/exec.js index 35e07dd009a1..2a43cb651ee9 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/initializers/exec.js @@ -47,6 +47,9 @@ class A { accessor #h; } +var nums = Array.from({ length: 46 }, (_, i) => i); +expect(log).toEqual(nums); + new A(); var nums = Array.from({ length: 54 }, (_, i) => i); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/static-accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/static-accessor-method-initializers/exec.js new file mode 100644 index 000000000000..e1ec45be3190 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering--to-es2015/static-accessor-method-initializers/exec.js @@ -0,0 +1,74 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + @logMethodDecoratorRun(2, 15, 31, 35) + @logMethodDecoratorRun(3, 14, 30, 34) + a() {} + + @logAccessorDecoratorRun(4, 11, 21, 25) + @logAccessorDecoratorRun(5, 10, 20, 24) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 27) + @logAccessorDecoratorRun(7, 12, 22, 26) + static accessor #c; + + @logMethodDecoratorRun(8, 17, 33, 37) + @logMethodDecoratorRun(9, 16, 32, 36) + #d() {} + + constructor() { + this.a(); + this.#d(); + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers-fields/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers-fields/exec.js new file mode 100644 index 000000000000..a59b51cc2314 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-initializers-fields/exec.js @@ -0,0 +1,67 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logFieldDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 27) + @logAccessorDecoratorRun(3, 10, 22, 26) + accessor a; + + @logFieldDecoratorRun(4, 15) + @logFieldDecoratorRun(5, 14) + b; + + @logFieldDecoratorRun(6, 17) + @logFieldDecoratorRun(7, 16) + #c; + + @logAccessorDecoratorRun(8, 13, 25, 29) + @logAccessorDecoratorRun(9, 12, 24, 28) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-method-initializers/exec.js new file mode 100644 index 000000000000..a3efd8e40a6d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-method-initializers/exec.js @@ -0,0 +1,75 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 31) + @logAccessorDecoratorRun(3, 10, 22, 30) + accessor a; + + @logMethodDecoratorRun(4, 13, 25, 35) + @logMethodDecoratorRun(5, 12, 24, 34) + b() {}; + + @logMethodDecoratorRun(6, 15, 27, 37) + @logMethodDecoratorRun(7, 14, 26, 36) + #c() {}; + + @logAccessorDecoratorRun(8, 17, 29, 33) + @logAccessorDecoratorRun(9, 16, 28, 32) + accessor #d; + + constructor() { + this.b(); + this.#c(); + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-method-initializers/exec.js new file mode 100644 index 000000000000..d940e8c8f379 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/accessor-static-method-initializers/exec.js @@ -0,0 +1,77 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + static { + A.b(), A.#c(); + } + + @logAccessorDecoratorRun(2, 15, 31, 35) + @logAccessorDecoratorRun(3, 14, 30, 34) + accessor a; + + @logMethodDecoratorRun(4, 11, 21, 25) + @logMethodDecoratorRun(5, 10, 20, 24) + static b() {}; + + @logMethodDecoratorRun(6, 13, 23, 27) + @logMethodDecoratorRun(7, 12, 22, 26) + static #c() {}; + + @logAccessorDecoratorRun(8, 17, 33, 37) + @logAccessorDecoratorRun(9, 16, 32, 36) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/static-accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/static-accessor-method-initializers/exec.js new file mode 100644 index 000000000000..e1ec45be3190 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-ordering/static-accessor-method-initializers/exec.js @@ -0,0 +1,74 @@ +var log = []; + +function push(x) { + log.push(x); + return x; +} + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + @logMethodDecoratorRun(2, 15, 31, 35) + @logMethodDecoratorRun(3, 14, 30, 34) + a() {} + + @logAccessorDecoratorRun(4, 11, 21, 25) + @logAccessorDecoratorRun(5, 10, 20, 24) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 27) + @logAccessorDecoratorRun(7, 12, 22, 26) + static accessor #c; + + @logMethodDecoratorRun(8, 17, 33, 37) + @logMethodDecoratorRun(9, 16, 32, 36) + #d() {} + + constructor() { + this.a(); + this.#d(); + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums);