Skip to content

Commit 0921375

Browse files
fix(transform): strip definite assignment before decorators run (#44066)
1 parent b443881 commit 0921375

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎packages/playwright/src/transform/babelBundle.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins
7878
path.traverse({
7979
Class(path) {
8080
for (const member of path.get('body.body')) {
81+
// Decorators can add an initializer before TypeScript's transform runs.
82+
if (member.isClassProperty() || member.isClassPrivateProperty())
83+
member.node.definite = false;
8184
if (!member.isClassProperty())
8285
continue;
8386
const { declare, value, decorators } = member.node;

‎tests/playwright-test/decorators.spec.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616

1717
import { test, expect } from './playwright-test-fixtures';
1818

19-
test('should decorate', async ({ runInlineTest }) => {
19+
test('should decorate', {
20+
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/44065' },
21+
}, async ({ runInlineTest }) => {
2022
const result = await runInlineTest({
2123
'a.spec.ts': `
2224
import { test, expect } from '@playwright/test';
2325
test('passes', () => {
24-
new POM().greet();
26+
const pom = new POM();
27+
expect(pom.id).toBe('decorated');
28+
expect(pom.secret()).toBe('decorated');
29+
pom.greet();
2530
});
2631
2732
function loggedMethod(originalMethod: any, _context: any) {
@@ -34,7 +39,21 @@ test('should decorate', async ({ runInlineTest }) => {
3439
return replacementMethod;
3540
}
3641
42+
function initializedField(_value: undefined, _context: ClassFieldDecoratorContext) {
43+
return () => 'decorated';
44+
}
45+
3746
class POM {
47+
@initializedField
48+
id!: string;
49+
50+
@initializedField
51+
#secret!: string;
52+
53+
secret() {
54+
return this.#secret;
55+
}
56+
3857
@loggedMethod
3958
greet() {
4059
console.log('%% Hello, world!');

0 commit comments

Comments
 (0)