-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathnaming-convention.js
More file actions
108 lines (90 loc) · 2.51 KB
/
Copy pathnaming-convention.js
File metadata and controls
108 lines (90 loc) · 2.51 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { pascalCase } from "change-case";
const REGEXP_RESOURCE_NAME = /^(.+?)Resource$/;
export default {
meta: {
type: "problem",
schema: [],
messages: {
selector:
"{{resourceName}}Resource#$select(): {{typeName}} is not {{expected}}",
property:
"{{resourceName}}Resource#{{keyName}}: {{typeName}} is not {{expected}}",
},
},
create(context) {
let resourceName;
return {
TSInterfaceDeclaration(node) {
if (node.id.type !== "Identifier") {
return;
}
const match = node.id.name.match(REGEXP_RESOURCE_NAME);
resourceName = match?.[1];
},
"TSInterfaceDeclaration:exit"() {
resourceName = undefined;
},
TSMethodSignature(node) {
if (!resourceName) {
return;
}
if (node.key.type !== "Identifier" || node.key.name !== "$select") {
return;
}
if (
node.returnType.type !== "TSTypeAnnotation" ||
node.returnType.typeAnnotation.type !== "TSTypeReference" ||
node.returnType.typeAnnotation.typeName.type !== "Identifier"
) {
return;
}
const typeName = node.returnType.typeAnnotation.typeName.name;
const expected = resourceName + "$SelectResource";
if (typeName !== expected) {
context.report({
node,
messageId: "selector",
data: {
resourceName,
typeName,
expected,
},
});
}
},
TSPropertySignature(node) {
if (!resourceName) {
return;
}
if (node.key.type !== "Identifier") {
return;
}
const keyName = node.key.name;
if (
node.typeAnnotation.type !== "TSTypeAnnotation" ||
node.typeAnnotation.typeAnnotation.type !== "TSTypeReference" ||
node.typeAnnotation.typeAnnotation.typeName.type !== "Identifier"
) {
return;
}
const typeName = node.typeAnnotation.typeAnnotation.typeName.name;
if (typeName === "Method") {
return;
}
const expected = resourceName + pascalCase(keyName) + "Resource";
if (typeName !== expected) {
context.report({
node,
messageId: "property",
data: {
resourceName,
keyName,
typeName,
expected,
},
});
}
},
};
},
};