forked from ivov/eslint-plugin-n8n-nodes-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunity-package-json-n8n-api-version-not-number.ts
63 lines (54 loc) · 1.55 KB
/
community-package-json-n8n-api-version-not-number.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
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.n8nNodesApiVersion` value in the `package.json` of a community package must be a number.",
recommended: "error",
},
schema: [],
messages: {
changeToNumber:
"Change the `n8n.n8nNodesApiVersion` value to number 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;
const apiVersion = getN8nNodesApiVersion(n8n);
if (!apiVersion) return;
if (!hasNumberValue(apiVersion)) {
context.report({
messageId: "changeToNumber",
node,
});
}
},
};
},
});
function getN8nNodesApiVersion(n8n: { ast: TSESTree.ObjectLiteralElement }) {
if (
n8n.ast.type === AST_NODE_TYPES.Property &&
n8n.ast.value.type === AST_NODE_TYPES.ObjectExpression
) {
return n8n.ast.value.properties.find(id.hasNodesApiVersion) ?? null;
}
return null;
}
function hasNumberValue(n8nNodesApiVersion: TSESTree.ObjectLiteralElement) {
return (
n8nNodesApiVersion.type === AST_NODE_TYPES.Property &&
n8nNodesApiVersion.value.type === AST_NODE_TYPES.Literal &&
typeof n8nNodesApiVersion.value.value === "number"
);
}