From e15efe75e7aa50b2a294a397ac9f9b2d4e00f27f Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 13 Oct 2019 17:20:17 +0200 Subject: [PATCH] code cleanup --- .eslintrc.js | 1 - src/Engine.ts | 5 +++-- src/ModelCustomization.ts | 16 ++++++++-------- src/ModelGeneration.ts | 8 ++++---- src/Utils.ts | 8 ++++---- test/utils/EntityFileToJson.ts | 21 ++++++++++++--------- test/utils/GeneralTestUtils.ts | 2 +- 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 587e3f8..5f47d85 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -28,7 +28,6 @@ module.exports = { "no-plusplus": ["error", { allowForLoopAfterthoughts: true }], "@typescript-eslint/no-non-null-assertion": ["off"], - "@typescript-eslint/no-object-literal-type-assertion": ["off"], "no-param-reassign": ["off"], "@typescript-eslint/no-explicit-any": ["off"], diff --git a/src/Engine.ts b/src/Engine.ts index cd9289a..026896c 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -10,6 +10,7 @@ import OracleDriver from "./drivers/OracleDriver"; import SqliteDriver from "./drivers/SqliteDriver"; import modelCustomizationPhase from "./ModelCustomization"; import modelGenerationPhase from "./ModelGeneration"; +import { Entity } from "./models/Entity"; export function createDriver(driverName: string): AbstractDriver { switch (driverName) { @@ -35,7 +36,7 @@ export async function createModelFromDatabase( driver: AbstractDriver, connectionOptions: IConnectionOptions, generationOptions: IGenerationOptions -) { +): Promise { let dbModel = await dataCollectionPhase( driver, connectionOptions, @@ -59,6 +60,6 @@ export async function dataCollectionPhase( driver: AbstractDriver, connectionOptions: IConnectionOptions, generationOptions: IGenerationOptions -) { +): Promise { return driver.GetDataFromServer(connectionOptions, generationOptions); } diff --git a/src/ModelCustomization.ts b/src/ModelCustomization.ts index 1bc2bea..6e0563d 100644 --- a/src/ModelCustomization.ts +++ b/src/ModelCustomization.ts @@ -9,7 +9,7 @@ export default function modelCustomizationPhase( dbModel: Entity[], generationOptions: IGenerationOptions, defaultValues: DataTypeDefaults -) { +): Entity[] { let namingStrategy: AbstractNamingStrategy; if ( generationOptions.customNamingStrategyPath && @@ -29,7 +29,7 @@ export default function modelCustomizationPhase( function removeColumnDefaultProperties( dbModel: Entity[], defaultValues: DataTypeDefaults -) { +): Entity[] { if (!defaultValues) { return dbModel; } @@ -70,7 +70,7 @@ function removeColumnDefaultProperties( function addImportsAndGenerationOptions( dbModel: Entity[], generationOptions: IGenerationOptions -) { +): Entity[] { dbModel.forEach(entity => { entity.relations.forEach(relation => { if (generationOptions.lazy) { @@ -91,14 +91,14 @@ function addImportsAndGenerationOptions( function applyNamingStrategy( namingStrategy: AbstractNamingStrategy, dbModel: Entity[] -) { +): Entity[] { let retVal = changeRelationNames(dbModel); retVal = changeRelationIdNames(retVal); retVal = changeEntityNames(retVal); retVal = changeColumnNames(retVal); return retVal; - function changeRelationIdNames(model: Entity[]) { + function changeRelationIdNames(model: Entity[]): Entity[] { model.forEach(entity => { entity.relationIds.forEach(relationId => { const oldName = relationId.fieldName; @@ -127,7 +127,7 @@ function applyNamingStrategy( return dbModel; } - function changeRelationNames(model: Entity[]) { + function changeRelationNames(model: Entity[]): Entity[] { model.forEach(entity => { entity.relations.forEach(relation => { const oldName = relation.fieldName; @@ -166,7 +166,7 @@ function applyNamingStrategy( return dbModel; } - function changeColumnNames(model: Entity[]) { + function changeColumnNames(model: Entity[]): Entity[] { model.forEach(entity => { entity.columns.forEach(column => { const oldName = column.tscName; @@ -187,7 +187,7 @@ function applyNamingStrategy( }); return model; } - function changeEntityNames(entities: Entity[]) { + function changeEntityNames(entities: Entity[]): Entity[] { entities.forEach(entity => { const newName = namingStrategy.entityName(entity.tscName, entity); entities.forEach(entity2 => { diff --git a/src/ModelGeneration.ts b/src/ModelGeneration.ts index 6f0b27e..743dd74 100644 --- a/src/ModelGeneration.ts +++ b/src/ModelGeneration.ts @@ -12,7 +12,7 @@ export default function modelGenerationPhase( connectionOptions: IConnectionOptions, generationOptions: IGenerationOptions, databaseModel: Entity[] -) { +): void { createHandlebarsHelpers(generationOptions); const templatePath = path.resolve(__dirname, "templates", "entity.mst"); const template = fs.readFileSync(templatePath, "UTF-8"); @@ -63,7 +63,7 @@ export default function modelGenerationPhase( }); } -function createHandlebarsHelpers(generationOptions: IGenerationOptions) { +function createHandlebarsHelpers(generationOptions: IGenerationOptions): void { Handlebars.registerHelper("json", context => { const json = JSON.stringify(context); const withoutQuotes = json.replace(/"([^(")"]+)":/g, "$1:"); @@ -158,7 +158,7 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) { }); } -function createTsConfigFile(outputPath: string) { +function createTsConfigFile(outputPath: string): void { const templatePath = path.resolve(__dirname, "templates", "tsconfig.mst"); const template = fs.readFileSync(templatePath, "UTF-8"); const compliedTemplate = Handlebars.compile(template, { @@ -175,7 +175,7 @@ function createTsConfigFile(outputPath: string) { function createTypeOrmConfig( outputPath: string, connectionOptions: IConnectionOptions -) { +): void { const templatePath = path.resolve(__dirname, "templates", "ormconfig.mst"); const template = fs.readFileSync(templatePath, "UTF-8"); const compliedTemplate = Handlebars.compile(template, { diff --git a/src/Utils.ts b/src/Utils.ts index 700db61..2556fc8 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -5,10 +5,10 @@ export function LogError( errText: string, isABug = true, passedError?: string | ErrorConstructor -) { +): void { let errObject = passedError; console.error(errText); - console.error(`Error occured in typeorm-model-generator.`); + console.error(`Error occurred in typeorm-model-generator.`); console.error(`${packageVersion()} node@${process.version}`); console.error( `If you think this is a bug please open an issue including this log on ${packagejson.bugs.url}` @@ -20,14 +20,14 @@ export function LogError( console.error(errObject); } } -export function packageVersion() { +export function packageVersion(): string { return `${packagejson.name}@${packagejson.version}`; } export function findNameForNewField( _fieldName: string, entity: Entity, columnOldName = "" -) { +): string { let fieldName = _fieldName; const validNameCondition = () => (entity.columns.every( diff --git a/test/utils/EntityFileToJson.ts b/test/utils/EntityFileToJson.ts index 094a818..b9ea9b8 100644 --- a/test/utils/EntityFileToJson.ts +++ b/test/utils/EntityFileToJson.ts @@ -2,7 +2,7 @@ class EntityJson { public entityName: string; - public entityOptions: any = {}; + public entityOptions: { [key: string]: string | boolean } = {}; public columns: EntityColumn[] = [] as EntityColumn[]; @@ -13,7 +13,7 @@ class EntityColumn { public columnTypes: string[] = []; - public columnOptions: any = {}; + public columnOptions: { [key: string]: string | boolean } = {}; public relationType: "OneToOne" | "OneToMany" | "ManyToOne" | "ManyToMany"; @@ -28,7 +28,7 @@ class EntityIndex { } export default class EntityFileToJson { - public static getEntityOptions(trimmedLine: string, ent: EntityJson) { + public static getEntityOptions(trimmedLine: string, ent: EntityJson): void { const decoratorParameters = trimmedLine.slice( trimmedLine.indexOf("(") + 1, trimmedLine.lastIndexOf(")") @@ -57,7 +57,7 @@ export default class EntityFileToJson { public static getColumnOptionsAndType( trimmedLine: string, col: EntityColumn - ) { + ): void { const decoratorParameters = trimmedLine.slice( trimmedLine.indexOf("(") + 1, trimmedLine.lastIndexOf(")") @@ -100,8 +100,8 @@ export default class EntityFileToJson { } else { let badJSON = !primaryGeneratedColumn ? decoratorParameters.substring( - decoratorParameters.indexOf(",") + 1 - ) + decoratorParameters.indexOf(",") + 1 + ) : decoratorParameters; badJSON = badJSON.trim(); if (badJSON.lastIndexOf(",") === badJSON.length - 3) { @@ -117,7 +117,10 @@ export default class EntityFileToJson { } } - public static getRelationOptions(trimmedLine: string, col: EntityColumn) { + public static getRelationOptions( + trimmedLine: string, + col: EntityColumn + ): void { const decoratorParameters = trimmedLine.slice( trimmedLine.indexOf("(") + 1, trimmedLine.lastIndexOf(")") @@ -146,7 +149,7 @@ export default class EntityFileToJson { } } - public static getIndexOptions(trimmedLine: string, ind: EntityIndex) { + public static getIndexOptions(trimmedLine: string, ind: EntityIndex): void { const decoratorParameters = trimmedLine.slice( trimmedLine.indexOf("(") + 1, trimmedLine.lastIndexOf(")") @@ -499,7 +502,7 @@ export default class EntityFileToJson { return retVal; } - public static isPartOfMultilineStatement(statement: string) { + public static isPartOfMultilineStatement(statement: string): boolean { const matchStarting = statement.split("(").length + statement.split("{").length; const matchEnding = diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index 3a3976c..f507070 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -324,7 +324,7 @@ export function compileTsFiles( return compiledWithoutErrors; } -export function getEnabledDbDrivers() { +export function getEnabledDbDrivers():string[] { const dbDrivers: string[] = []; if (process.env.SQLITE_Skip === "0") { dbDrivers.push("sqlite");