From 9dddf735ad8689c8e4fe68abf812c70ba0ee5f15 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Fri, 19 Jul 2024 10:49:38 +0200 Subject: [PATCH 1/5] feat(packages/eslint-plugin-sui): create lint rule --- packages/eslint-plugin-sui/src/index.js | 4 +- .../src/rules/validate-model.js | 62 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 packages/eslint-plugin-sui/src/rules/validate-model.js diff --git a/packages/eslint-plugin-sui/src/index.js b/packages/eslint-plugin-sui/src/index.js index 1620724c4..544b7814b 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 ValidateModel = require('./rules/validate-model.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -15,6 +16,7 @@ module.exports = { 'serialize-deserialize': SerializeDeserialize, commonjs: CommonJS, decorators: Decorators, - 'layers-arch': LayersArch + 'layers-arch': LayersArch, + 'validate-model': ValidateModel } } diff --git a/packages/eslint-plugin-sui/src/rules/validate-model.js b/packages/eslint-plugin-sui/src/rules/validate-model.js new file mode 100644 index 000000000..3f9c68c2f --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/validate-model.js @@ -0,0 +1,62 @@ +/** + * @fileoverview ensure domain model have a validate method + */ +'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 a validate method', + recommended: false, + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' + }, + fixable: null, + schema: [], + messages: { + missingValidateMethod: dedent` + If your class is a domain model (Value Object or Entity), you have to define a 'static validate' method. + `, + missingValidateMethodStatic: dedent` + The validate method should be static. + ` + } + }, + + create(context) { + return { + ClassDeclaration(node) { + const validateMethod = node.body.body.find(i => i.key.name === 'validate') + + const className = node?.id?.name ?? '' + + const allowedWords = ['VO', 'ValueObject', 'Entity'] + + const isDomainModel = allowedWords.some(allowWord => className.includes(allowWord)) + + if (!isDomainModel) return // eslint-disable-line + + if (!validateMethod) + return context.report({ + node: node.id, + messageId: 'missingValidateMethod' + }) + + const hasStaticModifier = validateMethod.static + + if (!hasStaticModifier) + return context.report({ + node: validateMethod, + messageId: 'missingValidateMethodStatic' + }) + } + } + } +} \ No newline at end of file From 0567d85a65addf22edce23e492d9167c5342dcb0 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Fri, 19 Jul 2024 10:50:07 +0200 Subject: [PATCH 2/5] feat(packages/sui-lint): add lint rule to config --- packages/eslint-plugin-sui/src/rules/validate-model.js | 2 +- packages/sui-lint/eslintrc.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/validate-model.js b/packages/eslint-plugin-sui/src/rules/validate-model.js index 3f9c68c2f..6c8ebf654 100644 --- a/packages/eslint-plugin-sui/src/rules/validate-model.js +++ b/packages/eslint-plugin-sui/src/rules/validate-model.js @@ -59,4 +59,4 @@ module.exports = { } } } -} \ No newline at end of file +} diff --git a/packages/sui-lint/eslintrc.js b/packages/sui-lint/eslintrc.js index 06787f9b6..f84662bdd 100644 --- a/packages/sui-lint/eslintrc.js +++ b/packages/sui-lint/eslintrc.js @@ -239,7 +239,8 @@ module.exports = { rules: { 'sui/factory-pattern': RULES.WARNING, 'sui/serialize-deserialize': RULES.WARNING, - 'sui/decorators': RULES.WARNING + 'sui/decorators': RULES.WARNING, + 'sui/validate-model': RULES.WARNING } }, { From ec0e093cb428f7f39f94cdb491167425cd1d9ea6 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Mon, 22 Jul 2024 11:45:07 +0200 Subject: [PATCH 3/5] feat(packages/eslint-plugin-sui): check if file is in VO/Entity folder --- .../eslint-plugin-sui/src/rules/validate-model.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin-sui/src/rules/validate-model.js b/packages/eslint-plugin-sui/src/rules/validate-model.js index 6c8ebf654..a2973aa43 100644 --- a/packages/eslint-plugin-sui/src/rules/validate-model.js +++ b/packages/eslint-plugin-sui/src/rules/validate-model.js @@ -4,6 +4,7 @@ 'use strict' const dedent = require('string-dedent') +const path = require('path') // ------------------------------------------------------------------------------ // Rule Definition @@ -31,6 +32,16 @@ module.exports = { }, 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 validateMethod = node.body.body.find(i => i.key.name === 'validate') @@ -41,7 +52,8 @@ module.exports = { const isDomainModel = allowedWords.some(allowWord => className.includes(allowWord)) - if (!isDomainModel) return // eslint-disable-line + if (!isDomainModelName && !isValueObjectPath) return + if (!isDomainModelName && !isEntityPath) return if (!validateMethod) return context.report({ From 9061e9d3b6daecd8cc751babee52d94740e7cb4a Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Mon, 22 Jul 2024 11:45:23 +0200 Subject: [PATCH 4/5] feat(packages/eslint-plugin-sui): check if file is in VO/Entity folder --- packages/eslint-plugin-sui/src/rules/validate-model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin-sui/src/rules/validate-model.js b/packages/eslint-plugin-sui/src/rules/validate-model.js index a2973aa43..e59d47b7b 100644 --- a/packages/eslint-plugin-sui/src/rules/validate-model.js +++ b/packages/eslint-plugin-sui/src/rules/validate-model.js @@ -50,7 +50,7 @@ module.exports = { const allowedWords = ['VO', 'ValueObject', 'Entity'] - const isDomainModel = allowedWords.some(allowWord => className.includes(allowWord)) + const isDomainModelName = allowedWords.some(allowWord => className.includes(allowWord)) if (!isDomainModelName && !isValueObjectPath) return if (!isDomainModelName && !isEntityPath) return From 69c122b5a8b301d6f53b96d914de1b5084284e01 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Tue, 23 Jul 2024 09:44:45 +0200 Subject: [PATCH 5/5] feat(packages/eslint-plugin-sui): check if keys exists --- packages/eslint-plugin-sui/src/rules/validate-model.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/validate-model.js b/packages/eslint-plugin-sui/src/rules/validate-model.js index e59d47b7b..b48dd9e47 100644 --- a/packages/eslint-plugin-sui/src/rules/validate-model.js +++ b/packages/eslint-plugin-sui/src/rules/validate-model.js @@ -44,9 +44,9 @@ module.exports = { return { ClassDeclaration(node) { - const validateMethod = node.body.body.find(i => i.key.name === 'validate') + const validateMethod = node.body?.body?.find(i => i.key?.name === 'validate') - const className = node?.id?.name ?? '' + const className = node.id?.name ?? '' const allowedWords = ['VO', 'ValueObject', 'Entity']