From 687202089ec7c70f8fc8cabe8c664a5862875d7c Mon Sep 17 00:00:00 2001 From: asyncapi-bot Date: Wed, 9 Oct 2024 12:57:42 +0200 Subject: [PATCH 1/9] ci: update .prettierignore from global .github repo (#1293) --- .prettierignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 000000000..fa29cdfff --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +** \ No newline at end of file From 85408328fe0b12f6a8dc92d99bbe3e8fdf26be44 Mon Sep 17 00:00:00 2001 From: rishabhr4 <118053976+rishabhr4@users.noreply.github.com> Date: Wed, 9 Oct 2024 23:57:03 +0530 Subject: [PATCH 2/9] refactor: switch to optional chaining in custom filters and removing unnecessary escape character in logMessages.js (#1285) Co-authored-by: Lukasz Gornicki --- apps/generator/lib/logMessages.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/generator/lib/logMessages.js b/apps/generator/lib/logMessages.js index 0ea167d6b..a02f3f1f1 100644 --- a/apps/generator/lib/logMessages.js +++ b/apps/generator/lib/logMessages.js @@ -2,7 +2,7 @@ const TEMPLATE_INSTALL_FLAG_MSG = 'because you passed --install flag'; const TEMPLATE_INSTALL_DISK_MSG = 'because the template cannot be found on disk'; -const NODE_MODULES_INSTALL ='Remember that your local template must have its own node_modules installed first, \"npm install\" is not triggered by the generator.'; +const NODE_MODULES_INSTALL = 'Remember that your local template must have its own node_modules installed first, "npm install" is not triggered by the generator.'; const NPM_INSTALL_TRIGGER = 'Installation of template located on disk technically means symlink creation betweed node_modules of the generator and template sources. Your local template must have its own node_modules, "npm install" is not triggered.'; @@ -19,7 +19,7 @@ function templateNotFound(templateName) { } function packageNotAvailable(packageDetails) { - if (packageDetails && packageDetails.pkgPath) { + if (packageDetails?.pkgPath) { return `Unable to resolve template location at ${packageDetails.pkgPath}. Package is not available locally.`; } From 950f4c8b3fc5589ac760637f2af6ac1b843a3373 Mon Sep 17 00:00:00 2001 From: rishabhr4 <118053976+rishabhr4@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:09:28 +0530 Subject: [PATCH 3/9] refactor: used optional chaining instead of expression in generator.js (#1290) Co-authored-by: Lukasz Gornicki --- apps/generator/lib/generator.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/generator/lib/generator.js b/apps/generator/lib/generator.js index 8a07a5670..b7286ce2b 100644 --- a/apps/generator/lib/generator.js +++ b/apps/generator/lib/generator.js @@ -562,8 +562,8 @@ class Generator { try { installedPkg = getTemplateDetails(this.templateName, PACKAGE_JSON_FILENAME); - pkgPath = installedPkg && installedPkg.pkgPath; - packageVersion = installedPkg && installedPkg.version; + pkgPath = installedPkg?.pkgPath; + packageVersion = installedPkg?.version; log.debug(logMessage.templateSource(pkgPath)); if (packageVersion) log.debug(logMessage.templateVersion(packageVersion)); From 074e520d080fd7b6dd5ffc149557cde02cda3506 Mon Sep 17 00:00:00 2001 From: Manikanta Mandala <77623256+ManikantaMandala@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:21:33 +0530 Subject: [PATCH 4/9] refactor: improve custom filters readability with optional chaining (#1275) Co-authored-by: Lukasz Gornicki --- apps/nunjucks-filters/src/customFilters.js | 56 +++++++++++----------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/apps/nunjucks-filters/src/customFilters.js b/apps/nunjucks-filters/src/customFilters.js index 7d5339f17..83ef06fbb 100644 --- a/apps/nunjucks-filters/src/customFilters.js +++ b/apps/nunjucks-filters/src/customFilters.js @@ -58,7 +58,7 @@ function getPayloadExamples(msg) { } const payload = msg.payload(); - if (payload && payload.examples()) { + if (payload?.examples()) { return payload.examples().map(example => ({ example })); } } @@ -90,7 +90,7 @@ function getHeadersExamples(msg) { } const headers = msg.headers(); - if (headers && headers.examples()) { + if (headers?.examples()) { return headers.examples().map(example => ({ example })); } } @@ -120,7 +120,7 @@ filter.oneLine = oneLine; /** * Generate JSDoc from message properties of the header and the payload - * + * * @example * docline( * Schema { @@ -134,9 +134,9 @@ filter.oneLine = oneLine; * my-app-header, * options.message.headers * ) - * + * * Returned value will be -> * @param {integer} options.message.headers.my-app-header - * + * * @field {object} - Property object * @fieldName {string} - Name of documented property * @scopePropName {string} - Name of param for JSDocs @@ -144,34 +144,34 @@ filter.oneLine = oneLine; */ function docline(field, fieldName, scopePropName) { /* eslint-disable sonarjs/cognitive-complexity */ - const buildLine = (f, fName, pName) => { - const type = f.type() ? f.type() : 'string'; - const description = f.description() ? ` - ${f.description().replace(/\r?\n|\r/g, '')}` : ''; - let def = f.default(); + const getType = (f) => f.type() || 'string'; + const getDescription = (f) => f.description() ? ` - ${f.description().replace(/\r?\n|\r/g, '')}` : ''; + const getDefault = (f, type) => (f.default() && type === 'string') ? `'${f.default()}'` : f.default(); + const getPName = (pName) => pName ? `${pName}.` : ''; - if (def && type === 'string') def = `'${def}'`; + const buildLineCore = (type, def, pName, fName) => { + return `* @param {${type}} ${pName}${fName}${def !== undefined ? `=${def}` : ''}`; + }; - let line; - if (def !== undefined) { - line = ` * @param {${type}} [${pName ? `${pName}.` : ''}${fName}=${def}]`; - } else { - line = ` * @param {${type}} ${pName ? `${pName}.` : ''}${fName}`; - } + const buildLine = (f, fName, pName) => { + const type = getType(f); + const def = getDefault(f, type); + const line = buildLineCore(type, def, getPName(pName), fName); + return line + (type === 'object' ? '' : getDescription(f)); + }; - if (type === 'object') { - let lines = `${line}\n`; - let first = true; - for (const propName in f.properties()) { - lines = `${lines}${first ? '' : '\n'}${buildLine(f.properties()[propName], propName, `${pName ? `${pName}.` : ''}${fName}`)}`; - first = false; - } - return lines; - } + const buildObjectLines = (f, fName, pName) => { + const properties = f.properties(); + const mainLine = buildLine(f, fName, pName); - return `${line}${description}`; + return `${mainLine }\n${ Object.keys(properties).map((propName) => + buildLine(properties[propName], propName, `${getPName(pName)}${fName}`) + ).join('\n')}`; }; - return buildLine(field, fieldName, scopePropName); + return getType(field) === 'object' + ? buildObjectLines(field, fieldName, scopePropName) + : buildLine(field, fieldName, scopePropName); } filter.docline = docline; @@ -179,7 +179,7 @@ filter.docline = docline; * Helper function to replace server variables in the url with actual values * @url {string} - url string * @serverserverVariables {Object} - Variables model map - * @returns {string} + * @returns {string} */ function replaceServerVariablesWithValues(url, serverVariables) { const getVariablesNamesFromUrl = (inputUrl) => { From 6f60d50f3e4544d7356e8b1c8dc40380dfb87ece Mon Sep 17 00:00:00 2001 From: rishabhr4 <118053976+rishabhr4@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:37:28 +0530 Subject: [PATCH 5/9] refactor: used object.hasown instead of object.hasownproperty in generator.js (#1291) Co-authored-by: Lukasz Gornicki --- apps/generator/lib/generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/generator/lib/generator.js b/apps/generator/lib/generator.js index b7286ce2b..cb4ffe950 100644 --- a/apps/generator/lib/generator.js +++ b/apps/generator/lib/generator.js @@ -759,7 +759,7 @@ class Generator { // Check if the filename dictates it should be separated let wasSeparated = false; for (const prop in fileNamesForSeparation) { - if (Object.prototype.hasOwnProperty.call(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) { + if (Object.hasOwn(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) { await this.generateSeparateFiles(asyncapiDocument, fileNamesForSeparation[prop], prop, stats.name, root); const templateFilePath = path.relative(this.templateContentDir, path.resolve(root, stats.name)); fs.unlink(path.resolve(this.targetDir, templateFilePath), next); From 0e3478b46ebb086d08fe7e41a87de8110d99e08d Mon Sep 17 00:00:00 2001 From: Bedi Gupta Date: Mon, 14 Oct 2024 15:01:21 +0530 Subject: [PATCH 6/9] refactor: use optional chaining in template validator (#1300) --- apps/generator/lib/templateConfigValidator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/generator/lib/templateConfigValidator.js b/apps/generator/lib/templateConfigValidator.js index 3b57ef759..9e075b98b 100644 --- a/apps/generator/lib/templateConfigValidator.js +++ b/apps/generator/lib/templateConfigValidator.js @@ -97,7 +97,7 @@ function getParamSuggestion(wrongParam, configParams) { * @param {Object} templateParams All parameters provided to generator */ function isProvidedParameterSupported(configParams, templateParams) { - const wrongParams = Object.keys(templateParams || {}).filter(key => !configParams || !configParams[key]); + const wrongParams = Object.keys(templateParams || {}).filter(key => !configParams?.[key]); if (!wrongParams.length) return; if (!configParams) throw new Error('This template doesn\'t have any params.'); From 254b07247f7d90f81ba6bd10d00a7eb478ce0f1d Mon Sep 17 00:00:00 2001 From: Bedi Gupta Date: Mon, 14 Oct 2024 21:50:55 +0530 Subject: [PATCH 7/9] refactor: use optional chaining in template params validation (#1299) Co-authored-by: Lukasz Gornicki --- apps/generator/lib/generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/generator/lib/generator.js b/apps/generator/lib/generator.js index cb4ffe950..8b2592a6f 100644 --- a/apps/generator/lib/generator.js +++ b/apps/generator/lib/generator.js @@ -134,7 +134,7 @@ class Generator { Object.defineProperty(this.templateParams, key, { enumerable: true, get() { - if (!self.templateConfig.parameters || !self.templateConfig.parameters[key]) { + if (!self.templateConfig.parameters?.[key]) { throw new Error(`Template parameter "${key}" has not been defined in the package.json file under generator property. Please make sure it's listed there before you use it in your template.`); } return templateParams[key]; From 298479a5773e4c4860d927d42b513f5f6018f1b4 Mon Sep 17 00:00:00 2001 From: Bedi Gupta Date: Mon, 14 Oct 2024 22:05:19 +0530 Subject: [PATCH 8/9] refactor: simplify variable initialization in tests (#1297) Co-authored-by: Lukasz Gornicki --- apps/generator/test/renderer.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/generator/test/renderer.test.js b/apps/generator/test/renderer.test.js index f6c0c6458..bd1a6489e 100644 --- a/apps/generator/test/renderer.test.js +++ b/apps/generator/test/renderer.test.js @@ -10,8 +10,8 @@ jest.mock('@asyncapi/generator-react-sdk'); describe('React renderer', () => { describe('saveRenderedReactContent', () => { - let util = undefined; - let AsyncReactSDK = undefined; + let util; + let AsyncReactSDK; beforeAll(() => { util = require('../lib/utils'); AsyncReactSDK = require('@asyncapi/generator-react-sdk'); From 25c4e53c06282e21f037ff61485cc3c594e043ac Mon Sep 17 00:00:00 2001 From: Bedi Gupta Date: Tue, 15 Oct 2024 13:25:53 +0530 Subject: [PATCH 9/9] refactor: better readability of `buildLineCore` filter (#1301) Co-authored-by: Lukasz Gornicki --- apps/nunjucks-filters/src/customFilters.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/nunjucks-filters/src/customFilters.js b/apps/nunjucks-filters/src/customFilters.js index 83ef06fbb..0914c821a 100644 --- a/apps/nunjucks-filters/src/customFilters.js +++ b/apps/nunjucks-filters/src/customFilters.js @@ -150,9 +150,11 @@ function docline(field, fieldName, scopePropName) { const getPName = (pName) => pName ? `${pName}.` : ''; const buildLineCore = (type, def, pName, fName) => { - return `* @param {${type}} ${pName}${fName}${def !== undefined ? `=${def}` : ''}`; + const paramName = `${pName}${fName}`; + const defaultValue = def !== undefined ? `=${def}` : ''; + return `* @param {${type}} ${paramName}${defaultValue}`; }; - + const buildLine = (f, fName, pName) => { const type = getType(f); const def = getDefault(f, type);