Skip to content

Commit

Permalink
feat(multipledeclaration-bigint): add support for multiple declaratio…
Browse files Browse the repository at this point in the history
…ns functions for types and values and biging (#163)

* add support for multiple declarations functions for types and values, refactoring internal ts auto mock variables names to be in one place, reset playground file

* add support for bigint type
  • Loading branch information
uittorio authored Jan 13, 2020
1 parent b5cc83d commit 00d9904
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 59 deletions.
2 changes: 1 addition & 1 deletion config/test/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function (debug, disableCache) {
options: {
getCustomTransformers: (program) => ({
before: [transformer.default(program, {
debug: debug ? 'file' : false,
debug: debug ? debug : false,
cacheBetweenTests: disableCache !== 'true'
})]
})
Expand Down
2 changes: 2 additions & 0 deletions src/transformer/descriptor/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export function GetDescriptor(node: ts.Node, scope: Scope): ts.Expression {
case ts.SyntaxKind.IndexedAccessType:
return GetIndexedAccessTypeDescriptor(node as ts.IndexedAccessTypeNode, scope);
case ts.SyntaxKind.BooleanKeyword:
case ts.SyntaxKind.TypePredicate:
case ts.SyntaxKind.FirstTypeNode:
return GetBooleanDescriptor();
case ts.SyntaxKind.ObjectKeyword:
return GetMockPropertiesFromSymbol([], [], scope);
Expand Down
20 changes: 19 additions & 1 deletion src/transformer/descriptor/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export namespace TypescriptHelper {
return declaration;
}

export function GetConcreteDeclarationFromSymbol(symbol: ts.Symbol): ts.Declaration {
const declaration: ts.Declaration = symbol.declarations[0];

if (ts.isImportSpecifier(declaration) || ts.isImportEqualsDeclaration(declaration)) {
return GetConcreteDeclarationForImport(declaration);
}

return declaration;
}

export function GetDeclarationForImport(node: ts.ImportClause | ts.ImportSpecifier | ts.ImportEqualsDeclaration): ts.Declaration {
const typeChecker: ts.TypeChecker = TypeChecker();
const symbol: ts.Symbol = typeChecker.getSymbolAtLocation(node.name);
Expand All @@ -37,6 +47,14 @@ export namespace TypescriptHelper {
return GetFirstValidDeclaration(originalSymbol.declarations);
}

export function GetConcreteDeclarationForImport(node: ts.ImportClause | ts.ImportSpecifier | ts.ImportEqualsDeclaration): ts.Declaration {
const typeChecker: ts.TypeChecker = TypeChecker();
const symbol: ts.Symbol = typeChecker.getSymbolAtLocation(node.name);
const originalSymbol: ts.Symbol = typeChecker.getAliasedSymbol(symbol);

return originalSymbol.declarations[0];
}

export function GetParameterOfNode(node: ts.EntityName): ts.NodeArray<ts.TypeParameterDeclaration> {
const declaration: ts.Declaration = GetDeclarationFromNode(node);

Expand Down Expand Up @@ -73,7 +91,7 @@ export namespace TypescriptHelper {

function GetFirstValidDeclaration(declarations: ts.Declaration[]): ts.Declaration {
return declarations.find((declaration: ts.Declaration) => {
return !ts.isVariableDeclaration(declaration);
return !ts.isVariableDeclaration(declaration) && !ts.isFunctionDeclaration(declaration);
}) || declarations[0];
}

Expand Down
8 changes: 3 additions & 5 deletions src/transformer/descriptor/mock/mockCall.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as ts from 'typescript';
import { TypescriptCreator } from '../../helper/creator';
import { GetMockInternalValuesName, GetMockObjectReturnValueName } from './mockDeclarationName';
import { MockIdentifierInternalValues, MockIdentifierObjectReturnValue } from '../../mockIdentifier/mockIdentifier';
import { GetMockMarkerProperty, Property } from './mockMarker';

export function GetMockCall(properties: ts.PropertyAssignment[], signature: ts.Expression): ts.CallExpression {
const mockObjectReturnValueName: ts.Identifier = GetMockObjectReturnValueName();
const mockInternalValuesName: ts.Identifier = GetMockInternalValuesName();

const mockObjectReturnValueName: ts.Identifier = MockIdentifierObjectReturnValue;
const statements: ts.Statement[] = [
ts.createVariableStatement([], [
TypescriptCreator.createVariableDeclaration(mockInternalValuesName, ts.createObjectLiteral()),
TypescriptCreator.createVariableDeclaration(MockIdentifierInternalValues, ts.createObjectLiteral()),
TypescriptCreator.createVariableDeclaration(mockObjectReturnValueName, ts.createObjectLiteral()),
]),
];
Expand Down
13 changes: 0 additions & 13 deletions src/transformer/descriptor/mock/mockDeclarationName.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/transformer/descriptor/mock/mockProperty.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import * as ts from 'typescript';
import { TypescriptCreator } from '../../helper/creator';
import { MockIdentifierInternalValues, MockIdentifierSetParameterName } from '../../mockIdentifier/mockIdentifier';
import { Scope } from '../../scope/scope';
import { GetDescriptor } from '../descriptor';
import { TypescriptHelper } from '../helper/helper';
import { GetMockInternalValuesName, GetMockSetParameterName } from './mockDeclarationName';
import { PropertyLike } from './propertyLike';

export function GetMockProperty(member: PropertyLike, scope: Scope): ts.PropertyAssignment {
const descriptor: ts.Expression = GetDescriptor(member, scope);

const propertyName: string = TypescriptHelper.GetStringPropertyName(member.name);

const declarations: ts.Identifier = GetMockInternalValuesName();

const variableDeclarationName: ts.ElementAccessExpression = ts.createElementAccess(declarations, ts.createStringLiteral(propertyName));
const setVariableParameterName: ts.Identifier = GetMockSetParameterName();
const variableDeclarationName: ts.ElementAccessExpression = ts.createElementAccess(MockIdentifierInternalValues, ts.createStringLiteral(propertyName));
const setVariableParameterName: ts.Identifier = MockIdentifierSetParameterName;

const expressionGetAssignment: ts.BinaryExpression = ts.createBinary(variableDeclarationName, ts.SyntaxKind.EqualsToken, descriptor);

Expand Down
8 changes: 4 additions & 4 deletions src/transformer/descriptor/typeParameter/typeParameter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ts from 'typescript';
import { TypescriptCreator } from '../../helper/creator';
import { MockDefiner } from '../../mockDefiner/mockDefiner';
import { MockGenericParameter, MockGenericParameterIds, MockGenericParameterValue } from '../../mockGeneric/mockGenericParameter';
import { MockIdentifierGenericParameter, MockIdentifierGenericParameterIds, MockIdentifierGenericParameterValue } from '../../mockIdentifier/mockIdentifier';
import { Scope } from '../../scope/scope';
import { TypeChecker } from '../../typeChecker/typeChecker';
import { GetDescriptor } from '../descriptor';
Expand Down Expand Up @@ -43,7 +43,7 @@ function createFunctionToAccessToGenericValue(key: string, descriptor: ts.Expres
function createFindGeneric(genericKey: string): ts.CallExpression {
return ts.createCall(
ts.createPropertyAccess(
MockGenericParameter,
MockIdentifierGenericParameter,
ts.createIdentifier('find'),
),
undefined,
Expand All @@ -53,7 +53,7 @@ function createFindGeneric(genericKey: string): ts.CallExpression {
ts.createPropertyAccess(
ts.createPropertyAccess(
ts.createIdentifier('generic'),
MockGenericParameterIds,
MockIdentifierGenericParameterIds,
),
ts.createIdentifier('indexOf'),
),
Expand Down Expand Up @@ -89,7 +89,7 @@ function getValueFromGenericIfExist(): ts.IfStatement {
[ts.createReturn(ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier('generic'),
MockGenericParameterValue,
MockIdentifierGenericParameterValue,
),
undefined,
[],
Expand Down
2 changes: 1 addition & 1 deletion src/transformer/descriptor/typeQuery/typeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ function getTypeQueryDeclarationFromSymbol(symbol: ts.Symbol): ts.NamedDeclarati
return declaration;
}

return TypescriptHelper.GetDeclarationFromSymbol(symbol);
return TypescriptHelper.GetConcreteDeclarationFromSymbol(symbol);
}
6 changes: 3 additions & 3 deletions src/transformer/genericDeclaration/genericDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ts from 'typescript';
import { GetDescriptor } from '../descriptor/descriptor';
import { TypescriptHelper } from '../descriptor/helper/helper';
import { TypescriptCreator } from '../helper/creator';
import { MockGenericParameterIds, MockGenericParameterValue } from '../mockGeneric/mockGenericParameter';
import { MockIdentifierGenericParameterIds, MockIdentifierGenericParameterValue } from '../mockIdentifier/mockIdentifier';
import { Scope } from '../scope/scope';
import { IGenericDeclaration } from './genericDeclaration.interface';
import { GenericDeclarationSupported } from './genericDeclarationSupported';
Expand Down Expand Up @@ -110,7 +110,7 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
return ts.createObjectLiteral(
[
ts.createPropertyAssignment(
MockGenericParameterIds,
MockIdentifierGenericParameterIds,
ts.createArrayLiteral(
s.ids.map((arr: string) => {
return ts.createStringLiteral(arr);
Expand All @@ -119,7 +119,7 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
),
),
ts.createPropertyAssignment(
MockGenericParameterValue,
MockIdentifierGenericParameterValue,
s.value,
),
],
Expand Down
1 change: 0 additions & 1 deletion src/transformer/mock/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Logger } from '../../logger/logger';
import { ArrayHelper } from '../array/array';
import { GetDescriptor } from '../descriptor/descriptor';
import { TypescriptHelper } from '../descriptor/helper/helper';
import { TypescriptCreator } from '../helper/creator';
import { getMockMergeExpression, getMockMergeIteratorExpression } from '../mergeExpression/mergeExpression';
import { MockDefiner } from '../mockDefiner/mockDefiner';
import { Scope } from '../scope/scope';
Expand Down
4 changes: 2 additions & 2 deletions src/transformer/mockDefiner/mockDefiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GetProperties } from '../descriptor/properties/properties';
import { GetTypeofEnumDescriptor } from '../descriptor/typeQuery/enumTypeQuery';
import { TypescriptCreator } from '../helper/creator';
import { createImportOnIdentifier } from '../helper/import';
import { MockGenericParameter } from '../mockGeneric/mockGenericParameter';
import { MockIdentifierGenericParameter } from '../mockIdentifier/mockIdentifier';
import { Scope } from '../scope/scope';
import { DeclarationCache } from './cache/declarationCache';
import { DeclarationListCache } from './cache/declarationListCache';
Expand Down Expand Up @@ -312,6 +312,6 @@ export class MockDefiner {
}

private _getMockGenericParameter(): ts.ParameterDeclaration {
return ts.createParameter([], [], undefined, MockGenericParameter);
return ts.createParameter([], [], undefined, MockIdentifierGenericParameter);
}
}
4 changes: 2 additions & 2 deletions src/transformer/mockFactoryCall/mockFactoryCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GenericDeclaration } from '../genericDeclaration/genericDeclaration';
import { IGenericDeclaration } from '../genericDeclaration/genericDeclaration.interface';
import { GenericDeclarationSupported } from '../genericDeclaration/genericDeclarationSupported';
import { MockDefiner } from '../mockDefiner/mockDefiner';
import { MockGenericParameter } from '../mockGeneric/mockGenericParameter';
import { MockIdentifierGenericParameter } from '../mockIdentifier/mockIdentifier';
import { Scope } from '../scope/scope';

export function GetMockFactoryCall(typeReferenceNode: ts.TypeReferenceNode, scope: Scope): ts.Expression {
Expand Down Expand Up @@ -63,7 +63,7 @@ export function GetMockFactoryCallForThis(mockKey: string): ts.Expression {
return ts.createCall(
mockFactoryCall,
[],
[MockGenericParameter],
[MockIdentifierGenericParameter],
);
}

Expand Down
5 changes: 0 additions & 5 deletions src/transformer/mockGeneric/mockGenericParameter.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/transformer/mockIdentifier/mockIdentifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as ts from 'typescript';

export const MockIdentifierGenericParameter: ts.Identifier = ts.createIdentifier('t');
export const MockIdentifierGenericParameterIds: ts.Identifier = ts.createIdentifier('i');
export const MockIdentifierGenericParameterValue: ts.Identifier = ts.createIdentifier('w');
export const MockIdentifierInternalValues: ts.Identifier = ts.createIdentifier('d');
export const MockIdentifierObjectReturnValue: ts.Identifier = ts.createIdentifier('m');
export const MockIdentifierSetParameterName: ts.Identifier = ts.createIdentifier('v');
18 changes: 18 additions & 0 deletions src/transformer/printNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as ts from 'typescript';
import { Logger } from '../logger/logger';
import { ILogger } from '../logger/logger.interface';

export function PrintNode(node: ts.Node): void {
const PrintNodeLogger: ILogger = Logger('PrintNode');

const resultFile: ts.SourceFile = ts.createSourceFile('someFileName.ts', '', ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS);
const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });

try {
const result: string = printer.printNode(ts.EmitHint.Unspecified, node, resultFile);
PrintNodeLogger.info(result);
} catch (e) {
PrintNodeLogger.warning('There was an error printing the node');
}

}
5 changes: 0 additions & 5 deletions test/playground/enums.ts

This file was deleted.

16 changes: 6 additions & 10 deletions test/playground/playground.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createMock, registerMock } from 'ts-auto-mock';
import { MyEnum } from './enums';
import { createMock } from 'ts-auto-mock';

/*
USE THIS FILE ONLY FOR TESTING NEW IMPLEMENTATION
Expand All @@ -10,13 +9,10 @@ import { MyEnum } from './enums';
*/

it('should work', () => {
interface A {
a: string;
}
const enumm2: typeof MyEnum = createMock<typeof MyEnum>();
expect(createMock<A>().a).toBe("ok");
interface A {
a: string;
}

expect(enumm2.A).toEqual(0);

expect(createMock<A>().a).toBe("ok");
const type: A = createMock<A>();
expect(type).toBeDefined();
});
17 changes: 17 additions & 0 deletions test/transformer/descriptor/declarations/declarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ describe('declarations', () => {
});
});

describe('function and interface', () => {
// @ts-ignore
interface AAA {
b: number
}

function AAA(): AAA {
// @ts-ignore
return { a: 'ss' };
}

it('should return the properties from the interface', () => {
const properties: AAA = createMock<AAA>();
expect(properties.b).toBe(0);
});
});

describe('interface imported', () => {
it('should ignore variables declaration', () => {
const properties: MultipleInterfaceDeclaration = createMock<MultipleInterfaceDeclaration>();
Expand Down
20 changes: 19 additions & 1 deletion test/transformer/descriptor/typeQuery/typeQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ describe('typeQuery', () => {

expect(functionMock).toBeUndefined();
});

it('should return correct properties with multiple declarations', () => {
function MultipleDeclaration(): MultipleDeclaration {
// @ts-ignore
return { a: 's'};
}

interface MultipleDeclaration {
b: string;
}

const functionMock: typeof MultipleDeclaration = createMock<typeof MultipleDeclaration>();

// @ts-ignore
expect(functionMock()).toEqual({
b: '',
});
});
});

describe('for class', () => {
Expand Down Expand Up @@ -102,7 +120,7 @@ describe('typeQuery', () => {
it('should assign the enum to the mock', () => {
enum Enum {
A,
B = 'some'
B = 'some',
}

const enumMock: typeof Enum = createMock<typeof Enum>();
Expand Down

0 comments on commit 00d9904

Please sign in to comment.