eslint-plugin-sui
Advanced tools
Comparing version 1.6.0 to 1.7.0
{ | ||
"name": "eslint-plugin-sui", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"access": "public", | ||
@@ -5,0 +5,0 @@ "description": "Set of sui lint rules", |
@@ -25,6 +25,9 @@ /** | ||
messages: { | ||
notFoundExportedFactoryNamedDeclaration: dedent` | ||
You have to define a constant named 'factory' that returns the create method. | ||
`, | ||
notFoundFactoryFunction: dedent` | ||
You have to define at least one static function that return an instance of your class. | ||
Avoid to use the 'new' keyword directly in your code. | ||
Use always a factory function | ||
You have to define at least one static method that returns an instance of your class. | ||
Avoid using the 'new' keyword directly in your code. | ||
Always use a factory function | ||
` | ||
@@ -35,3 +38,22 @@ } | ||
// variables should be defined here | ||
const ENTITY_SUPER_CLASS = 'Entity' | ||
const ERROR_SUPER_CLASS = 'Error' | ||
const MAPPER_SUPER_CLASS = 'Mapper' | ||
const REPOSITORY_SUPER_CLASS = 'Repository' | ||
const SERVICE_SUPER_CLASS = 'Service' | ||
const USE_CASE_SUPER_CLASS = 'UseCase' | ||
const VALUE_OBJECT_SUPER_CLASS = 'ValueObject' | ||
const SUPER_CLASS_TYPES = { | ||
ENTITY: ENTITY_SUPER_CLASS, | ||
ERROR: ERROR_SUPER_CLASS, | ||
MAPPER: MAPPER_SUPER_CLASS, | ||
REPOSITORY: REPOSITORY_SUPER_CLASS, | ||
SERVICE: SERVICE_SUPER_CLASS, | ||
USE_CASE: USE_CASE_SUPER_CLASS, | ||
VALUE_OBJECT: VALUE_OBJECT_SUPER_CLASS | ||
} | ||
const SUPER_CLASS_TYPES_WITH_CUSTOM_CREATE_METHOD = [SUPER_CLASS_TYPES.ENTITY, SUPER_CLASS_TYPES.VALUE_OBJECT] | ||
// ---------------------------------------------------------------------- | ||
@@ -49,17 +71,49 @@ // Helpers | ||
ClassDeclaration(node) { | ||
const hasStaticFactoryMethod = Boolean( | ||
node.body?.body?.find(methodDefinition => { | ||
return ( | ||
methodDefinition.static && | ||
methodDefinition.value?.body?.body?.find?.(body => body.type === 'ReturnStatement')?.argument?.callee?.name === node?.id?.name // eslint-disable-line | ||
if (!SUPER_CLASS_TYPES_WITH_CUSTOM_CREATE_METHOD.includes(node?.superClass?.name)) { | ||
const createMethod = node?.body?.body?.find( | ||
body => body?.type === 'MethodDefinition' && body?.static && body?.key?.name === 'create' | ||
) | ||
if (!createMethod) { | ||
context.report({ | ||
node: node?.id ?? node?.superClass ?? node, | ||
messageId: 'notFoundFactoryFunction' | ||
}) | ||
} | ||
if (createMethod) { | ||
const hasReturnStatement = Boolean( | ||
createMethod?.value?.body?.body?.find( | ||
body => body?.type === 'ReturnStatement' && body?.argument?.type === 'NewExpression' | ||
) | ||
) | ||
}) | ||
) | ||
if (!hasStaticFactoryMethod) { | ||
context.report({ | ||
node: node?.id ?? node.superClass ?? node, | ||
messageId: 'notFoundFactoryFunction' | ||
}) | ||
if (!hasReturnStatement) { | ||
context.report({ | ||
node: node?.id ?? node?.superClass ?? node, | ||
messageId: 'notFoundFactoryFunction' | ||
}) | ||
} | ||
} | ||
} | ||
const isUseCase = node?.superClass?.name === USE_CASE_SUPER_CLASS | ||
if (isUseCase) { | ||
const hasExportedFactoryVariable = Boolean( | ||
node.parent.body.find( | ||
body => | ||
body?.type === 'ExportNamedDeclaration' && | ||
body?.declaration?.type === 'VariableDeclaration' && | ||
body?.declaration?.declarations?.[0]?.id?.name === 'factory' | ||
) | ||
) | ||
if (!hasExportedFactoryVariable) { | ||
context.report({ | ||
node: node?.id ?? node?.superClass ?? node, | ||
messageId: 'notFoundExportedFactoryNamedDeclaration' | ||
}) | ||
} | ||
} | ||
} | ||
@@ -66,0 +120,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
22817
560
0