Skip to content

Commit

Permalink
fix(definitelyTyped): do not fail on mocking module declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pmyl authored Sep 15, 2020
1 parent faf4922 commit 4660f58
Show file tree
Hide file tree
Showing 11 changed files with 9,482 additions and 5,073 deletions.
68 changes: 35 additions & 33 deletions config/modules/base/webpack.base.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const DetermineDevToolFromEnvironmentDebugMode = require("../../utils/devtool");

module.exports = (options) => {
const tsConfigFile = options.tsConfigFile;
const tsConfigFile = options.tsConfigFile;

return {
mode: "production",
node: {
__dirname: false
return {
mode: "production",
node: {
__dirname: false
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
loader: 'eslint-loader'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
loader: 'eslint-loader'
},
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
configFile: tsConfigFile,
onlyCompileBundledFiles: true
}
}
]
},
output: {
libraryTarget: "commonjs2",
filename: "[name].js"
},
plugins: [
new CleanWebpackPlugin()
]
}
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
configFile: tsConfigFile,
onlyCompileBundledFiles: true
}
}
]
},
devtool: DetermineDevToolFromEnvironmentDebugMode(),
output: {
libraryTarget: "commonjs2",
filename: "[name].js"
},
plugins: [
new CleanWebpackPlugin()
]
}
};
2 changes: 0 additions & 2 deletions config/modules/transformer/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ const webpackNodeExternals = require("webpack-node-externals");
const path = require("path");
const { merge } = require("webpack-merge");
const base = require("../base/webpack.base");
const DetermineDevToolFromEnvironmentDebugMode = require("../../utils/devtool");

module.exports = merge(base({
tsConfigFile: 'config/modules/transformer/tsconfig.json'
}), {
devtool: DetermineDevToolFromEnvironmentDebugMode(),
target: "node",
externals: [
webpackNodeExternals()
Expand Down
16 changes: 9 additions & 7 deletions definitelyTypedTests/src/transformer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as path from 'path';
import { GetPropertiesFromSourceFileOrModuleDeclaration } from '../../../src/transformer/descriptor/module/module';
import { Scope } from '../../../src/transformer/scope/scope';
import { GetMockPropertiesFromDeclarations } from '../../../src/transformer/descriptor/mock/mockProperties';
import { SetCurrentCreateMock } from '../../../src/transformer/mock/currentCreateMockNode';

const customFunctions: CustomFunction[] = [
{
Expand All @@ -28,6 +29,7 @@ function visitNode(node: ts.CallExpression & { typeArguments: ts.NodeArray<ts.Ty
const [nodeToMock]: ts.NodeArray<ts.TypeNode> = node.typeArguments;

if (isCreateDefinitelyTypedMock(declaration) && ts.isTypeQueryNode(nodeToMock)) {
SetCurrentCreateMock(node);
const typeChecker: ts.TypeChecker = TypeChecker();
const typeQuerySymbol: ts.Symbol | undefined = typeChecker.getSymbolAtLocation(nodeToMock.exprName);

Expand All @@ -44,13 +46,13 @@ function visitNode(node: ts.CallExpression & { typeArguments: ts.NodeArray<ts.Ty

const symbol: ts.Symbol = typeChecker.getAliasedSymbol(symbolAlias);


if (!symbol.declarations) {
const moduleName: string =
((typeQuerySymbolDeclaration.moduleReference as ts.ExternalModuleReference).expression as ts.StringLiteral).text;
const pathModule = path.resolve(moduleName);
const pathModule: string = path.resolve(moduleName);
const moduleWithoutExportsFile: ts.SourceFile = GetProgram().getSourceFiles().find((file: ts.SourceFile) =>
file.fileName.includes(`${pathModule}/index.d.ts`)) as 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
Expand All @@ -67,10 +69,10 @@ function visitNode(node: ts.CallExpression & { typeArguments: ts.NodeArray<ts.Ty

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

const nodeToMock: ts.TypeReferenceNode = ts.createTypeReferenceNode(name, undefined);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build:modules": "webpack --config config/modules/webpack.js",
"build:modules:debug": "cross-env DEBUG=true webpack --config config/modules/webpack.js",
"build:transformer:definitelyTyped": "webpack --config config/modules/definitelyTypedTransformer/webpack.functions.js && webpack --config config/modules/definitelyTypedTransformer/webpack.js",
"build:transformer:definitelyTyped:debug": "cross-env DEBUG=true webpack --config config/modules/definitelyTypedTransformer/webpack.functions.js && cross-env DEBUG=true webpack --config config/modules/definitelyTypedTransformer/webpack.js",
"build:playground": "ttsc --project ./test/playground/tsconfig.build.json",
"test": "npm run test:transformer && npm run test:noTransformer && npm run test:framework:context && npm run test:framework && npm run test:frameworkDeprecated && npm run test:registerMock && npm run test:features && npm run test:filesFilter && npm run test:logs && npm run test:unit",
"test:unit": "cross-env JASMINE_CONFIG=./test/unit/jasmine.json TSCONFIG=./test/tsconfig.json npm run test:common",
Expand Down
19 changes: 18 additions & 1 deletion src/transformer/descriptor/typeQuery/typeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { TypeChecker } from '../../typeChecker/typeChecker';
import { GetDescriptor } from '../descriptor';
import { TypescriptHelper } from '../helper/helper';
import { GetMethodDeclarationDescriptor } from '../method/methodDeclaration';
import { GetModuleDescriptor } from '../module/module';
import {
GetModuleDescriptor,
GetPropertiesFromSourceFileOrModuleDeclaration,
} from '../module/module';
import { GetNullDescriptor } from '../null/null';
import { GetType } from '../type/type';
import { GetTypeReferenceDescriptor } from '../typeReference/typeReference';
import { GetUndefinedDescriptor } from '../undefined/undefined';
import { GetMockPropertiesFromDeclarations } from '../mock/mockProperties';

export function GetTypeQueryDescriptor(
node: ts.TypeQueryNode,
Expand Down Expand Up @@ -56,9 +60,22 @@ export function GetTypeQueryDescriptorFromDeclaration(
),
scope
);
// NamespaceImport, ImportEqualsDeclaration and ModuleDeclaration cannot be used in a typeof
// but to test definitely typed this is the only way, eventually we should move this code in the definitely typed folder
// and use it using an eventual extensibility opening of this transformer
case ts.SyntaxKind.NamespaceImport:
case ts.SyntaxKind.ImportEqualsDeclaration:
return GetModuleDescriptor(declaration, scope);
case ts.SyntaxKind.ModuleDeclaration:
return GetMockPropertiesFromDeclarations(
GetPropertiesFromSourceFileOrModuleDeclaration(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(declaration as any).symbol as ts.Symbol,
scope
),
[],
scope
);
case ts.SyntaxKind.EnumDeclaration:
// TODO: Use following two lines when issue #17552 on typescript github is resolved (https://github.com/microsoft/TypeScript/issues/17552)
// TheNewEmitResolver.ensureEmitOf(GetImportDeclarationOf(node.eprName as ts.Identifier);
Expand Down
Loading

0 comments on commit 4660f58

Please sign in to comment.