style: apply strict style rules for this feature

This commit is contained in:
Michel Jonathan Schmitz
2019-06-05 16:29:08 +02:00
parent 843e59811a
commit 23cbc53fef
10 changed files with 90 additions and 67 deletions

View File

@@ -29,9 +29,9 @@ import {
getNodeMetaInformationMap, getNodeMetaInformationMap,
} from './routes'; } from './routes';
import {Converter, getValidatableTypesFromReflection} from './schema'; import {Converter, getValidatableTypesFromReflection} from './schema';
import {createDiagram, createDiagramFromString} from './uml/createDiagram'; import {createDiagram, createDiagramFromString} from './uml/create-diagram';
import {readDefinitions} from './uml/readDefinitions'; import {readDefinitions} from './uml/read-definitions';
import {UMLConfig} from './uml/umlConfig'; import {UMLConfig} from './uml/uml-config';
import {validateFiles, writeReport} from './validate'; import {validateFiles, writeReport} from './validate';
// handle unhandled promise rejections // handle unhandled promise rejections
@@ -169,9 +169,11 @@ commander
} }
}); });
commander.command('pack').action(async () => { commander
await pack(); .command('pack')
}); .action(async () => {
await pack();
});
commander commander
.command('plantuml <srcPath> <plantumlserver>') .command('plantuml <srcPath> <plantumlserver>')
@@ -250,7 +252,8 @@ commander
commander commander
.command('plantuml-file <inputFile> <plantumlserver> [outputFile]') .command('plantuml-file <inputFile> <plantumlserver> [outputFile]')
.action(async (file: string, plantumlserver: string, outputFile: string) => { .action(async (file: string, plantumlserver: string, outputFile: string) => {
const fileContent = readFileSync(resolve(file)).toString(); const fileContent = readFileSync(resolve(file))
.toString();
await createDiagramFromString(fileContent, plantumlserver, outputFile); await createDiagramFromString(fileContent, plantumlserver, outputFile);
}); });

View File

@@ -21,7 +21,7 @@ import {join, sep} from 'path';
import {Definition} from 'ts-json-schema-generator'; import {Definition} from 'ts-json-schema-generator';
import {Application, ProjectReflection} from 'typedoc'; import {Application, ProjectReflection} from 'typedoc';
import {promisify} from 'util'; import {promisify} from 'util';
import {LightweightType} from './uml/model/LightweightType'; import {LightweightType} from './uml/model/lightweight-type';
export const globPromisified = promisify(glob); export const globPromisified = promisify(glob);
export const mkdirPromisified = promisify(mkdir); export const mkdirPromisified = promisify(mkdir);
@@ -143,7 +143,7 @@ export interface ExpectableValidationErrors {
* *
* @param srcPath Path to get reflection from * @param srcPath Path to get reflection from
*/ */
export function getProjectReflection(srcPath: PathLike, excludeExternals: boolean = true): ProjectReflection { export function getProjectReflection(srcPath: PathLike, excludeExternals = true): ProjectReflection {
Logger.info(`Generating project reflection for ${srcPath.toString()}.`); Logger.info(`Generating project reflection for ${srcPath.toString()}.`);
const tsconfigPath = getTsconfigPath(srcPath.toString()); const tsconfigPath = getTsconfigPath(srcPath.toString());
@@ -254,13 +254,14 @@ export function getFullTypeName(type: LightweightType): string {
} }
if (type.isLiteral) { if (type.isLiteral) {
// literals are a sink // literals are a sink
return "'" + fullName + "'"; return `'${fullName}'`;
} }
if (type.isUnion && type.specificationTypes.length > 0) { if (type.isUnion && type.specificationTypes.length > 0) {
const tempNames: string[] = []; const tempNames: string[] = [];
for (const easyType of type.specificationTypes) { for (const easyType of type.specificationTypes) {
tempNames.push(getFullTypeName(easyType)); tempNames.push(getFullTypeName(easyType));
} }
// since unions can't be applied to other types, it is a sink. // since unions can't be applied to other types, it is a sink.
return tempNames.join(' | '); return tempNames.join(' | ');
} }
@@ -270,11 +271,12 @@ export function getFullTypeName(type: LightweightType): string {
for (const easyType of type.genericsTypes) { for (const easyType of type.genericsTypes) {
tempNames.push(getFullTypeName(easyType)); tempNames.push(getFullTypeName(easyType));
} }
fullName += '<' + tempNames.join(', ') + '>'; fullName = `${fullName}<${tempNames.join(', ')}>`;
} }
// check if type is array // check if type is array
if (type.isArray) { if (type.isArray) {
fullName += '[]'; fullName += '[]';
} }
return fullName; return fullName;
} }

View File

