-
Notifications
You must be signed in to change notification settings - Fork 33
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
Feat/scmi 124866 [Domain models initiative] private attribute lint rule #1789
base: master
Are you sure you want to change the base?
Feat/scmi 124866 [Domain models initiative] private attribute lint rule #1789
Conversation
|
||
// Check if a private attribute has a public accessor | ||
const privateAttributes = node.body.body.filter(node => { | ||
return node.type === 'PropertyDefinition' && node.key.type === 'PrivateIdentifier' |
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.
I'm not sure if this code will get attributes starting with a #
and with a _
. Those two ways, are common in javascript to declare an attribute as private.
This suggestion implies also to methods.
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.
We decided in the agreement that all private attributes will be defined by #, as it is the native way to define private attributes in the latest versions of ECMAScript.
const existNativeGetterWithAttributeKey = method.key.name === attribute.key.name && method.kind === 'get' | ||
const existCustomGetterWithAttributeKey = method.key.name === customGetterName | ||
|
||
if (existNativeGetterWithAttributeKey | existCustomGetterWithAttributeKey) { |
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.
I'm not sure if the condition is well, it's intentionally with one |
or should it be ||
?
if (existNativeGetterWithAttributeKey | existCustomGetterWithAttributeKey) { | |
if (existNativeGetterWithAttributeKey || existCustomGetterWithAttributeKey) { |
ClassDeclaration(node) { | ||
const className = node?.id?.name ?? '' | ||
|
||
const allowedWords = ['VO', 'ValueObject', 'Entity'] |
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.
This is a good approach and this rule will cover a lot of cases, but it will not check classes without names but under the correct place (valueObjects
or entities
folders).
To achieve that, with the lines below, you can check if the file is inside those folders and run this rule inside them too.
const filePath = context.getFilename()
const relativePath = path.relative(context.getCwd(), filePath)
// Check if the file is inside the requierd folders (entities, services, repositories, ...)
const entitiesPattern = /entities|Entities/i
const isEntityPath = entitiesPattern.test(relativePath)
// ... same with value objects
See this PR lines, and follow the code to understand.
I suggest you detect different cases in our code base and run it there to see if this rule is working fine in different scenarios.
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.
Description
In this pull request we ensure that a doamin model class (value object or entity) has all attributes as private and each attribute has his getter.
Related Issue
SCMI-124866