Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/eslint-plugin-sui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +16,7 @@ module.exports = {
'serialize-deserialize': SerializeDeserialize,
commonjs: CommonJS,
decorators: Decorators,
'layers-arch': LayersArch
'layers-arch': LayersArch,
'creational-pattern-model': CreationalPatternModel
}
}
Original file line number Diff line number Diff line change
@@ -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'
})
}
}
}
}
3 changes: 2 additions & 1 deletion packages/sui-lint/eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
{
Expand Down