Skip to content

Commit 3f0bd8c

Browse files
committed
fix(transformer): Store reference to parent generic to avoid a rebound this
1 parent c35347b commit 3f0bd8c

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/transformer/genericDeclaration/genericDeclaration.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ts from 'typescript';
22
import { GetDescriptor } from '../descriptor/descriptor';
33
import { TypescriptHelper } from '../descriptor/helper/helper';
44
import { TypescriptCreator } from '../helper/creator';
5-
import { MockIdentifierGenericParameterIds, MockIdentifierGenericParameterValue } from '../mockIdentifier/mockIdentifier';
5+
import { MockIdentifierGenericCircularReference, MockIdentifierGenericParameterIds, MockIdentifierGenericParameterValue } from '../mockIdentifier/mockIdentifier';
66
import { Scope } from '../scope/scope';
77
import { IGenericDeclaration } from './genericDeclaration.interface';
88
import { GenericDeclarationSupported } from './genericDeclarationSupported';
@@ -64,23 +64,33 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
6464
genericDescriptor ? TypescriptCreator.createFunctionExpression(
6565
ts.createBlock(
6666
[
67+
TypescriptCreator.createVariableStatement([
68+
TypescriptCreator.createVariableDeclaration(MockIdentifierGenericCircularReference, ts.createIdentifier('this')),
69+
]),
6770
ts.createExpressionStatement(
6871
ts.createCall(
6972
ts.createPropertyAccess(
7073
ts.createIdentifier('Object'),
71-
ts.createIdentifier('assign'),
74+
ts.createIdentifier('defineProperties'),
7275
),
7376
undefined,
7477
[
7578
ts.createIdentifier('this'),
76-
genericDescriptor,
79+
ts.createCall(
80+
ts.createPropertyAccess(
81+
ts.createIdentifier('Object'),
82+
ts.createIdentifier('getOwnPropertyDescriptors'),
83+
),
84+
undefined,
85+
[genericDescriptor]
86+
),
7787
]
7888
),
7989
),
8090
],
8191
),
8292
) : ts.createPropertyAccess(
83-
ts.createIdentifier('this'),
93+
MockIdentifierGenericCircularReference,
8494
ts.createIdentifier('constructor'),
8595
),
8696
undefined,

src/transformer/mockIdentifier/mockIdentifier.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ts from 'typescript';
22

3+
export const MockIdentifierGenericCircularReference: ts.Identifier = ts.createIdentifier('that');
34
export const MockIdentifierGenericParameter: ts.Identifier = ts.createIdentifier('t');
45
export const MockIdentifierGenericParameterIds: ts.Identifier = ts.createIdentifier('i');
56
export const MockIdentifierGenericParameterValue: ts.Identifier = ts.createIdentifier('w');

test/transformer/descriptor/generic/extends.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ describe('for generic', () => {
255255
it('should avoid infinite extension', () => {
256256
const propertiesA: A = createMock<A>();
257257
const propertiesB: B = createMock<B>();
258-
expect(propertiesA.a.b).toBe(0);
258+
expect(propertiesA.a.a.b).toBe(0);
259259
expect(propertiesA.b).toBe(0);
260-
expect(propertiesB.a.c).toBe('');
260+
expect(propertiesB.a.a.c).toBe('');
261261
expect(propertiesB.c).toBe('');
262262
});
263263
});

0 commit comments

Comments
 (0)