@clipboard-health/eslint-plugin 2.1.87 → 2.1.89
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/package.json
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Rule to require controller methods to use ts-rest as per our best practices on backend REST APIs
|
|
3
|
+
*/
|
|
4
|
+
import { type TSESLint } from "@typescript-eslint/utils";
|
|
5
|
+
declare const rule: TSESLint.RuleModule<"missingDecorator" | "missingReturn" | "decoratorNotFromPackage" | "callNotFromPackage", [], unknown, TSESLint.RuleListener>;
|
|
2
6
|
export default rule;
|
|
@@ -5,6 +5,63 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
*/
|
|
6
6
|
const utils_1 = require("@typescript-eslint/utils");
|
|
7
7
|
const createRule_1 = require("../../createRule");
|
|
8
|
+
function validateDecorators(context, node, symbolName, decoratorImportedCorrectly) {
|
|
9
|
+
const decorators = node.decorators ?? [];
|
|
10
|
+
const hasMatchingDecorator = decorators.some((decorator) => decorator.expression.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
|
11
|
+
decorator.expression.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
12
|
+
decorator.expression.callee.name === "TsRestHandler");
|
|
13
|
+
if (!hasMatchingDecorator) {
|
|
14
|
+
context.report({
|
|
15
|
+
node,
|
|
16
|
+
messageId: "missingDecorator",
|
|
17
|
+
data: { name: symbolName },
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
decorators.forEach((decorator) => {
|
|
21
|
+
if (decorator.expression.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
|
22
|
+
decorator.expression.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
23
|
+
decorator.expression.callee.name === "TsRestHandler" &&
|
|
24
|
+
!decoratorImportedCorrectly) {
|
|
25
|
+
context.report({
|
|
26
|
+
node: decorator,
|
|
27
|
+
messageId: "decoratorNotFromPackage",
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function validateReturnStatement(context, node, symbolName, methodImportedCorrectly) {
|
|
33
|
+
const body = node.value?.body;
|
|
34
|
+
if (body?.type !== utils_1.AST_NODE_TYPES.BlockStatement ||
|
|
35
|
+
body.body.length !== 1 ||
|
|
36
|
+
body.body[0]?.type !== utils_1.AST_NODE_TYPES.ReturnStatement) {
|
|
37
|
+
context.report({
|
|
38
|
+
node,
|
|
39
|
+
messageId: "missingReturn",
|
|
40
|
+
data: { name: symbolName },
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const returnValueExpr = body.body[0].argument;
|
|
45
|
+
if (returnValueExpr?.type !== utils_1.AST_NODE_TYPES.CallExpression ||
|
|
46
|
+
returnValueExpr.callee.type !== utils_1.AST_NODE_TYPES.Identifier ||
|
|
47
|
+
returnValueExpr.callee.name !== "tsRestHandler") {
|
|
48
|
+
context.report({
|
|
49
|
+
node,
|
|
50
|
+
messageId: "missingReturn",
|
|
51
|
+
data: { name: symbolName },
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (returnValueExpr &&
|
|
55
|
+
returnValueExpr?.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
|
56
|
+
returnValueExpr.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
57
|
+
returnValueExpr.callee.name === "tsRestHandler" &&
|
|
58
|
+
!methodImportedCorrectly) {
|
|
59
|
+
context.report({
|
|
60
|
+
node: returnValueExpr,
|
|
61
|
+
messageId: "callNotFromPackage",
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
8
65
|
const rule = (0, createRule_1.createRule)({
|
|
9
66
|
name: "enforce-ts-rest-in-controllers",
|
|
10
67
|
defaultOptions: [],
|
|
@@ -54,73 +111,13 @@ const rule = (0, createRule_1.createRule)({
|
|
|
54
111
|
*/
|
|
55
112
|
MethodDefinition(node) {
|
|
56
113
|
const symbolName = (node.key.type === utils_1.AST_NODE_TYPES.Identifier && node.key.name) || "<unknown>";
|
|
57
|
-
// Ignore this rule for constructor and private methods
|
|
58
114
|
if (node.kind === "constructor" ||
|
|
59
115
|
node.accessibility === "private" ||
|
|
60
116
|
(node.key.type === utils_1.AST_NODE_TYPES.Identifier && node.key.name === "constructor")) {
|
|
61
117
|
return;
|
|
62
118
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const hasMatchingDecorator = decorators.some((decorator) => decorator.expression.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
|
66
|
-
decorator.expression.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
67
|
-
decorator.expression.callee.name === "TsRestHandler");
|
|
68
|
-
if (!hasMatchingDecorator) {
|
|
69
|
-
context.report({
|
|
70
|
-
node,
|
|
71
|
-
messageId: "missingDecorator",
|
|
72
|
-
data: {
|
|
73
|
-
name: symbolName,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
decorators.forEach((decorator) => {
|
|
78
|
-
if (decorator.expression.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
|
79
|
-
decorator.expression.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
80
|
-
decorator.expression.callee.name === "TsRestHandler" &&
|
|
81
|
-
!decoratorImportedCorrectly) {
|
|
82
|
-
context.report({
|
|
83
|
-
node: decorator,
|
|
84
|
-
messageId: "decoratorNotFromPackage",
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
// Check for returning the result of `tsRestHandler()` method (without any other statement present), and ensure it comes from `@ts-rest/nest` package
|
|
89
|
-
const body = node.value?.body;
|
|
90
|
-
if (body?.type !== utils_1.AST_NODE_TYPES.BlockStatement ||
|
|
91
|
-
body.body.length !== 1 ||
|
|
92
|
-
body.body[0]?.type !== utils_1.AST_NODE_TYPES.ReturnStatement) {
|
|
93
|
-
context.report({
|
|
94
|
-
node,
|
|
95
|
-
messageId: "missingReturn",
|
|
96
|
-
data: {
|
|
97
|
-
name: symbolName,
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
const returnValueExpr = body.body[0].argument;
|
|
103
|
-
if (returnValueExpr?.type !== utils_1.AST_NODE_TYPES.CallExpression ||
|
|
104
|
-
returnValueExpr.callee.type !== utils_1.AST_NODE_TYPES.Identifier ||
|
|
105
|
-
returnValueExpr.callee.name !== "tsRestHandler") {
|
|
106
|
-
context.report({
|
|
107
|
-
node,
|
|
108
|
-
messageId: "missingReturn",
|
|
109
|
-
data: {
|
|
110
|
-
name: symbolName,
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
if (returnValueExpr &&
|
|
115
|
-
returnValueExpr?.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
|
116
|
-
returnValueExpr.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
117
|
-
returnValueExpr.callee.name === "tsRestHandler" &&
|
|
118
|
-
!methodImportedCorrectly) {
|
|
119
|
-
context.report({
|
|
120
|
-
node: returnValueExpr,
|
|
121
|
-
messageId: "callNotFromPackage",
|
|
122
|
-
});
|
|
123
|
-
}
|
|
119
|
+
validateDecorators(context, node, symbolName, decoratorImportedCorrectly);
|
|
120
|
+
validateReturnStatement(context, node, symbolName, methodImportedCorrectly);
|
|
124
121
|
},
|
|
125
122
|
};
|
|
126
123
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../packages/eslint-plugin/src/lib/rules/enforce-ts-rest-in-controllers/index.ts"],"names":[],"mappings":";;AAAA;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../packages/eslint-plugin/src/lib/rules/enforce-ts-rest-in-controllers/index.ts"],"names":[],"mappings":";;AAAA;;GAEG;AACH,oDAAwF;AAExF,iDAA8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