Skip to content

Commit

Permalink
fix: A few of the issues with the type converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Oct 18, 2020
1 parent 2058981 commit 6ff7fcd
Show file tree
Hide file tree
Showing 64 changed files with 1,789 additions and 2,411 deletions.
46 changes: 19 additions & 27 deletions src/lib/converter/context.ts
Expand Up @@ -68,7 +68,7 @@ export class Context {
/**
* The currently set type parameters.
*/
typeParameters?: ts.MapLike<Type>;
typeParameters?: Record<string, Type | undefined>;

/**
* The currently set type arguments.
Expand Down Expand Up @@ -133,6 +133,11 @@ export class Context {
}
}

/** @internal */
get logger() {
return this.converter.application.logger;
}

/**
* Return the compiler options.
*/
Expand Down Expand Up @@ -215,18 +220,10 @@ export class Context {
* passed to this method to ensure that the project helper functions work correctly.
*
* @param reflection The reflection that should be registered.
* @param node The node the given reflection was resolved from.
* @param symbol The symbol the given reflection was resolved from.
*/
registerReflection(reflection: Reflection, symbol?: ts.Symbol) {
if (symbol) {
this.project.registerReflection(
reflection,
this.checker.getFullyQualifiedName(symbol)
);
} else {
this.project.registerReflection(reflection);
}
this.project.registerReflection(reflection, symbol);
}

/**
Expand Down Expand Up @@ -436,27 +433,22 @@ export class Context {
const typeParameters: ts.MapLike<Type> = {};

if (preserve) {
Object.keys(this.typeParameters || {}).forEach((key) => {
typeParameters[key] = this.typeParameters![key];
});
Object.assign(typeParameters, this.typeParameters);
}

parameters.forEach(
(declaration: ts.TypeParameterDeclaration, index: number) => {
if (!declaration.symbol) {
return;
}
const name = declaration.symbol.name;
if (this.typeArguments && this.typeArguments[index]) {
typeParameters[name] = this.typeArguments[index];
} else {
const param = createTypeParameter(this, declaration);
if (param) {
typeParameters[name] = param;
}
let index = 0;
for (const declaration of parameters) {
const name = declaration.name.text;
if (this.typeArguments && this.typeArguments[index]) {
typeParameters[name] = this.typeArguments[index];
} else {
const param = createTypeParameter(this, declaration);
if (param) {
typeParameters[name] = param;
}
}
);
++index;
}

return typeParameters;
}
Expand Down
30 changes: 6 additions & 24 deletions src/lib/converter/converter.ts
Expand Up @@ -18,6 +18,7 @@ import {
ComponentClass,
} from "../utils/component";
import { BindOption, removeIfPresent } from "../utils";
import { convertType } from "./types";

/**
* Compiles source files using TypeScript and converts compiler symbols to reflections.
Expand Down Expand Up @@ -316,28 +317,9 @@ export class Converter extends ChildableComponent<
*/
convertType(
context: Context,
node?: ts.Node,
type?: ts.Type
): Type | undefined {
// Run all node based type conversions
if (node) {
type = type || context.getTypeAtLocation(node);

for (const converter of this.typeNodeConverters) {
if (converter.supportsNode(context, node, type)) {
return converter.convertNode(context, node, type);
}
}
}

// Run all type based type conversions
if (type) {
for (const converter of this.typeTypeConverters) {
if (converter.supportsType(context, type)) {
return converter.convertType(context, type);
}
}
}
node: ts.TypeNode | ts.Type | undefined
): Type {
return convertType(context, node);
}

/**
Expand All @@ -348,12 +330,12 @@ export class Converter extends ChildableComponent<
*/
convertTypes(
context: Context,
nodes: ReadonlyArray<ts.Node> = [],
nodes: ReadonlyArray<ts.TypeNode> = [],
types: ReadonlyArray<ts.Type> = []
): Type[] {
const result: Type[] = [];
_.zip(nodes, types).forEach(([node, type]) => {
const converted = this.convertType(context, node, type);
const converted = this.convertType(context, node ?? type!);
if (converted) {
result.push(converted);
}
Expand Down
19 changes: 8 additions & 11 deletions src/lib/converter/factories/parameter.ts
@@ -1,14 +1,13 @@
import * as ts from "typescript";

import {
ParameterReflection,
ReflectionFlag,
ReflectionKind,
ParameterReflection,
SignatureReflection,
} from "../../models/reflections/index";
} from "../../models";
import { Context } from "../context";
import { Converter } from "../converter";
import { convertDefaultValue } from "../convert-expression";
import { Converter } from "../converter";

/**
* Create a parameter reflection for the given node.
Expand Down Expand Up @@ -37,18 +36,16 @@ export function createParameter(
);
context.registerReflection(parameter);
context.withScope(parameter, () => {
parameter.type = context.converter.convertType(
context,
node.type ?? context.getTypeAtLocation(node)
);

if (
ts.isArrayBindingPattern(node.name) ||
ts.isObjectBindingPattern(node.name)
) {
parameter.type = context.converter.convertType(context, node.name);
parameter.name = "__namedParameters";
} else {
parameter.type = context.converter.convertType(
context,
node.type,
context.getTypeAtLocation(node)
);
}

parameter.defaultValue = convertDefaultValue(node);
Expand Down
13 changes: 3 additions & 10 deletions src/lib/converter/factories/reference.ts
Expand Up @@ -36,10 +36,7 @@ export function createReferenceType(
name = checker.symbolToString(symbol.parent) + "." + name;
}

return new ReferenceType(
name,
context.checker.getFullyQualifiedName(symbol)
);
return new ReferenceType(name, symbol, context.project);
}

export function createReferenceOrDeclarationReflection(
Expand All @@ -63,15 +60,11 @@ export function createReferenceOrDeclarationReflection(
return;
}

const targetFqn = context.checker.getFullyQualifiedName(target);
let reflection: DeclarationReflection | undefined;
if (context.project.getReflectionFromFQN(targetFqn)) {
if (context.project.getReflectionFromSymbol(target)) {
reflection = new ReferenceReflection(
source.name,
[
ReferenceState.Unresolved,
context.checker.getFullyQualifiedName(target),
],
[ReferenceState.Unresolved, target],
context.scope
);

Expand Down
5 changes: 2 additions & 3 deletions src/lib/converter/factories/signature.ts
Expand Up @@ -73,13 +73,12 @@ function extractSignatureType(
}
return context.converter.convertType(
context,
node.type,
checker.getReturnTypeOfSignature(signature)
node.type ?? checker.getReturnTypeOfSignature(signature)
);
} catch (error) {
// ignore
}
}

return context.converter.convertType(context, node.type || node);
return context.converter.convertType(context, node.type);
}
3 changes: 1 addition & 2 deletions src/lib/converter/nodes/alias.ts
Expand Up @@ -34,8 +34,7 @@ export class AliasConverter extends ConverterNodeComponent<
context.withScope(alias, node.typeParameters, () => {
alias!.type = this.owner.convertType(
context,
node.type,
context.getTypeAtLocation(node.type)
node.type ?? context.getTypeAtLocation(node.type)
);
});

Expand Down
15 changes: 1 addition & 14 deletions src/lib/converter/nodes/block.ts
Expand Up @@ -55,11 +55,6 @@ export class BlockConverter extends ConverterNodeComponent<
let result: Reflection | undefined = context.scope;

context.withSourceFile(node, () => {
console.log(
node.fileName,
this.getExports(context, node).map((n) => n.name)
);

if (context.inFirstPass) {
result = createDeclaration(
context,
Expand All @@ -75,9 +70,7 @@ export class BlockConverter extends ConverterNodeComponent<
context.checker.getSymbolAtLocation(node) ?? node.symbol;

if (symbol) {
result = context.project.getReflectionFromFQN(
context.checker.getFullyQualifiedName(symbol)
);
result = context.project.getReflectionFromSymbol(symbol);

context.withScope(result, () => {
this.convertReExports(context, node);
Expand All @@ -98,7 +91,6 @@ export class BlockConverter extends ConverterNodeComponent<
for (const exp of this.getExports(context, node).filter(
(exp) => context.resolveAliasedSymbol(exp) === exp
)) {
console.log("\tEXP", exp.name);
for (const decl of exp.getDeclarations() ?? []) {
this.owner.convertNode(context, decl);
}
Expand All @@ -112,11 +104,6 @@ export class BlockConverter extends ConverterNodeComponent<
for (const exp of this.getExports(context, node).filter(
(exp) => context.resolveAliasedSymbol(exp) !== exp
)) {
console.log(
"\tREEXP",
exp.name,
exp.getDeclarations()?.map((d) => ts.SyntaxKind[d.kind])
);
for (const decl of exp.getDeclarations() ?? []) {
this.owner.convertNode(context, decl);
}
Expand Down
17 changes: 6 additions & 11 deletions src/lib/converter/nodes/class.ts
Expand Up @@ -83,8 +83,7 @@ export class ClassConverter extends ConverterNodeComponent<
}
const convertedType = this.owner.convertType(
context,
baseType,
type
baseType ?? type
);
if (convertedType) {
reflection!.extendedTypes.push(convertedType);
Expand Down Expand Up @@ -113,16 +112,12 @@ export class ClassConverter extends ConverterNodeComponent<
: undefined;

typesToInheritFrom.forEach((typeToInheritFrom: ts.Type) => {
// TODO: The TS declaration file claims that:
// 1. type.symbol is non-nullable
// 2. symbol.declarations is non-nullable
// These are both incorrect, GH#1207 for #2 and existing tests for #1.
// Figure out why this is the case and document.
typeToInheritFrom.symbol?.declarations?.forEach(
(declaration) => {
typeToInheritFrom
.getSymbol()
?.getDeclarations()
?.forEach((declaration) => {
context.inherit(declaration, typeArguments);
}
);
});
});
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/lib/converter/nodes/constructor.ts
Expand Up @@ -68,8 +68,8 @@ export class ConstructorConverter extends ConverterNodeComponent<
if (!node.type) {
signature.type = new ReferenceType(
parent.name,
ReferenceType.SYMBOL_FQN_RESOLVED,
parent
parent,
context.project
);
}
method!.signatures = method!.signatures || [];
Expand Down Expand Up @@ -121,8 +121,7 @@ export class ConstructorConverter extends ConverterNodeComponent<
property.setFlag(ReflectionFlag.Static, false);
property.type = this.owner.convertType(
context,
parameter.type,
context.getTypeAtLocation(parameter)
parameter.type ?? context.getTypeAtLocation(parameter)
);

if (comment) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/nodes/export.ts
Expand Up @@ -39,8 +39,8 @@ export class ExportConverter extends ConverterNodeComponent<
return;
}

const reflection = project.getReflectionFromFQN(
context.checker.getFullyQualifiedName(declaration.symbol)
const reflection = project.getReflectionFromSymbol(
declaration.symbol
);
if (
node.isExportEquals &&
Expand Down
21 changes: 10 additions & 11 deletions src/lib/converter/nodes/interface.ts
Expand Up @@ -61,8 +61,7 @@ export class InterfaceConverter extends ConverterNodeComponent<
}
const convertedType = this.owner.convertType(
context,
baseType,
type
baseType
);
if (convertedType) {
reflection!.extendedTypes.push(convertedType);
Expand All @@ -76,15 +75,15 @@ export class InterfaceConverter extends ConverterNodeComponent<

typesToInheritFrom.forEach(
(typeToInheritFrom: ts.Type) => {
typeToInheritFrom.symbol &&
typeToInheritFrom.symbol.declarations.forEach(
(declaration) => {
context.inherit(
declaration,
baseType.typeArguments
);
}
);
typeToInheritFrom
.getSymbol()
?.getDeclarations()
?.forEach((declaration) => {
context.inherit(
declaration,
baseType.typeArguments
);
});
}
);
}
Expand Down

0 comments on commit 6ff7fcd

Please sign in to comment.