Skip to content

Commit

Permalink
fix(genericNotProvided): make sure required generic will default to n…
Browse files Browse the repository at this point in the history
…ull when are not provided (#180)
  • Loading branch information
uittorio authored Jan 16, 2020
1 parent 5b02465 commit ad76471
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/transformer/descriptor/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as ts from 'typescript';
import { TypescriptCreator } from '../../helper/creator';
import { Scope } from '../../scope/scope';
import { TypeChecker } from '../../typeChecker/typeChecker';
import { GetDescriptor } from '../descriptor';
import { TypescriptHelper } from '../helper/helper';
import { GetMockPropertiesFromDeclarations } from '../mock/mockProperties';
import { PropertyLike } from '../mock/propertyLike';
Expand Down
20 changes: 12 additions & 8 deletions src/transformer/genericDeclaration/genericDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
return !!node.typeArguments && !!node.typeArguments[index];
}

function getGenericTypeNode(node: ts.TypeReferenceNode | ts.ExpressionWithTypeArguments, nodeDeclaration: ts.TypeParameterDeclaration, index: number): ts.TypeNode {
return isGenericProvided(node, index) ? node.typeArguments[index] : nodeDeclaration.default;
function getGenericNode(node: ts.TypeReferenceNode | ts.ExpressionWithTypeArguments, nodeDeclaration: ts.TypeParameterDeclaration, index: number): ts.Node {
if (isGenericProvided(node, index)) {
return node.typeArguments[index];
}

return nodeDeclaration.default || ts.createNull();
}

function addGenericParameterToExisting(
Expand Down Expand Up @@ -55,12 +59,12 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
}

typeParameterDeclarations.forEach((declaration: ts.TypeParameterDeclaration, index: number) => {
const genericTypeNode: ts.TypeNode = getGenericTypeNode(node, declaration, index);
const genericNode: ts.Node = getGenericNode(node, declaration, index);

const genericParameter: GenericParameter = createGenericParameter(
declarationKey,
typeParameterDeclarations[index],
GetDescriptor(genericTypeNode, scope));
GetDescriptor(genericNode, scope));

generics.push(genericParameter);
});
Expand All @@ -78,10 +82,10 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
}

extensionDeclarationTypeParameters.reduce((acc: GenericParameter[], declaration: ts.TypeParameterDeclaration, index: number) => {
const genericTypeNode: ts.TypeNode = getGenericTypeNode(extension, declaration, index);
const genericNode: ts.Node = getGenericNode(extension, declaration, index);

if (ts.isTypeReferenceNode(genericTypeNode)) {
const typeParameterDeclaration: ts.Declaration = TypescriptHelper.GetDeclarationFromNode(genericTypeNode.typeName);
if (ts.isTypeReferenceNode(genericNode)) {
const typeParameterDeclaration: ts.Declaration = TypescriptHelper.GetDeclarationFromNode(genericNode.typeName);
if (ts.isTypeParameterDeclaration(typeParameterDeclaration)) {
addGenericParameterToExisting(
extensionDeclarationTypeParameters[index],
Expand All @@ -97,7 +101,7 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
const genericParameter: GenericParameter = createGenericParameter(
extensionDeclarationKey,
extensionDeclarationTypeParameters[index],
GetDescriptor(genericTypeNode, scope),
GetDescriptor(genericNode, scope),
);

acc.push(genericParameter);
Expand Down
18 changes: 18 additions & 0 deletions test/transformer/descriptor/generic/null.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createMock } from 'ts-auto-mock';

describe('generic null', () => {
it('should assign null value when not provided even if it was supposed to', () => {
interface A<P> {
prop: P;
data: {
a: string;
};
}

// @ts-ignore
const mock: A = createMock<A>();

expect(mock.prop).toBeNull();
expect(mock.data.a).toBe('');
});
});

0 comments on commit ad76471

Please sign in to comment.