Skip to content

Commit cbe2b88

Browse files
authored
Definitely typed - enable typeof namespace conversion (#278)
* definitely typed improvements * remove enable definitely type on branch push
1 parent 43cfaa8 commit cbe2b88

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

definitelyTypedTests/src/transformer/transformer.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
import * as ts from 'typescript';
77
import { GetProgram } from "../../../src/transformer/program/program";
88
import { baseTransformer } from '../../../src/transformer/base/base';
9-
import { GetTypeQueryDescriptor } from '../../../src/transformer/descriptor/typeQuery/typeQuery';
10-
import { Scope } from '../../../src/transformer/scope/scope';
119
import { DefinitelyTypedTransformerLogger } from './logger';
1210
import * as path from 'path';
11+
import { GetPropertiesFromSourceFileOrModuleDeclaration } from '../../../src/transformer/descriptor/module/module';
12+
import { Scope } from '../../../src/transformer/scope/scope';
13+
import { GetMockPropertiesFromDeclarations } from '../../../src/transformer/descriptor/mock/mockProperties';
1314

1415
const customFunctions: CustomFunction[] = [
1516
{
@@ -21,6 +22,8 @@ const customFunctions: CustomFunction[] = [
2122
const transformer = baseTransformer(visitNode, customFunctions);
2223
export { transformer };
2324

25+
type CompatibleStatement = ts.InterfaceDeclaration | ts.FunctionDeclaration | ts.ClassDeclaration | ts.ModuleDeclaration;
26+
2427
function visitNode(node: ts.CallExpression, declaration: ts.FunctionDeclaration): ts.Node {
2528
const typeQueryNode: ts.TypeNode = node.typeArguments[0];
2629

@@ -42,20 +45,34 @@ function visitNode(node: ts.CallExpression, declaration: ts.FunctionDeclaration)
4245
(statement: ts.Statement) => statement.kind === ts.SyntaxKind.InterfaceDeclaration
4346
|| statement.kind === ts.SyntaxKind.FunctionDeclaration
4447
|| statement.kind === ts.SyntaxKind.ClassDeclaration
48+
|| statement.kind === ts.SyntaxKind.ModuleDeclaration
4549
);
4650

4751
if (compatibleStatements.length > 0) {
48-
return ts.createArrayLiteral(compatibleStatements.map((workingStatement: ts.InterfaceDeclaration | ts.FunctionDeclaration | ts.ClassDeclaration) => {
49-
const nodeToMock: ts.TypeReferenceNode = ts.createTypeReferenceNode(workingStatement.name, []);
52+
return ts.createArrayLiteral(compatibleStatements.map(
53+
(workingStatement: CompatibleStatement) => {
54+
const name: ts.Identifier = workingStatement.name as ts.Identifier;
55+
const scope = new Scope();
56+
57+
if (ts.isModuleDeclaration(workingStatement)) {
58+
return GetMockPropertiesFromDeclarations(
59+
GetPropertiesFromSourceFileOrModuleDeclaration((workingStatement as any).symbol, scope),
60+
[],
61+
scope
62+
)
63+
}
64+
65+
const nodeToMock: ts.TypeReferenceNode = ts.createTypeReferenceNode(name, undefined);
5066
return getMock(nodeToMock, node);
51-
}));
67+
68+
}, []));
5269
}
5370
DefinitelyTypedTransformerLogger().moduleWithoutValidTypeStatements(moduleName);
5471

5572
return node;
5673
}
5774

58-
return GetTypeQueryDescriptor(typeQueryNode, new Scope());
75+
return getMock(typeQueryNode, node);
5976
}
6077

6178
return node;

src/transformer/descriptor/module/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ function isExternalSource(declaration: ts.Node): declaration is ExternalSource {
2727
}
2828

2929
function GetPropertiesFromSourceFileOrModuleDeclarationDescriptor(sourceFile: ExternalSource, symbol: ts.Symbol, scope: Scope): ts.Expression {
30-
return GetMockPropertiesFromDeclarations(GetPropertiesFromSourceFileOrModuleDeclaration(sourceFile, symbol, scope), [], scope);
30+
return GetMockPropertiesFromDeclarations(GetPropertiesFromSourceFileOrModuleDeclaration(symbol, scope), [], scope);
3131
}
3232

33-
function GetPropertiesFromSourceFileOrModuleDeclaration(sourceFile: ExternalSource, symbol: ts.Symbol, scope: Scope): ts.PropertySignature[] {
33+
export function GetPropertiesFromSourceFileOrModuleDeclaration(symbol: ts.Symbol, scope: Scope): ts.PropertySignature[] {
3434
const typeChecker: ts.TypeChecker = TypeChecker();
3535
const moduleExports: ts.Symbol[] = typeChecker.getExportsOfModule(symbol);
3636

@@ -46,7 +46,7 @@ function GetPropertiesFromSourceFileOrModuleDeclaration(sourceFile: ExternalSour
4646
if (ts.isExportSpecifier(declaration) && ts.isSourceFile(originalDeclaration)) {
4747
const exportSpecifierSymbol: ts.Symbol = typeChecker.getSymbolAtLocation(declaration.name);
4848
const exportSpecifierAliasSymbol: ts.Symbol = typeChecker.getAliasedSymbol(exportSpecifierSymbol);
49-
const exportSpecifierProperties: ts.PropertySignature[] = GetPropertiesFromSourceFileOrModuleDeclaration(originalDeclaration, exportSpecifierAliasSymbol, scope);
49+
const exportSpecifierProperties: ts.PropertySignature[] = GetPropertiesFromSourceFileOrModuleDeclaration(exportSpecifierAliasSymbol, scope);
5050
const propertyType: ts.TypeNode = ts.createTypeLiteralNode(exportSpecifierProperties);
5151

5252
return TypescriptCreator.createPropertySignature(declaration.name, propertyType);

0 commit comments

Comments
 (0)