Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract & expose toString for stringifying types #1216

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { Application } from './lib/application';
export { CliApplication } from './lib/cli';
export * from './lib/stringifiers';
haltcase marked this conversation as resolved.
Show resolved Hide resolved

export { EventDispatcher, Event } from './lib/utils/events';
export { createMinimatch } from './lib/utils/paths';
Expand Down
4 changes: 3 additions & 1 deletion src/lib/models/types/abstract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { stringifyType } from '../../stringifiers';

/**
* Base class of all type definitions.
*
Expand Down Expand Up @@ -31,7 +33,7 @@ export abstract class Type {
* Return a string representation of this type.
*/
toString(): string {
return 'void';
return stringifyType(this);
}

/**
Expand Down
14 changes: 1 addition & 13 deletions src/lib/models/types/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type, UnionType, IntersectionType } from './index';
haltcase marked this conversation as resolved.
Show resolved Hide resolved
import { Type } from './index';

/**
* Represents an array type.
Expand Down Expand Up @@ -50,16 +50,4 @@ export class ArrayType extends Type {
}
return type.elementType.equals(this.elementType);
}

/**
* Return a string representation of this type.
*/
toString() {
const elementTypeStr = this.elementType.toString();
if (this.elementType instanceof UnionType || this.elementType instanceof IntersectionType) {
return '(' + elementTypeStr + ')[]';
} else {
return elementTypeStr + '[]';
}
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,4 @@ export class ConditionalType extends Type {
this.trueType.equals(type.trueType) &&
this.falseType.equals(type.falseType);
}

/**
* Return a string representation of this type.
*/
toString() {
return this.checkType + ' extends ' + this.extendsType + ' ? ' + this.trueType + ' : ' + this.falseType;
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/indexed-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,4 @@ export class IndexedAccessType extends Type {
}
return type.objectType.equals(this.objectType) && type.indexType.equals(this.indexType);
}

/**
* Return a string representation of this type.
*/
toString() {
return `${this.objectType.toString()}[${this.indexType.toString()}]`;
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/inferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,4 @@ export class InferredType extends Type {
}
return this.name === type.name;
}

/**
* Return a string representation of this type.
*/
toString() {
return `infer ${this.name}`;
}
}
12 changes: 0 additions & 12 deletions src/lib/models/types/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,4 @@ export class IntersectionType extends Type {
}
return Type.isTypeListSimilar(type.types, this.types);
}

/**
* Return a string representation of this type.
*/
toString() {
const names: string[] = [];
this.types.forEach((element) => {
names.push(element.toString());
});

return names.join(' & ');
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/intrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,4 @@ export class IntrinsicType extends Type {
return type instanceof IntrinsicType &&
type.name === this.name;
}

/**
* Return a string representation of this type.
*/
toString() {
return this.name;
}
}
12 changes: 0 additions & 12 deletions src/lib/models/types/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,4 @@ export class PredicateType extends Type {
&& this.asserts === type.asserts
&& (this.targetType?.equals(type.targetType!) ?? true);
}

/**
* Return a string representation of this type.
*/
toString() {
const out = this.asserts ? ['asserts', this.name] : [this.name];
if (this.targetType) {
out.push('is', this.targetType.toString());
}

return out.join(' ');
}
}
4 changes: 0 additions & 4 deletions src/lib/models/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ export class QueryType extends Type {
equals(other: Type) {
return other instanceof QueryType && this.queryType.equals(other.queryType);
}

toString() {
return `typeof ${this.queryType.toString()}`;
}
}
16 changes: 0 additions & 16 deletions src/lib/models/types/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,4 @@ export class ReferenceType extends Type {
equals(other: ReferenceType): boolean {
return other instanceof ReferenceType && (other.symbolFullyQualifiedName === this.symbolFullyQualifiedName || other.reflection === this.reflection);
}

/**
* Return a string representation of this type.
* @example EventEmitter<any>
*/
toString() {
const name = this.reflection ? this.reflection.name : this.name;
let typeArgs = '';
if (this.typeArguments) {
typeArgs += '<';
typeArgs += this.typeArguments.map(arg => arg.toString()).join(', ');
typeArgs += '>';
}

return name + typeArgs;
}
}
11 changes: 0 additions & 11 deletions src/lib/models/types/reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,4 @@ export class ReflectionType extends Type {
equals(type: ReflectionType): boolean {
return type === this;
}

/**
* Return a string representation of this type.
*/
toString() {
if (!this.declaration.children && this.declaration.signatures) {
return 'function';
} else {
return 'object';
}
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/string-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,4 @@ export class StringLiteralType extends Type {
return type instanceof StringLiteralType &&
type.value === this.value;
}

/**
* Return a string representation of this type.
*/
toString(): string {
return '"' + this.value + '"';
}
}
12 changes: 0 additions & 12 deletions src/lib/models/types/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,4 @@ export class TupleType extends Type {
}
return Type.isTypeListEqual(type.elements, this.elements);
}

/**
* Return a string representation of this type.
*/
toString() {
const names: string[] = [];
this.elements.forEach((element) => {
names.push(element.toString());
});

return '[' + names.join(', ') + ']';
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/type-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,4 @@ export class TypeOperatorType extends Type {

return type.target.equals(this.target);
}

/**
* Return a string representation of this type.
*/
toString() {
return `${this.operator} ${this.target.toString()}`;
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/type-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,4 @@ export class TypeParameterType extends Type {
return false;
}
}

/**
* Return a string representation of this type.
*/
toString() {
return this.name;
}
}
12 changes: 0 additions & 12 deletions src/lib/models/types/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,4 @@ export class UnionType extends Type {
}
return Type.isTypeListSimilar(type.types, this.types);
}

/**
* Return a string representation of this type.
*/
toString() {
const names: string[] = [];
this.types.forEach((element) => {
names.push(element.toString());
});

return names.join(' | ');
}
}
7 changes: 0 additions & 7 deletions src/lib/models/types/unknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,4 @@ export class UnknownType extends Type {
return type instanceof UnknownType &&
type.name === this.name;
}

/**
* Return a string representation of this type.
*/
toString() {
return this.name;
}
}
Loading