Skip to content

Commit

Permalink
refactor(TypeParameterParser): rename property type to constraint (
Browse files Browse the repository at this point in the history
…#90)

BREAKING CHANGE: `TypeParameterParser#type` has been renamed to `#constraint`.
  • Loading branch information
RealShadowNova committed Oct 24, 2022
1 parent 51a8c6a commit 93bedae
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 34 deletions.
145 changes: 122 additions & 23 deletions src/bin/lib/migrateProjectJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ClassParser } from '../../lib/structures/class-parser';
import type { EnumParser } from '../../lib/structures/enum-parser';
import type { FunctionParser } from '../../lib/structures/FunctionParser';
import type { InterfaceParser } from '../../lib/structures/interface-parser';
import type { ParameterParser, SignatureParser, SourceParser } from '../../lib/structures/misc';
import type { ParameterParser, SignatureParser, SourceParser, TypeParameterParser } from '../../lib/structures/misc';
import type { NamespaceParser } from '../../lib/structures/NamespaceParser';
import { ProjectParser } from '../../lib/structures/ProjectParser';
import type { TypeAliasParser } from '../../lib/structures/TypeAliasParser';
Expand Down Expand Up @@ -221,7 +221,7 @@ function migrateClassJson(
abstract,
extendsType,
implementsType,
typeParameters,
typeParameters: typeParameters.map((typeParameterJson) => migrateTypeParameterJson(typeParameterJson, typeDocJsonParserVersion)),
construct: {
id: construct.id,
name: construct.name,
Expand Down Expand Up @@ -687,7 +687,7 @@ function migrateTypeAlias(
comment,
source: source ? migrateSourceJson(source, typeDocJsonParserVersion) : null,
external,
typeParameters,
typeParameters: typeParameters.map((typeParameterJson) => migrateTypeParameterJson(typeParameterJson, typeDocJsonParserVersion)),
type
};
}
Expand Down Expand Up @@ -862,7 +862,10 @@ function migrateParameterJson(parameterJson: Migration.MajorTwo.MinorOne.Misc.Pa
}

function migrateSignatureJson(
signatureJson: Migration.MajorTwo.MinorOne.Misc.SignatureJson | Migration.MajorTwo.MinorThree.Misc.SignatureJson,
signatureJson:
| Migration.MajorTwo.MinorOne.Misc.SignatureJson
| Migration.MajorTwo.MinorThree.Misc.SignatureJson
| Migration.MajorSeven.MinorZero.Misc.SignatureJson,
typeDocJsonParserVersion: string
): SignatureParser.Json {
const { id, name, typeParameters, parameters, returnType } = signatureJson;
Expand All @@ -877,7 +880,7 @@ function migrateSignatureJson(
id,
name,
comment: { description: null, blockTags: [], modifierTags: [] },
typeParameters,
typeParameters: typeParameters.map((typeParameterJson) => migrateTypeParameterJson(typeParameterJson, typeDocJsonParserVersion)),
parameters: parameters.map((parameterJson) => migrateParameterJson(parameterJson, typeDocJsonParserVersion)),
returnType
};
Expand Down Expand Up @@ -917,7 +920,7 @@ function migrateSignatureJson(
id,
name,
comment,
typeParameters,
typeParameters: typeParameters.map((typeParameterJson) => migrateTypeParameterJson(typeParameterJson, typeDocJsonParserVersion)),
parameters: parameters.map((parameterJson) => migrateParameterJson(parameterJson, typeDocJsonParserVersion)),
returnType
};
Expand All @@ -927,6 +930,69 @@ function migrateSignatureJson(
throw new Error(`Unsupported typeDocJsonParserVersion: ${typeDocJsonParserVersion}`);
}

function migrateTypeParameterJson(
typeParameterJson: Migration.MajorTwo.MinorOne.Misc.TypeParameterJson | Migration.MajorSeven.MinorZero.Misc.TypeParameterJson,
typeDocJsonParserVersion: string
): TypeParameterParser.Json {
const { id, name, default: _default } = typeParameterJson;

switch (typeDocJsonParserVersion) {
case '2.1.0':

case '2.2.0':

case '2.2.1':

case '2.3.0':

case '2.3.1':

case '3.0.0':

case '3.1.0':

case '3.2.0':

case '4.0.0':

case '5.0.0':

case '5.0.1':

case '5.1.0':

case '5.2.0':

case '6.0.0':

case '6.0.1':

case '6.0.2': {
const { type } = typeParameterJson as Migration.MajorTwo.MinorOne.Misc.TypeParameterJson;

return {
id,
name,
constraint: type,
default: _default
};
}

case '7.0.0': {
const { constraint } = typeParameterJson as Migration.MajorSeven.MinorZero.Misc.TypeParameterJson;

return {
id,
name,
constraint,
default: _default
};
}
}

throw new Error(`Unsupported typeDocJsonParserVersion: ${typeDocJsonParserVersion}`);
}

export namespace Migration {
export namespace MajorTwo {
export namespace MinorOne {
Expand All @@ -946,8 +1012,8 @@ export namespace Migration {
export interface ClassJson extends Parser {
external: boolean;
abstract: boolean;
extendsType: Type | null;
implementsType: Type[];
extendsType: TypeJson | null;
implementsType: TypeJson[];
construct: ClassJson.ConstructorJson;
properties: ClassJson.PropertyJson[];
methods: ClassJson.MethodJson[];
Expand All @@ -964,7 +1030,7 @@ export namespace Migration {
static: boolean;
readonly: boolean;
optional: boolean;
type: Type;
type: TypeJson;
}

export interface MethodJson extends Parser {
Expand All @@ -983,7 +1049,7 @@ export namespace Migration {

export interface ConstantJson extends Parser {
external: boolean;
type: Type;
type: TypeJson;
value: string;
}

Expand Down Expand Up @@ -1012,7 +1078,7 @@ export namespace Migration {
export namespace InterfaceJson {
export interface PropertyJson extends Parser {
readonly: boolean;
type: Type;
type: TypeJson;
}
}

Expand All @@ -1030,7 +1096,7 @@ export namespace Migration {
export interface TypeAliasJson extends Parser {
external: boolean;
typeParameters: Misc.TypeParameterJson[];
type: Type;
type: TypeJson;
}

export interface Parser {
Expand All @@ -1040,11 +1106,11 @@ export namespace Migration {
source: Misc.SourceJson | null;
}

export interface Type {
kind: Type.Kind;
export interface TypeJson {
kind: TypeJson.Kind;
}

export namespace Type {
export namespace TypeJson {
export enum Kind {
Array = 'array',
Conditional = 'conditional',
Expand Down Expand Up @@ -1086,15 +1152,15 @@ export namespace Migration {
export interface ParameterJson {
id: number;
name: string;
type: Type;
type: TypeJson;
}

export interface SignatureJson {
id: number;
name: string;
typeParameters: TypeParameterJson[];
parameters: ParameterJson[];
returnType: Type;
returnType: TypeJson;
}

export interface SourceJson {
Expand All @@ -1106,8 +1172,8 @@ export namespace Migration {
export interface TypeParameterJson {
id: number;
name: string;
type: Type | null;
default: Type | null;
type: TypeJson | null;
default: TypeJson | null;
}
}
}
Expand Down Expand Up @@ -1342,12 +1408,45 @@ export namespace Migration {

export namespace MajorSeven {
export namespace MinorZero {
export interface InterfaceJson extends MajorSix.MinorZero.InterfaceJson {
typeParameters: MajorTwo.MinorOne.Misc.TypeParameterJson[];
export interface ProjectJson extends Omit<MajorSix.MinorZero.ProjectJson, 'classes' | 'interfaces' | 'functions'> {
classes: ClassJson[];
interfaces: InterfaceJson[];
functions: FunctionJson[];
}

export interface NamespaceJson extends Omit<MajorThree.MinorZero.NamespaceJson, 'classes' | 'constants' | 'enums' | 'interfaces'> {
interfaces: InterfaceJson[];
export interface ClassJson extends Omit<MajorSix.MinorZero.ClassJson, 'methods'> {
methods: ClassJson.MethodJson[];
}

export namespace ClassJson {
export interface MethodJson extends Omit<MajorSix.MinorZero.ClassJson.MethodJson, 'signatures'> {
signatures: Misc.SignatureJson[];
}
}

export interface InterfaceJson extends Omit<MajorSix.MinorZero.InterfaceJson, 'methods'> {
typeParameters: Misc.TypeParameterJson[];
methods: InterfaceJson.MethodJson[];
}

export namespace InterfaceJson {
export interface MethodJson extends Omit<MajorSix.MinorZero.InterfaceJson.MethodJson, 'signatures'> {
signatures: Misc.SignatureJson[];
}
}

export interface FunctionJson extends Omit<MajorThree.MinorZero.FunctionJson, 'signatures'> {
signatures: Misc.SignatureJson[];
}

export namespace Misc {
export interface SignatureJson extends Omit<MajorTwo.MinorOne.Misc.SignatureJson, 'typeParameters'> {
typeParameters: TypeParameterJson[];
}

export interface TypeParameterJson extends Omit<MajorTwo.MinorOne.Misc.TypeParameterJson, 'type'> {
constraint: MajorTwo.MinorOne.TypeJson | null;
}
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/lib/structures/misc/TypeParameterParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class TypeParameterParser {
* The type of this type parameter.
* @since 1.0.0
*/
public readonly type: TypeParser | null;
public readonly constraint: TypeParser | null;

/**
* The default value of this type parameter.
Expand All @@ -32,11 +32,11 @@ export class TypeParameterParser {
public readonly default: TypeParser | null;

public constructor(data: TypeParameterParser.Data) {
const { id, name, type, default: defaultValue } = data;
const { id, name, constraint, default: defaultValue } = data;

this.id = id;
this.name = name;
this.type = type;
this.constraint = constraint;
this.default = defaultValue;
}

Expand All @@ -49,7 +49,7 @@ export class TypeParameterParser {
return {
id: this.id,
name: this.name,
type: this.type ? this.type.toJSON() : null,
constraint: this.constraint ? this.constraint.toJSON() : null,
default: this.default ? this.default.toJSON() : null
};
}
Expand All @@ -71,7 +71,7 @@ export class TypeParameterParser {
return new TypeParameterParser({
id,
name,
type: type ? TypeParser.generateFromTypeDoc(type) : null,
constraint: type ? TypeParser.generateFromTypeDoc(type) : null,
default: _default ? TypeParser.generateFromTypeDoc(_default) : null
});
}
Expand All @@ -82,12 +82,12 @@ export class TypeParameterParser {
* @returns The generated parser.
*/
public static generateFromJson(json: TypeParameterParser.Json): TypeParameterParser {
const { id, name, type, default: _default } = json;
const { id, name, constraint, default: _default } = json;

return new TypeParameterParser({
id,
name,
type: type ? TypeParser.generateFromJson(type) : null,
constraint: constraint ? TypeParser.generateFromJson(constraint) : null,
default: _default ? TypeParser.generateFromJson(_default) : null
});
}
Expand All @@ -108,10 +108,10 @@ export namespace TypeParameterParser {
name: string;

/**
* The type of this type parameter.
* The constraint of this type parameter.
* @since 1.0.0
*/
type: TypeParser | null;
constraint: TypeParser | null;

/**
* The default value of this type parameter.
Expand All @@ -134,10 +134,10 @@ export namespace TypeParameterParser {
name: string;

/**
* The type of this type parameter in a Json compatible format.
* The constraint of this type parameter in a Json compatible format.
* @since 1.0.0
*/
type: TypeParser.Json | null;
constraint: TypeParser.Json | null;

/**
* The default value of this type parameter in a Json compatible format.
Expand Down

0 comments on commit 93bedae

Please sign in to comment.