Skip to content

Commit

Permalink
feat(type-parsers): add the project parameter to the toString() m…
Browse files Browse the repository at this point in the history
…ethod (#93)

BREAKING CHANGE: `*TypeParser.formatToString()` now accepts a single parameter of `TypeParser.FormatToStringOptions<*TypeParser>`.
  • Loading branch information
RealShadowNova committed Oct 25, 2022
1 parent bb02200 commit e6059d7
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 95 deletions.
12 changes: 8 additions & 4 deletions src/lib/structures/type-parsers/ArrayTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -38,19 +39,22 @@ export class ArrayTypeParser implements TypeParser {
/**
* Converts this parser to a string.
* @since 1.0.0
* @param project The project to convert this parser to a string.
* @returns The string representation of this parser.
*/
public toString(): string {
return ArrayTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return ArrayTypeParser.formatToString({ parser: this, project });
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @param options The options to format this type parser to a string.
* @returns The string representation of this parser.
*/
public static formatToString(parser: ArrayTypeParser): string {
public static formatToString(options: TypeParser.FormatToStringOptions<ArrayTypeParser>): string {
const { parser } = options;

return `${TypeParser.wrap(parser.type, TypeParser.BindingPowers[TypeParser.Kind.Array])}[]`;
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/lib/structures/type-parsers/ConditionalTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -64,21 +65,22 @@ export class ConditionalTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return ConditionalTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return ConditionalTypeParser.formatToString({ parser: this, project });
}

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

return `${TypeParser.wrap(parser.checkType, TypeParser.BindingPowers[TypeParser.Kind.Conditional])} extends ${parser.extendsType.toString(
project
)} ? ${parser.trueType.toString(project)} : ${parser.falseType.toString(project)}`;
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/lib/structures/type-parsers/IndexedAccessTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -48,18 +49,20 @@ export class IndexedAccessTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return IndexedAccessTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return IndexedAccessTypeParser.formatToString({ parser: this, project });
}

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

return `${parser.objectType.toString(project)}[${parser.indexType.toString(project)}]`;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/lib/structures/type-parsers/InferredTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -40,17 +41,19 @@ export class InferredTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return InferredTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return InferredTypeParser.formatToString({ parser: this, project });
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @param options The options to format this type parser to a string.
* @returns The string representation of this parser.
*/
public static formatToString(parser: InferredTypeParser): string {
public static formatToString(options: TypeParser.FormatToStringOptions<InferredTypeParser>): string {
const { parser } = options;

return `infer ${parser.type}`;
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib/structures/type-parsers/IntersectionTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -40,17 +41,19 @@ export class IntersectionTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return IntersectionTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return IntersectionTypeParser.formatToString({ parser: this, project });
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @param options The options to format this type parser to a string.
* @returns The string representation of this parser.
*/
public static formatToString(parser: IntersectionTypeParser): string {
public static formatToString(options: TypeParser.FormatToStringOptions<IntersectionTypeParser>): string {
const { parser } = options;

return parser.types.map((type) => TypeParser.wrap(type, TypeParser.BindingPowers[TypeParser.Kind.Intersection])).join(' & ');
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib/structures/type-parsers/IntrinsicTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -40,17 +41,19 @@ export class IntrinsicTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return IntrinsicTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return IntrinsicTypeParser.formatToString({ parser: this, project });
}

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

return parser.type;
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib/structures/type-parsers/LiteralTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -40,17 +41,19 @@ export class LiteralTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return LiteralTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return LiteralTypeParser.formatToString({ parser: this, project });
}

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

return parser.value;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/lib/structures/type-parsers/MappedTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -80,23 +81,24 @@ export class MappedTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return MappedTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return MappedTypeParser.formatToString({ parser: this, project });
}

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

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

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

Expand Down
13 changes: 8 additions & 5 deletions src/lib/structures/type-parsers/NamedTupleMemberTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -56,18 +57,20 @@ export class NamedTupleMemberTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return NamedTupleMemberTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return NamedTupleMemberTypeParser.formatToString({ parser: this, project });
}

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

return `${parser.name}${parser.optional ? '?' : ''}: ${parser.type.toString(project)}`;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/lib/structures/type-parsers/OptionalTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -40,17 +41,19 @@ export class OptionalTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return OptionalTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return OptionalTypeParser.formatToString({ parser: this, project });
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @param options The options to format this type parser to a string.
* @returns The string representation of this parser.
*/
public static formatToString(parser: OptionalTypeParser): string {
public static formatToString(options: TypeParser.FormatToStringOptions<OptionalTypeParser>): string {
const { parser } = options;

return `${TypeParser.wrap(parser.type, TypeParser.BindingPowers[TypeParser.Kind.Optional])}?`;
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/lib/structures/type-parsers/PredicateTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import { TypeParser } from './TypeParser';

/**
Expand Down Expand Up @@ -57,18 +58,20 @@ export class PredicateTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return PredicateTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return PredicateTypeParser.formatToString({ parser: this, project });
}

/**
* Formats this type parser to a string.
* @since 4.0.0
* @param parser The parser to format.
* @param options The options to format this type parser to a string.
* @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()}`;
public static formatToString(options: TypeParser.FormatToStringOptions<PredicateTypeParser>): string {
const { parser, project } = options;

return parser.asserts ? `asserts ${parser.name}` : `${parser.name} is ${parser.type!.toString(project)}`;
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/lib/structures/type-parsers/QueryTypeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectParser } from '../ProjectParser';
import type { ReferenceTypeParser } from './ReferenceTypeParser';
import { TypeParser } from './TypeParser';

Expand Down Expand Up @@ -41,18 +42,20 @@ export class QueryTypeParser implements TypeParser {
* @since 1.0.0
* @returns The string representation of this parser.
*/
public toString(): string {
return QueryTypeParser.formatToString(this);
public toString(project?: ProjectParser): string {
return QueryTypeParser.formatToString({ parser: this, project });
}

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

return `typeof ${parser.query.toString(project)}`;
}
}

Expand Down

0 comments on commit e6059d7

Please sign in to comment.