Skip to content

Commit

Permalink
feat(class-parser): add parent getter (#53)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `ClassConstructorParser.generateFromTypeDoc()` now takes 3 parameters instead of 2.
BREAKING CHANGE: `ClassMethodParser.generateFromTypeDoc()` now takes 3 parameters instead of 2.
BREAKING CHANGE: `ClassPropertyParser.generateFromTypeDoc()` now takes 3 parameters instead of 2.
  • Loading branch information
RealShadowNova committed Sep 12, 2022
1 parent defeb42 commit 0ad08e0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
37 changes: 34 additions & 3 deletions src/lib/structures/class-parser/ClassConstructorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import { ReflectionKind } from '../../types';
import { CommentParser, ParameterParser, SourceParser } from '../misc';
import { Parser } from '../Parser';
import type { ProjectParser } from '../ProjectParser';
import type { ClassParser } from './ClassParser';

export class ClassConstructorParser extends Parser {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
public readonly parentId: number;

/**
* The parameter parsers of this constructor.
* @since 1.0.0
Expand All @@ -14,11 +21,20 @@ export class ClassConstructorParser extends Parser {
public constructor(data: ClassConstructorParser.Data, project: ProjectParser) {
super(data, project);

const { parameters } = data;
const { parentId, parameters } = data;

this.parentId = parentId;
this.parameters = parameters;
}

/**
* The parent class parser.
* @since 4.0.0
*/
public get parent(): ClassParser {
return this.project.find(this.parentId) as ClassParser;
}

/**
* Converts this parser to a JSON compatible format.
* @since 1.0.0
Expand All @@ -27,6 +43,7 @@ export class ClassConstructorParser extends Parser {
public toJSON(): ClassConstructorParser.JSON {
return {
...super.toJSON(),
parentId: this.parentId,
parameters: this.parameters.map((parameter) => parameter.toJSON())
};
}
Expand All @@ -38,7 +55,7 @@ export class ClassConstructorParser extends Parser {
* @param project The project to generate the parser from.
* @returns The generated parser.
*/
public static generateFromTypeDoc(reflection: JSONOutput.DeclarationReflection, project: ProjectParser): ClassConstructorParser {
public static generateFromTypeDoc(reflection: JSONOutput.DeclarationReflection, parentId: number, project: ProjectParser): ClassConstructorParser {
const { kind, kindString = 'Unknown', id, name, comment = { summary: [] }, sources = [], signatures = [] } = reflection;

if (kind !== ReflectionKind.Constructor) {
Expand All @@ -57,21 +74,23 @@ export class ClassConstructorParser extends Parser {
name,
comment: CommentParser.generateFromTypeDoc(comment, project),
source: sources.length ? SourceParser.generateFromTypeDoc(sources[0], project) : null,
parentId,
parameters: parameters.map((parameter) => ParameterParser.generateFromTypeDoc(parameter, project))
},
project
);
}

public static generateFromJSON(data: ClassConstructorParser.JSON, project: ProjectParser): ClassConstructorParser {
const { id, name, comment, source, parameters } = data;
const { id, name, comment, source, parentId, parameters } = data;

return new ClassConstructorParser(
{
id,
name,
comment: CommentParser.generateFromJSON(comment, project),
source: source ? SourceParser.generateFromJSON(source, project) : null,
parentId,
parameters: parameters.map((parameter) => ParameterParser.generateFromJSON(parameter, project))
},
project
Expand All @@ -81,6 +100,12 @@ export class ClassConstructorParser extends Parser {

export namespace ClassConstructorParser {
export interface Data extends Parser.Data {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
parentId: number;

/**
* The parameter parsers of this constructor.
* @since 1.0.0
Expand All @@ -89,6 +114,12 @@ export namespace ClassConstructorParser {
}

export interface JSON extends Parser.JSON {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
parentId: number;

/**
* The parameter parsers of this constructor.
* @since 1.0.0
Expand Down
36 changes: 33 additions & 3 deletions src/lib/structures/class-parser/ClassMethodParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { ClassParser } from './ClassParser';
* @since 1.0.0
*/
export class ClassMethodParser extends Parser {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
public readonly parentId: number;

/**
* The accessibility of this method.
* @since 1.0.0
Expand Down Expand Up @@ -37,14 +43,23 @@ export class ClassMethodParser extends Parser {
public constructor(data: ClassMethodParser.Data, project: ProjectParser) {
super(data, project);

const { accessibility, abstract, static: _static, signatures } = data;
const { parentId, accessibility, abstract, static: _static, signatures } = data;

this.parentId = parentId;
this.accessibility = accessibility;
this.abstract = abstract;
this.static = _static;
this.signatures = signatures;
}

/**
* The parent class parser.
* @since 4.0.0
*/
public get parent(): ClassParser {
return this.project.find(this.parentId) as ClassParser;
}

/**
* Convert this parser to a JSON compatible format.
* @since 1.0.0
Expand All @@ -53,6 +68,7 @@ export class ClassMethodParser extends Parser {
public toJSON(): ClassMethodParser.JSON {
return {
...super.toJSON(),
parentId: this.parentId,
accessibility: this.accessibility,
abstract: this.abstract,
static: this.static,
Expand All @@ -67,7 +83,7 @@ export class ClassMethodParser extends Parser {
* @param project The project this parser belongs to.
* @returns The generated parser.
*/
public static generateFromTypeDoc(reflection: JSONOutput.DeclarationReflection, project: ProjectParser): ClassMethodParser {
public static generateFromTypeDoc(reflection: JSONOutput.DeclarationReflection, parentId: number, project: ProjectParser): ClassMethodParser {
const { kind, kindString = 'Unknown', id, name, comment = { summary: [] }, sources = [], flags, signatures = [] } = reflection;

if (kind !== ReflectionKind.Method) throw new Error(`Expected Method (${ReflectionKind.Method}), but received ${kindString} (${kind})`);
Expand All @@ -78,6 +94,7 @@ export class ClassMethodParser extends Parser {
name,
comment: CommentParser.generateFromTypeDoc(comment, project),
source: sources.length ? SourceParser.generateFromTypeDoc(sources[0], project) : null,
parentId,
accessibility: flags.isPrivate
? ClassParser.Accessibility.Private
: flags.isProtected
Expand All @@ -92,14 +109,15 @@ export class ClassMethodParser extends Parser {
}

public static generateFromJSON(json: ClassMethodParser.JSON, project: ProjectParser): ClassMethodParser {
const { id, name, comment, source, accessibility, abstract, static: _static, signatures } = json;
const { id, name, comment, source, parentId, accessibility, abstract, static: _static, signatures } = json;

return new ClassMethodParser(
{
id,
name,
comment: CommentParser.generateFromJSON(comment, project),
source: source ? SourceParser.generateFromJSON(source, project) : null,
parentId,
accessibility,
abstract,
static: _static,
Expand All @@ -112,6 +130,12 @@ export class ClassMethodParser extends Parser {

export namespace ClassMethodParser {
export interface Data extends Parser.Data {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
parentId: number;

/**
* The accessibility of this method.
* @since 1.0.0
Expand All @@ -138,6 +162,12 @@ export namespace ClassMethodParser {
}

export interface JSON extends Parser.JSON {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
parentId: number;

/**
* The accessibility of this method.
* @since 1.0.0
Expand Down
6 changes: 3 additions & 3 deletions src/lib/structures/class-parser/ClassParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ export class ClassParser extends Parser {

const properties = children
.filter((child) => child.kind === ReflectionKind.Property || (child.kind === ReflectionKind.Accessor && child.getSignature))
.map((child) => ClassPropertyParser.generateFromTypeDoc(child, project));
.map((child) => ClassPropertyParser.generateFromTypeDoc(child, id, project));

const methods = children
.filter((child) => child.kind === ReflectionKind.Method)
.map((child) => ClassMethodParser.generateFromTypeDoc(child, project));
.map((child) => ClassMethodParser.generateFromTypeDoc(child, id, project));

return new ClassParser(
{
Expand All @@ -132,7 +132,7 @@ export class ClassParser extends Parser {
abstract: Boolean(flags.isAbstract),
extendsType: extendedTypes.length ? TypeParser.generateFromTypeDoc(extendedTypes[0]) : null,
implementsType: implementedTypes.map((implementedType) => TypeParser.generateFromTypeDoc(implementedType)),
construct: ClassConstructorParser.generateFromTypeDoc(construct, project),
construct: ClassConstructorParser.generateFromTypeDoc(construct, id, project),
properties,
methods
},
Expand Down
37 changes: 34 additions & 3 deletions src/lib/structures/class-parser/ClassPropertyParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { ClassParser } from './ClassParser';
* @since 1.0.0
*/
export class ClassPropertyParser extends Parser {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
public readonly parentId: number;

/**
* The accessibility of this property.
* @since 1.0.0
Expand Down Expand Up @@ -50,8 +56,9 @@ export class ClassPropertyParser extends Parser {
public constructor(data: ClassPropertyParser.Data, project: ProjectParser) {
super(data, project);

const { accessibility, abstract, static: _static, readonly, optional, type } = data;
const { parentId, accessibility, abstract, static: _static, readonly, optional, type } = data;

this.parentId = parentId;
this.accessibility = accessibility;
this.abstract = abstract;
this.static = _static;
Expand All @@ -60,6 +67,14 @@ export class ClassPropertyParser extends Parser {
this.type = type;
}

/**
* The parent class parser.
* @since 4.0.0
*/
public get parent(): ClassParser {
return this.project.find(this.parentId) as ClassParser;
}

/**
* Converts this parser to a JSON compatible format.
* @since 1.0.0
Expand All @@ -68,6 +83,7 @@ export class ClassPropertyParser extends Parser {
public toJSON(): ClassPropertyParser.JSON {
return {
...super.toJSON(),
parentId: this.parentId,
accessibility: this.accessibility,
abstract: this.abstract,
static: this.static,
Expand All @@ -84,7 +100,7 @@ export class ClassPropertyParser extends Parser {
* @param project The project this parser belongs to.
* @returns The generated parser.
*/
public static generateFromTypeDoc(reflection: JSONOutput.DeclarationReflection, project: ProjectParser): ClassPropertyParser {
public static generateFromTypeDoc(reflection: JSONOutput.DeclarationReflection, parentId: number, project: ProjectParser): ClassPropertyParser {
const { kind, kindString = 'Unknown', id, name, comment = { summary: [] }, sources = [], type, flags, getSignature } = reflection;

if (kind !== ReflectionKind.Property && kind !== ReflectionKind.Accessor) {
Expand All @@ -104,6 +120,7 @@ export class ClassPropertyParser extends Parser {
name,
comment: CommentParser.generateFromTypeDoc(comment, project),
source: sources.length ? SourceParser.generateFromTypeDoc(sources[0], project) : null,
parentId,
accessibility: flags.isPrivate
? ClassParser.Accessibility.Private
: flags.isProtected
Expand All @@ -125,6 +142,7 @@ export class ClassPropertyParser extends Parser {
name,
comment: CommentParser.generateFromTypeDoc(comment, project),
source: sources.length ? SourceParser.generateFromTypeDoc(sources[0], project) : null,
parentId,
accessibility: flags.isPrivate
? ClassParser.Accessibility.Private
: flags.isProtected
Expand All @@ -141,14 +159,15 @@ export class ClassPropertyParser extends Parser {
}

public static generateFromJSON(json: ClassPropertyParser.JSON, project: ProjectParser): ClassPropertyParser {
const { id, name, comment, source, accessibility, abstract, static: _static, readonly, optional, type } = json;
const { id, name, comment, source, parentId, accessibility, abstract, static: _static, readonly, optional, type } = json;

return new ClassPropertyParser(
{
id,
name,
comment: CommentParser.generateFromJSON(comment, project),
source: source ? SourceParser.generateFromJSON(source, project) : null,
parentId,
accessibility,
abstract,
static: _static,
Expand All @@ -163,6 +182,12 @@ export class ClassPropertyParser extends Parser {

export namespace ClassPropertyParser {
export interface Data extends Parser.Data {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
parentId: number;

/**
* The accessibility of this property.
* @since 1.0.0
Expand Down Expand Up @@ -201,6 +226,12 @@ export namespace ClassPropertyParser {
}

export interface JSON extends Parser.JSON {
/**
* The id of the parent class parser.
* @since 4.0.0
*/
parentId: number;

/**
* The accessibility of this property.
* @since 1.0.0
Expand Down

0 comments on commit 0ad08e0

Please sign in to comment.