Skip to content

Commit

Permalink
fix(this): make sure literal type will not interfere with "this" refe…
Browse files Browse the repository at this point in the history
…rence

* #88 add test to cover case where interface has a "this" property and a type literal

* #88 fix test to cover case where "this" property is declared after type literal property and fix the problem

Problem fixed creating a getDescriptor function for type literal so that type literal don't modify the declaration node in the scope

* #88 adjust naming and typings
  • Loading branch information
Pmyl authored Nov 20, 2019
1 parent eba6f1c commit fd2270b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/transformer/descriptor/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { GetObjectLiteralDescriptor } from './objectLiteral/objectLiteral';
import { GetPropertyDescriptor } from './property/propertySignature';
import { GetStringDescriptor } from './string/string';
import { GetTypeAliasDescriptor } from './typeAlias/typeAlias';
import { GetTypeLiteralDescriptor } from './typeLiteral/typeLiteral';
import { GetTypeParameterDescriptor } from './typeParameter/typeParameter';
import { GetTypeReferenceDescriptorReusable } from './typeReference/typeReferenceReusable';
import { GetUndefinedDescriptor } from './undefined/undefined';
Expand All @@ -37,6 +38,7 @@ export function GetDescriptor(node: ts.Node, scope: Scope): ts.Expression {
case ts.SyntaxKind.TypeReference:
return GetTypeReferenceDescriptorReusable(node as ts.TypeReferenceNode, scope);
case ts.SyntaxKind.TypeLiteral:
return GetTypeLiteralDescriptor(node as ts.TypeLiteralNode, scope);
case ts.SyntaxKind.InterfaceDeclaration:
return GetInterfaceDeclarationDescriptor(node as ts.InterfaceDeclaration, scope);
case ts.SyntaxKind.ClassDeclaration:
Expand Down
14 changes: 14 additions & 0 deletions src/transformer/descriptor/typeLiteral/typeLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as ts from 'typescript';
import { Scope } from '../../scope/scope';
import { TypeChecker } from '../../typeChecker/typeChecker';
import { GetMockPropertiesFromSymbol } from '../mock/mockProperties';

export function GetTypeLiteralDescriptor(node: ts.TypeLiteralNode, scope: Scope): ts.Expression {
const typeChecker: ts.TypeChecker = TypeChecker();
const type: ts.Type = typeChecker.getTypeAtLocation(node);

const properties: ts.Symbol[] = typeChecker.getPropertiesOfType(type);
const signatures: ReadonlyArray<ts.Signature> = type.getCallSignatures();

return GetMockPropertiesFromSymbol(properties, signatures, scope);
}
16 changes: 16 additions & 0 deletions test/transformer/descriptor/this/this.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,20 @@ describe('This', () => {
expect(properties.another2.getThis.prop).toBe(0);
});
});

describe('for interface that contains a type literal', () => {
interface BaseInterface {
getThisBefore: this;
typeLiteral: { a: string };
getThisAfter: this;
}

it('should be able to reference to itself ', () => {
const properties: BaseInterface = createMock<BaseInterface>();
expect(properties.typeLiteral.a).toBe('');
expect(properties.getThisAfter.typeLiteral.a).toBe('');
expect(properties.getThisBefore.typeLiteral.a).toBe('');
expect(properties.getThisAfter.getThisBefore.getThisAfter.typeLiteral.a).toBe('');
});
});
});

0 comments on commit fd2270b

Please sign in to comment.