-
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?
Changes from 2 commits
758d8a9
37e382f
1af4c66
4dba341
3cd13fe
6310ab2
c5b49a2
7ff536f
4fbb929
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||
/** | ||||||
* @fileoverview ensure domain model have a private attributes and each attribute has a getter | ||||||
*/ | ||||||
'use strict' | ||||||
|
||||||
const dedent = require('string-dedent') | ||||||
|
||||||
// ------------------------------------------------------------------------------ | ||||||
// Rule Definition | ||||||
// ------------------------------------------------------------------------------ | ||||||
|
||||||
/** @type {import('eslint').Rule.RuleModule} */ | ||||||
module.exports = { | ||||||
meta: { | ||||||
type: 'suggestion', | ||||||
docs: { | ||||||
description: 'Ensure domain models have all attributes as private and each attribute has a getter', | ||||||
recommended: false, | ||||||
url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' | ||||||
}, | ||||||
fixable: null, | ||||||
schema: [], | ||||||
messages: { | ||||||
attributeHasToBePrivate: dedent` | ||||||
If your class is a domain model (Value Object or Entity), you have to define all attributes as private. | ||||||
`, | ||||||
privateAttributeHasToHaveGetter: dedent` | ||||||
If your class is a domain model (Value Object or Entity), you have to define a getter for the attribute #{{attributeName}}. | ||||||
You can define a native getter (get {{attributeName}}) or a custom getter ({{customGetterName}}). | ||||||
` | ||||||
} | ||||||
}, | ||||||
|
||||||
create(context) { | ||||||
return { | ||||||
ClassDeclaration(node) { | ||||||
const className = node?.id?.name ?? '' | ||||||
|
||||||
const allowedWords = ['VO', 'ValueObject', 'Entity'] | ||||||
|
||||||
const isDomainModel = allowedWords.some(allowWord => className.includes(allowWord)) | ||||||
|
||||||
if (!isDomainModel) return // eslint-disable-line | ||||||
|
||||||
// Check if all attributes are public | ||||||
const publicAttributes = node.body.body.filter(node => { | ||||||
return node.type === 'PropertyDefinition' && node.key.type === 'Identifier' | ||||||
}) | ||||||
|
||||||
publicAttributes.forEach(attribute => { | ||||||
context.report({ | ||||||
node: attribute, | ||||||
messageId: 'attributeHasToBePrivate' | ||||||
}) | ||||||
}) | ||||||
|
||||||
// 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 commentThe 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 This suggestion implies also to methods. 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. 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 classMethods = node.body.body.filter(node => node.type === 'MethodDefinition') | ||||||
|
||||||
privateAttributes.forEach(attribute => { | ||||||
let hasGetter = false | ||||||
const customGetterName = `get${attribute.key.name.charAt(0).toUpperCase()}${attribute.key.name.slice(1)}` | ||||||
|
||||||
classMethods.forEach(method => { | ||||||
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 commentThe 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
Suggested change
|
||||||
hasGetter = true | ||||||
} | ||||||
}) | ||||||
|
||||||
if (!hasGetter) { | ||||||
context.report({ | ||||||
node: attribute, | ||||||
messageId: 'privateAttributeHasToHaveGetter', | ||||||
data: { | ||||||
attributeName: attribute.key.name, | ||||||
customGetterName | ||||||
} | ||||||
}) | ||||||
} | ||||||
}) | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
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
orentities
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.
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.