Skip to content

Commit

Permalink
feat(type-parsers): add static formatToString method (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealShadowNova committed Sep 12, 2022
1 parent b3fd346 commit 9a567e6
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 27 deletions.
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/ArrayTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class ArrayTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `${TypeParser.wrap(this.type, TypeParser.BindingPowers[TypeParser.Kind.Array])}[]`;
return ArrayTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: ArrayTypeParser): string => {
return `${TypeParser.wrap(parser.type, TypeParser.BindingPowers[TypeParser.Kind.Array])}[]`;
};
}

export namespace ArrayTypeParser {
Expand Down
16 changes: 13 additions & 3 deletions src/lib/structures/type-parsers/ConditionalTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,21 @@ export class ConditionalTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return ConditionalTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: ConditionalTypeParser): string => {
return `${TypeParser.wrap(
this.checkType,
parser.checkType,
TypeParser.BindingPowers[TypeParser.Kind.Conditional]
)} extends ${this.extendsType.toString()} ? ${this.trueType.toString()} : ${this.falseType.toString()}`;
}
)} extends ${parser.extendsType.toString()} ? ${parser.trueType.toString()} : ${parser.falseType.toString()}`;
};
}

export namespace ConditionalTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/IndexedAccessType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ export class IndexedAccessTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `${this.objectType.toString()}[${this.indexType.toString()}]`;
return IndexedAccessTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: IndexedAccessTypeParser): string => {
return `${parser.objectType.toString()}[${parser.indexType.toString()}]`;
};
}

export namespace IndexedAccessTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/InferredTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class InferredTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `infer ${this.type}`;
return InferredTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: InferredTypeParser): string => {
return `infer ${parser.type}`;
};
}

export namespace InferredTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/IntersectionTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class IntersectionTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return this.types.map((type) => TypeParser.wrap(type, TypeParser.BindingPowers[TypeParser.Kind.Intersection])).join(' & ');
return IntersectionTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: IntersectionTypeParser): string => {
return parser.types.map((type) => TypeParser.wrap(type, TypeParser.BindingPowers[TypeParser.Kind.Intersection])).join(' & ');
};
}

export namespace IntersectionTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/IntrinsicTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class IntrinsicTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return this.type;
return IntrinsicTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: IntrinsicTypeParser): string => {
return parser.type;
};
}

export namespace IntrinsicTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/LiteralTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class LiteralTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return this.value;
return LiteralTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: LiteralTypeParser): string => {
return parser.value;
};
}

export namespace LiteralTypeParser {
Expand Down
18 changes: 14 additions & 4 deletions src/lib/structures/type-parsers/MappedTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,23 @@ export class MappedTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return MappedTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: MappedTypeParser): string => {
const readonly =
this.readonly === MappedTypeParser.Modifier.Add ? 'readonly' : this.readonly === MappedTypeParser.Modifier.Remove ? '-readonly' : '';
parser.readonly === MappedTypeParser.Modifier.Add ? 'readonly' : parser.readonly === MappedTypeParser.Modifier.Remove ? '-readonly' : '';

const optional = this.optional === MappedTypeParser.Modifier.Add ? '?' : this.optional === MappedTypeParser.Modifier.Remove ? '-?' : '';
const optional = parser.optional === MappedTypeParser.Modifier.Add ? '?' : parser.optional === MappedTypeParser.Modifier.Remove ? '-?' : '';

return `{ ${readonly}[${this.parameter} in ${this.parameterType.toString()}]${optional}: ${this.templateType.toString()} }`;
}
return `{ ${readonly}[${parser.parameter} in ${parser.parameterType.toString()}]${optional}: ${parser.templateType.toString()} }`;
};
}

export namespace MappedTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/NamedTupleMemberTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,18 @@ export class NamedTupleMemberTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `${this.name}${this.optional ? '?' : ''}: ${this.type.toString()}`;
return NamedTupleMemberTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: NamedTupleMemberTypeParser): string => {
return `${parser.name}${parser.optional ? '?' : ''}: ${parser.type.toString()}`;
};
}

export namespace NamedTupleMemberTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/OptionalTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class OptionalTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `${TypeParser.wrap(this.type, TypeParser.BindingPowers[TypeParser.Kind.Optional])}?`;
return OptionalTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: OptionalTypeParser): string => {
return `${TypeParser.wrap(parser.type, TypeParser.BindingPowers[TypeParser.Kind.Optional])}?`;
};
}

export namespace OptionalTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/PredicateTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ export class PredicateTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return this.asserts ? `asserts ${this.name}` : `${this.name} is ${this.type!.toString()}`;
return PredicateTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: PredicateTypeParser): string => {
return parser.asserts ? `asserts ${parser.name}` : `${parser.name} is ${parser.type!.toString()}`;
};
}

export namespace PredicateTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/QueryTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ export class QueryTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `typeof ${this.query.toString()}`;
return QueryTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: QueryTypeParser): string => {
return `typeof ${parser.query.toString()}`;
};
}

export namespace QueryTypeParser {
Expand Down
16 changes: 13 additions & 3 deletions src/lib/structures/type-parsers/ReferenceTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,20 @@ export class ReferenceTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
const typeArguments = this.typeArguments.length > 0 ? `<${this.typeArguments.map((type) => type.toString()).join(', ')}>` : '';

return `${this.packageName ? `${this.packageName}.` : ''}${this.name}${typeArguments}`;
return ReferenceTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: ReferenceTypeParser): string => {
const typeArguments = parser.typeArguments.length > 0 ? `<${parser.typeArguments.map((type) => type.toString()).join(', ')}>` : '';

return `${parser.packageName ? `${parser.packageName}.` : ''}${parser.name}${typeArguments}`;
};
}

export namespace ReferenceTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/ReflectionTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ export class ReflectionTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return !this.reflection?.children && this.reflection?.signatures ? 'Function' : 'Object';
return ReflectionTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: ReflectionTypeParser): string => {
return !parser.reflection?.children && parser.reflection?.signatures ? 'Function' : 'Object';
};
}

export namespace ReflectionTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/RestTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class RestTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `...${TypeParser.wrap(this.type, TypeParser.BindingPowers[TypeParser.Kind.Rest])}`;
return RestTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: RestTypeParser): string => {
return `...${TypeParser.wrap(parser.type, TypeParser.BindingPowers[TypeParser.Kind.Rest])}`;
};
}

export namespace RestTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/TemplateLiteralTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ export class TemplateLiteralTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `\`${this.head}${this.tail.map((tail) => `\${${tail.type.toString()}}${tail.text}`).join('')}\``;
return TemplateLiteralTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: TemplateLiteralTypeParser): string => {
return `\`${parser.head}${parser.tail.map((tail) => `\${${tail.type.toString()}}${tail.text}`).join('')}\``;
};
}

export namespace TemplateLiteralTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/TupleTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class TupleTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `[${this.types.map((type) => type.toString()).join(', ')}]`;
return TupleTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: TupleTypeParser): string => {
return `[${parser.types.map((type) => type.toString()).join(', ')}]`;
};
}

export namespace TupleTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/TypeOperatorTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ export class TypeOperatorTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return `${this.operator} ${this.type.toString()}`;
return TypeOperatorTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: TypeOperatorTypeParser): string => {
return `${parser.operator} ${parser.type.toString()}`;
};
}

export namespace TypeOperatorTypeParser {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/type-parsers/UnionTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ export class UnionTypeParser implements TypeParser {
* @returns The string representation of this parser.
*/
public toString(): string {
return this.types.map((type) => TypeParser.wrap(type, TypeParser.BindingPowers[TypeParser.Kind.Union])).join(' | ');
return UnionTypeParser.formatToString(this);
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @returns The string representation of this parser.
*/
public static formatToString = (parser: UnionTypeParser): string => {
return parser.types.map((type) => TypeParser.wrap(type, TypeParser.BindingPowers[TypeParser.Kind.Union])).join(' | ');
};
}

export namespace UnionTypeParser {
Expand Down

0 comments on commit 9a567e6

Please sign in to comment.