Skip to content

Add support for extends and implements #122

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 15 additions & 4 deletions src/declarations/ClassDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AccessorDeclaration } from './AccessorDeclaration';
import { ConstructorDeclaration } from './ConstructorDeclaration';
import { ClassLikeDeclaration, ExportableDeclaration, GenericDeclaration } from './Declaration';
import { AbstractDeclaration, ClassLikeDeclaration, ExportableDeclaration, GenericDeclaration,
} from './Declaration';
import { InterfaceDeclaration } from './InterfaceDeclaration';
import { MethodDeclaration } from './MethodDeclaration';
import { PropertyDeclaration } from './PropertyDeclaration';

Expand All @@ -12,18 +14,27 @@ import { PropertyDeclaration } from './PropertyDeclaration';
* @implements {ClassLikeDeclaration}
* @implements {ExportableDeclaration}
* @implements {GenericDeclaration}
* @implements {AbstractDeclaration}
*/
export class ClassDeclaration implements ClassLikeDeclaration, ExportableDeclaration, GenericDeclaration {
export class ClassDeclaration
implements
ClassLikeDeclaration,
ExportableDeclaration,
GenericDeclaration,
AbstractDeclaration
{
public ctor: ConstructorDeclaration | undefined;
public accessors: AccessorDeclaration[] = [];
public properties: PropertyDeclaration[] = [];
public methods: MethodDeclaration[] = [];
public typeParameters: string[] | undefined;

public isAbstract: boolean = false;
public implements: InterfaceDeclaration[] = [];
public extends: ClassDeclaration[] = [];
constructor(
public name: string,
public isExported: boolean,
public start?: number,
public end?: number,
) { }
) {}
}
18 changes: 18 additions & 0 deletions src/declarations/Declaration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Node } from '../Node';
import { AccessorDeclaration } from './AccessorDeclaration';
import { ClassDeclaration } from './ClassDeclaration';
import { DeclarationVisibility } from './DeclarationVisibility';
import { InterfaceDeclaration } from './InterfaceDeclaration';
import { MethodDeclaration } from './MethodDeclaration';
import { ParameterDeclaration } from './ParameterDeclaration';
import { PropertyDeclaration } from './PropertyDeclaration';
Expand Down Expand Up @@ -131,6 +133,22 @@ export interface ClassLikeDeclaration extends Declaration {
* @memberof ClassLikeDeclaration
*/
methods: MethodDeclaration[];

/**
* The methods of the declaration.
*
* @type {InterfaceDeclaration[]}
* @memberof ClassLikeDeclaration
*/
implements: InterfaceDeclaration[];

/**
* The methods of the declaration.
*
* @type {ClassDeclaration[]}
* @memberof ClassLikeDeclaration
*/
extends: ClassDeclaration[];
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/declarations/InterfaceDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AccessorDeclaration } from './AccessorDeclaration';
import { ClassDeclaration } from './ClassDeclaration';
import { ClassLikeDeclaration, ExportableDeclaration, GenericDeclaration } from './Declaration';
import { MethodDeclaration } from './MethodDeclaration';
import { PropertyDeclaration } from './PropertyDeclaration';
Expand All @@ -16,6 +17,8 @@ export class InterfaceDeclaration implements ClassLikeDeclaration, ExportableDec
public typeParameters: string[] | undefined;
public properties: PropertyDeclaration[] = [];
public methods: MethodDeclaration[] = [];
implements: InterfaceDeclaration[] = [];
extends: ClassDeclaration[] = [];

constructor(
public name: string,
Expand Down
32 changes: 32 additions & 0 deletions src/node-parser/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
Node,
ObjectBindingPattern,
SyntaxKind,
isHeritageClause,
} from 'typescript';

import { GetterDeclaration, SetterDeclaration } from '../declarations/AccessorDeclaration';
import { ClassDeclaration as TshClass } from '../declarations/ClassDeclaration';
import { InterfaceDeclaration as TshInterface } from '../declarations/InterfaceDeclaration';
import { ConstructorDeclaration as TshConstructor } from '../declarations/ConstructorDeclaration';
import { DefaultDeclaration as TshDefault } from '../declarations/DefaultDeclaration';
import { MethodDeclaration as TshMethod } from '../declarations/MethodDeclaration';
Expand Down Expand Up @@ -130,6 +132,9 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
classDeclaration.typeParameters = node.typeParameters.map(param => param.getText());
}

classDeclaration.isAbstract = node.modifiers !== undefined
&& node.modifiers.some(m => m.kind === SyntaxKind.AbstractKeyword);

if (node.members) {
node.members.forEach((o) => {
if (isPropertyDeclaration(o)) {
Expand Down Expand Up @@ -214,6 +219,33 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
}
});
}

if (node.heritageClauses) {
node.heritageClauses.forEach((o) => {
if(isHeritageClause(o)){
o.types.forEach((type) => {
if(o.token == SyntaxKind.ExtendsKeyword){
const className = (type.expression as Identifier).escapedText;
classDeclaration.extends.push(
new TshClass(
className.toString(),
classDeclaration.isExported
)
);
}else if(o.token == SyntaxKind.ImplementsKeyword){
const interfaceName = (type.expression as Identifier).escapedText;
classDeclaration.implements.push(
new TshInterface(
interfaceName.toString(),
classDeclaration.isExported
)
);
}
});
}
})

}

parseClassIdentifiers(tsResource, node);

Expand Down
12 changes: 12 additions & 0 deletions src/type-guards/TypescriptGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ExternalModuleReference,
FunctionDeclaration,
GetAccessorDeclaration,
HeritageClause,
Identifier,
ImportDeclaration,
ImportEqualsDeclaration,
Expand Down Expand Up @@ -230,3 +231,14 @@ export function isGetAccessorDeclaration(node?: Node): node is GetAccessorDeclar
export function isSetAccessorDeclaration(node?: Node): node is SetAccessorDeclaration {
return node !== undefined && node.kind === SyntaxKind.SetAccessor;
}

/**
* Determines if the given node is a HeritageClause.
*
* @export
* @param {Node} [node]
* @returns {node is SetAccessorDeclaration}
*/
export function isHeritageClause(node?: Node): node is HeritageClause {
return node !== undefined && node.kind === SyntaxKind.HeritageClause;
}