Skip to content

Commit

Permalink
Merge pull request #12 from aashutoshrathi/v0.2.4
Browse files Browse the repository at this point in the history
v0.2.4
  • Loading branch information
aashutoshrathi authored Jan 27, 2024
2 parents a3d4209 + 43e2322 commit 40b2551
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ But with Mongoose there is a catch, if you attach inject model to non-model vari

One such incident is shown below:

<img alt="GIF of mishap" src="https://s3.ap-south-1.amazonaws.com/shared.aashutosh.dev/eslint-rule-gif.gif">
| Before | After |
| :---: | :---: |
| <img alt="Before" src="https://s3.ap-south-1.amazonaws.com/shared.aashutosh.dev/eslint-rule-gif.gif"> | <img alt="After" src="https://s3.ap-south-1.amazonaws.com/shared.aashutosh.dev/after-eslint.gif"> |

## Installation 🛠️

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-nestjs-orm",
"version": "0.2.3",
"version": "0.2.4",
"description": "Guardrails to prevent bad practices or do some serious mistakes when using NestJS Moongose or ORMs.",
"keywords": ["eslint", "eslintplugin", "eslint-plugin", "nestjs", "mongoose"],
"homepage": "https://github.com/aashutoshrathi/eslint-plugin-nestjs-orm",
Expand Down
12 changes: 6 additions & 6 deletions src/rules/mongoose-no-bad-model-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const findNodeWithDecorator = (node: any, decoratorName: string) => {
return null;
}
return node.decorators.find(
(decorator: any) => decorator.expression.callee.name === decoratorName
(decorator: any) => decorator.expression.callee.name === decoratorName,
);
};

const badModelInjectionRule = (
context: TSESLint.RuleContext<MessageIds, []>
context: TSESLint.RuleContext<MessageIds, []>,
) => {
return {
ClassBody(node: any) {
const constructorNode = node.body.find(
(bodyNode: any) =>
bodyNode.type === AST_NODE_TYPES.MethodDefinition &&
bodyNode.kind === "constructor"
bodyNode.kind === "constructor",
);

if (!constructorNode) {
Expand All @@ -41,7 +41,7 @@ const badModelInjectionRule = (
}

const paramWithModelDecorator = params.find((param: any) =>
findNodeWithDecorator(param, DECORATOR_NAME)
findNodeWithDecorator(param, DECORATOR_NAME),
);

if (!paramWithModelDecorator) {
Expand All @@ -64,7 +64,7 @@ const badModelInjectionRule = (
}

if (parameter.typeAnnotation?.typeAnnotation) {
const { typeName, typeArguments } =
const { typeName, typeParameters } =
parameter.typeAnnotation.typeAnnotation;

if (typeName.name !== "Model") {
Expand All @@ -74,7 +74,7 @@ const badModelInjectionRule = (
});
}

if (!typeArguments || typeArguments?.params?.length !== 1) {
if (!typeParameters || typeParameters?.params?.length !== 1) {
return context.report({
node: paramWithModelDecorator,
messageId: MessageIdsEnum.missingModelType,
Expand Down
9 changes: 9 additions & 0 deletions src/tests/rules/mongoose-no-bad-model-injection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ ruleTester.run("mongoose-no-bad-model-injection", noBadModelInjectionRule, {
) {}
}`,
},
{
code: `export class HahaService {
constructor(
@InjectModel(OA.name)
private readonly aModel: Model<OADoc>,
private readonly aFactory: AFactory
) {}
}`,
},
{
code: `class HahaTest {
constructor(
Expand Down

0 comments on commit 40b2551

Please sign in to comment.