-
-
Notifications
You must be signed in to change notification settings - Fork 238
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
Changes from 10 commits
a31d964
a5488e7
35a32fd
ec7e381
15e7f0c
d21cb1a
cbaad47
985a80f
e650570
01b92b0
6df7246
6823c6b
14f9f9d
d60d4cc
78192c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here there's one condition also exists There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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.