forked from ivov/eslint-plugin-n8n-nodes-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-class-description-missing-subtitle.ts
83 lines (67 loc) · 2.03 KB
/
node-class-description-missing-subtitle.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
78
79
80
81
82
83
import { NODE_CLASS_DESCRIPTION_SUBTITLE } from "../constants";
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
import { TSESTree } from "@typescript-eslint/utils";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description: "`subtitle` in node class description must be present.",
recommended: "error",
},
fixable: "code",
schema: [],
messages: {
addSubtitle: `Add subtitle: '${NODE_CLASS_DESCRIPTION_SUBTITLE}' [autofixable]`,
},
},
defaultOptions: [],
create(context) {
return {
ObjectExpression(node) {
if (!id.isNodeClassDescription(node)) return;
const allDisplayNames = getAllDisplayNames(node);
if (!allDisplayNames) return;
// "Resource" and "Operation" required for subtitle
const hasNoSubtitleComponents = !allDisplayNames.every((dn) =>
["Resource", "Operation"].includes(dn)
);
if (hasNoSubtitleComponents) return;
if (!getters.nodeClassDescription.getSubtitle(node)) {
const version =
getters.nodeClassDescription.getVersion(node) ??
getters.nodeClassDescription.getDefaultVersion(node); // legacy node
if (!version) return;
const { range, indentation } = utils.getInsertionArgs(version);
context.report({
messageId: "addSubtitle",
node,
fix: (fixer) =>
fixer.insertTextAfterRange(
range,
`\n${indentation}subtitle: '${NODE_CLASS_DESCRIPTION_SUBTITLE}',`
),
});
}
},
};
},
});
function getAllDisplayNames(nodeParam: TSESTree.ObjectExpression) {
const properties = nodeParam.properties.find(
id.nodeClassDescription.isProperties
);
if (!properties) return null;
const displayNames = properties.value.elements.reduce<string[]>(
(acc, element) => {
const found = element.properties?.find(id.nodeParam.isDisplayName);
if (found) acc.push(found.value.value);
return acc;
},
[]
);
if (!displayNames.length) return null;
return displayNames;
}