Skip to content

Commit

Permalink
#88 fix test to cover case where "this" property is declared after ty…
Browse files Browse the repository at this point in the history
…pe 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
  • Loading branch information
Pmyl committed Nov 20, 2019
1 parent 8526477 commit 94114eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 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 { GetTypeLiteralDeclarationDescriptor } from './typeLiteral/typeLiteralDeclaration';
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 GetTypeLiteralDeclarationDescriptor(node as ts.InterfaceDeclaration, 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/typeLiteralDeclaration.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 GetTypeLiteralDeclarationDescriptor(node: ts.InterfaceDeclaration, 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);
}
8 changes: 5 additions & 3 deletions test/transformer/descriptor/this/this.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ describe('This', () => {

describe('for interface that contains a type literal', () => {
interface BaseInterface {
getThis: this;
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.getThis.typeLiteral.a).toBe('');
expect(properties.getThis.getThis.getThis.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 94114eb

Please sign in to comment.