Skip to content

Commit

Permalink
feat(*): export alternative transformer function to allow providing c…
Browse files Browse the repository at this point in the history
…ustomized typescript (#802)

Co-authored-by: Vittorio Guerriero <[email protected]>
  • Loading branch information
Pmyl and uittorio authored Jun 24, 2021
1 parent 6d78caa commit 9f1dce4
Show file tree
Hide file tree
Showing 105 changed files with 15,304 additions and 539 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import path from 'path';
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { customFunctionWithTypeArgument } from '../../../../src/transformer/customFunctions/helpers/custom-function-with-type-argument';
import { GetMockPropertiesFromDeclarations } from '../../../../src/transformer/descriptor/mock/mockProperties';
import { GetPropertiesFromSourceFileOrModuleDeclaration } from '../../../../src/transformer/descriptor/module/module';
import { CustomFunction } from '../../../../src/transformer/matcher/matcher';
import { SetCurrentCreateMock } from '../../../../src/transformer/mock/currentCreateMockNode';
import { getMock } from '../../../../src/transformer/mock/mock';
import { GetProgram } from '../../../../src/transformer/program/program';
import { Scope } from '../../../../src/transformer/scope/scope';
import { TypeChecker } from '../../../../src/transformer/typeChecker/typeChecker';
import { core } from '../../../../src/transformer/core/core';
import { DefinitelyTypedTransformerLogger } from '../logger';

type CompatibleStatement = ts.InterfaceDeclaration | ts.FunctionDeclaration | ts.ClassDeclaration | ts.ModuleDeclaration;
Expand All @@ -17,9 +16,9 @@ export const createDefinitelyTypedMockCustomFunction: CustomFunction = customFun
'create-definitely-typed-mock.d.ts',
'createDefinitelyTypedMock',
(node: ts.CallExpression, nodeToMock: ts.TypeNode): ts.Node => {
if (ts.isTypeQueryNode(nodeToMock)) {
if (core.ts.isTypeQueryNode(nodeToMock)) {
SetCurrentCreateMock(node);
const typeChecker: ts.TypeChecker = TypeChecker();
const typeChecker: ts.TypeChecker = core.typeChecker;
const typeQuerySymbol: ts.Symbol | undefined = typeChecker.getSymbolAtLocation(nodeToMock.exprName);

if (!typeQuerySymbol) {
Expand All @@ -39,32 +38,32 @@ export const createDefinitelyTypedMockCustomFunction: CustomFunction = customFun
const moduleName: string =
((typeQuerySymbolDeclaration.moduleReference as ts.ExternalModuleReference).expression as ts.StringLiteral).text;
const pathModule: string = path.resolve(moduleName);
const moduleWithoutExportsFile: ts.SourceFile = GetProgram().getSourceFiles().find((file: ts.SourceFile) =>
const moduleWithoutExportsFile: ts.SourceFile = core.program.getSourceFiles().find((file: ts.SourceFile) =>
path.relative(file.fileName, path.join(pathModule, 'index.d.ts')) === ''
) as ts.SourceFile;

const compatibleStatements: ts.Statement[] = moduleWithoutExportsFile.statements.filter(
(statement: ts.Statement) => statement.kind === ts.SyntaxKind.InterfaceDeclaration
|| statement.kind === ts.SyntaxKind.FunctionDeclaration
|| statement.kind === ts.SyntaxKind.ClassDeclaration
|| statement.kind === ts.SyntaxKind.ModuleDeclaration
(statement: ts.Statement) => statement.kind === core.ts.SyntaxKind.InterfaceDeclaration
|| statement.kind === core.ts.SyntaxKind.FunctionDeclaration
|| statement.kind === core.ts.SyntaxKind.ClassDeclaration
|| statement.kind === core.ts.SyntaxKind.ModuleDeclaration
);

if (compatibleStatements.length > 0) {
return ts.createArrayLiteral(compatibleStatements.map(
return core.ts.createArrayLiteral(compatibleStatements.map(
(workingStatement: CompatibleStatement) => {
const name: ts.Identifier = workingStatement.name as ts.Identifier;
const scope = new Scope();

if (ts.isModuleDeclaration(workingStatement)) {
if (core.ts.isModuleDeclaration(workingStatement)) {
return GetMockPropertiesFromDeclarations(
GetPropertiesFromSourceFileOrModuleDeclaration((workingStatement as any).symbol, scope),
[],
scope
);
}

const nodeToMock: ts.TypeReferenceNode = ts.createTypeReferenceNode(name, undefined);
const nodeToMock: ts.TypeReferenceNode = core.ts.createTypeReferenceNode(name, undefined);
return getMock(node, { nodeToMock });

}, []));
Expand Down
3 changes: 2 additions & 1 deletion definitelyTypedTests/src/transformer/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ts from 'typescript';
import {
CustomFunction
} from "../../../src/transformer/matcher/matcher";
Expand All @@ -6,5 +7,5 @@ import { createDefinitelyTypedMockCustomFunction } from './customFunctions/creat

const customFunctions: CustomFunction[] = [createDefinitelyTypedMockCustomFunction];

const transformer = baseTransformer(customFunctions);
const transformer = baseTransformer(customFunctions, ts);
export { transformer };
Loading

0 comments on commit 9f1dce4

Please sign in to comment.