From 51a7e69bd2d1eb0a782074bd9417283896e84f41 Mon Sep 17 00:00:00 2001 From: Thiago Martins Date: Wed, 21 Feb 2024 12:55:25 -0300 Subject: [PATCH] refactor: simplify getProviderType code --- .../enforce-custom-provider-type.rule.ts | 46 +++++-------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/src/rules/enforce-custom-provider-type.rule.ts b/src/rules/enforce-custom-provider-type.rule.ts index cda26e7..82fdf5a 100644 --- a/src/rules/enforce-custom-provider-type.rule.ts +++ b/src/rules/enforce-custom-provider-type.rule.ts @@ -146,40 +146,18 @@ function getProviderType(node: TSESTree.Identifier): ProviderType | undefined { if (init?.type === AST_NODE_TYPES.ObjectExpression) { const properties = init.properties; for (const property of properties) { - if ( - property.type === AST_NODE_TYPES.Property && - ASTUtils.isIdentifier(property.key) && - property.key.name === 'useFactory' - ) { - type = 'factory'; - break; - } - - if ( - property.type === AST_NODE_TYPES.Property && - ASTUtils.isIdentifier(property.key) && - property.key.name === 'useClass' - ) { - type = 'class'; - break; - } - - if ( - property.type === AST_NODE_TYPES.Property && - ASTUtils.isIdentifier(property.key) && - property.key.name === 'useValue' - ) { - type = 'value'; - break; - } - - if ( - property.type === AST_NODE_TYPES.Property && - ASTUtils.isIdentifier(property.key) && - property.key.name === 'useExisting' - ) { - type = 'existing'; - break; + if (property.type === AST_NODE_TYPES.Property) { + const propertyKey = (property.key as TSESTree.Identifier)?.name; + type = + propertyKey === 'useClass' + ? 'class' + : propertyKey === 'useFactory' + ? 'factory' + : propertyKey === 'useValue' + ? 'value' + : propertyKey === 'useExisting' + ? 'existing' + : undefined; } } }