eslint-plugin-zod-utils 1.0.3 → 1.0.4

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.
package/dist/index.cjs CHANGED
@@ -48,6 +48,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
48
48
  "enum",
49
49
  "file",
50
50
  "function",
51
+ "fromJSONSchema",
51
52
  "instanceof",
52
53
  "intersection",
53
54
  "ipv4",
@@ -56,6 +57,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
56
57
  "ksuid",
57
58
  "lazy",
58
59
  "literal",
60
+ "looseObject",
59
61
  "map",
60
62
  "nan",
61
63
  "nanoid",
@@ -72,6 +74,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
72
74
  "set",
73
75
  "string",
74
76
  "stringbool",
77
+ "strictObject",
75
78
  "symbol",
76
79
  "templateLiteral",
77
80
  "tuple",
@@ -88,6 +91,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
88
91
  "xid"
89
92
  ]);
90
93
  var ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["z", "coerce", "iso"]);
94
+ var ZOD_ISO_FACTORY_IMPORTS = /* @__PURE__ */ new Set(["date", "datetime", "duration", "time"]);
91
95
  var ZOD_EXECUTION_METHODS = /* @__PURE__ */ new Set([
92
96
  "parse",
93
97
  "parseAsync",
@@ -170,6 +174,33 @@ function getRootMethodName(node) {
170
174
  return rootMethodName;
171
175
  }
172
176
  }
177
+ function getMemberPathFromRoot(node) {
178
+ const memberPath = [];
179
+ let current = node;
180
+ while (true) {
181
+ if (current.type === "CallExpression") {
182
+ current = current.callee;
183
+ continue;
184
+ }
185
+ if (current.type === "ChainExpression") {
186
+ current = current.expression;
187
+ continue;
188
+ }
189
+ if (current.type === "TSAsExpression" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion") {
190
+ current = current.expression;
191
+ continue;
192
+ }
193
+ if (current.type === "MemberExpression") {
194
+ if (current.computed || current.property.type !== "Identifier") {
195
+ return null;
196
+ }
197
+ memberPath.unshift(current.property.name);
198
+ current = current.object;
199
+ continue;
200
+ }
201
+ return memberPath;
202
+ }
203
+ }
173
204
  function getCallMethodName(node) {
174
205
  const { callee } = node;
175
206
  if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier") {
@@ -177,25 +208,46 @@ function getCallMethodName(node) {
177
208
  }
178
209
  return null;
179
210
  }
180
- function isZodImportSpecifier(node) {
211
+ function getZodImportKind(node) {
181
212
  if (node.type !== "ImportDefaultSpecifier" && node.type !== "ImportNamespaceSpecifier" && node.type !== "ImportSpecifier") {
182
- return false;
213
+ return null;
183
214
  }
184
215
  const declaration = node.parent;
185
216
  if (declaration?.type !== "ImportDeclaration") {
186
- return false;
217
+ return null;
187
218
  }
188
219
  if (declaration.source.value !== "zod") {
189
- return false;
220
+ return null;
190
221
  }
191
222
  if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") {
192
- return true;
223
+ return { importedName: "z", type: "namespace" };
193
224
  }
194
225
  if (node.importKind === "type" || declaration.importKind === "type") {
195
- return false;
226
+ return null;
196
227
  }
197
228
  const importedName = node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
198
- return ZOD_NAMESPACE_IMPORTS.has(importedName) || ZOD_FACTORY_IMPORTS.has(importedName);
229
+ if (ZOD_NAMESPACE_IMPORTS.has(importedName)) {
230
+ return { importedName, type: "namespace" };
231
+ }
232
+ if (ZOD_FACTORY_IMPORTS.has(importedName)) {
233
+ return { type: "factory" };
234
+ }
235
+ return null;
236
+ }
237
+ function isZodSchemaNamespaceCall(importName, memberPath) {
238
+ if (importName === "coerce") {
239
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
240
+ }
241
+ if (importName === "iso") {
242
+ return memberPath[0] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[0]);
243
+ }
244
+ if (memberPath[0] === "coerce") {
245
+ return memberPath[1] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[1]);
246
+ }
247
+ if (memberPath[0] === "iso") {
248
+ return memberPath[1] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[1]);
249
+ }
250
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
199
251
  }
200
252
  function hasFullTypeInformation(services) {
201
253
  return services.program !== null;
@@ -262,7 +314,20 @@ var noInlineZodSchema = import_utils.ESLintUtils.RuleCreator(
262
314
  }
263
315
  const scope = sourceCode.getScope(root);
264
316
  const variable = findVariable(scope, root.name);
265
- return variable?.defs.some((definition) => isZodImportSpecifier(definition.node)) ?? false;
317
+ return variable?.defs.some((definition) => {
318
+ const importKind = getZodImportKind(definition.node);
319
+ if (importKind?.type === "factory") {
320
+ return true;
321
+ }
322
+ if (importKind?.type !== "namespace") {
323
+ return false;
324
+ }
325
+ const memberPath = getMemberPathFromRoot(node.callee);
326
+ if (memberPath === null || memberPath.length === 0) {
327
+ return false;
328
+ }
329
+ return isZodSchemaNamespaceCall(importKind.importedName, memberPath);
330
+ }) ?? false;
266
331
  }
