Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use optional chaining for improved readability and maintained functionality #1280

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 0 additions & 198 deletions apps/generator/cli.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get back this file pls.

This file was deleted.

25 changes: 20 additions & 5 deletions apps/generator/lib/generator.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unwanted comments from this file

Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,32 @@ class Generator {
/** @type {Object} The template parameters. The structure for this object is based on each individual template. */
this.templateParams = {};
Object.keys(templateParams).forEach(key => {
const self = this;
Object.defineProperty(this.templateParams, key, {
enumerable: true,
get() {
if (!self.templateConfig.parameters || !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.`);
get: () => {
// Using ?. to check if parameters and the key exist
// This is shorter and does the same thing as the old if statements
if (this.templateConfig.parameters?.[key] == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here there's one condition also exists !self.templateConfig.parameters pls. add that also in validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check PR description where I mention the issue that I solved in this PR sonarcloud highlight the issue that Prefer using an optional chain expression instead, as it's more concise and easier to read. that's what I did if i implement one more codition !self.templateConfig.parameters then this PR makes no sense here I hope you understand

This change makes the code more concise and easier to read while maintaining the same functionality. It checks if either the parameters object doesn't exist or if the specific key is not defined within it.

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.`
);
}
// If we got here, the parameter exists, so just return it
return templateParams[key];
}
},
});
});
// CHANGES EXPLAINED:
// 1. Removed 'self' variable: Arrow function in the getter automatically binds 'this'
// to the correct context, eliminating the need for 'const self = this'.
// 2. Simplified null check: Using '?.' and '== null' checks for both null and undefined
// in a more concise way.
// 3. Improved readability: The code is now more compact and easier to understand at a glance.
// 4. Maintained functionality: Despite the changes, the core logic remains the same,
// ensuring backwards compatibility.

// NOTE: This refactoring addresses the SonarCloud suggestion to use optional chaining
// and improves overall code quality by using modern JavaScript features.
}

/**
Expand Down
Loading