From 946a59420bd694679562f15732892999bb65cb26 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 21 Jun 2024 00:41:38 +0900 Subject: [PATCH 1/2] Fix complexity/useOptionalChain Signed-off-by: Sora Morimoto --- src/code-formatter.js | 2 +- src/code-gen-process.js | 2 +- src/schema-parser/base-schema-parsers/enum.js | 2 +- src/schema-parser/schema-formatters.js | 2 +- src/schema-parser/schema-utils.js | 6 ++---- src/schema-routes/schema-routes.js | 21 ++++++------------- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/code-formatter.js b/src/code-formatter.js index 90141fba..af585e46 100644 --- a/src/code-formatter.js +++ b/src/code-formatter.js @@ -23,7 +23,7 @@ class CodeFormatter { { newLineCharacter: ts.sys.newLine }, )[0]; - if (fileTextChanges && fileTextChanges.textChanges.length) { + if (fileTextChanges?.textChanges.length) { return _.reduceRight( fileTextChanges.textChanges, (content, { span, newText }) => diff --git a/src/code-gen-process.js b/src/code-gen-process.js index 0c352497..1256c25d 100644 --- a/src/code-gen-process.js +++ b/src/code-gen-process.js @@ -533,7 +533,7 @@ class CodeGenProcess { createApiConfig = (swaggerSchema) => { const { info, servers, host, basePath, externalDocs, tags } = swaggerSchema; - const server = (servers && servers[0]) || { url: "" }; + const server = servers?.[0] || { url: "" }; const { title = "No title", version } = info || {}; const { url: serverUrl } = server; diff --git a/src/schema-parser/base-schema-parsers/enum.js b/src/schema-parser/base-schema-parsers/enum.js index af380c42..98a05545 100644 --- a/src/schema-parser/base-schema-parsers/enum.js +++ b/src/schema-parser/base-schema-parsers/enum.js @@ -38,7 +38,7 @@ class EnumSchemaParser extends MonoSchemaParser { } const refType = this.schemaUtils.getSchemaRefType(this.schema); - const $ref = (refType && refType.$ref) || null; + const $ref = refType?.$ref || null; // fix schema when enum has length 1+ but value is [] if (Array.isArray(this.schema.enum)) { diff --git a/src/schema-parser/schema-formatters.js b/src/schema-parser/schema-formatters.js index 4a079e13..d1c65da4 100644 --- a/src/schema-parser/schema-formatters.js +++ b/src/schema-parser/schema-formatters.js @@ -105,7 +105,7 @@ class SchemaFormatters { _.get(parsedSchema, ["schemaType"]) || _.get(parsedSchema, ["$parsed", "schemaType"]); const formatterFn = _.get(this, [formatType, schemaType]); - return (formatterFn && formatterFn(parsedSchema)) || parsedSchema; + return formatterFn?.(parsedSchema) || parsedSchema; }; formatDescription = (description, inline) => { diff --git a/src/schema-parser/schema-utils.js b/src/schema-parser/schema-utils.js index f297f181..05bc8398 100644 --- a/src/schema-parser/schema-utils.js +++ b/src/schema-parser/schema-utils.js @@ -33,7 +33,7 @@ class SchemaUtils { }; isRefSchema = (schema) => { - return !!(schema && schema["$ref"]); + return !!schema?.["$ref"]; }; getEnumNames = (schema) => { @@ -143,9 +143,7 @@ class SchemaUtils { const refData = this.getSchemaRefType(childSchema); if (refData) { - const refObjectProperties = _.keys( - (refData.rawTypeData && refData.rawTypeData.properties) || {}, - ); + const refObjectProperties = _.keys(refData.rawTypeData?.properties || {}); const existedRequiredKeys = refObjectProperties.filter((key) => required.includes(key), ); diff --git a/src/schema-routes/schema-routes.js b/src/schema-routes/schema-routes.js index 81a73a8f..b94a00f7 100644 --- a/src/schema-routes/schema-routes.js +++ b/src/schema-routes/schema-routes.js @@ -160,7 +160,7 @@ class SchemaRoutes { const queryParamMatches = fixedRoute.match(/(\{\?.*\})/g); const queryParams = []; - if (queryParamMatches && queryParamMatches.length) { + if (queryParamMatches?.length) { queryParamMatches.forEach((match) => { fixedRoute = fixedRoute.replace(match, ""); }); @@ -220,11 +220,7 @@ class SchemaRoutes { this.schemaParserFabric.schemaUtils.getSchemaRefType(parameter); let routeParam = null; - if ( - refTypeInfo && - refTypeInfo.rawTypeData.in && - refTypeInfo.rawTypeData - ) { + if (refTypeInfo?.rawTypeData.in && refTypeInfo.rawTypeData) { if (!routeParams[refTypeInfo.rawTypeData.in]) { routeParams[refTypeInfo.rawTypeData.in] = []; } @@ -345,7 +341,7 @@ class SchemaRoutes { /* for example: dataType = "multipart/form-data" */ for (const dataType in content) { - if (content[dataType] && content[dataType].schema) { + if (content[dataType]?.schema) { return { ...content[dataType].schema, dataType, @@ -510,9 +506,7 @@ class SchemaRoutes { responses: responseInfos, success: { schema: successResponse, - type: - (successResponse && successResponse.type) || - this.config.Ts.Keyword.Any, + type: successResponse?.type || this.config.Ts.Keyword.Any, }, error: { schemas: errorResponses, @@ -640,10 +634,7 @@ class SchemaRoutes { } return { - paramName: - requestBodyName || - (requestBody && requestBody.name) || - DEFAULT_BODY_ARG_NAME, + paramName: requestBodyName || requestBody?.name || DEFAULT_BODY_ARG_NAME, contentTypes, contentKind, schema, @@ -892,7 +883,7 @@ class SchemaRoutes { moduleNameFirstTag && firstTag ? _.camelCase(firstTag) : _.camelCase(_.compact(_.split(route, "/"))[moduleNameIndex]); - let hasSecurity = !!(globalSecurity && globalSecurity.length); + let hasSecurity = !!globalSecurity?.length; if (security) { hasSecurity = security.length > 0; } From a6e8c84689ef0f7c1f7b7d38509dc6492627a54a Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 21 Jun 2024 00:45:58 +0900 Subject: [PATCH 2/2] Fix complexity/useLiteralKeys Signed-off-by: Sora Morimoto --- src/schema-parser/schema-parser.js | 5 +---- src/schema-parser/schema-utils.js | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/schema-parser/schema-parser.js b/src/schema-parser/schema-parser.js index b4cc4b56..6b4cd7ad 100644 --- a/src/schema-parser/schema-parser.js +++ b/src/schema-parser/schema-parser.js @@ -213,10 +213,7 @@ class SchemaParser { this.schema = { type: this.config.Ts.Keyword.Null }; } // schema is response schema - if ( - "content" in this.schema && - typeof this.schema["content"] === "object" - ) { + if ("content" in this.schema && typeof this.schema.content === "object") { const schema = this.extractSchemaFromResponseStruct(this.schema); const schemaParser = this.schemaParserFabric.createSchemaParser({ schema, diff --git a/src/schema-parser/schema-utils.js b/src/schema-parser/schema-utils.js index 05bc8398..ddf058d1 100644 --- a/src/schema-parser/schema-utils.js +++ b/src/schema-parser/schema-utils.js @@ -33,13 +33,13 @@ class SchemaUtils { }; isRefSchema = (schema) => { - return !!schema?.["$ref"]; + return !!schema?.$ref; }; getEnumNames = (schema) => { return ( schema["x-enumNames"] || - schema["xEnumNames"] || + schema.xEnumNames || schema["x-enumnames"] || schema["x-enum-varnames"] );