267
332
  function isTypedZodSchemaCombinatorCall(node) {
268
333
  const rootMethodName = getRootMethodName(node);
@@ -333,7 +398,7 @@ var rules = {
333
398
  var plugin = {
334
399
  meta: {
335
400
  name: "eslint-plugin-zod-utils",
336
- version: "1.0.3"
401
+ version: "1.0.4"
337
402
  },
338
403
  rules,
339
404
  configs: {}
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
20
20
  "enum",
21
21
  "file",
22
22
  "function",
23
+ "fromJSONSchema",
23
24
  "instanceof",
24
25
  "intersection",
25
26
  "ipv4",
@@ -28,6 +29,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
28
29
  "ksuid",
29
30
  "lazy",
30
31
  "literal",
32
+ "looseObject",
31
33
  "map",
32
34
  "nan",
33
35
  "nanoid",
@@ -44,6 +46,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
44
46
  "set",
45
47
  "string",
46
48
  "stringbool",
49
+ "strictObject",
47
50
  "symbol",
48
51
  "templateLiteral",
49
52
  "tuple",
@@ -60,6 +63,7 @@ var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
60
63
  "xid"
61
64
  ]);
62
65
  var ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["z", "coerce", "iso"]);
66
+ var ZOD_ISO_FACTORY_IMPORTS = /* @__PURE__ */ new Set(["date", "datetime", "duration", "time"]);
63
67
  var ZOD_EXECUTION_METHODS = /* @__PURE__ */ new Set([
64
68
  "parse",
65
69
  "parseAsync",
@@ -142,6 +146,33 @@ function getRootMethodName(node) {
142
146
  return rootMethodName;
143
147
  }
144
148
  }
149
+ function getMemberPathFromRoot(node) {
150
+ const memberPath = [];
151
+ let current = node;
152
+ while (true) {
153
+ if (current.type === "CallExpression") {
154
+ current = current.callee;
155
+ continue;
156
+ }
157
+ if (current.type === "ChainExpression") {
158
+ current = current.expression;
159
+ continue;
160
+ }
161
+ if (current.type === "TSAsExpression" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion") {
162
+ current = current.expression;
163
+ continue;
164
+ }
165
+ if (current.type === "MemberExpression") {
166
+ if (current.computed || current.property.type !== "Identifier") {
167
+ return null;
168
+ }
169
+ memberPath.unshift(current.property.name);
170
+ current = current.object;
171
+ continue;
172
+ }
173
+ return memberPath;
174
+ }
175
+ }
145
176
  function getCallMethodName(node) {
146
177
  const { callee } = node;
147
178
  if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier") {
@@ -149,25 +180,46 @@ function getCallMethodName(node) {
149
180
  }
150
181
  return null;
151
182
  }
152
- function isZodImportSpecifier(node) {
183
+ function getZodImportKind(node) {
153
184
  if (node.type !== "ImportDefaultSpecifier" && node.type !== "ImportNamespaceSpecifier" && node.type !== "ImportSpecifier") {
154
- return false;
185
+ return null;
155
186
  }
156
187
  const declaration = node.parent;
157
188
  if (declaration?.type !== "ImportDeclaration") {
158
- return false;
189
+ return null;
159
190
  }
160
191
  if (declaration.source.value !== "zod") {
161
- return false;
192
+ return null;
162
193
  }
163
194
  if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") {
164
- return true;
195
+ return { importedName: "z", type: "namespace" };
165
196
  }
166
197
  if (node.importKind === "type" || declaration.importKind === "type") {
167
- return false;
198
+ return null;
168
199
  }
169
200
  const importedName = node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
170
- return ZOD_NAMESPACE_IMPORTS.has(importedName) || ZOD_FACTORY_IMPORTS.has(importedName);
201
+ if (ZOD_NAMESPACE_IMPORTS.has(importedName)) {
202
+ return { importedName, type: "namespace" };
203
+ }
204
+ if (ZOD_FACTORY_IMPORTS.has(importedName)) {
205
+ return { type: "factory" };
206
+ }
207
+ return null;
208
+ }
209
+ function isZodSchemaNamespaceCall(importName, memberPath) {
210
+ if (importName === "coerce") {
211
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
212
+ }
213
+ if (importName === "iso") {
214
+ return memberPath[0] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[0]);
215
+ }
216
+ if (memberPath[0] === "coerce") {
217
+ return memberPath[1] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[1]);
218
+ }
219
+ if (memberPath[0] === "iso") {
220
+ return memberPath[1] !== void 0 && ZOD_ISO_FACTORY_IMPORTS.has(memberPath[1]);
221
+ }
222
+ return memberPath[0] !== void 0 && ZOD_FACTORY_IMPORTS.has(memberPath[0]);
171
223
  }
172
224
  function hasFullTypeInformation(services) {
173
225
  return services.program !== null;
@@ -234,7 +286,20 @@ var noInlineZodSchema = ESLintUtils.RuleCreator(
234
286
  }
235
287
  const scope = sourceCode.getScope(root);
236
288
  const variable = findVariable(scope, root.name);
237
- return variable?.defs.some((definition) => isZodImportSpecifier(definition.node)) ?? false;
289
+ return variable?.defs.some((definition) => {
290
+ const importKind = getZodImportKind(definition.node);
291
+ if (importKind?.type === "factory") {
292
+ return true;
293
+ }
294
+ if (importKind?.type !== "namespace") {
295
+ return false;
296
+ }
297
+ const memberPath = getMemberPathFromRoot(node.callee);
298
+ if (memberPath === null || memberPath.length === 0) {
299
+ return false;
300
+ }
301
+ return isZodSchemaNamespaceCall(importKind.importedName, memberPath);
302
+ }) ?? false;
238
303
  }
239
304
  function isTypedZodSchemaCombinatorCall(node) {
240
305
  const rootMethodName = getRootMethodName(node);
@@ -305,7 +370,7 @@ var rules = {
305
370
  var plugin = {
306
371
  meta: {
307
372
  name: "eslint-plugin-zod-utils",
308
- version: "1.0.3"
373
+ version: "1.0.4"
309
374
  },
310
375
  rules,
311
376
  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.4",
4
4
  "description": "ESLint utilities for safer Zod schema usage.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",