forked from ivov/eslint-plugin-n8n-nodes-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-param-operation-option-description-wrong-for-get-many.ts
77 lines (63 loc) · 2.06 KB
/
node-param-operation-option-description-wrong-for-get-many.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
76
77
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description:
"The property `description` in a Get Many option in an Operation node parameter must mention `many` instead of `all`.",
recommended: "error",
},
fixable: "code",
schema: [],
messages: {
changeToGetMany: "Change to '{{ newDescription }}' [autofixable]",
},
},
defaultOptions: [],
create(context) {
return {
ObjectExpression(node) {
if (!id.nodeParam.isOperation(node)) return;
const options = getters.nodeParam.getOptions(node);
if (!options) return;
// skip `options: [...].sort()`, see EditImage.node.ts
if (!Array.isArray(options.ast.value.elements)) return;
const getAllOption = options.ast.value.elements.find(
getters.nodeParam.getGetAllOption
);
if (!getAllOption) return;
const descriptionNode =
getAllOption.properties.find(isOptionDescription);
if (!descriptionNode) return;
const { value: description } = descriptionNode.value;
if (description.includes(" all ")) {
const [start, end] = description.split(" all ");
const newDescription = [start, "many", end].join(" ");
const fixed = utils.keyValue("description", newDescription);
context.report({
messageId: "changeToGetMany",
node: descriptionNode,
fix: (fixer) => fixer.replaceText(descriptionNode, fixed),
data: { newDescription },
});
}
},
};
},
});
function isOptionDescription(
property: TSESTree.ObjectLiteralElement
): property is TSESTree.Property & { value: { value: string } } {
return (
property.type === AST_NODE_TYPES.Property &&
property.computed === false &&
property.key.type === AST_NODE_TYPES.Identifier &&
property.key.name === "description" &&
property.value.type === AST_NODE_TYPES.Literal &&
typeof property.value.value === "string"
);
}