From 0995e7500ccb0de429d4a4acf6e0106e04dc947d Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Mon, 30 Jan 2023 11:53:21 +0100 Subject: [PATCH] refactor: adjust to stricter eslint rules --- src/cli.ts | 24 +++++++++++------------ src/easy-ast/ast-internal-util.ts | 14 ++++++------- src/easy-ast/easy-ast.ts | 11 +++++------ src/easy-ast/types/lightweight-project.ts | 2 +- src/pack.ts | 4 ++-- src/uml/create-diagram.ts | 2 +- src/util/guards.ts | 2 +- src/validate.ts | 2 +- 8 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 16b77bb2..a8f1e6ca 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -187,16 +187,16 @@ commander unexpected = unexpected || errorsPerFile[file].some(error => !error.expected); } - if (typeof relativeReportPath !== 'undefined') { + if (relativeReportPath !== undefined) { const reportPath = path.resolve(relativeReportPath); await writeReport(reportPath, errorsPerFile); } - if (!unexpected) { - Logger.ok('Successfully finished validation.'); - } else { + if (unexpected) { await Logger.error('Unexpected errors occurred during validation'); process.exit(1); + } else { + Logger.ok('Successfully finished validation.'); } }); @@ -219,17 +219,17 @@ commander .option('--outputFileName ', 'Defines the filename of the output') .action(async (relativeSourcePath, plantumlServer, options) => { const plantUmlConfig: UMLConfig = { - definitions: typeof options.definitions !== 'undefined' ? options.definitions : [], - showAssociations: typeof options.showAssociations !== 'undefined' ? options.showAssociations : false, - showEnumValues: typeof options.showEnumValues !== 'undefined' ? options.showEnumValues : false, - showInheritance: typeof options.showInheritance !== 'undefined' ? options.showInheritance : false, + definitions: options.definitions === undefined ? [] : options.definitions, + showAssociations: options.showAssociations === undefined ? false : options.showAssociations, + showEnumValues: options.showEnumValues === undefined ? false : options.showEnumValues, + showInheritance: options.showInheritance === undefined ? false : options.showInheritance, showInheritedProperties: - typeof options.showInheritedProperties !== 'undefined' ? options.showInheritedProperties : false, + options.showInheritedProperties === undefined ? false : options.showInheritedProperties, showOptionalProperties: - typeof options.showOptionalProperties !== 'undefined' ? options.showOptionalProperties : false, - showProperties: typeof options.showProperties !== 'undefined' ? options.showProperties : false, + options.showOptionalProperties === undefined ? false : options.showOptionalProperties, + showProperties: options.showProperties === undefined ? false : options.showProperties, }; - if (typeof options.outputFileName !== 'undefined') { + if (options.outputFileName !== undefined) { plantUmlConfig.outputFileName = options.outputFileName; } diff --git a/src/easy-ast/ast-internal-util.ts b/src/easy-ast/ast-internal-util.ts index e4cb22b4..9edcd14c 100644 --- a/src/easy-ast/ast-internal-util.ts +++ b/src/easy-ast/ast-internal-util.ts @@ -55,7 +55,7 @@ export function extractComment(node: ts.Node): LightweightComment | undefined { ); const comment = jsDocument?.comment?.split('\n\n'); - return typeof jsDocument === 'undefined' + return jsDocument === undefined ? undefined : cleanupEmpty({ shortSummary: first(comment), @@ -109,11 +109,11 @@ export function getModifiers(text: string, kind: string): string[] { /** @internal */ export function resolvePropertyName(name?: PropertyName): string | undefined { - return typeof name !== 'undefined' - ? isComputedPropertyName(name) - ? 'UNSUPPORTED_IDENTIFIER_TYPE' - : name.getText() - : undefined; + return name === undefined + ? undefined + : isComputedPropertyName(name) + ? 'UNSUPPORTED_IDENTIFIER_TYPE' + : name.getText(); } /** @internal */ @@ -124,7 +124,7 @@ export function resolveTypeName(type?: TypeNode): string | undefined { /** @internal */ export function isArrayLikeType(typeNode?: TypeNode): typeNode is ArrayTypeNode | TypeReferenceNode { - return typeof typeNode !== 'undefined' && (isArrayTypeNode(typeNode) || isArrayReference(typeNode)); + return typeNode !== undefined && (isArrayTypeNode(typeNode) || isArrayReference(typeNode)); } /** @internal */ diff --git a/src/easy-ast/easy-ast.ts b/src/easy-ast/easy-ast.ts index 732b8a2e..f418ac6a 100644 --- a/src/easy-ast/easy-ast.ts +++ b/src/easy-ast/easy-ast.ts @@ -190,9 +190,9 @@ class LightweightDefinitionBuilder { type: this.lightweightTypeAtNode(property), properties: this.collectProperties((property.type as TypeLiteralNode)?.members), optional: isPropertyDeclaration(property) - ? typeof property.questionToken !== 'undefined' - ? true - : undefined + ? property.questionToken === undefined + ? undefined + : true : undefined, }), ), @@ -220,9 +220,8 @@ class LightweightDefinitionBuilder { return out; } - const isReference = - typeof typeNode !== 'undefined' && isTypeReferenceNode(typeNode) && !isEnumLiteralType(type); - const isTypeLiteral = typeof typeNode !== 'undefined' && isTypeLiteralNode(typeNode); + const isReference = typeNode !== undefined && isTypeReferenceNode(typeNode) && !isEnumLiteralType(type); + const isTypeLiteral = typeNode !== undefined && isTypeLiteralNode(typeNode); // @ts-expect-error intrinsic name & value exist const intrinsicName = (type.intrinsicName ?? type.value) as string | undefined; diff --git a/src/easy-ast/types/lightweight-project.ts b/src/easy-ast/types/lightweight-project.ts index 4cc3ac73..c9f99109 100644 --- a/src/easy-ast/types/lightweight-project.ts +++ b/src/easy-ast/types/lightweight-project.ts @@ -81,7 +81,7 @@ export class LightweightProjectWithIndex { */ async instantiateDefinitionByName(name: string, findCompiledModule = true): Promise { const fsPath = this.index[name]; - if (typeof fsPath === 'undefined') { + if (fsPath === undefined) { return undefined; } diff --git a/src/pack.ts b/src/pack.ts index 7b1b71a8..230c761a 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -153,7 +153,7 @@ async function packTypeDefinitions(): Promise { }); // add list of already imported objects for module - if (typeof imports[module] === 'undefined') { + if (imports[module] === undefined) { imports[module] = []; } @@ -255,7 +255,7 @@ async function packJavaScriptFiles(): Promise { // replace lines with internal requires if (match !== null) { - if (typeof match[6] === 'undefined') { + if (match[6] === undefined) { match[6] = match[8]; } diff --git a/src/uml/create-diagram.ts b/src/uml/create-diagram.ts index 65727497..7467108c 100644 --- a/src/uml/create-diagram.ts +++ b/src/uml/create-diagram.ts @@ -148,7 +148,7 @@ export async function createDiagramFromString( */ function getReferenceTypes(type: LightweightType): string[] { const types: string[] = []; - if (typeof type.referenceName !== 'undefined') { + if (type.referenceName !== undefined) { types.push(type.referenceName); } diff --git a/src/util/guards.ts b/src/util/guards.ts index b403ba17..707505a7 100644 --- a/src/util/guards.ts +++ b/src/util/guards.ts @@ -19,7 +19,7 @@ import {SchemaWithDefinitions} from '../types/schema'; * Guard for if a JSON schema is in fact a schema with definitions */ export function isSchemaWithDefinitions(schema: JSONSchema): schema is SchemaWithDefinitions { - return typeof schema.definitions !== 'undefined'; + return schema.definitions !== undefined; } /** diff --git a/src/validate.ts b/src/validate.ts index 5a9129c8..c4cc14a1 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -89,7 +89,7 @@ export class Validator { * @param schema Name of schema to validate instance against or the schema itself */ public validate(instance: unknown, schema?: string | Schema): ValidationResult { - if (typeof schema === 'undefined') { + if (schema === undefined) { if (isThingWithType(instance)) { // schema name can be inferred from type string const schemaSuffix = (instance as {type: string}).type