eslint-plugin-zod 4.2.0 → 4.2.1

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.
@@ -32,9 +32,9 @@ const arrayStyle = require_create_plugin_rule.createZodPluginRule({
32
32
  return {
33
33
  ImportDeclaration: importDeclarationListener,
34
34
  CallExpression(node) {
35
- const zodSchema = detectZodSchemaRootNode(node);
36
- if (!zodSchema) return;
37
- const { schemaDecl, schemaType } = zodSchema;
35
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
36
+ if (!zodSchemaMeta) return;
37
+ const { schemaDecl, schemaType } = zodSchemaMeta;
38
38
  if (style === "method") {
39
39
  if (schemaType === "array") {
40
40
  if (schemaDecl === "namespace") {
@@ -31,9 +31,9 @@ const arrayStyle = createZodPluginRule({
31
31
  return {
32
32
  ImportDeclaration: importDeclarationListener,
33
33
  CallExpression(node) {
34
- const zodSchema = detectZodSchemaRootNode(node);
35
- if (!zodSchema) return;
36
- const { schemaDecl, schemaType } = zodSchema;
34
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
35
+ if (!zodSchemaMeta) return;
36
+ const { schemaDecl, schemaType } = zodSchemaMeta;
37
37
  if (style === "method") {
38
38
  if (schemaType === "array") {
39
39
  if (schemaDecl === "namespace") {
@@ -15,27 +15,19 @@ const noNativeEnum = require_create_plugin_rule.createZodPluginRule({
15
15
  },
16
16
  defaultOptions: [],
17
17
  create(context) {
18
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, getNamedImportOriginal } = trackZodSchemaImports();
18
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
19
19
  return {
20
20
  ImportDeclaration: importDeclarationListener,
21
21
  CallExpression(node) {
22
- const zodSchema = detectZodSchemaRootNode(node);
23
- const rootMethod = (zodSchema ? collectZodChainMethods(zodSchema.node) : void 0)?.[0];
24
- const isNativeEnumRoot = zodSchema?.schemaDecl === "namespace" ? rootMethod?.name === "nativeEnum" : getNamedImportOriginal(rootMethod?.name ?? "") === "nativeEnum";
25
- if (!zodSchema || !isNativeEnumRoot) return;
26
- if (zodSchema.schemaDecl === "named") {
27
- context.report({
28
- node,
29
- messageId: "useEnum"
30
- });
31
- return;
32
- }
22
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
23
+ if (zodSchemaMeta?.schemaType !== "nativeEnum") return;
24
+ const [{ node: rootMethodNode }] = collectZodChainMethods(zodSchemaMeta.node);
33
25
  context.report({
34
26
  node,
35
27
  messageId: "useEnum",
36
28
  fix(fixer) {
37
- if (rootMethod?.node.callee.type !== _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression || rootMethod.node.callee.property.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return null;
38
- return fixer.replaceText(rootMethod.node.callee.property, "enum");
29
+ if (rootMethodNode.callee.type !== _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression) return null;
30
+ return fixer.replaceText(rootMethodNode.callee.property, "enum");
39
31
  }
40
32
  });
41
33
  }
@@ -14,27 +14,19 @@ const noNativeEnum = createZodPluginRule({
14
14
  },
15
15
  defaultOptions: [],
16
16
  create(context) {
17
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, getNamedImportOriginal } = trackZodSchemaImports();
17
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
18
18
  return {
19
19
  ImportDeclaration: importDeclarationListener,
20
20
  CallExpression(node) {
21
- const zodSchema = detectZodSchemaRootNode(node);
22
- const rootMethod = (zodSchema ? collectZodChainMethods(zodSchema.node) : void 0)?.[0];
23
- const isNativeEnumRoot = zodSchema?.schemaDecl === "namespace" ? rootMethod?.name === "nativeEnum" : getNamedImportOriginal(rootMethod?.name ?? "") === "nativeEnum";
24
- if (!zodSchema || !isNativeEnumRoot) return;
25
- if (zodSchema.schemaDecl === "named") {
26
- context.report({
27
- node,
28
- messageId: "useEnum"
29
- });
30
- return;
31
- }
21
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
22
+ if (zodSchemaMeta?.schemaType !== "nativeEnum") return;
23
+ const [{ node: rootMethodNode }] = collectZodChainMethods(zodSchemaMeta.node);
32
24
  context.report({
33
25
  node,
34
26
  messageId: "useEnum",
35
27
  fix(fixer) {
36
- if (rootMethod?.node.callee.type !== AST_NODE_TYPES.MemberExpression || rootMethod.node.callee.property.type !== AST_NODE_TYPES.Identifier) return null;
37
- return fixer.replaceText(rootMethod.node.callee.property, "enum");
28
+ if (rootMethodNode.callee.type !== AST_NODE_TYPES.MemberExpression) return null;
29
+ return fixer.replaceText(rootMethodNode.callee.property, "enum");
38
30
  }
39
31
  });
40
32
  }
@@ -25,17 +25,11 @@ const noNumberSchemaWithInt = require_create_plugin_rule.createZodPluginRule({
25
25
  const intIndex = methods.findIndex((m) => m.name === "int");
26
26
  if (intIndex === -1) return;
27
27
  const numberIndex = methods.findIndex((m) => m.name === "number");
28
- if (zodSchemaMeta.schemaDecl === "named") {
29
- context.report({
30
- node,
31
- messageId: "removeNumber"
32
- });
33
- return;
34
- }
35
28
  context.report({
36
29
  node,
37
30
  messageId: "removeNumber",
38
31
  fix(fixer) {
32
+ if (zodSchemaMeta.schemaDecl === "named") return null;
39
33
  return (0, _eslint_zod_utils.buildZodChainReplacementFix)({
40
34
  sourceCode,
41
35
  fixer,
@@ -24,17 +24,11 @@ const noNumberSchemaWithInt = createZodPluginRule({
24
24
  const intIndex = methods.findIndex((m) => m.name === "int");
25
25
  if (intIndex === -1) return;
26
26
  const numberIndex = methods.findIndex((m) => m.name === "number");
27
- if (zodSchemaMeta.schemaDecl === "named") {
28
- context.report({
29
- node,
30
- messageId: "removeNumber"
31
- });
32
- return;
33
- }
34
27
  context.report({
35
28
  node,
36
29
  messageId: "removeNumber",
37
30
  fix(fixer) {
31
+ if (zodSchemaMeta.schemaDecl === "named") return null;
38
32
  return buildZodChainReplacementFix({
39
33
  sourceCode,
40
34
  fixer,
@@ -25,17 +25,11 @@ const noNumberSchemaWithSafe = require_create_plugin_rule.createZodPluginRule({
25
25
  const safeIndex = methods.findIndex((m) => m.name === "safe" && m.node === node);
26
26
  if (safeIndex === -1) return;
27
27
  const numberIndex = methods.findIndex((m) => m.name === "number");
28
- if (zodSchemaMeta.schemaDecl === "named") {
29
- context.report({
30
- node,
31
- messageId: "useInt"
32
- });
33
- return;
34
- }
35
28
  context.report({
36
29
  node,
37
30
  messageId: "useInt",
38
31
  fix(fixer) {
32
+ if (zodSchemaMeta.schemaDecl === "named") return null;
39
33
  return (0, _eslint_zod_utils.buildZodChainReplacementFix)({
40
34
  sourceCode,
41
35
  fixer,
@@ -24,17 +24,11 @@ const noNumberSchemaWithSafe = createZodPluginRule({
24
24
  const safeIndex = methods.findIndex((m) => m.name === "safe" && m.node === node);
25
25
  if (safeIndex === -1) return;
26
26
  const numberIndex = methods.findIndex((m) => m.name === "number");
27
- if (zodSchemaMeta.schemaDecl === "named") {
28
- context.report({
29
- node,
30
- messageId: "useInt"
31
- });
32
- return;
33
- }
34
27
  context.report({
35
28
  node,
36
29
  messageId: "useInt",
37
30
  fix(fixer) {
31
+ if (zodSchemaMeta.schemaDecl === "named") return null;
38
32
  return buildZodChainReplacementFix({
39
33
  sourceCode,
40
34
  fixer,
@@ -29,17 +29,11 @@ const noStringSchemaWithUuid = require_create_plugin_rule.createZodPluginRule({
29
29
  const uuidIndex = methods.findIndex((m) => m.name === "uuid");
30
30
  if (uuidIndex === -1) return;
31
31
  const stringIndex = methods.findIndex((m) => m.name === "string");
32
- if (zodSchemaMeta.schemaDecl === "named") {
33
- context.report({
34
- node,
35
- messageId: "useUuid"
36
- });
37
- return;
38
- }
39
32
  context.report({
40
33
  node,
41
34
  messageId: "useUuid",
42
35
  fix(fixer) {
36
+ if (zodSchemaMeta.schemaDecl === "named") return null;
43
37
  return (0, _eslint_zod_utils.buildZodChainReplacementFix)({
44
38
  sourceCode,
45
39
  fixer,
@@ -28,17 +28,11 @@ const noStringSchemaWithUuid = createZodPluginRule({
28
28
  const uuidIndex = methods.findIndex((m) => m.name === "uuid");
29
29
  if (uuidIndex === -1) return;
30
30
  const stringIndex = methods.findIndex((m) => m.name === "string");
31
- if (zodSchemaMeta.schemaDecl === "named") {
32
- context.report({
33
- node,
34
- messageId: "useUuid"
35
- });
36
- return;
37
- }
38
31
  context.report({
39
32
  node,
40
33
  messageId: "useUuid",
41
34
  fix(fixer) {
35
+ if (zodSchemaMeta.schemaDecl === "named") return null;
42
36
  return buildZodChainReplacementFix({
43
37
  sourceCode,
44
38
  fixer,
@@ -19,9 +19,9 @@ const preferEnumOverLiteralUnion = require_create_plugin_rule.createZodPluginRul
19
19
  return {
20
20
  ImportDeclaration: importDeclarationListener,
21
21
  CallExpression(node) {
22
- const zodSchema = detectZodSchemaRootNode(node);
23
- if (zodSchema?.schemaType !== "union") return;
24
- const union = collectZodChainMethods(zodSchema.node).find((it) => it.name === "union");
22
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
23
+ if (zodSchemaMeta?.schemaType !== "union") return;
24
+ const union = collectZodChainMethods(zodSchemaMeta.node).find((it) => it.name === "union");
25
25
  if (!union) return;
26
26
  const unionNode = union.node;
27
27
  const unionArgument = unionNode.arguments.at(0);
@@ -39,17 +39,11 @@ const preferEnumOverLiteralUnion = require_create_plugin_rule.createZodPluginRul
39
39
  return null;
40
40
  });
41
41
  if (zodLiteralStrings.some((it) => it === null)) return;
42
- if (zodSchema.schemaDecl === "named") {
43
- context.report({
44
- node,
45
- messageId: "useEnum"
46
- });
47
- return;
48
- }
49
42
  context.report({
50
43
  node,
51
44
  messageId: "useEnum",
52
45
  fix(fixer) {
46
+ if (zodSchemaMeta.schemaDecl === "named") return null;
53
47
  return [fixer.replaceText(unionNode.callee.property, "enum"), fixer.replaceText(unionNode.arguments[0], `[${zodLiteralStrings.join(", ")}]`)];
54
48
  }
55
49
  });
@@ -18,9 +18,9 @@ const preferEnumOverLiteralUnion = createZodPluginRule({
18
18
  return {
19
19
  ImportDeclaration: importDeclarationListener,
20
20
  CallExpression(node) {
21
- const zodSchema = detectZodSchemaRootNode(node);
22
- if (zodSchema?.schemaType !== "union") return;
23
- const union = collectZodChainMethods(zodSchema.node).find((it) => it.name === "union");
21
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
22
+ if (zodSchemaMeta?.schemaType !== "union") return;
23
+ const union = collectZodChainMethods(zodSchemaMeta.node).find((it) => it.name === "union");
24
24
  if (!union) return;
25
25
  const unionNode = union.node;
26
26
  const unionArgument = unionNode.arguments.at(0);
@@ -38,17 +38,11 @@ const preferEnumOverLiteralUnion = createZodPluginRule({
38
38
  return null;
39
39
  });
40
40
  if (zodLiteralStrings.some((it) => it === null)) return;
41
- if (zodSchema.schemaDecl === "named") {
42
- context.report({
43
- node,
44
- messageId: "useEnum"
45
- });
46
- return;
47
- }
48
41
  context.report({
49
42
  node,
50
43
  messageId: "useEnum",
51
44
  fix(fixer) {
45
+ if (zodSchemaMeta.schemaDecl === "named") return null;
52
46
  return [fixer.replaceText(unionNode.callee.property, "enum"), fixer.replaceText(unionNode.arguments[0], `[${zodLiteralStrings.join(", ")}]`)];
53
47
  }
54
48
  });
@@ -26,17 +26,11 @@ const preferStringSchemaWithTrim = require_create_plugin_rule.createZodPluginRul
26
26
  })) return;
27
27
  const methods = collectZodChainMethods(zodSchemaMeta.node);
28
28
  if (methods.some((it) => it.name === "trim")) return;
29
- if (zodSchemaMeta.schemaDecl === "named") {
30
- context.report({
31
- node,
32
- messageId: "addTrim"
33
- });
34
- return;
35
- }
36
29
  context.report({
37
30
  node,
38
31
  messageId: "addTrim",
39
32
  fix(fixer) {
33
+ if (zodSchemaMeta.schemaDecl === "named") return null;
40
34
  const lastMethod = methods.at(0);
41
35
  return fixer.insertTextAfter(lastMethod.node, ".trim()");
42
36
  }
@@ -25,17 +25,11 @@ const preferStringSchemaWithTrim = createZodPluginRule({
25
25
  })) return;
26
26
  const methods = collectZodChainMethods(zodSchemaMeta.node);
27
27
  if (methods.some((it) => it.name === "trim")) return;
28
- if (zodSchemaMeta.schemaDecl === "named") {
29
- context.report({
30
- node,
31
- messageId: "addTrim"
32
- });
33
- return;
34
- }
35
28
  context.report({
36
29
  node,
37
30
  messageId: "addTrim",
38
31
  fix(fixer) {
32
+ if (zodSchemaMeta.schemaDecl === "named") return null;
39
33
  const lastMethod = methods.at(0);
40
34
  return fixer.insertTextAfter(lastMethod.node, ".trim()");
41
35
  }
@@ -156,17 +156,6 @@ const preferTopLevelStringFormats = require_create_plugin_rule.createZodPluginRu
156
156
  if (!formatMethod) return;
157
157
  if (!isTopLevelStringFormatMethodName(formatMethod.name)) return;
158
158
  const { replacementMethodName, sourceMethodName } = TOP_LEVEL_STRING_FORMATS_BY_SOURCE[formatMethod.name];
159
- if (zodSchemaMeta.schemaDecl === "named") {
160
- context.report({
161
- node,
162
- messageId: "preferTopLevelStringFormat",
163
- data: {
164
- replacementMethod: replacementMethodName,
165
- sourceMethod: sourceMethodName
166
- }
167
- });
168
- return;
169
- }
170
159
  context.report({
171
160
  node,
172
161
  messageId: "preferTopLevelStringFormat",
@@ -175,6 +164,7 @@ const preferTopLevelStringFormats = require_create_plugin_rule.createZodPluginRu
175
164
  sourceMethod: sourceMethodName
176
165
  },
177
166
  fix(fixer) {
167
+ if (zodSchemaMeta.schemaDecl === "named") return null;
178
168
  return (0, _eslint_zod_utils.buildZodChainReplacementFix)({
179
169
  sourceCode,
180
170
  fixer,
@@ -155,17 +155,6 @@ const preferTopLevelStringFormats = createZodPluginRule({
155
155
  if (!formatMethod) return;
156
156
  if (!isTopLevelStringFormatMethodName(formatMethod.name)) return;
157
157
  const { replacementMethodName, sourceMethodName } = TOP_LEVEL_STRING_FORMATS_BY_SOURCE[formatMethod.name];
158
- if (zodSchemaMeta.schemaDecl === "named") {
159
- context.report({
160
- node,
161
- messageId: "preferTopLevelStringFormat",
162
- data: {
163
- replacementMethod: replacementMethodName,
164
- sourceMethod: sourceMethodName
165
- }
166
- });
167
- return;
168
- }
169
158
  context.report({
170
159
  node,
171
160
  messageId: "preferTopLevelStringFormat",
@@ -174,6 +163,7 @@ const preferTopLevelStringFormats = createZodPluginRule({
174
163
  sourceMethod: sourceMethodName
175
164
  },
176
165
  fix(fixer) {
166
+ if (zodSchemaMeta.schemaDecl === "named") return null;
177
167
  return buildZodChainReplacementFix({
178
168
  sourceCode,
179
169
  fixer,
@@ -35,17 +35,11 @@ const preferTrimBeforeStringLengthChecks = require_create_plugin_rule.createZodP
35
35
  const firstLengthCheckIndex = methods.findIndex((m) => LENGTH_CHECK_METHODS.includes(m.name));
36
36
  if (firstLengthCheckIndex === -1) return;
37
37
  if (trimIndex < firstLengthCheckIndex) return;
38
- if (zodSchemaMeta.schemaDecl === "named") {
39
- context.report({
40
- node,
41
- messageId: "trimBeforeLengthCheck"
42
- });
43
- return;
44
- }
45
38
  context.report({
46
39
  node,
47
40
  messageId: "trimBeforeLengthCheck",
48
41
  fix(fixer) {
42
+ if (zodSchemaMeta.schemaDecl === "named") return null;
49
43
  const stringMethodNode = methods[0].node;
50
44
  const trimMethodNode = methods[trimIndex].node;
51
45
  const trimCallee = trimMethodNode.callee;
@@ -34,17 +34,11 @@ const preferTrimBeforeStringLengthChecks = createZodPluginRule({
34
34
  const firstLengthCheckIndex = methods.findIndex((m) => LENGTH_CHECK_METHODS.includes(m.name));
35
35
  if (firstLengthCheckIndex === -1) return;
36
36
  if (trimIndex < firstLengthCheckIndex) return;
37
- if (zodSchemaMeta.schemaDecl === "named") {
38
- context.report({
39
- node,
40
- messageId: "trimBeforeLengthCheck"
41
- });
42
- return;
43
- }
44
37
  context.report({
45
38
  node,
46
39
  messageId: "trimBeforeLengthCheck",
47
40
  fix(fixer) {
41
+ if (zodSchemaMeta.schemaDecl === "named") return null;
48
42
  const stringMethodNode = methods[0].node;
49
43
  const trimMethodNode = methods[trimIndex].node;
50
44
  const trimCallee = trimMethodNode.callee;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zod",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "type": "module",
5
5
  "description": "ESLint plugin that adds custom linting rules to enforce best practices when using Zod",
6
6
  "engines": {
@@ -43,7 +43,7 @@
43
43
  "dependencies": {
44
44
  "@typescript-eslint/utils": "^8.57.0",
45
45
  "esquery": "^1.6.0",
46
- "@eslint-zod/utils": "1.0.1"
46
+ "@eslint-zod/utils": "1.1.0"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "eslint": "^9 || ^10",