Skip to content

Commit

Permalink
Refactoring debts (#32)
Browse files Browse the repository at this point in the history
* Apply some refactoring debt fixes
* Apply all Strict Type-Checking options from TS;
* Apply all Additional Checks options from TS;
* Remove all overwritten rules from tslint (except no-relative-imports);
* Fix all erros;
* Use namespaces for Factories;
* Rely on interfaces

* Add Jest for a faster test run

* First Handbook test added

* Add Namespaces Handbook test case
  • Loading branch information
Marcos Vinícius Rubido authored May 22, 2019
1 parent 2d78715 commit 5cda20f
Show file tree
Hide file tree
Showing 85 changed files with 6,844 additions and 2,139 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#Compiled files
dist
*.js
!*.spec.js

#IDE
.vscode
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Will convert a Typescript file to a PlantUML file. Following all inheritances.

```
npm install --global tplant
tplant --input sample/Classes/Greeter.ts --output sample/Classes/Greeter.puml
tplant --input test/Playground/Classes/Greeter.ts --output test/Playground/Classes/Greeter.puml
```

## Options
Expand Down
5,558 changes: 4,777 additions & 781 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tplant",
"version": "2.1.8",
"version": "2.2.0",
"description": "Typescript to PlantUML",
"keywords": [
"Class Diagram",
Expand All @@ -24,8 +24,8 @@
"files": [
"dist"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/tplant.js",
"types": "dist/tplant.d.ts",
"bin": {
"tplant": "dist/index.js"
},
Expand All @@ -36,7 +36,7 @@
"lint:fix": "tslint -p . --fix",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"test": "npm run build && mocha --timeout 0"
"test": "jest"
},
"dependencies": {
"commander": "^2.20.0",
Expand All @@ -45,14 +45,20 @@
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.13",
"@types/node": "^12.0.0",
"mocha": "^6.1.4",
"jest": "^24.8.0",
"pre-commit": "^1.2.2",
"ts-jest": "^24.0.2",
"tslint": "^5.16.0",
"tslint-microsoft-contrib": "^6.1.1"
},
"pre-commit": [
"lint",
"test"
]
],
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
}
}
40 changes: 22 additions & 18 deletions src/components/Class.ts → src/Components/Class.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@

import * as os from 'os';
import { ComponentKind } from './ComponentKind';
import { IComponentComposite } from './IComponentComposite';
import { Interface } from './Interface';
import { Method } from './Method';
import { Property } from './Property';
import { TypeParameter } from './TypeParameter';
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for a class within a typescript file.
*/
export class Class implements IComponentComposite {
public componentKind: ComponentKind = ComponentKind.CLASS;
public name: string = '';
public readonly componentKind: ComponentKind = ComponentKind.CLASS;
public readonly name: string;
public isAbstract: boolean = false;
public isStatic: boolean = false;
public constructorMethods: Method[] = [];
public members: (Method | Property)[] = [];
public extendsClass: string = '';
public constructorMethods: IComponentComposite[] = [];
public members: IComponentComposite[] = [];
public extendsClass: string | undefined;
public implementsInterfaces: string[] = [];
public typeParameters: TypeParameter[] = [];
public typeParameters: IComponentComposite[] = [];

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
const result: string[] = [];
const firstLine: string[] = [];
Expand All @@ -30,22 +30,26 @@ export class Class implements IComponentComposite {
if (this.typeParameters.length > 0) {
firstLine.push('<');
firstLine.push(this.typeParameters
.map((typeParameter: TypeParameter): string => typeParameter.toPUML())
.join(', '));
.map((typeParameter: IComponentComposite): string => typeParameter.toPUML())
.join(', '));
firstLine.push('>');
}
if (this.extendsClass !== '') {
if (this.extendsClass !== undefined) {
firstLine.push(` extends ${this.extendsClass}`);
}
if (this.implementsInterfaces.length > 0) {
firstLine.push(` implements ${this.implementsInterfaces.join(', ')}`);
}
firstLine.push(' {');
if (this.members.length > 0) {
firstLine.push(' {');
}
result.push(firstLine.join(''));
this.members.forEach((member: IComponentComposite): void => {
result.push(` ${member.toPUML()}`);
});
result.push('}');
if (this.members.length > 0) {
result.push('}');
}

return result.join(os.EOL);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Components/Enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as os from 'os';
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for an Enum within a typescript file.
*/
export class Enum implements IComponentComposite {
public readonly componentKind: ComponentKind = ComponentKind.ENUM;
public readonly name: string;
public values: IComponentComposite[] = [];

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
const result: string[] = [];
let declaration: string = `enum ${this.name}`;
if (this.values.length > 0) {
declaration += ' {';
}
result.push(declaration);
this.values.forEach((enumValue: IComponentComposite): void => {
result.push(` ${enumValue.toPUML()}`);
});
if (this.values.length > 0) {
result.push('}');
}

return result.join(os.EOL);
}
}
19 changes: 19 additions & 0 deletions src/Components/EnumValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for the value or member of an enum within typescript
*/
export class EnumValue implements IComponentComposite {
public readonly componentKind: ComponentKind = ComponentKind.PROPERTY;
public readonly name: string;
public value: string | undefined;

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
return this.name;
}
}
14 changes: 5 additions & 9 deletions src/components/File.ts → src/Components/File.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

