eslint-plugin-zod-utils 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.cjs +161 -14
  2. package/dist/index.js +161 -14
  3. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -37,6 +37,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
37
37
  "boolean",
38
38
  "cidrv4",
39
39
  "cidrv6",
40
+ "codec",
40
41
  "cuid",
41
42
  "cuid2",
42
43
  "custom",
@@ -46,35 +47,65 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
46
47
  "email",
47
48
  "emoji",
48
49
  "enum",
50
+ "exactOptional",
49
51
  "file",
52
+ "float32",
53
+ "float64",
50
54
  "function",
55
+ "fromJSONSchema",
56
+ "guid",
57
+ "hash",
58
+ "hex",
59
+ "hostname",
60
+ "httpUrl",
51
61
  "instanceof",
62
+ "int",
63
+ "int32",
64
+ "int64",
52
65
  "intersection",
66
+ "invertCodec",
53
67
  "ipv4",
54
68
  "ipv6",
69
+ "json",
55
70
  "jwt",
71
+ "keyof",
56
72
  "ksuid",
57
73
  "lazy",
58
74
  "literal",
75
+ "looseObject",
76
+ "looseRecord",
77
+ "mac",
59
78
  "map",
60
79
  "nan",
61
80
  "nanoid",
62
81
  "nativeEnum",
63
82
  "never",
83
+ "nonoptional",
64
84
  "null",
65
85
  "nullable",
86
+ "nullish",
66
87
  "number",
67
88
  "object",
68
89
  "optional",
90
+ "partialRecord",
91
+ "pipe",
92
+ "prefault",
69
93
  "preprocess",
70
94
  "promise",
95
+ "readonly",
71
96
  "record",
72
97
  "set",
73
98
  "string",
99
+ "stringFormat",
74
100
  "stringbool",
101
+ "strictObject",
102
+ "success",
75
103
  "symbol",
76
104
  "templateLiteral",
105
+ "transform",
77
106
  "tuple",
107
+ "uint32",
108
+ "uint64",
78
109
  "undefined",
79
110
  "union",
80
111
  "unknown",
@@ -85,9 +116,21 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
85
116
  "uuidv6",
86
117
  "uuidv7",
87
118
  "void",
119
+ "xor",
88
120
  "xid"
89
121
  ]);
122
+ var ZOD_DIRECT_COMBINATOR_IMPORTS = /* @__PURE__ */ new Set([
123
+ "catchall",
124
+ "extend",
125
+ "merge",
126
+ "omit",
127
+ "partial",
128
+ "pick",
129
+ "required",
130
+ "safeExtend"
131
+ ]);
90
132
  var ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["z", "coerce", "iso"]);