@@ -16,12 +16,12 @@ import {Logger} from '@openstapps/logger';
import {createWriteStream} from 'fs'; import {createWriteStream} from 'fs';
import * as request from 'got'; import * as request from 'got';
import {getFullTypeName} from '../common'; import {getFullTypeName} from '../common';
import {LightweightClassDefinition} from './model/LightweightClassDefinition'; import {LightweightClassDefinition} from './model/lightweight-class-definition';
import {LightweightDefinition} from './model/LightweightDefinition'; import {LightweightDefinition} from './model/lightweight-definition';
import {LightweightEnumDefinition} from './model/LightweightEnumDefinition'; import {LightweightEnumDefinition} from './model/lightweight-enum-definition';
import {LightweightProperty} from './model/LightweightProperty'; import {LightweightProperty} from './model/lightweight-property';
import {LightweightType} from './model/LightweightType'; import {LightweightType} from './model/lightweight-type';
import {UMLConfig} from './umlConfig'; import {UMLConfig} from './uml-config';
/** /**
* Converts the lightweight class/enum definitions according to the configuration, * Converts the lightweight class/enum definitions according to the configuration,
@@ -57,7 +57,7 @@ export async function createDiagram(
config.definitions = config.definitions.concat(inheritedDefinitions); config.definitions = config.definitions.concat(inheritedDefinitions);
} }
let modelPlantUMLCode: string = ''; let modelPlantUMLCode = '';
// creates a UML definition for every specified definition name // creates a UML definition for every specified definition name
// however if no definitions were provided all definitions will be transformed // however if no definitions were provided all definitions will be transformed
for (const definition of definitions) { for (const definition of definitions) {
@@ -70,7 +70,7 @@ export async function createDiagram(
} }
// either the definitions are empty or the definition was specified, proceed // either the definitions are empty or the definition was specified, proceed
let definitionPlantUMLCode: string = ''; let definitionPlantUMLCode = '';
if (definition instanceof LightweightClassDefinition) { if (definition instanceof LightweightClassDefinition) {
definitionPlantUMLCode = createPlantUMLCodeForClass(config, definition); definitionPlantUMLCode = createPlantUMLCodeForClass(config, definition);
} else if (definition instanceof LightweightEnumDefinition) { } else if (definition instanceof LightweightEnumDefinition) {
@@ -81,14 +81,16 @@ export async function createDiagram(
modelPlantUMLCode += definitionPlantUMLCode; modelPlantUMLCode += definitionPlantUMLCode;
} }
return await createDiagramFromString(modelPlantUMLCode, plantUmlBaseURL, config.outputFileName); return createDiagramFromString(modelPlantUMLCode, plantUmlBaseURL, config.outputFileName);
} }
/** /**
* This will encode the plantuml code and post the code to the plantuml server * This will encode the plantuml code and post the code to the plantuml server
* The server will then parse the code and create a corresponding diagram * The server will then parse the code and create a corresponding diagram
* *
* @param modelPlantUMLCode * @param modelPlantUMLCode raw PlantUML code
* @param plantUmlBaseURL PlantUML server address that shall be used
* @param outputFile filename of the output file without file extension
*/ */
export async function createDiagramFromString( export async function createDiagramFromString(
modelPlantUMLCode: string, modelPlantUMLCode: string,
@@ -101,7 +103,8 @@ export async function createDiagramFromString(
let response; let response;
try { try {
response = await request(url); response = await request(url);
if (response.statusCode !== 200) { const httpOK = 200;
if (response.statusCode !== httpOK) {
Logger.error(`Plantuml Server responded with an error.\n${response.statusMessage}`); Logger.error(`Plantuml Server responded with an error.\n${response.statusMessage}`);
throw new Error('Response not okay'); throw new Error('Response not okay');
} }
@@ -112,11 +115,13 @@ export async function createDiagramFromString(
// attach file extension // attach file extension
const fileName = `${outputFile}.svg`; const fileName = `${outputFile}.svg`;
try { try {
createWriteStream(fileName).write(response.body); createWriteStream(fileName)
.write(response.body);
Logger.log(`Writen data to file: ${fileName}`); Logger.log(`Writen data to file: ${fileName}`);
} catch (e) { } catch (e) {
throw new Error('Could not write file. Are you missing permissions?'); throw new Error('Could not write file. Are you missing permissions?');
} }
return fileName; return fileName;
} }
@@ -146,6 +151,7 @@ function gatherTypeAssociations(
); );
} }
} }
return abstractions; return abstractions;
} }
@@ -178,6 +184,7 @@ function getReferenceTypes(type: LightweightType): string[] {
} }
} }
} }
return types; return types;
} }
@@ -192,7 +199,7 @@ function createPlantUMLCodeForClass(
readerClass: LightweightClassDefinition, readerClass: LightweightClassDefinition,
): string { ): string {
// create the definition header, what type the definition is, it's name and it's inheritance // create the definition header, what type the definition is, it's name and it's inheritance
let model: string = `${readerClass.type} ${readerClass.name}`; let model = `${readerClass.type} ${readerClass.name}`;
if (readerClass.typeParameters.length > 0) { if (readerClass.typeParameters.length > 0) {
model += `<${readerClass.typeParameters.join(', ')}>`; model += `<${readerClass.typeParameters.join(', ')}>`;
@@ -253,7 +260,7 @@ function createPlantUMLCodeForEnum(
readerEnum: LightweightEnumDefinition, readerEnum: LightweightEnumDefinition,
): string { ): string {
// create enum header // create enum header
let model: string = `enum ${readerEnum.name} {`; let model = `enum ${readerEnum.name} {`;
// add values // add values
if (config.showEnumValues) { if (config.showEnumValues) {
for (const value of readerEnum.values) { for (const value of readerEnum.values) {
@@ -269,11 +276,7 @@ function createPlantUMLCodeForEnum(
* Creates a property PlantUML Line * Creates a property PlantUML Line
*/ */
function createPropertyLine(property: LightweightProperty): string { function createPropertyLine(property: LightweightProperty): string {
return ( const prefix = `${(property.inherited ? '/ ' : '')}${(property.optional ? '? ' : '')}`;
(property.inherited ? '/ ' : '') +
(property.optional ? '?' : '') + return `${prefix}${property.name} : ${getFullTypeName(property.type)}`;
property.name +
' : ' +
getFullTypeName(property.type)
);
} }

View File

@@ -13,8 +13,8 @@
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {LightweightDefinition} from './LightweightDefinition'; import {LightweightDefinition} from './lightweight-definition';
import {LightweightProperty} from './LightweightProperty'; import {LightweightProperty} from './lightweight-property';
/** /**
* Represents a class definition * Represents a class definition
*/ */

View File

@@ -13,7 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {LightweightDefinition} from './LightweightDefinition'; import {LightweightDefinition} from './lightweight-definition';
/** /**
* Represents an enum definition * Represents an enum definition
*/ */

View File

@@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>. * this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {LightweightType} from './LightweightType'; import {LightweightType} from './lightweight-type';
/** /**
* Represents a property definition * Represents a property definition
@@ -45,7 +45,7 @@ export class LightweightProperty {
* @param type Type of the property * @param type Type of the property
* @param optional Is the property optional * @param optional Is the property optional
*/ */
constructor(name: string, type: LightweightType, optional: boolean = true) { constructor(name: string, type: LightweightType, optional = true) {
this.name = name; this.name = name;
this.optional = optional; this.optional = optional;
this.inherited = false; this.inherited = false;

View File

@@ -25,47 +25,47 @@ export class LightweightType {
/** /**
* Does the type have generic-parameters * Does the type have generic-parameters
*/ */
hasTypeInformation: boolean = false; hasTypeInformation = false;
/** /**
* Does the type represent an array type * Does the type represent an array type
*/ */
isArray: boolean = false; isArray = false;
/** /**
* Does the type represent a literal type * Does the type represent a literal type
*/ */
isLiteral: boolean = false; isLiteral = false;
/** /**
* Does the type represent a primitive type * Does the type represent a primitive type
*/ */
isPrimitive: boolean = false; isPrimitive = false;
/** /**
* Does the type contain a reference to * Does the type contain a reference to
*/ */
isReference: boolean = false; isReference = false;
/** /**
* Is the type a reflection and not avaiblabe at compile time * Is the type a reflection and not avaiblabe at compile time
*/ */
isReflection: boolean = false; isReflection = false;
/** /**
* Does the type have type parameters * Does the type have type parameters
*/ */
isTyped: boolean = false; isTyped = false;
/** /**
* Is the type a typed parameter * Is the type a typed parameter
*/ */
isTypeParameter: boolean = false; isTypeParameter = false;
/** /**
* Is the type a union type * Is the type a union type
*/ */
isUnion: boolean = false; isUnion = false;
/** /**
* Name of the type * Name of the type

View File

@@ -28,11 +28,11 @@ import {
UnionType, UnionType,
} from 'typedoc/dist/lib/models'; } from 'typedoc/dist/lib/models';
import {getFullTypeName} from '../common'; import {getFullTypeName} from '../common';
import {LightweightClassDefinition} from './model/LightweightClassDefinition'; import {LightweightClassDefinition} from './model/lightweight-class-definition';
import {LightweightDefinition} from './model/LightweightDefinition'; import {LightweightDefinition} from './model/lightweight-definition';
import {LightweightEnumDefinition} from './model/LightweightEnumDefinition'; import {LightweightEnumDefinition} from './model/lightweight-enum-definition';
import {LightweightProperty} from './model/LightweightProperty'; import {LightweightProperty} from './model/lightweight-property';
import {LightweightType} from './model/LightweightType'; import {LightweightType} from './model/lightweight-type';
/** /**
* Reads the reflection model from typedoc and converts it into a flatter, easier to handle model * Reads the reflection model from typedoc and converts it into a flatter, easier to handle model
@@ -142,6 +142,7 @@ function getTypeInformation(type: LightweightType): string[] {
} else { } else {
values.push(type.name); values.push(type.name);
} }
return values; return values;
} }
@@ -225,23 +226,30 @@ export function readAsClassDefinition(
function readTypeInformation(declarationType: Type): LightweightType { function readTypeInformation(declarationType: Type): LightweightType {
if (declarationType instanceof ReflectionType) { if (declarationType instanceof ReflectionType) {
return readAsReflectionType(declarationType); return readAsReflectionType(declarationType);
} else if (declarationType instanceof TypeOperatorType) {
return readAsTypeOperatorType(declarationType);
} else if (declarationType instanceof TypeParameterType) {
return readAsTypeParameterType(declarationType);
} else if (declarationType instanceof IntrinsicType) {
return readAsIntrinsicType(declarationType);
} else if (declarationType instanceof StringLiteralType) {
return readAsStringLiteralType(declarationType);
} else if (declarationType instanceof ReferenceType) {
return readAsReferenceType(declarationType);
} else if (declarationType instanceof ArrayType) {
return readAsArrayType(declarationType);
} else if (declarationType instanceof UnionType) {
return readAsUnionType(declarationType);
} else {
throw new Error(`Could not read type ${declarationType.type}`);
} }
if (declarationType instanceof TypeOperatorType) {
return readAsTypeOperatorType(declarationType);
}
if (declarationType instanceof TypeParameterType) {
return readAsTypeParameterType(declarationType);
}
if (declarationType instanceof IntrinsicType) {
return readAsIntrinsicType(declarationType);
}
if (declarationType instanceof StringLiteralType) {
return readAsStringLiteralType(declarationType);
}
if (declarationType instanceof ReferenceType) {
return readAsReferenceType(declarationType);
}
if (declarationType instanceof ArrayType) {
return readAsArrayType(declarationType);
}
if (declarationType instanceof UnionType) {
return readAsUnionType(declarationType);
}
throw new Error(`Could not read type ${declarationType.type}`);
} }
/** /**
@@ -256,6 +264,7 @@ function readAsIntrinsicType(type: IntrinsicType): LightweightType {
easyType.name = type.name; easyType.name = type.name;
easyType.isPrimitive = true; easyType.isPrimitive = true;
easyType.hasTypeInformation = true; easyType.hasTypeInformation = true;
return easyType; return easyType;
} }
@@ -271,6 +280,7 @@ function readAsStringLiteralType(type: StringLiteralType): LightweightType {
returnType.name = type.value; returnType.name = type.value;
returnType.isLiteral = true; returnType.isLiteral = true;
returnType.hasTypeInformation = true; returnType.hasTypeInformation = true;
return returnType; return returnType;
} }
@@ -333,6 +343,7 @@ function readAsArrayType(type: ArrayType): LightweightType {
returnType.name = getFullTypeName(typeOfArray); returnType.name = getFullTypeName(typeOfArray);
returnType.specificationTypes = [typeOfArray]; returnType.specificationTypes = [typeOfArray];
returnType.isArray = true; returnType.isArray = true;
return returnType; return returnType;
} }
@@ -355,6 +366,7 @@ function readAsUnionType(type: UnionType): LightweightType {
returnType.specificationTypes = typesOfUnion; returnType.specificationTypes = typesOfUnion;
returnType.name = getFullTypeName(returnType); returnType.name = getFullTypeName(returnType);
returnType.isUnion = true; returnType.isUnion = true;
return returnType; return returnType;
} }
@@ -379,6 +391,7 @@ function readAsReflectionType(type: ReflectionType): LightweightType {
} }
returnType.name = 'object'; returnType.name = 'object';
returnType.isReflection = true; returnType.isReflection = true;
return returnType; return returnType;
} }
@@ -399,6 +412,7 @@ function readAsTypeOperatorType(type: TypeOperatorType): LightweightType {
// can't be traced deeper! so might as well be a primitive // can't be traced deeper! so might as well be a primitive
returnType.isPrimitive = true; returnType.isPrimitive = true;
returnType.hasTypeInformation = true; returnType.hasTypeInformation = true;
return returnType; return returnType;
} }
@@ -419,5 +433,6 @@ function readAsTypeParameterType(type: TypeParameterType): LightweightType {
returnType.name = type.name; returnType.name = type.name;
returnType.isTypeParameter = true; returnType.isTypeParameter = true;
returnType.hasTypeInformation = true; returnType.hasTypeInformation = true;
return returnType; return returnType;
} }