import * as os from 'os';
import { Class } from './Class';
import { ComponentKind } from './ComponentKind';
import { Enum } from './Enum';
import { IComponentComposite } from './IComponentComposite';
import { Interface } from './Interface';
import { Namespace } from './Namespace';
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for a file containing typescript
*/
export class File implements IComponentComposite {
public componentKind: ComponentKind = ComponentKind.FILE;
public name: string = '';
public readonly componentKind: ComponentKind = ComponentKind.FILE;
public readonly name: string = '';
public parts: IComponentComposite[] = [];

public toPUML(): string {
const result: string[] = [];
this.parts.forEach((part: IComponentComposite): void => {
Expand Down
46 changes: 46 additions & 0 deletions src/Components/Interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as os from 'os';
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for an interface within typescript
*/
export class Interface implements IComponentComposite {
public readonly componentKind: ComponentKind = ComponentKind.INTERFACE;
public readonly name: string;
public members: IComponentComposite[] = [];
public extendsInterface: string[] = [];
public typeParameters: IComponentComposite[] = [];

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
const result: string[] = [];
const firstLine: string[] = [];
firstLine.push(`interface ${this.name}`);
if (this.typeParameters.length > 0) {
firstLine.push('<');
firstLine.push(this.typeParameters
.map((typeParameter: IComponentComposite): string => typeParameter.toPUML())
.join(', '));
firstLine.push('>');
}
if (this.extendsInterface.length > 0) {
firstLine.push(` extends ${this.extendsInterface.join(', ')}`);
}
if (this.members.length > 0) {
firstLine.push(' {');
}
result.push(firstLine.join(''));
this.members.forEach((member: IComponentComposite): void => {
result.push(` ${member.toPUML()}`);
});
if (this.members.length > 0) {
result.push('}');
}

return result.join(os.EOL);
}
}
25 changes: 14 additions & 11 deletions src/components/Method.ts → src/Components/Method.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@

import { ComponentKind } from './ComponentKind';
import { IComponentComposite } from './IComponentComposite';
import { Modifier } from './Modifier';
import { Parameter } from './Parameter';
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';
import { Modifier } from '../Models/Modifier';

/**
* Represents the metadata for a method within typescript
*/
export class Method implements IComponentComposite {
public componentKind: ComponentKind = ComponentKind.METHOD;
public name: string = '';
public parameters: Parameter[] = [];
public returnType: string = '';
public readonly componentKind: ComponentKind = ComponentKind.METHOD;
public readonly name: string;
public parameters: IComponentComposite[] = [];
public returnType: string = 'any';
public modifier: Modifier = 'public';
public isAbstract: boolean = false;
public isOptional: boolean = false;
public isStatic: boolean = false;

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
let result: string = { public: '+', private: '-', protected: '#' }[this.modifier];
result += (this.isAbstract ? '{abstract} ' : '');
result += (this.isStatic ? '{static} ' : '');
result += `${this.name}(`;
result += this.parameters
.map((parameter: Parameter): string => parameter.toPUML())
.join(', ');
.map((parameter: IComponentComposite): string => parameter.toPUML())
.join(', ');
result += `): ${this.returnType}`;

return result;
Expand Down
37 changes: 37 additions & 0 deletions src/Components/Namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as os from 'os';
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for a namespace within typescript
*/
export class Namespace implements IComponentComposite {
public readonly name: string;
public readonly componentKind: ComponentKind = ComponentKind.NAMESPACE;
public parts: IComponentComposite[] = [];

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
const result: string[] = [];
const declaration: string[] = [];
declaration.push(`namespace ${this.name}`);
if (this.parts.length > 0) {
declaration.push(' {');
}
result.push(declaration.join(''));
this.parts.forEach((part: IComponentComposite): void => {
result.push(
part.toPUML()
.replace(/^(?!\s*$)/gm, ' ')
);
});
if (this.parts.length > 0) {
result.push('}');
}

return result.join(os.EOL);
}
}
21 changes: 21 additions & 0 deletions src/Components/Parameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';

/**
* Represents the metadata for a parameter within typescript
*/
export class Parameter implements IComponentComposite {
public readonly componentKind: ComponentKind = ComponentKind.PARAMETER;
public readonly name: string;
public hasInitializer: boolean = false;
public isOptional: boolean = false;
public parameterType: string = 'any';

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
return `${this.name}${this.isOptional || this.hasInitializer ? '?' : ''}: ${this.parameterType}`;
}
}
24 changes: 24 additions & 0 deletions src/Components/Property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ComponentKind } from '../Models/ComponentKind';
import { IComponentComposite } from '../Models/IComponentComposite';
import { Modifier } from '../Models/Modifier';

/**
* Represents the metadata for a property within typescript
*/
export class Property implements IComponentComposite {
public readonly componentKind: ComponentKind = ComponentKind.PROPERTY;
public readonly name: string;
public modifier: Modifier = 'public';
public returnType: string = 'any';
public isOptional: boolean = false;
public isStatic: boolean = false;

constructor(name: string) {
this.name = name;
}

public toPUML(): string {
return `${{ public: '+', private: '-', protected: '#' }[this.modifier]}${(this.isStatic ? '{static} ' : '')
}${this.name}${(this.isOptional ? '?' : '')}: ${this.returnType}`;
}
}
Loading

0 comments on commit 5cda20f

Please sign in to comment.