From 78c79863eeab70b291f1492f05f0f2e24ea52512 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Thu, 18 Jul 2024 14:11:57 +0200 Subject: [PATCH 1/7] feat(packages/eslint-plugin-sui): create linter rule --- packages/eslint-plugin-sui/src/index.js | 4 +- .../src/rules/creational-pattern-model.js | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 packages/eslint-plugin-sui/src/rules/creational-pattern-model.js 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..74989b93b --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js @@ -0,0 +1,59 @@ +/** + * @fileoverview ensure entity createFromPrimitive mMethod + */ +'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 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) { + 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 isDomainModel = allowedWords.some(allowWord => className.includes(allowWord)) + + if (!isDomainModel) return // eslint-disable-line + + if (!createFromPrimitives) + return context.report({ + node: node.id, + messageId: 'missingCreateFromPrimitivesMethod' + }) + + const hasStaticModifier = createFromPrimitives.static + + if(!hasStaticModifier) + return context.report({ + node: createFromPrimitives, + messageId: 'missingCreateFromPrimitivesMethodStatic' + }) + } + } + } +} From 230c95a68ed28a27a415772b6bc0962c6d9c84a1 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Thu, 18 Jul 2024 14:14:52 +0200 Subject: [PATCH 2/7] feat(packages/sui-lint): add new rule to sui-lint --- packages/sui-lint/eslintrc.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 } }, { From 918d9a0108fa83e0fb6e19e41ba89e33ded0e22b Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Thu, 18 Jul 2024 14:48:30 +0200 Subject: [PATCH 3/7] feat(packages/eslint-plugin-sui): write dedent correctly --- .../src/rules/creational-pattern-model.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js index 74989b93b..c91047f88 100644 --- a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js @@ -21,17 +21,20 @@ module.exports = { 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` + 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) { return { ClassDeclaration(node) { - const createFromPrimitives = node.body.body.find(i => i.key.name === 'createFromPrimitives') - + const className = node?.id?.name ?? '' const allowedWords = ['VO', 'ValueObject', 'Entity'] @@ -46,7 +49,7 @@ module.exports = { messageId: 'missingCreateFromPrimitivesMethod' }) - const hasStaticModifier = createFromPrimitives.static + const hasStaticModifier = createFromPrimitives.static if(!hasStaticModifier) return context.report({ From 974afafd7a1e8530bea0511e645ac4da4abf1b7b Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Thu, 18 Jul 2024 14:50:48 +0200 Subject: [PATCH 4/7] feat(packages/eslint-plugin-sui): lint --- .../eslint-plugin-sui/src/rules/creational-pattern-model.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js index c91047f88..a4b682ab7 100644 --- a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js @@ -49,9 +49,9 @@ module.exports = { messageId: 'missingCreateFromPrimitivesMethod' }) - const hasStaticModifier = createFromPrimitives.static + const hasStaticModifier = createFromPrimitives.static - if(!hasStaticModifier) + if (!hasStaticModifier) return context.report({ node: createFromPrimitives, messageId: 'missingCreateFromPrimitivesMethodStatic' From f2fc2e58ca268af3e07f0c775e9ea50112086a5d Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Mon, 22 Jul 2024 11:46:44 +0200 Subject: [PATCH 5/7] feat(packages/eslint-plugin-sui): check if file is in VO/entity folder --- .../src/rules/creational-pattern-model.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js index a4b682ab7..78b62629f 100644 --- a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-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 createFromPrimitives = node.body.body.find(i => i.key.name === 'createFromPrimitives') @@ -39,9 +50,10 @@ module.exports = { const allowedWords = ['VO', 'ValueObject', 'Entity'] - const isDomainModel = allowedWords.some(allowWord => className.includes(allowWord)) + const isDomainModelName = allowedWords.some(allowWord => className.includes(allowWord)) - if (!isDomainModel) return // eslint-disable-line + if (!isDomainModelName && !isValueObjectPath) return + if (!isDomainModelName && !isEntityPath) return if (!createFromPrimitives) return context.report({ From ba2d2f40de742373b25dedf9f6ae9e9152f35239 Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Tue, 23 Jul 2024 09:49:36 +0200 Subject: [PATCH 6/7] feat(packages/eslint-plugin-sui): check if keys exists --- .../eslint-plugin-sui/src/rules/creational-pattern-model.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js index 78b62629f..6f1773a85 100644 --- a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js @@ -44,9 +44,9 @@ module.exports = { return { ClassDeclaration(node) { - const createFromPrimitives = node.body.body.find(i => i.key.name === 'createFromPrimitives') + const createFromPrimitives = node.body?.body?.find(i => i.key.name === 'createFromPrimitives') - const className = node?.id?.name ?? '' + const className = node.id?.name ?? '' const allowedWords = ['VO', 'ValueObject', 'Entity'] From 41a788a24cab7895f1d1be33e4049d9329e7e3ac Mon Sep 17 00:00:00 2001 From: aleix-ferrer Date: Tue, 23 Jul 2024 10:35:08 +0200 Subject: [PATCH 7/7] feat(packages/eslint-plugin-sui): typo --- .../eslint-plugin-sui/src/rules/creational-pattern-model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js index 6f1773a85..52908b87f 100644 --- a/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js +++ b/packages/eslint-plugin-sui/src/rules/creational-pattern-model.js @@ -1,5 +1,5 @@ /** - * @fileoverview ensure entity createFromPrimitive mMethod + * @fileoverview ensure entity createFromPrimitive method */ 'use strict'