133
+ var ZOD_ISO_FACTORY_IMPORTS = /* @__PURE__ */ new Set(["date", "datetime", "duration", "time"]);
91
134
  var ZOD_EXECUTION_METHODS = /* @__PURE__ */ new Set([
92
135
  "parse",
93
136
  "parseAsync",
@@ -130,6 +173,15 @@ function findVariable(scope, name) {
130
173
  }
131
174
  return null;
132
175
  }
176
+ function getStaticMemberName(node) {
177
+ if (!node.computed && node.property.type === "Identifier") {
178
+ return node.property.name;
179
+ }
180
+ if (node.computed && node.property.type === "Literal" && typeof node.property.value === "string") {
181
+ return node.property.value;
182
+ }
183
+ return null;
184
+ }
133
185
  function getRootIdentifier(node) {
134
186
  if (!node) {
135
187
  return null;
@@ -161,8 +213,8 @@ function getRootMethodName(node) {
161
213
  continue;
162
214
  }
163
215
  if (current.type === "MemberExpression") {
164
- if (current.object.type === "Identifier" && !current.computed && current.property.type === "Identifier") {
165
- rootMethodName = current.property.name;
216
+ if (current.object.type === "Identifier") {
217
+ rootMethodName = getStaticMemberName(current);
166
218
  }
167
219
  current = current.object;
168
220
  continue;
@@ -170,32 +222,81 @@ function getRootMethodName(node) {
170
222
  return rootMethodName;
171
223
  }
172
224
  }
225
+ function getMemberPathFromRoot(node) {
226
+ const memberPath = [];
227
+ let current = node;
228
+ while (true) {
229
+ if (current.type === "CallExpression") {
230
+ current = current.callee;
231
+ continue;
232
+ }
233
+ if (current.type === "ChainExpression") {
234
+ current = current.expression;
235
+ continue;
236
+ }
237
+ if (current.type === "TSAsExpression" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion") {
238
+ current = current.expression;
239
+ continue;
240
+ }
241
+ if (current.type === "MemberExpression") {
242
+ const memberName = getStaticMemberName(current);
243
+ if (memberName === null) {
244
+ return null;
245
+ }
246
+ memberPath.unshift(memberName);
247
+ current = current.object;
248
+ continue;
249
+ }
250
+ return memberPath;
251
+ }
252
+ }
173
253
  function getCallMethodName(node) {
174
254
  const { callee } = node;
175
- if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier") {
176
- return callee.property.name;
255
+ if (callee.type === "MemberExpression" && getStaticMemberName(callee) !== null) {
256
+ return getStaticMemberName(callee);
177
257
  }
178
258
  return null;
179
259
  }
180
- function isZodImportSpecifier(node) {
260
+ function getZodImportKind(node) {
181
261
  if (node.type !== "ImportDefaultSpecifier" && node.type !== "ImportNamespaceSpecifier" && node.type !== "ImportSpecifier") {
182
- return false;
262
+ return null;
183
263
  }
184
264
  const declaration = node.parent;
185
265
  if (declaration?.type !== "ImportDeclaration") {
186
- return false;
266
+ return null;
187
267
  }
188
268
  if (declaration.source.value !== "zod") {
189
- return false;
269
+ return null;
190
270
  }
191
271
  if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") {
192
- return true;
272
+ return { importedName: "z", type: "namespace" };
193
273
  }
194
274
  if (node.importKind === "type" || declaration.importKind === "type") {
195
- return false;
275
+ return null;
196
276
  }
197
277
  const importedName = node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
198
- return ZOD_NAMESPACE_IMPORTS.has(importedName) || ZOD_FACTORY_IMPORTS.has(importedName);
278
+ if (ZOD_NAMESPACE_IMPORTS.has(importedName)) {
279
+ return { importedName, type: "namespace" };
280
+ }
281
+ if (ZOD_FACTORY_IMPORTS.has(importedName) || ZOD_DIRECT_COMBINATOR_IMPORTS.has(importedName)) {
282
+ return { type: "factory" };
283
+ }
284
+ return null;
285
+ }
286
+ function isZodSchemaNamespaceCall(importName, memberPath) {
287
+ if (importName === "coerce") {
288
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
289
+ }
290
+ if (importName === "iso") {
291
+ return memberPath[0] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[0]);
292
+ }
293
+ if (memberPath[0] === "coerce") {
294
+ return memberPath[1] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[1]);
295
+ }
296
+ if (memberPath[0] === "iso") {
297
+ return memberPath[1] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[1]);
298
+ }
299
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
199
300
  }
200
301
  function hasFullTypeInformation(services) {
201
302
  return services.program !== null;
@@ -262,7 +363,20 @@ var noInlineZodSchema = import_utils.ESLintUtils.RuleCreator(
262
363
  }
263
364
  const scope = sourceCode.getScope(root);
264
365
  const variable = findVariable(scope, root.name);
265
- return variable?.defs.some((definition) => isZodImportSpecifier(definition.node)) ?? false;
366
+ return variable?.defs.some((definition) => {
367
+ const importKind = getZodImportKind(definition.node);
368
+ if (importKind?.type === "factory") {
369
+ return true;
370
+ }
371
+ if (importKind?.type !== "namespace") {
372
+ return false;
373
+ }
374
+ const memberPath = getMemberPathFromRoot(node.callee);
375
+ if (memberPath === null || memberPath.length === 0) {
376
+ return false;
377
+ }
378
+ return isZodSchemaNamespaceCall(importKind.importedName, memberPath);
379
+ }) ?? false;
266
380
  }
267
381
  function isTypedZodSchemaCombinatorCall(node) {
268
382
  const rootMethodName = getRootMethodName(node);
@@ -282,6 +396,37 @@ var noInlineZodSchema = import_utils.ESLintUtils.RuleCreator(
282
396
  function isSchemaCreationCall(node) {
283
397
  return isZodSchemaCall(node) || isTypedZodSchemaCombinatorCall(node);
284
398
  }
399
+ function isInModuleInitializationPath(node) {
400
+ let current = node;
401
+ while (current) {
402
+ if (current.type === "Program") {
403
+ return true;
404
+ }
405
+ if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") {
406
+ return false;
407
+ }
408
+ if (current.type === "PropertyDefinition" && !current.static) {
409
+ return false;
410
+ }
411
+ current = current.parent;
412
+ }
413
+ return false;
414
+ }
415
+ function isFunctionExecutedDuringModuleInitialization(node) {
416
+ if (node.type === "FunctionDeclaration") {
417
+ return false;
418
+ }
419
+ const parent = node.parent;
420
+ if (parent?.type === "CallExpression") {
421
+ if (parent.callee === node) {
422
+ return isInModuleInitializationPath(parent);
423
+ }
424
+ if (parent.arguments.includes(node)) {
425
+ return isInModuleInitializationPath(parent);
426
+ }
427
+ }
428
+ return false;
429
+ }
285
430
  function hasSchemaCreationCallAncestor(node) {
286
431
  let current = node.parent;
287
432
  while (current) {
@@ -296,7 +441,9 @@ var noInlineZodSchema = import_utils.ESLintUtils.RuleCreator(
296
441
  let current = node.parent;
297
442
  while (current) {
298
443
  if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") {
299
- return true;
444
+ if (!isFunctionExecutedDuringModuleInitialization(current)) {
445
+ return true;
446
+ }
300
447
  }
301
448
  if (current.type === "PropertyDefinition" && !current.static) {
302
449
  return true;
@@ -333,7 +480,7 @@ var rules = {
333
480
  var plugin = {
334
481
  meta: {
335
482
  name: "eslint-plugin-zod-utils",
336
- version: "1.0.3"
483
+ version: "1.0.5"
337
484
  },
338
485
  rules,
339
486
  configs: {}
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
9
9
  "boolean",
10
10
  "cidrv4",
11
11
  "cidrv6",
12
+ "codec",
12
13
  "cuid",
13
14
  "cuid2",
14
15
  "custom",
@@ -18,35 +19,65 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
18
19
  "email",
19
20
  "emoji",
20
21
  "enum",
22
+ "exactOptional",
21
23
  "file",
24
+ "float32",
25
+ "float64",
22
26
  "function",
27
+ "fromJSONSchema",
28
+ "guid",
29
+ "hash",
30
+ "hex",
31
+ "hostname",
32
+ "httpUrl",
23
33
  "instanceof",
34
+ "int",
35
+ "int32",
36
+ "int64",
24
37
  "intersection",
38
+ "invertCodec",
25
39
  "ipv4",
26
40
  "ipv6",
41
+ "json",
27
42
  "jwt",
43
+ "keyof",
28
44
  "ksuid",
29
45
  "lazy",
30
46
  "literal",
47
+ "looseObject",
48
+ "looseRecord",
49
+ "mac",
31
50
  "map",
32
51
  "nan",
33
52
  "nanoid",
34
53
  "nativeEnum",
35
54
  "never",
55
+ "nonoptional",
36
56
  "null",
37
57
  "nullable",
58
+ "nullish",
38
59
  "number",
39
60
  "object",
40
61
  "optional",
62
+ "partialRecord",
63
+ "pipe",
64
+ "prefault",
41
65
  "preprocess",
42
66
  "promise",
67
+ "readonly",
43
68
  "record",
44
69
  "set",
45
70
  "string",
71
+ "stringFormat",
46
72
  "stringbool",
73
+ "strictObject",
74
+ "success",
47
75
  "symbol",
48
76
  "templateLiteral",
77
+ "transform",
49
78
  "tuple",
79
+ "uint32",
80
+ "uint64",
50
81
  "undefined",
51
82
  "union",
52
83
  "unknown",
@@ -57,9 +88,21 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
57
88
  "uuidv6",
58
89
  "uuidv7",
59
90
  "void",
91
+ "xor",
60
92
  "xid"
61
93
  ]);
94
+ var ZOD_DIRECT_COMBINATOR_IMPORTS = /* @__PURE__ */ new Set([
95
+ "catchall",
96
+ "extend",
97
+ "merge",
98
+ "omit",
99
+ "partial",
100
+ "pick",
101
+ "required",
102
+ "safeExtend"
103
+ ]);
62
104
  var ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["z", "coerce", "iso"]);
105
+ var ZOD_ISO_FACTORY_IMPORTS = /* @__PURE__ */ new Set(["date", "datetime", "duration", "time"]);
63
106
  var ZOD_EXECUTION_METHODS = /* @__PURE__ */ new Set([
64
107
  "parse",
65
108
  "parseAsync",
@@ -102,6 +145,15 @@ function findVariable(scope, name) {
102
145
  }
103
146
  return null;
104
147
  }
148
+ function getStaticMemberName(node) {
149
+ if (!node.computed && node.property.type === "Identifier") {
150
+ return node.property.name;
151
+ }
152
+ if (node.computed && node.property.type === "Literal" && typeof node.property.value === "string") {
153
+ return node.property.value;
154
+ }
155
+ return null;
156
+ }
105
157
  function getRootIdentifier(node) {
106
158
  if (!node) {
107
159
  return null;
@@ -133,8 +185,8 @@ function getRootMethodName(node) {
133
185
  continue;
134
186
  }
135
187
  if (current.type === "MemberExpression") {
136
- if (current.object.type === "Identifier" && !current.computed && current.property.type === "Identifier") {
137
- rootMethodName = current.property.name;
188
+ if (current.object.type === "Identifier") {
189
+ rootMethodName = getStaticMemberName(current);
138
190
  }
139
191
  current = current.object;
140
192
  continue;
@@ -142,32 +194,81 @@ function getRootMethodName(node) {
142
194
  return rootMethodName;
143
195
  }
144
196
  }
197
+ function getMemberPathFromRoot(node) {
198
+ const memberPath = [];
199
+ let current = node;
200
+ while (true) {
201
+ if (current.type === "CallExpression") {
202
+ current = current.callee;
203
+ continue;
204
+ }
205
+ if (current.type === "ChainExpression") {
206
+ current = current.expression;
207
+ continue;
208
+ }
209
+ if (current.type === "TSAsExpression" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion") {
210
+ current = current.expression;
211
+ continue;
212
+ }
213
+ if (current.type === "MemberExpression") {
214
+ const memberName = getStaticMemberName(current);
215
+ if (memberName === null) {
216
+ return null;
217
+ }
218
+ memberPath.unshift(memberName);
219
+ current = current.object;
220
+ continue;
221
+ }
222
+ return memberPath;
223
+ }
224
+ }
145
225
  function getCallMethodName(node) {
146
226
  const { callee } = node;
147
- if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier") {
148
- return callee.property.name;
227
+ if (callee.type === "MemberExpression" && getStaticMemberName(callee) !== null) {
228
+ return getStaticMemberName(callee);
149
229
  }
150
230
  return null;
151
231
  }
152
- function isZodImportSpecifier(node) {
232
+ function getZodImportKind(node) {
153
233
  if (node.type !== "ImportDefaultSpecifier" && node.type !== "ImportNamespaceSpecifier" && node.type !== "ImportSpecifier") {
154
- return false;
234
+ return null;
155
235
  }
156
236
  const declaration = node.parent;
157
237
  if (declaration?.type !== "ImportDeclaration") {
158
- return false;
238
+ return null;
159
239
  }
160
240
  if (declaration.source.value !== "zod") {
161
- return false;
241
+ return null;
162
242
  }
163
243
  if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") {
164
- return true;
244
+ return { importedName: "z", type: "namespace" };
165
245
  }
166
246
  if (node.importKind === "type" || declaration.importKind === "type") {
167
- return false;
247
+ return null;
168
248
  }
169
249
  const importedName = node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
170
- return ZOD_NAMESPACE_IMPORTS.has(importedName) || ZOD_FACTORY_IMPORTS.has(importedName);
250
+ if (ZOD_NAMESPACE_IMPORTS.has(importedName)) {
251
+ return { importedName, type: "namespace" };
252
+ }
253
+ if (ZOD_FACTORY_IMPORTS.has(importedName) || ZOD_DIRECT_COMBINATOR_IMPORTS.has(importedName)) {
254
+ return { type: "factory" };
255
+ }
256
+ return null;
257
+ }
258
+ function isZodSchemaNamespaceCall(importName, memberPath) {
259
+ if (importName === "coerce") {
260
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
261
+ }
262
+ if (importName === "iso") {
263
+ return memberPath[0] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[0]);
264
+ }
265
+ if (memberPath[0] === "coerce") {
266
+ return memberPath[1] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[1]);
267
+ }
268
+ if (memberPath[0] === "iso") {
269
+ return memberPath[1] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[1]);
270
+ }
271
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
171
272
  }
172
273
  function hasFullTypeInformation(services) {
173
274
  return services.program !== null;
@@ -234,7 +335,20 @@ var noInlineZodSchema = ESLintUtils.RuleCreator(
234
335
  }
235
336
  const scope = sourceCode.getScope(root);
236
337
  const variable = findVariable(scope, root.name);
237
- return variable?.defs.some((definition) => isZodImportSpecifier(definition.node)) ?? false;
338
+ return variable?.defs.some((definition) => {
339
+ const importKind = getZodImportKind(definition.node);
340
+ if (importKind?.type === "factory") {
341
+ return true;
342
+ }
343
+ if (importKind?.type !== "namespace") {
344
+ return false;
345
+ }
346
+ const memberPath = getMemberPathFromRoot(node.callee);
347
+ if (memberPath === null || memberPath.length === 0) {
348
+ return false;
349
+ }
350
+ return isZodSchemaNamespaceCall(importKind.importedName, memberPath);
351
+ }) ?? false;
238
352
  }
239
353
  function isTypedZodSchemaCombinatorCall(node) {
240
354
  const rootMethodName = getRootMethodName(node);
@@ -254,6 +368,37 @@ var noInlineZodSchema = ESLintUtils.RuleCreator(
254
368
  function isSchemaCreationCall(node) {
255
369
  return isZodSchemaCall(node) || isTypedZodSchemaCombinatorCall(node);
256
370
  }
371
+ function isInModuleInitializationPath(node) {
372
+ let current = node;
373
+ while (current) {
374
+ if (current.type === "Program") {
375
+ return true;
376
+ }
377
+ if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") {
378
+ return false;
379
+ }
380
+ if (current.type === "PropertyDefinition" && !current.static) {
381
+ return false;
382
+ }
383
+ current = current.parent;
384
+ }
385
+ return false;
386
+ }
387
+ function isFunctionExecutedDuringModuleInitialization(node) {
388
+ if (node.type === "FunctionDeclaration") {
389
+ return false;
390
+ }
391
+ const parent = node.parent;
392
+ if (parent?.type === "CallExpression") {
393
+ if (parent.callee === node) {
394
+ return isInModuleInitializationPath(parent);
395
+ }
396
+ if (parent.arguments.includes(node)) {
397
+ return isInModuleInitializationPath(parent);
398
+ }
399
+ }
400
+ return false;
401
+ }
257
402
  function hasSchemaCreationCallAncestor(node) {
258
403
  let current = node.parent;
259
404
  while (current) {
@@ -268,7 +413,9 @@ var noInlineZodSchema = ESLintUtils.RuleCreator(
268
413
  let current = node.parent;
269
414
  while (current) {
270
415
  if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") {
271
- return true;
416
+ if (!isFunctionExecutedDuringModuleInitialization(current)) {
417
+ return true;
418
+ }
272
419
  }
273
420
  if (current.type === "PropertyDefinition" && !current.static) {
274
421
  return true;
@@ -305,7 +452,7 @@ var rules = {
305
452
  var plugin = {
306
453
  meta: {
307
454
  name: "eslint-plugin-zod-utils",
308
- version: "1.0.3"
455
+ version: "1.0.5"
309
456
  },
310
457
  rules,
311
458
  configs: {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zod-utils",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "ESLint utilities for safer Zod schema usage.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",