From dd309dbe8b27c8237400ea5e79f709b13bbfdc30 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com> Date: Fri, 12 Jul 2024 15:37:30 +0300 Subject: [PATCH] HCK-7056: fix sonar issues (#14) --- forward_engineering/api.js | 20 ++++++++------- helper/schemaHelper.js | 16 ++++++------ reverse_engineering/SchemaCreator.js | 38 +++++++++++++--------------- reverse_engineering/api.js | 15 ++++++----- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/forward_engineering/api.js b/forward_engineering/api.js index 29b01ef..a3b0c08 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -81,9 +81,9 @@ module.exports = { this.setProperties(schema, fieldProperties, data); if (type === 'alias') { - return Object.assign({}, schema, this.getAliasSchema(field, data)); + return { ...schema, ...this.getAliasSchema(field, data) }; } else if (type === 'join') { - return Object.assign({}, schema, this.getJoinSchema(field)); + return { ...schema, ...this.getJoinSchema(field) }; } else if ( [ 'completion', @@ -105,7 +105,7 @@ module.exports = { arrData = field.items[0]; } - schema = Object.assign(schema, this.getField(arrData, data)); + schema = { ...schema, ...this.getField(arrData, data) }; } return schema; @@ -267,14 +267,16 @@ module.exports = { } if (item.children.length === 1) { - return Object.assign({}, result, { - [item.parent]: (item.children[0] || {}).name, - }); + return { + ...result, + [item.parent]: item.children?.[0]?.name + }; } - return Object.assign({}, result, { - [item.parent]: item.children.map(item => item.name || ''), - }); + return { + ...result, + [item.parent]: item.children.map(item => item.name || '') + }; }, {}); return { relations }; diff --git a/helper/schemaHelper.js b/helper/schemaHelper.js index ab1003f..8e28494 100644 --- a/helper/schemaHelper.js +++ b/helper/schemaHelper.js @@ -64,7 +64,7 @@ const getNameByPath = (schema, path) => { return ['[' + i + ']']; } - return ['[' + i + ']', ...getNameByPath(property, path.slice(1), '[' + i + ']')]; + return ['[' + i + ']', ...getNameByPath(property, path.slice(1))]; }, []); } else if (Object(schema.items) === schema.items) { const property = schema.items; @@ -77,7 +77,7 @@ const getNameByPath = (schema, path) => { return ['[0]']; } - return ['[0]', ...getNameByPath(property, path.slice(1), '[0]')]; + return ['[0]', ...getNameByPath(property, path.slice(1))]; } }; @@ -95,7 +95,7 @@ const findFieldNameById = (id, source) => { let path = getPathById(source, id, []); if (path) { - const name = joinIndex(getNameByPath(source, path, '')); + const name = joinIndex(getNameByPath(source, path)); return name[name.length - 1] || ''; } else { @@ -104,11 +104,11 @@ const findFieldNameById = (id, source) => { }; const getPathName = (id, sources) => { - for (let i = 0; i < sources.length; i++) { - let path = getPathById(sources[i], id, []); + for (const source of sources) { + let path = getPathById(source, id, []); if (path) { - const name = getNameByPath(sources[i], path, ''); + const name = getNameByPath(source, path); return name .slice(1) @@ -122,8 +122,8 @@ const getPathName = (id, sources) => { const getNamesByIds = (ids, sources) => { return ids.reduce((names, id) => { - for (let i = 0; i < sources.length; i++) { - const name = findFieldNameById(id, sources[i]); + for (const source of sources) { + const name = findFieldNameById(id, source); if (name) { return [...names, name]; diff --git a/reverse_engineering/SchemaCreator.js b/reverse_engineering/SchemaCreator.js index 8257e0d..621765e 100644 --- a/reverse_engineering/SchemaCreator.js +++ b/reverse_engineering/SchemaCreator.js @@ -118,7 +118,10 @@ module.exports = { } const hasProperties = !!fieldData.properties; - schema = Object.assign(schema, this.getType(fieldData.type, sample, hasProperties)); + schema = { + ...schema, + ...this.getType(fieldData.type, sample, hasProperties), + }; let isArrayType = ['nested', 'array', 'geo-point'].indexOf(schema.type) !== -1; @@ -242,15 +245,14 @@ module.exports = { type: scalar, }; } - } else { - if (hasProperties) { - return { type: 'object' }; - } else { - return {}; - } + } else if (hasProperties) { + return { type: 'object' }; } + + return {}; } - }, + } +}, getScalar(value) { return typeof value; @@ -367,17 +369,13 @@ module.exports = { }, handleCompletionSnippet(schema) { - return Object.assign( - {}, - this.handleSnippet( - Object.assign({}, schema, { - subType: schema.subType === 'array' ? 'completionArray' : 'completionObject', - }), - ), - { - subType: schema.subType, - }, - ); + const subType = schema.subType === 'array' ? 'completionArray' : 'completionObject'; + const snippetSchema = this.handleSnippet({ ...schema, subType }); + + return { + ...snippetSchema, + subType: schema.subType, + }; }, handleSnippet(schema) { @@ -428,7 +426,7 @@ module.exports = { }, setProperties(schema, fieldData) { - const properties = helper.getFieldProperties(schema.type, Object.assign({ mode: fieldData.type }, fieldData), { + const properties = helper.getFieldProperties(schema.type, { mode: fieldData.type, ...fieldData}, { 'stringfields': 'fields', }); diff --git a/reverse_engineering/api.js b/reverse_engineering/api.js index bb746ae..b86b61b 100644 --- a/reverse_engineering/api.js +++ b/reverse_engineering/api.js @@ -226,10 +226,10 @@ module.exports = { async.map( indices, (indexName, nextIndex) => { - let bucketInfo = Object.assign( - getBucketData(jsonSchemas[indexName] || {}), - defaultBucketInfo, - ); + let bucketInfo = { + ...getBucketData(jsonSchemas[indexName] || {}), + ...defaultBucketInfo, + }; if (!indexTypes[indexName]) { if (includeEmptyCollection) { @@ -439,7 +439,7 @@ const getTypesByVersion = (version, types, indexes) => { indexes = Array.isArray(indexes) ? indexes : []; return indexes.reduce((result, indexName) => { - return Object.assign({}, result, { [indexName]: [] }); + return {...result, [indexName]: [] }; }, {}); }; @@ -454,9 +454,10 @@ const getIndexes = (client, includeSystemCollection) => { } }) .reduce((result, indexName) => { - return Object.assign({}, result, { + return { + ...result, [indexName]: data[indexName], - }); + }; }, {}); }); };