@@ -2,7 +2,6 @@ import { TSESLint, AST_NODE_TYPES } from "@typescript-eslint/utils";
22
33export enum MessageIdsEnum {
44 missingModelType = "missingModelType" ,
5- missingModelParameter = "missingModelParameter" ,
65 missingModelParameterType = "missingModelParameterType" ,
76 nonModelTypeUsed = "nonModelTypeUsed" ,
87}
@@ -16,19 +15,19 @@ const findNodeWithDecorator = (node: any, decoratorName: string) => {
1615 return null ;
1716 }
1817 return node . decorators . find (
19- ( decorator : any ) => decorator . expression . callee . name === decoratorName ,
18+ ( decorator : any ) => decorator . expression . callee . name === decoratorName
2019 ) ;
2120} ;
2221
2322const badModelInjectionRule = (
24- context : TSESLint . RuleContext < MessageIds , [ ] > ,
23+ context : TSESLint . RuleContext < MessageIds , [ ] >
2524) => {
2625 return {
2726 ClassBody ( node : any ) {
2827 const constructorNode = node . body . find (
2928 ( bodyNode : any ) =>
3029 bodyNode . type === AST_NODE_TYPES . MethodDefinition &&
31- bodyNode . kind === "constructor" ,
30+ bodyNode . kind === "constructor"
3231 ) ;
3332
3433 if ( ! constructorNode ) {
@@ -41,44 +40,39 @@ const badModelInjectionRule = (
4140 }
4241
4342 const paramWithModelDecorator = params . find ( ( param : any ) =>
44- findNodeWithDecorator ( param , DECORATOR_NAME ) ,
43+ findNodeWithDecorator ( param , DECORATOR_NAME )
4544 ) ;
4645
4746 if ( ! paramWithModelDecorator ) {
4847 return ;
4948 }
5049
5150 const { parameter } = paramWithModelDecorator ;
52- if ( ! parameter ) {
53- return context . report ( {
54- node : paramWithModelDecorator ,
55- messageId : MessageIdsEnum . missingModelParameter ,
56- } ) ;
57- }
58-
59- if ( ! parameter ?. typeAnnotation ) {
60- return context . report ( {
61- node : paramWithModelDecorator ,
62- messageId : MessageIdsEnum . missingModelParameterType ,
63- } ) ;
64- }
65-
66- if ( parameter . typeAnnotation ?. typeAnnotation ) {
67- const { typeName, typeParameters } =
68- parameter . typeAnnotation . typeAnnotation ;
69-
70- if ( typeName . name !== "Model" ) {
51+ if ( parameter ) {
52+ if ( ! parameter . typeAnnotation ) {
7153 return context . report ( {
7254 node : paramWithModelDecorator ,
73- messageId : MessageIdsEnum . nonModelTypeUsed ,
55+ messageId : MessageIdsEnum . missingModelParameterType ,
7456 } ) ;
7557 }
7658
77- if ( ! typeParameters || typeParameters ?. params ?. length !== 1 ) {
78- return context . report ( {
79- node : paramWithModelDecorator ,
80- messageId : MessageIdsEnum . missingModelType ,
81- } ) ;
59+ if ( parameter . typeAnnotation ?. typeAnnotation ) {
60+ const { typeName, typeParameters } =
61+ parameter . typeAnnotation . typeAnnotation ;
62+
63+ if ( typeName . name !== "Model" ) {
64+ return context . report ( {
65+ node : paramWithModelDecorator ,
66+ messageId : MessageIdsEnum . nonModelTypeUsed ,
67+ } ) ;
68+ }
69+
70+ if ( ! typeParameters || typeParameters ?. params ?. length !== 1 ) {
71+ return context . report ( {
72+ node : paramWithModelDecorator ,
73+ messageId : MessageIdsEnum . missingModelType ,
74+ } ) ;
75+ }
8276 }
8377 }
8478 } ,
@@ -91,8 +85,6 @@ const rule: TSESLint.RuleModule<MessageIds> = {
9185 type : "problem" ,
9286 schema : [ ] ,
9387 messages : {
94- missingModelParameter :
95- "The statement with @InjectModel() decorator should contain a parameter" ,
9688 nonModelTypeUsed : "Parameter type should be Model<T>" ,
9789 missingModelParameterType : "Parameter doesn't have a type annotation" ,
9890 missingModelType :
0 commit comments