@arrirpc/eslint-plugin 0.43.2

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2024 Joshua Sosso
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Arri-RPC Eslint Plugin
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ # npm
7
+ npm i --save-dev @arrirpc/eslint-plugin
8
+
9
+ # pnpm
10
+ pnpm i --save-dev @arrirpc/eslint-plugin
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Use the default recommended configuration
16
+
17
+ ```json
18
+ {
19
+ "extends": ["plugin:@arrirpc/recommended"]
20
+ }
21
+ ```
22
+
23
+ ### Access the rules directly
24
+
25
+ ```json
26
+ {
27
+ "plugins": ["@arrirpc"],
28
+ "rules": {
29
+ // check to see if an ID has been assigned to root a.object() schemas
30
+ "@arrirpc/no-anonymous-object": 2,
31
+ // check to see if an ID has been assigned to a.enumerator() or a.stringEnum() schemas
32
+ "@arrirpc/no-anonymous-enumerator": 2,
33
+ // check to see if an ID has been assigned to a.discriminator() schemas
34
+ "@arrirpc/no-anonymous-discriminator": 2,
35
+ // check to see if an ID has been assigned to a.recursive() schemas
36
+ "@arrirpc/no-anonymous-recursive": 2
37
+ }
38
+ }
39
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,305 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function argHasIdKey(arg) {
6
+ if (arg.type === "ObjectExpression") {
7
+ for (const prop of arg.properties) {
8
+ if (prop.type === "Property") {
9
+ if (prop.key.type === "Identifier" && prop.key.name === "id") {
10
+ return true;
11
+ }
12
+ if (prop.key.type === "Literal" && prop.key.value?.toString() === "id") {
13
+ return true;
14
+ }
15
+ }
16
+ }
17
+ }
18
+ return false;
19
+ }
20
+ function isNestedInSchema(schemaTypes, context, log = false) {
21
+ const ancestors = context.getAncestors();
22
+ if (log) {
23
+ console.log(ancestors);
24
+ }
25
+ for (const node of ancestors) {
26
+ if (node.type !== "CallExpression" || node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a") {
27
+ continue;
28
+ }
29
+ if (node.callee.property.type !== "Identifier") {
30
+ continue;
31
+ }
32
+ const nodeName = node.callee.property.name;
33
+ if (schemaTypes.includes(nodeName)) {
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ }
39
+
40
+ const noAnonymousDiscriminator = {
41
+ meta: {
42
+ type: "suggestion"
43
+ },
44
+ create(context) {
45
+ return {
46
+ CallExpression(node) {
47
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
48
+ return;
49
+ }
50
+ const propName = node.callee.property.name;
51
+ if (propName !== "discriminator") {
52
+ return;
53
+ }
54
+ if (isNestedInSchema(["recursive"], context)) {
55
+ return;
56
+ }
57
+ if (node.arguments.length < 3) {
58
+ context.report({
59
+ message: "discriminator schemas must specify an id",
60
+ node
61
+ });
62
+ return;
63
+ }
64
+ const arg1 = node.arguments[0];
65
+ const arg2 = node.arguments[1];
66
+ const arg3 = node.arguments[2];
67
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0 && arg2.type === "Literal" && (arg2.value?.toString().length ?? 0) > 0) {
68
+ return;
69
+ }
70
+ if (argHasIdKey(arg3)) {
71
+ return;
72
+ }
73
+ context.report({
74
+ message: "discriminator schemas must specify an id",
75
+ node
76
+ });
77
+ }
78
+ };
79
+ }
80
+ };
81
+
82
+ const noAnonymousEnumerator = {
83
+ meta: {
84
+ type: "suggestion"
85
+ },
86
+ create(context) {
87
+ return {
88
+ CallExpression(node) {
89
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
90
+ return;
91
+ }
92
+ const propName = node.callee.property.name;
93
+ if (propName !== "enumerator" && propName !== "stringEnum") {
94
+ return;
95
+ }
96
+ if (node.arguments.length < 2) {
97
+ context.report({
98
+ message: "enum schemas must specify an id",
99
+ node
100
+ });
101
+ return;
102
+ }
103
+ const arg1 = node.arguments[0];
104
+ const arg2 = node.arguments[1];
105
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0) {
106
+ return;
107
+ }
108
+ if (argHasIdKey(arg2)) {
109
+ return;
110
+ }
111
+ context.report({
112
+ message: "enum schemas must specify an id",
113
+ node
114
+ });
115
+ }
116
+ };
117
+ }
118
+ };
119
+
120
+ const guardedSchemaTypes = ["object", "partial", "pick", "extend"];
121
+ const defaultMessage = "root object schemas should specify an id";
122
+ const noAnonymousObject = {
123
+ meta: {
124
+ type: "suggestion"
125
+ },
126
+ create(context) {
127
+ return {
128
+ CallExpression: (node) => {
129
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
130
+ return;
131
+ }
132
+ const nodeName = node.callee.property.name;
133
+ if (!guardedSchemaTypes.includes(nodeName)) {
134
+ return;
135
+ }
136
+ switch (nodeName) {
137
+ case "pick":
138
+ handlePick(node, context);
139
+ return;
140
+ case "partial":
141
+ handlePartial(node, context);
142
+ return;
143
+ case "extend":
144
+ handleExtend(node, context);
145
+ return;
146
+ case "object":
147
+ handleObject(node, context);
148
+ }
149
+ }
150
+ };
151
+ }
152
+ };
153
+ function handleObject(node, context) {
154
+ if (!isRootObjectSchema(context)) {
155
+ return;
156
+ }
157
+ if (node.arguments.length < 2) {
158
+ context.report({
159
+ message: defaultMessage,
160
+ node
161
+ });
162
+ return;
163
+ }
164
+ const arg1 = node.arguments[0];
165
+ const arg2 = node.arguments[1];
166
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0) {
167
+ return;
168
+ }
169
+ if (argHasIdKey(arg2)) {
170
+ return;
171
+ }
172
+ context.report({
173
+ message: defaultMessage,
174
+ node
175
+ });
176
+ }
177
+ function handlePartial(node, context) {
178
+ if (!isRootObjectSchema(context)) {
179
+ return;
180
+ }
181
+ if (node.arguments.length < 2) {
182
+ context.report({
183
+ message: defaultMessage,
184
+ node
185
+ });
186
+ return;
187
+ }
188
+ if (argHasIdKey(node.arguments[1])) {
189
+ return;
190
+ }
191
+ context.report({
192
+ message: defaultMessage,
193
+ node
194
+ });
195
+ }
196
+ function handlePick(node, context) {
197
+ if (!isRootObjectSchema(context)) {
198
+ return;
199
+ }
200
+ if (node.arguments.length < 3) {
201
+ context.report({
202
+ message: defaultMessage,
203
+ node
204
+ });
205
+ return;
206
+ }
207
+ if (argHasIdKey(node.arguments[2])) {
208
+ return;
209
+ }
210
+ context.report({
211
+ message: defaultMessage,
212
+ node
213
+ });
214
+ }
215
+ function handleExtend(node, context) {
216
+ if (!isRootObjectSchema(context)) {
217
+ return;
218
+ }
219
+ if (node.arguments.length < 3) {
220
+ context.report({
221
+ message: defaultMessage,
222
+ node
223
+ });
224
+ return;
225
+ }
226
+ if (argHasIdKey(node.arguments[2])) {
227
+ return;
228
+ }
229
+ context.report({
230
+ message: defaultMessage,
231
+ node
232
+ });
233
+ }
234
+ function isRootObjectSchema(context, log = false) {
235
+ return !isNestedInSchema(
236
+ [...guardedSchemaTypes, "discriminator", "recursive"],
237
+ context,
238
+ log
239
+ );
240
+ }
241
+
242
+ const noAnonymousRecursive = {
243
+ meta: {
244
+ type: "suggestion"
245
+ },
246
+ create(context) {
247
+ return {
248
+ CallExpression(node) {
249
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
250
+ return;
251
+ }
252
+ const propName = node.callee.property.name;
253
+ if (propName !== "recursive") {
254
+ return;
255
+ }
256
+ if (node.arguments.length < 2) {
257
+ context.report({
258
+ message: "recursive schemas must specify an id",
259
+ node
260
+ });
261
+ return;
262
+ }
263
+ const arg1 = node.arguments[0];
264
+ const arg2 = node.arguments[1];
265
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0) {
266
+ return;
267
+ }
268
+ if (argHasIdKey(arg2)) {
269
+ return;
270
+ }
271
+ context.report({
272
+ message: "recursive schemas must specify an id",
273
+ node
274
+ });
275
+ }
276
+ };
277
+ }
278
+ };
279
+
280
+ const meta = {
281
+ name: "@arrirpc"
282
+ };
283
+ const configs = {
284
+ recommended: {
285
+ plugins: ["@arrirpc"],
286
+ rules: {
287
+ "@arrirpc/no-anonymous-object": "error",
288
+ "@arrirpc/no-anonymous-recursive": "error",
289
+ "@arrirpc/no-anonymous-discriminator": "error",
290
+ "@arrirpc/no-anonymous-enumerator": "error"
291
+ }
292
+ }
293
+ };
294
+ const rules = {
295
+ "no-anonymous-object": noAnonymousObject,
296
+ "no-anonymous-recursive": noAnonymousRecursive,
297
+ "no-anonymous-discriminator": noAnonymousDiscriminator,
298
+ "no-anonymous-enumerator": noAnonymousEnumerator
299
+ };
300
+ const index = { meta, configs, rules };
301
+
302
+ exports.configs = configs;
303
+ exports.default = index;
304
+ exports.meta = meta;
305
+ exports.rules = rules;
package/dist/index.mjs ADDED
@@ -0,0 +1,298 @@
1
+ function argHasIdKey(arg) {
2
+ if (arg.type === "ObjectExpression") {
3
+ for (const prop of arg.properties) {
4
+ if (prop.type === "Property") {
5
+ if (prop.key.type === "Identifier" && prop.key.name === "id") {
6
+ return true;
7
+ }
8
+ if (prop.key.type === "Literal" && prop.key.value?.toString() === "id") {
9
+ return true;
10
+ }
11
+ }
12
+ }
13
+ }
14
+ return false;
15
+ }
16
+ function isNestedInSchema(schemaTypes, context, log = false) {
17
+ const ancestors = context.getAncestors();
18
+ if (log) {
19
+ console.log(ancestors);
20
+ }
21
+ for (const node of ancestors) {
22
+ if (node.type !== "CallExpression" || node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a") {
23
+ continue;
24
+ }
25
+ if (node.callee.property.type !== "Identifier") {
26
+ continue;
27
+ }
28
+ const nodeName = node.callee.property.name;
29
+ if (schemaTypes.includes(nodeName)) {
30
+ return true;
31
+ }
32
+ }
33
+ return false;
34
+ }
35
+
36
+ const noAnonymousDiscriminator = {
37
+ meta: {
38
+ type: "suggestion"
39
+ },
40
+ create(context) {
41
+ return {
42
+ CallExpression(node) {
43
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
44
+ return;
45
+ }
46
+ const propName = node.callee.property.name;
47
+ if (propName !== "discriminator") {
48
+ return;
49
+ }
50
+ if (isNestedInSchema(["recursive"], context)) {
51
+ return;
52
+ }
53
+ if (node.arguments.length < 3) {
54
+ context.report({
55
+ message: "discriminator schemas must specify an id",
56
+ node
57
+ });
58
+ return;
59
+ }
60
+ const arg1 = node.arguments[0];
61
+ const arg2 = node.arguments[1];
62
+ const arg3 = node.arguments[2];
63
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0 && arg2.type === "Literal" && (arg2.value?.toString().length ?? 0) > 0) {
64
+ return;
65
+ }
66
+ if (argHasIdKey(arg3)) {
67
+ return;
68
+ }
69
+ context.report({
70
+ message: "discriminator schemas must specify an id",
71
+ node
72
+ });
73
+ }
74
+ };
75
+ }
76
+ };
77
+
78
+ const noAnonymousEnumerator = {
79
+ meta: {
80
+ type: "suggestion"
81
+ },
82
+ create(context) {
83
+ return {
84
+ CallExpression(node) {
85
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
86
+ return;
87
+ }
88
+ const propName = node.callee.property.name;
89
+ if (propName !== "enumerator" && propName !== "stringEnum") {
90
+ return;
91
+ }
92
+ if (node.arguments.length < 2) {
93
+ context.report({
94
+ message: "enum schemas must specify an id",
95
+ node
96
+ });
97
+ return;
98
+ }
99
+ const arg1 = node.arguments[0];
100
+ const arg2 = node.arguments[1];
101
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0) {
102
+ return;
103
+ }
104
+ if (argHasIdKey(arg2)) {
105
+ return;
106
+ }
107
+ context.report({
108
+ message: "enum schemas must specify an id",
109
+ node
110
+ });
111
+ }
112
+ };
113
+ }
114
+ };
115
+
116
+ const guardedSchemaTypes = ["object", "partial", "pick", "extend"];
117
+ const defaultMessage = "root object schemas should specify an id";
118
+ const noAnonymousObject = {
119
+ meta: {
120
+ type: "suggestion"
121
+ },
122
+ create(context) {
123
+ return {
124
+ CallExpression: (node) => {
125
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
126
+ return;
127
+ }
128
+ const nodeName = node.callee.property.name;
129
+ if (!guardedSchemaTypes.includes(nodeName)) {
130
+ return;
131
+ }
132
+ switch (nodeName) {
133
+ case "pick":
134
+ handlePick(node, context);
135
+ return;
136
+ case "partial":
137
+ handlePartial(node, context);
138
+ return;
139
+ case "extend":
140
+ handleExtend(node, context);
141
+ return;
142
+ case "object":
143
+ handleObject(node, context);
144
+ }
145
+ }
146
+ };
147
+ }
148
+ };
149
+ function handleObject(node, context) {
150
+ if (!isRootObjectSchema(context)) {
151
+ return;
152
+ }
153
+ if (node.arguments.length < 2) {
154
+ context.report({
155
+ message: defaultMessage,
156
+ node
157
+ });
158
+ return;
159
+ }
160
+ const arg1 = node.arguments[0];
161
+ const arg2 = node.arguments[1];
162
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0) {
163
+ return;
164
+ }
165
+ if (argHasIdKey(arg2)) {
166
+ return;
167
+ }
168
+ context.report({
169
+ message: defaultMessage,
170
+ node
171
+ });
172
+ }
173
+ function handlePartial(node, context) {
174
+ if (!isRootObjectSchema(context)) {
175
+ return;
176
+ }
177
+ if (node.arguments.length < 2) {
178
+ context.report({
179
+ message: defaultMessage,
180
+ node
181
+ });
182
+ return;
183
+ }
184
+ if (argHasIdKey(node.arguments[1])) {
185
+ return;
186
+ }
187
+ context.report({
188
+ message: defaultMessage,
189
+ node
190
+ });
191
+ }
192
+ function handlePick(node, context) {
193
+ if (!isRootObjectSchema(context)) {
194
+ return;
195
+ }
196
+ if (node.arguments.length < 3) {
197
+ context.report({
198
+ message: defaultMessage,
199
+ node
200
+ });
201
+ return;
202
+ }
203
+ if (argHasIdKey(node.arguments[2])) {
204
+ return;
205
+ }
206
+ context.report({
207
+ message: defaultMessage,
208
+ node
209
+ });
210
+ }
211
+ function handleExtend(node, context) {
212
+ if (!isRootObjectSchema(context)) {
213
+ return;
214
+ }
215
+ if (node.arguments.length < 3) {
216
+ context.report({
217
+ message: defaultMessage,
218
+ node
219
+ });
220
+ return;
221
+ }
222
+ if (argHasIdKey(node.arguments[2])) {
223
+ return;
224
+ }
225
+ context.report({
226
+ message: defaultMessage,
227
+ node
228
+ });
229
+ }
230
+ function isRootObjectSchema(context, log = false) {
231
+ return !isNestedInSchema(
232
+ [...guardedSchemaTypes, "discriminator", "recursive"],
233
+ context,
234
+ log
235
+ );
236
+ }
237
+
238
+ const noAnonymousRecursive = {
239
+ meta: {
240
+ type: "suggestion"
241
+ },
242
+ create(context) {
243
+ return {
244
+ CallExpression(node) {
245
+ if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "Identifier" || node.callee.object.name !== "a" || node.callee.property.type !== "Identifier") {
246
+ return;
247
+ }
248
+ const propName = node.callee.property.name;
249
+ if (propName !== "recursive") {
250
+ return;
251
+ }
252
+ if (node.arguments.length < 2) {
253
+ context.report({
254
+ message: "recursive schemas must specify an id",
255
+ node
256
+ });
257
+ return;
258
+ }
259
+ const arg1 = node.arguments[0];
260
+ const arg2 = node.arguments[1];
261
+ if (arg1.type === "Literal" && (arg1.value?.toString().length ?? 0) > 0) {
262
+ return;
263
+ }
264
+ if (argHasIdKey(arg2)) {
265
+ return;
266
+ }
267
+ context.report({
268
+ message: "recursive schemas must specify an id",
269
+ node
270
+ });
271
+ }
272
+ };
273
+ }
274
+ };
275
+
276
+ const meta = {
277
+ name: "@arrirpc"
278
+ };
279
+ const configs = {
280
+ recommended: {
281
+ plugins: ["@arrirpc"],
282
+ rules: {
283
+ "@arrirpc/no-anonymous-object": "error",
284
+ "@arrirpc/no-anonymous-recursive": "error",
285
+ "@arrirpc/no-anonymous-discriminator": "error",
286
+ "@arrirpc/no-anonymous-enumerator": "error"
287
+ }
288
+ }
289
+ };
290
+ const rules = {
291
+ "no-anonymous-object": noAnonymousObject,
292
+ "no-anonymous-recursive": noAnonymousRecursive,
293
+ "no-anonymous-discriminator": noAnonymousDiscriminator,
294
+ "no-anonymous-enumerator": noAnonymousEnumerator
295
+ };
296
+ const index = { meta, configs, rules };
297
+
298
+ export { configs, index as default, meta, rules };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@arrirpc/eslint-plugin",
3
+ "version": "0.43.2",
4
+ "license": "MIT",
5
+ "author": {
6
+ "name": "joshmossas",
7
+ "url": "https://github.com/joshmossas"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/modiimedia/arri/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/modiimedia/arri.git",
15
+ "directory": "packages/eslint-plugin"
16
+ },
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.mjs",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {},
23
+ "devDependencies": {
24
+ "@types/eslint": "^8.56.10"
25
+ },
26
+ "peerDependencies": {
27
+ "eslint": ">=8.0.0"
28
+ }
29
+ }