forked from ivov/eslint-plugin-n8n-nodes-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-execute-block-double-assertion-for-items.ts
75 lines (68 loc) · 2.11 KB
/
node-execute-block-double-assertion-for-items.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { utils } from "../ast/utils";
import { getters } from "../ast/getters";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description:
"In the `execute()` method there is no need to double assert the type of `items.length`.",
recommended: "error",
},
fixable: "code",
schema: [],
messages: {
removeDoubleAssertion: "Remove double assertion [autofixable]",
},
},
defaultOptions: [],
create(context) {
return {
MethodDefinition(node) {
if (!utils.isNodeFile(context.getFilename())) return;
const executeContent = getters.nodeExecuteBlock.getExecuteContent(node);
if (!executeContent) return;
const init = getDoublyAssertedDeclarationInit(executeContent);
if (init) {
context.report({
messageId: "removeDoubleAssertion",
node: init,
fix: (fixer) => fixer.replaceText(init, "items.length"),
});
}
},
};
},
});
// TODO: Refactor
function getDoublyAssertedDeclarationInit(
executeMethod: TSESTree.BlockStatement
) {
for (const node of executeMethod.body) {
if (node.type === AST_NODE_TYPES.VariableDeclaration) {
for (const declaration of node.declarations) {
if (!declaration.init) continue;
if (
declaration.init.type === AST_NODE_TYPES.TSAsExpression &&
declaration.init.typeAnnotation.type ===
AST_NODE_TYPES.TSNumberKeyword &&
declaration.init.expression.type === AST_NODE_TYPES.TSAsExpression &&
declaration.init.expression.typeAnnotation.type ===
AST_NODE_TYPES.TSUnknownKeyword &&
declaration.init.expression.expression.type ===
AST_NODE_TYPES.MemberExpression &&
declaration.init.expression.expression.object.type ===
AST_NODE_TYPES.Identifier &&
declaration.init.expression.expression.object.name === "items" &&
declaration.init.expression.expression.property.type ===
AST_NODE_TYPES.Identifier &&
declaration.init.expression.expression.property.name === "length"
) {
return declaration.init;
}
}
}
}
return null;
}