-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommunity-package-json-n8n-nodes-empty.ts
62 lines (54 loc) · 1.45 KB
/
community-package-json-n8n-nodes-empty.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
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
import { utils } from "../ast/utils";
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description:
"The `n8n.nodes` value in the `package.json` of a community package must contain at least one filepath.",
recommended: "strict",
},
schema: [],
messages: {
addOneFilepath:
"Enter at least one filepath in `n8n.nodes` in package.json",
},
},
defaultOptions: [],
create(context) {
return {
ObjectExpression(node) {
if (!id.isCommunityPackageJson(context.getFilename(), node)) return;
const n8n = getters.communityPackageJson.getN8n(node);
if (!n8n) return;
if (!hasAtLeastOneFilepath(n8n)) {
context.report({
messageId: "addOneFilepath",
node,
});
}
},
};
},
});
function hasAtLeastOneFilepath(n8n: { ast: TSESTree.ObjectLiteralElement }) {
if (
n8n.ast.type === AST_NODE_TYPES.Property &&
n8n.ast.value.type === AST_NODE_TYPES.ObjectExpression
) {
const nodes = n8n.ast.value.properties.find(id.hasNodesLiteral);
if (!nodes) return false;
if (
nodes.type === AST_NODE_TYPES.Property &&
nodes.value.type === AST_NODE_TYPES.ArrayExpression
) {
return nodes.value.elements.some(
(element) => element?.type === AST_NODE_TYPES.Literal
);
}
}
return null;
}