diff --git a/packages/eslint-plugin-sui/src/index.js b/packages/eslint-plugin-sui/src/index.js index 1620724c4..5be4fed3d 100644 --- a/packages/eslint-plugin-sui/src/index.js +++ b/packages/eslint-plugin-sui/src/index.js @@ -3,6 +3,7 @@ const SerializeDeserialize = require('./rules/serialize-deserialize.js') const CommonJS = require('./rules/commonjs.js') const Decorators = require('./rules/decorators.js') const LayersArch = require('./rules/layers-architecture.js') +const CreationalPatternModel = require('./rules/creational-pattern-model.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -15,6 +16,7 @@ module.exports = { 'serialize-deserialize': SerializeDeserialize, commonjs: CommonJS, decorators: Decorators, - 'layers-arch': LayersArch + 'layers-arch': LayersArch, + 'creational-pattern-model': CreationalPatternModel } } diff --git a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js new file mode 100644 index 000000000..52908b87f --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js @@ -0,0 +1,74 @@ +/** + * @fileoverview ensure entity createFromPrimitive method + */ +'use strict' + +const dedent = require('string-dedent') +const path = require('path') + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + meta: { + type: 'suggestion', + docs: { + description: 'Ensure domain models have createFromPrimitives method', + recommended: false, + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' + }, + fixable: null, + schema: [], + messages: { + missingCreateFromPrimitivesMethod: dedent` + If your class is a domain model (Value Object or Entity), you have to define a 'static createFromPrimitives' method. + `, + missingCreateFromPrimitivesMethodStatic: dedent` + The createFromPrimitives method should be static + ` + } + }, + + create(context) { + const filePath = context.getFilename() + const relativePath = path.relative(context.getCwd(), filePath) + + // Check if the file is inside requierd folders (useCases, services, repositories, ...) + const valueObjectPattern = /valueObjects|valueobjects|ValueObjects|Valueobjects/i + const isValueObjectPath = valueObjectPattern.test(relativePath) + + const entityPattern = /entity|Entity/i + const isEntityPath = entityPattern.test(relativePath) + + return { + ClassDeclaration(node) { + const createFromPrimitives = node.body?.body?.find(i => i.key.name === 'createFromPrimitives') + + const className = node.id?.name ?? '' + + const allowedWords = ['VO', 'ValueObject', 'Entity'] + + const isDomainModelName = allowedWords.some(allowWord => className.includes(allowWord)) + + if (!isDomainModelName && !isValueObjectPath) return + if (!isDomainModelName && !isEntityPath) return + + if (!createFromPrimitives) + return context.report({ + node: node.id, + messageId: 'missingCreateFromPrimitivesMethod' + }) + + const hasStaticModifier = createFromPrimitives.static + + if (!hasStaticModifier) + return context.report({ + node: createFromPrimitives, + messageId: 'missingCreateFromPrimitivesMethodStatic' + }) + } + } + } +} diff --git a/packages/sui-lint/eslintrc.js b/packages/sui-lint/eslintrc.js index de5f356b0..7bf00c68c 100644 --- a/packages/sui-lint/eslintrc.js +++ b/packages/sui-lint/eslintrc.js @@ -210,7 +210,8 @@ module.exports = { rules: { 'sui/factory-pattern': RULES.WARNING, 'sui/serialize-deserialize': RULES.WARNING, - 'sui/decorators': RULES.WARNING + 'sui/decorators': RULES.WARNING, + 'sui/creational-pattern-model': RULES.WARNING } }, {