Skip to content

Commit c24d016

Browse files
fix: #14
1 parent 40b2551 commit c24d016

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/rules/mongoose-no-bad-model-injection.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { TSESLint, AST_NODE_TYPES } from "@typescript-eslint/utils";
22

33
export 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

2322
const 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:

src/tests/rules/mongoose-no-bad-model-injection.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ ruleTester.run("mongoose-no-bad-model-injection", noBadModelInjectionRule, {
2525
) {}
2626
}`,
2727
},
28+
{
29+
code: `export class HahaService {
30+
constructor(
31+
@InjectModel(OA.name)
32+
aModel: Model<OADoc>,
33+
) {}
34+
}`,
35+
},
2836
{
2937
code: `class HahaTest {
3038
constructor(

0 commit comments

Comments
 (0)