@getlarge/eslint-plugin-nestjs-tools 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ ## 0.1.1 (2024-09-13)
2
+
3
+ This was a version bump only for eslint-plugin-nestjs-tools to align it with other projects, there were no code changes.
4
+
5
+ ## 0.1.0 (2024-09-13)
6
+
7
+
8
+ ### 🚀 Features
9
+
10
+ - **nestjs-tools-eslint-rules:** initial implementation ([1a3ed41](https://github.com/getlarge/nestjs-tools/commit/1a3ed41))
11
+
12
+
13
+ ### ❤️ Thank You
14
+
15
+ - getlarge
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # eslint-plugin
2
+
3
+ [![npm][npm-image]][npm-url]
4
+
5
+ [npm-image]: https://img.shields.io/npm/v/@getlarge/eslint-plugin-nestjs-tools.svg?style=flat
6
+ [npm-url]: https://npmjs.org/package/@getlarge/eslint-plugin-nestjs-tools
7
+
8
+ This set of ESLint rules is provided to enforce a consistent patterns and practices across all NestJS projects.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install --save @getlarge/eslint-plugin-nestjs-tools
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### return-class-instance
19
+
20
+ This rule enforces that all public Service methods return a class instance of the same return type.
21
+ The purpose of this rule is to ensure you return class instances instead of plain objects, which is essentials when using the [`ClassSerializerInterceptor`](https://docs.nestjs.com/techniques/serialization#class-serializer-interceptor).
22
+
23
+ ```json
24
+ {
25
+ "rules": {
26
+ "@getlarge/eslint-plugin-nestjs-tools/return-class-instance": "error"
27
+ }
28
+ }
29
+ ```
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@getlarge/eslint-plugin-nestjs-tools",
3
+ "version": "0.1.1",
4
+ "description": "ESLint rules for NestJS applications",
5
+ "keywords": [
6
+ "eslint",
7
+ "eslint-plugin",
8
+ "eslintplugin",
9
+ "nestjs",
10
+ "rules",
11
+ "typescript",
12
+ "lint",
13
+ "style"
14
+ ],
15
+ "license": "Apache-2.0",
16
+ "author": "Edouard Maleix <ed@getlarge.eu>",
17
+ "homepage": "https://github.com/getlarge/nestjs-tools/tree/main/packages/eslint-plugin",
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "dependencies": {
22
+ "@typescript-eslint/utils": "8.5.0"
23
+ },
24
+ "peerDependencies": {
25
+ "@typescript-eslint/parser": "^8.0.0 || ^8.0.0-alpha.0",
26
+ "eslint": "^8.57.0 || ^9.0.0"
27
+ },
28
+ "type": "commonjs",
29
+ "main": "./src/index.js",
30
+ "typings": "./src/index.d.ts",
31
+ "types": "./src/index.d.ts"
32
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ declare const _default: {
2
+ meta: {
3
+ name: string;
4
+ version: string;
5
+ };
6
+ rules: {
7
+ "@getlarge/eslint-plugin-nestjs-tools/return-class-instance": import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"missingReturnType" | "returnClassInstance", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener>;
8
+ };
9
+ };
10
+ export default _default;
package/src/index.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const return_class_instance_1 = require("./lib/return-class-instance");
4
+ // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
5
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
6
+ const { name, version } = require('../package.json');
7
+ exports.default = {
8
+ meta: {
9
+ name,
10
+ version,
11
+ },
12
+ rules: { [return_class_instance_1.RULE_NAME]: return_class_instance_1.rule },
13
+ };
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/eslint-plugin/src/index.ts"],"names":[],"mappings":";;AAEA,uEAAgH;AAEhH,sHAAsH;AACtH,8DAA8D;AAC9D,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAGlD,CAAC;AAEF,kBAAe;IACb,IAAI,EAAE;QACJ,IAAI;QACJ,OAAO;KACR;IACD,KAAK,EAAE,EAAE,CAAC,iCAAuB,CAAC,EAAE,4BAAmB,EAAE;CAClC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ export declare const RULE_NAME = "@getlarge/eslint-plugin-nestjs-tools/return-class-instance";
3
+ export declare const rule: ESLintUtils.RuleModule<"missingReturnType" | "returnClassInstance", [], unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = exports.RULE_NAME = void 0;
4
+ /* eslint-disable max-lines-per-function */
5
+ const utils_1 = require("@typescript-eslint/utils");
6
+ exports.RULE_NAME = '@getlarge/eslint-plugin-nestjs-tools/return-class-instance';
7
+ const getTypeNameFromReturnType = (rawReturnType) => {
8
+ return rawReturnType.replaceAll(/Promise<([^<>]*)>/g, '$1').replace(': ', '');
9
+ };
10
+ const isPrimitive = (value) => {
11
+ return value === 'string' || value === 'number' || value === 'boolean';
12
+ };
13
+ const isVoid = (value) => {
14
+ return value === 'void';
15
+ };
16
+ const isLiteralType = (type) => {
17
+ return /^'[^']*'$/.test(type) || /^"[^"]*"$/.test(type);
18
+ };
19
+ const isNullable = (type) => ['null', 'undefined'].includes(type);
20
+ const extractUnionTypes = (typeName) => typeName.split('|').map((type) => type.trim());
21
+ const isValidReturnType = (type) => isPrimitive(type) || isVoid(type) || isLiteralType(type) || isNullable(type);
22
+ exports.rule = utils_1.ESLintUtils.RuleCreator(() => __filename)({
23
+ name: exports.RULE_NAME,
24
+ meta: {
25
+ type: 'problem',
26
+ docs: {
27
+ description: 'Ensure service methods return class instances',
28
+ },
29
+ schema: [],
30
+ messages: {
31
+ missingReturnType: 'Public service methods should explicitly declare a return type that is a class instance, a primitive or void.',
32
+ returnClassInstance: 'Public service methods should return a class instance of the declared return type.',
33
+ },
34
+ },
35
+ defaultOptions: [],
36
+ create(context) {
37
+ const ignoredKinds = new Set(['constructor', 'get', 'set']);
38
+ const ignoredMethods = new Set([
39
+ 'onModuleInit',
40
+ 'onModuleDestroy',
41
+ 'onApplicationBootstrap',
42
+ 'beforeApplicationShutdown',
43
+ 'onApplicationShutdown',
44
+ ]);
45
+ return {
46
+ MethodDefinition(node) {
47
+ if (node.static === false &&
48
+ node.type === 'MethodDefinition' &&
49
+ node.key.type === 'Identifier' &&
50
+ ignoredMethods.has(node.key.name) === false &&
51
+ ignoredKinds.has(node.kind) === false &&
52
+ node.computed === false &&
53
+ node.accessibility !== 'private' &&
54
+ node.accessibility !== 'protected' &&
55
+ node.parent?.type === 'ClassBody' &&
56
+ node.parent.parent.type === 'ClassDeclaration' &&
57
+ node.parent.parent.decorators?.some((decorator) => decorator.expression.type === 'CallExpression' &&
58
+ decorator.expression.callee.type === 'Identifier' &&
59
+ decorator.expression.callee?.name === 'Injectable')) {
60
+ const returnType = node.value.returnType;
61
+ if (!returnType) {
62
+ context.report({
63
+ node,
64
+ messageId: 'missingReturnType',
65
+ });
66
+ }
67
+ else {
68
+ const returnTypeText = context.sourceCode.getText(returnType);
69
+ const typeName = getTypeNameFromReturnType(returnTypeText);
70
+ const extractedTypes = extractUnionTypes(getTypeNameFromReturnType(returnTypeText));
71
+ // If all union types are primitive, void, or literals, we return early
72
+ if (extractedTypes.every(isValidReturnType)) {
73
+ return;
74
+ }
75
+ const returnStatements = node.value?.body?.body?.filter((node) => node.type === 'ReturnStatement') ?? [];
76
+ const returnClassInstance = returnStatements.every((returnStatement) => {
77
+ const returnArgument = returnStatement.argument;
78
+ if (returnArgument?.type === 'NewExpression' && returnArgument.callee?.type === 'Identifier') {
79
+ return returnArgument.callee.name === typeName;
80
+ }
81
+ if (returnArgument?.type === 'CallExpression') {
82
+ const calleeName = returnArgument.callee?.type === 'Identifier'
83
+ ? returnArgument.callee.name
84
+ : returnArgument.callee.type === 'MemberExpression' &&
85
+ returnArgument.callee.property?.type === 'Identifier'
86
+ ? returnArgument.callee.property.name
87
+ : '';
88
+ // Handle Promise.resolve cases
89
+ if (calleeName === 'Promise' &&
90
+ returnArgument.arguments[0].type === 'NewExpression' &&
91
+ returnArgument.arguments[0].callee?.type === 'Identifier') {
92
+ return returnArgument.arguments[0].callee.name === typeName;
93
+ }
94
+ return true;
95
+ }
96
+ // Handle literal types and null/undefined
97
+ if (returnArgument?.type === 'Literal' || returnArgument === null) {
98
+ return returnArgument?.value?.toString()
99
+ ? extractedTypes.includes(returnArgument.value.toString())
100
+ : extractedTypes.includes('null');
101
+ }
102
+ return false;
103
+ });
104
+ if (!returnClassInstance) {
105
+ context.report({
106
+ node,
107
+ messageId: 'returnClassInstance',
108
+ });
109
+ }
110
+ }
111
+ }
112
+ },
113
+ };
114
+ },
115
+ });
116
+ //# sourceMappingURL=return-class-instance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"return-class-instance.js","sourceRoot":"","sources":["../../../../../packages/eslint-plugin/src/lib/return-class-instance.ts"],"names":[],"mappings":";;;AAAA,2CAA2C;AAC3C,oDAAuD;AAE1C,QAAA,SAAS,GAAG,4DAA4D,CAAC;AAEtF,MAAM,yBAAyB,GAAG,CAAC,aAAqB,EAAE,EAAE;IAC1D,OAAO,aAAa,CAAC,UAAU,CAAC,oBAAoB,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAChF,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAC,KAAa,EAAE,EAAE;IACpC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,SAAS,CAAC;AACzE,CAAC,CAAC;AAEF,MAAM,MAAM,GAAG,CAAC,KAAa,EAAE,EAAE;IAC/B,OAAO,KAAK,KAAK,MAAM,CAAC;AAC1B,CAAC,CAAC;AAEF,MAAM,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