eslint-plugin-zod 3.5.1 → 3.5.2

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 (138) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/index.cjs +94 -83
  3. package/dist/index.d.cts +17 -16
  4. package/dist/index.d.mts +24 -0
  5. package/dist/index.mjs +98 -0
  6. package/dist/meta.cjs +11 -11
  7. package/dist/meta.mjs +10 -0
  8. package/dist/rules/array-style.cjs +88 -101
  9. package/dist/rules/array-style.mjs +89 -0
  10. package/dist/rules/consistent-import-source.cjs +59 -66
  11. package/dist/rules/consistent-import-source.mjs +60 -0
  12. package/dist/rules/consistent-import.cjs +146 -172
  13. package/dist/rules/consistent-import.mjs +152 -0
  14. package/dist/rules/consistent-object-schema-type.cjs +81 -84
  15. package/dist/rules/consistent-object-schema-type.mjs +81 -0
  16. package/dist/rules/no-any-schema.cjs +62 -67
  17. package/dist/rules/no-any-schema.mjs +62 -0
  18. package/dist/rules/no-empty-custom-schema.cjs +32 -40
  19. package/dist/rules/no-empty-custom-schema.mjs +34 -0
  20. package/dist/rules/no-number-schema-with-int.cjs +56 -62
  21. package/dist/rules/no-number-schema-with-int.mjs +57 -0
  22. package/dist/rules/no-optional-and-default-together.cjs +69 -80
  23. package/dist/rules/no-optional-and-default-together.mjs +71 -0
  24. package/dist/rules/no-string-schema-with-uuid.cjs +57 -63
  25. package/dist/rules/no-string-schema-with-uuid.mjs +58 -0
  26. package/dist/rules/no-throw-in-refine.cjs +69 -87
  27. package/dist/rules/no-throw-in-refine.mjs +69 -0
  28. package/dist/rules/no-unknown-schema.cjs +29 -35
  29. package/dist/rules/no-unknown-schema.mjs +31 -0
  30. package/dist/rules/prefer-enum-over-literal-union.cjs +63 -79
  31. package/dist/rules/prefer-enum-over-literal-union.mjs +63 -0
  32. package/dist/rules/prefer-meta-last.cjs +47 -63
  33. package/dist/rules/prefer-meta-last.mjs +49 -0
  34. package/dist/rules/prefer-meta.cjs +38 -48
  35. package/dist/rules/prefer-meta.mjs +40 -0
  36. package/dist/rules/prefer-namespace-import.cjs +110 -142
  37. package/dist/rules/prefer-namespace-import.mjs +116 -0
  38. package/dist/rules/prefer-string-schema-with-trim.cjs +45 -52
  39. package/dist/rules/prefer-string-schema-with-trim.mjs +47 -0
  40. package/dist/rules/require-brand-type-parameter.cjs +47 -60
  41. package/dist/rules/require-brand-type-parameter.mjs +49 -0
  42. package/dist/rules/require-error-message.cjs +83 -102
  43. package/dist/rules/require-error-message.mjs +83 -0
  44. package/dist/rules/require-schema-suffix.cjs +67 -78
  45. package/dist/rules/require-schema-suffix.mjs +67 -0
  46. package/dist/rules/schema-error-property-style.cjs +95 -107
  47. package/dist/rules/schema-error-property-style.mjs +94 -0
  48. package/dist/utils/build-zod-chain-replacement-fix.cjs +28 -29
  49. package/dist/utils/build-zod-chain-replacement-fix.mjs +30 -0
  50. package/dist/utils/create-plugin-rule.cjs +6 -6
  51. package/dist/utils/create-plugin-rule.mjs +6 -0
  52. package/dist/utils/detect-zod-schema-root-node.cjs +94 -96
  53. package/dist/utils/detect-zod-schema-root-node.mjs +101 -0
  54. package/dist/utils/is-zod-import-source.cjs +14 -18
  55. package/dist/utils/is-zod-import-source.mjs +16 -0
  56. package/dist/utils/track-zod-schema-imports.cjs +61 -63
  57. package/dist/utils/track-zod-schema-imports.mjs +64 -0
  58. package/package.json +14 -17
  59. package/dist/index.d.ts +0 -22
  60. package/dist/index.js +0 -84
  61. package/dist/meta.d.cts +0 -3
  62. package/dist/meta.d.ts +0 -3
  63. package/dist/meta.js +0 -8
  64. package/dist/rules/array-style.d.cts +0 -11
  65. package/dist/rules/array-style.d.ts +0 -11
  66. package/dist/rules/array-style.js +0 -99
  67. package/dist/rules/consistent-import-source.d.cts +0 -11
  68. package/dist/rules/consistent-import-source.d.ts +0 -11
  69. package/dist/rules/consistent-import-source.js +0 -64
  70. package/dist/rules/consistent-import.d.cts +0 -12
  71. package/dist/rules/consistent-import.d.ts +0 -12
  72. package/dist/rules/consistent-import.js +0 -176
  73. package/dist/rules/consistent-object-schema-type.d.cts +0 -13
  74. package/dist/rules/consistent-object-schema-type.d.ts +0 -13
  75. package/dist/rules/consistent-object-schema-type.js +0 -82
  76. package/dist/rules/no-any-schema.d.cts +0 -6
  77. package/dist/rules/no-any-schema.d.ts +0 -6
  78. package/dist/rules/no-any-schema.js +0 -65
  79. package/dist/rules/no-empty-custom-schema.d.cts +0 -5
  80. package/dist/rules/no-empty-custom-schema.d.ts +0 -5
  81. package/dist/rules/no-empty-custom-schema.js +0 -38
  82. package/dist/rules/no-number-schema-with-int.d.cts +0 -5
  83. package/dist/rules/no-number-schema-with-int.d.ts +0 -5
  84. package/dist/rules/no-number-schema-with-int.js +0 -60
  85. package/dist/rules/no-optional-and-default-together.d.cts +0 -12
  86. package/dist/rules/no-optional-and-default-together.d.ts +0 -12
  87. package/dist/rules/no-optional-and-default-together.js +0 -78
  88. package/dist/rules/no-string-schema-with-uuid.d.cts +0 -5
  89. package/dist/rules/no-string-schema-with-uuid.d.ts +0 -5
  90. package/dist/rules/no-string-schema-with-uuid.js +0 -61
  91. package/dist/rules/no-throw-in-refine.d.cts +0 -5
  92. package/dist/rules/no-throw-in-refine.d.ts +0 -5
  93. package/dist/rules/no-throw-in-refine.js +0 -85
  94. package/dist/rules/no-unknown-schema.d.cts +0 -5
  95. package/dist/rules/no-unknown-schema.d.ts +0 -5
  96. package/dist/rules/no-unknown-schema.js +0 -33
  97. package/dist/rules/prefer-enum-over-literal-union.d.cts +0 -5
  98. package/dist/rules/prefer-enum-over-literal-union.d.ts +0 -5
  99. package/dist/rules/prefer-enum-over-literal-union.js +0 -77
  100. package/dist/rules/prefer-meta-last.d.cts +0 -5
  101. package/dist/rules/prefer-meta-last.d.ts +0 -5
  102. package/dist/rules/prefer-meta-last.js +0 -61
  103. package/dist/rules/prefer-meta.d.cts +0 -5
  104. package/dist/rules/prefer-meta.d.ts +0 -5
  105. package/dist/rules/prefer-meta.js +0 -46
  106. package/dist/rules/prefer-namespace-import.d.cts +0 -5
  107. package/dist/rules/prefer-namespace-import.d.ts +0 -5
  108. package/dist/rules/prefer-namespace-import.js +0 -146
  109. package/dist/rules/prefer-string-schema-with-trim.d.cts +0 -5
  110. package/dist/rules/prefer-string-schema-with-trim.d.ts +0 -5
  111. package/dist/rules/prefer-string-schema-with-trim.js +0 -50
  112. package/dist/rules/require-brand-type-parameter.d.cts +0 -6
  113. package/dist/rules/require-brand-type-parameter.d.ts +0 -6
  114. package/dist/rules/require-brand-type-parameter.js +0 -58
  115. package/dist/rules/require-error-message.d.cts +0 -5
  116. package/dist/rules/require-error-message.d.ts +0 -5
  117. package/dist/rules/require-error-message.js +0 -100
  118. package/dist/rules/require-schema-suffix.d.cts +0 -9
  119. package/dist/rules/require-schema-suffix.d.ts +0 -9
  120. package/dist/rules/require-schema-suffix.js +0 -76
  121. package/dist/rules/schema-error-property-style.d.cts +0 -10
  122. package/dist/rules/schema-error-property-style.d.ts +0 -10
  123. package/dist/rules/schema-error-property-style.js +0 -102
  124. package/dist/utils/build-zod-chain-replacement-fix.d.cts +0 -12
  125. package/dist/utils/build-zod-chain-replacement-fix.d.ts +0 -12
  126. package/dist/utils/build-zod-chain-replacement-fix.js +0 -28
  127. package/dist/utils/create-plugin-rule.d.cts +0 -13
  128. package/dist/utils/create-plugin-rule.d.ts +0 -13
  129. package/dist/utils/create-plugin-rule.js +0 -3
  130. package/dist/utils/detect-zod-schema-root-node.d.cts +0 -10
  131. package/dist/utils/detect-zod-schema-root-node.d.ts +0 -10
  132. package/dist/utils/detect-zod-schema-root-node.js +0 -101
  133. package/dist/utils/is-zod-import-source.d.cts +0 -4
  134. package/dist/utils/is-zod-import-source.d.ts +0 -4
  135. package/dist/utils/is-zod-import-source.js +0 -17
  136. package/dist/utils/track-zod-schema-imports.d.cts +0 -18
  137. package/dist/utils/track-zod-schema-imports.d.ts +0 -18
  138. package/dist/utils/track-zod-schema-imports.js +0 -64
@@ -0,0 +1,58 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
3
+ import { buildZodChainReplacementFix } from "../utils/build-zod-chain-replacement-fix.mjs";
4
+ //#region src/rules/no-string-schema-with-uuid.ts
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("zod");
6
+ const noStringSchemaWithUuid = createZodPluginRule({
7
+ name: "no-string-schema-with-uuid",
8
+ meta: {
9
+ fixable: "code",
10
+ type: "problem",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Disallow usage of `z.string().uuid()` in favor of the dedicated `z.uuid()` schema",
14
+ url: "https://zod.dev/api#uuids"
15
+ },
16
+ messages: { useUuid: "`z.string().uuid()` is redundant. Use `z.uuid()` instead." },
17
+ schema: []
18
+ },
19
+ defaultOptions: [],
20
+ create(context) {
21
+ const { sourceCode } = context;
22
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
23
+ return {
24
+ ImportDeclaration: importDeclarationListener,
25
+ CallExpression(node) {
26
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
27
+ if (zodSchemaMeta?.schemaType !== "string") return;
28
+ const methods = collectZodChainMethods(node);
29
+ const uuidIndex = methods.findIndex((m) => m.name === "uuid");
30
+ if (uuidIndex === -1) return;
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
+ context.report({
40
+ node,
41
+ messageId: "useUuid",
42
+ fix(fixer) {
43
+ return buildZodChainReplacementFix({
44
+ sourceCode,
45
+ fixer,
46
+ methods,
47
+ fromIndex: stringIndex,
48
+ toIndex: uuidIndex,
49
+ toMethodName: "uuid"
50
+ });
51
+ }
52
+ });
53
+ }
54
+ };
55
+ }
56
+ });
57
+ //#endregion
58
+ export { noStringSchemaWithUuid };
@@ -1,88 +1,70 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noThrowInRefine = void 0;
4
- const utils_1 = require("@typescript-eslint/utils");
5
- const create_plugin_rule_js_1 = require("../utils/create-plugin-rule.cjs");
6
- const track_zod_schema_imports_js_1 = require("../utils/track-zod-schema-imports.cjs");
7
- const { zodImportAllowedSource, trackZodSchemaImports, } = (0, track_zod_schema_imports_js_1.createZodSchemaImportTrack)('zod');
8
- exports.noThrowInRefine = (0, create_plugin_rule_js_1.createZodPluginRule)({
9
- name: 'no-throw-in-refine',
10
- meta: {
11
- type: 'problem',
12
- docs: {
13
- zodImportAllowedSource,
14
- description: 'Disallow throwing errors directly inside Zod refine callbacks',
15
- },
16
- messages: {
17
- noThrowInRefine: 'Do not throw errors directly inside a z.refine callback.',
18
- },
19
- schema: [],
20
- },
21
- defaultOptions: [],
22
- create(context) {
23
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, } = trackZodSchemaImports();
24
- function checkNode(node) {
25
- if (!node) {
26
- return;
27
- }
28
- switch (node.type) {
29
- case utils_1.AST_NODE_TYPES.ThrowStatement:
30
- context.report({ node, messageId: 'noThrowInRefine' });
31
- break;
32
- case utils_1.AST_NODE_TYPES.BlockStatement:
33
- node.body.forEach(checkNode);
34
- break;
35
- case utils_1.AST_NODE_TYPES.IfStatement:
36
- checkNode(node.consequent);
37
- if (node.alternate) {
38
- checkNode(node.alternate);
39
- }
40
- break;
41
- case utils_1.AST_NODE_TYPES.ForStatement:
42
- case utils_1.AST_NODE_TYPES.ForInStatement:
43
- case utils_1.AST_NODE_TYPES.ForOfStatement:
44
- case utils_1.AST_NODE_TYPES.WhileStatement:
45
- case utils_1.AST_NODE_TYPES.DoWhileStatement:
46
- checkNode(node.body);
47
- break;
48
- case utils_1.AST_NODE_TYPES.TryStatement:
49
- checkNode(node.block);
50
- if (node.handler) {
51
- checkNode(node.handler.body);
52
- }
53
- if (node.finalizer) {
54
- checkNode(node.finalizer);
55
- }
56
- break;
57
- case utils_1.AST_NODE_TYPES.FunctionExpression:
58
- case utils_1.AST_NODE_TYPES.ArrowFunctionExpression:
59
- case utils_1.AST_NODE_TYPES.FunctionDeclaration:
60
- return;
61
- default:
62
- if ('body' in node && Array.isArray(node.body)) {
63
- node.body.forEach(checkNode);
64
- }
65
- break;
66
- }
67
- }
68
- return {
69
- ImportDeclaration: importDeclarationListener,
70
- CallExpression(node) {
71
- const zodSchemaMeta = detectZodSchemaRootNode(node);
72
- if (!zodSchemaMeta) {
73
- return;
74
- }
75
- const methods = collectZodChainMethods(node);
76
- const refineMethod = methods.find((it) => it.name === 'refine');
77
- if (!refineMethod) {
78
- return;
79
- }
80
- const [callback] = refineMethod.node.arguments;
81
- if (callback.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
82
- callback.type === utils_1.AST_NODE_TYPES.FunctionExpression) {
83
- checkNode(callback.body);
84
- }
85
- },
86
- };
87
- },
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ const require_track_zod_schema_imports = require("../utils/track-zod-schema-imports.cjs");
4
+ let _typescript_eslint_utils = require("@typescript-eslint/utils");
5
+ //#region src/rules/no-throw-in-refine.ts
6
+ const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("zod");
7
+ const noThrowInRefine = require_create_plugin_rule.createZodPluginRule({
8
+ name: "no-throw-in-refine",
9
+ meta: {
10
+ type: "problem",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Disallow throwing errors directly inside Zod refine callbacks"
14
+ },
15
+ messages: { noThrowInRefine: "Do not throw errors directly inside a z.refine callback." },
16
+ schema: []
17
+ },
18
+ defaultOptions: [],
19
+ create(context) {
20
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
21
+ function checkNode(node) {
22
+ if (!node) return;
23
+ switch (node.type) {
24
+ case _typescript_eslint_utils.AST_NODE_TYPES.ThrowStatement:
25
+ context.report({
26
+ node,
27
+ messageId: "noThrowInRefine"
28
+ });
29
+ break;
30
+ case _typescript_eslint_utils.AST_NODE_TYPES.BlockStatement:
31
+ node.body.forEach(checkNode);
32
+ break;
33
+ case _typescript_eslint_utils.AST_NODE_TYPES.IfStatement:
34
+ checkNode(node.consequent);
35
+ if (node.alternate) checkNode(node.alternate);
36
+ break;
37
+ case _typescript_eslint_utils.AST_NODE_TYPES.ForStatement:
38
+ case _typescript_eslint_utils.AST_NODE_TYPES.ForInStatement:
39
+ case _typescript_eslint_utils.AST_NODE_TYPES.ForOfStatement:
40
+ case _typescript_eslint_utils.AST_NODE_TYPES.WhileStatement:
41
+ case _typescript_eslint_utils.AST_NODE_TYPES.DoWhileStatement:
42
+ checkNode(node.body);
43
+ break;
44
+ case _typescript_eslint_utils.AST_NODE_TYPES.TryStatement:
45
+ checkNode(node.block);
46
+ if (node.handler) checkNode(node.handler.body);
47
+ if (node.finalizer) checkNode(node.finalizer);
48
+ break;
49
+ case _typescript_eslint_utils.AST_NODE_TYPES.FunctionExpression:
50
+ case _typescript_eslint_utils.AST_NODE_TYPES.ArrowFunctionExpression:
51
+ case _typescript_eslint_utils.AST_NODE_TYPES.FunctionDeclaration: return;
52
+ default:
53
+ if ("body" in node && Array.isArray(node.body)) node.body.forEach(checkNode);
54
+ break;
55
+ }
56
+ }
57
+ return {
58
+ ImportDeclaration: importDeclarationListener,
59
+ CallExpression(node) {
60
+ if (!detectZodSchemaRootNode(node)) return;
61
+ const refineMethod = collectZodChainMethods(node).find((it) => it.name === "refine");
62
+ if (!refineMethod) return;
63
+ const [callback] = refineMethod.node.arguments;
64
+ if (callback.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrowFunctionExpression || callback.type === _typescript_eslint_utils.AST_NODE_TYPES.FunctionExpression) checkNode(callback.body);
65
+ }
66
+ };
67
+ }
88
68
  });
69
+ //#endregion
70
+ exports.noThrowInRefine = noThrowInRefine;
@@ -0,0 +1,69 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
3
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
4
+ //#region src/rules/no-throw-in-refine.ts
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("zod");
6
+ const noThrowInRefine = createZodPluginRule({
7
+ name: "no-throw-in-refine",
8
+ meta: {
9
+ type: "problem",
10
+ docs: {
11
+ zodImportAllowedSource,
12
+ description: "Disallow throwing errors directly inside Zod refine callbacks"
13
+ },
14
+ messages: { noThrowInRefine: "Do not throw errors directly inside a z.refine callback." },
15
+ schema: []
16
+ },
17
+ defaultOptions: [],
18
+ create(context) {
19
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
20
+ function checkNode(node) {
21
+ if (!node) return;
22
+ switch (node.type) {
23
+ case AST_NODE_TYPES.ThrowStatement:
24
+ context.report({
25
+ node,
26
+ messageId: "noThrowInRefine"
27
+ });
28
+ break;
29
+ case AST_NODE_TYPES.BlockStatement:
30
+ node.body.forEach(checkNode);
31
+ break;
32
+ case AST_NODE_TYPES.IfStatement:
33
+ checkNode(node.consequent);
34
+ if (node.alternate) checkNode(node.alternate);
35
+ break;
36
+ case AST_NODE_TYPES.ForStatement:
37
+ case AST_NODE_TYPES.ForInStatement:
38
+ case AST_NODE_TYPES.ForOfStatement:
39
+ case AST_NODE_TYPES.WhileStatement:
40
+ case AST_NODE_TYPES.DoWhileStatement:
41
+ checkNode(node.body);
42
+ break;
43
+ case AST_NODE_TYPES.TryStatement:
44
+ checkNode(node.block);
45
+ if (node.handler) checkNode(node.handler.body);
46
+ if (node.finalizer) checkNode(node.finalizer);
47
+ break;
48
+ case AST_NODE_TYPES.FunctionExpression:
49
+ case AST_NODE_TYPES.ArrowFunctionExpression:
50
+ case AST_NODE_TYPES.FunctionDeclaration: return;
51
+ default:
52
+ if ("body" in node && Array.isArray(node.body)) node.body.forEach(checkNode);
53
+ break;
54
+ }
55
+ }
56
+ return {
57
+ ImportDeclaration: importDeclarationListener,
58
+ CallExpression(node) {
59
+ if (!detectZodSchemaRootNode(node)) return;
60
+ const refineMethod = collectZodChainMethods(node).find((it) => it.name === "refine");
61
+ if (!refineMethod) return;
62
+ const [callback] = refineMethod.node.arguments;
63
+ if (callback.type === AST_NODE_TYPES.ArrowFunctionExpression || callback.type === AST_NODE_TYPES.FunctionExpression) checkNode(callback.body);
64
+ }
65
+ };
66
+ }
67
+ });
68
+ //#endregion
69
+ export { noThrowInRefine };
@@ -1,36 +1,30 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUnknownSchema = void 0;
4
- const create_plugin_rule_js_1 = require("../utils/create-plugin-rule.cjs");
5
- const track_zod_schema_imports_js_1 = require("../utils/track-zod-schema-imports.cjs");
6
- const { zodImportAllowedSource, trackZodSchemaImports, } = (0, track_zod_schema_imports_js_1.createZodSchemaImportTrack)('all');
7
- exports.noUnknownSchema = (0, create_plugin_rule_js_1.createZodPluginRule)({
8
- name: 'no-unknown-schema',
9
- meta: {
10
- type: 'suggestion',
11
- docs: {
12
- zodImportAllowedSource,
13
- description: 'Disallow usage of `z.unknown()` in Zod schemas',
14
- },
15
- messages: {
16
- noZUnknown: 'Using `z.unknown()` is not allowed. Please use a more specific schema.',
17
- },
18
- schema: [],
19
- },
20
- defaultOptions: [],
21
- create(context) {
22
- const { importDeclarationListener, detectZodSchemaRootNode, } = trackZodSchemaImports();
23
- return {
24
- ImportDeclaration: importDeclarationListener,
25
- CallExpression(node) {
26
- const zodSchemaMeta = detectZodSchemaRootNode(node);
27
- if ((zodSchemaMeta === null || zodSchemaMeta === void 0 ? void 0 : zodSchemaMeta.schemaType) === 'unknown') {
28
- context.report({
29
- node,
30
- messageId: 'noZUnknown',
31
- });
32
- }
33
- },
34
- };
35
- },
1
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
2
+ //#region src/rules/no-unknown-schema.ts
3
+ const { zodImportAllowedSource, trackZodSchemaImports } = require("../utils/track-zod-schema-imports.cjs").createZodSchemaImportTrack("all");
4
+ const noUnknownSchema = require_create_plugin_rule.createZodPluginRule({
5
+ name: "no-unknown-schema",
6
+ meta: {
7
+ type: "suggestion",
8
+ docs: {
9
+ zodImportAllowedSource,
10
+ description: "Disallow usage of `z.unknown()` in Zod schemas"
11
+ },
12
+ messages: { noZUnknown: "Using `z.unknown()` is not allowed. Please use a more specific schema." },
13
+ schema: []
14
+ },
15
+ defaultOptions: [],
16
+ create(context) {
17
+ const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
18
+ return {
19
+ ImportDeclaration: importDeclarationListener,
20
+ CallExpression(node) {
21
+ if (detectZodSchemaRootNode(node)?.schemaType === "unknown") context.report({
22
+ node,
23
+ messageId: "noZUnknown"
24
+ });
25
+ }
26
+ };
27
+ }
36
28
  });
29
+ //#endregion
30
+ exports.noUnknownSchema = noUnknownSchema;
@@ -0,0 +1,31 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
3
+ //#region src/rules/no-unknown-schema.ts
4
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("all");
5
+ const noUnknownSchema = createZodPluginRule({
6
+ name: "no-unknown-schema",
7
+ meta: {
8
+ type: "suggestion",
9
+ docs: {
10
+ zodImportAllowedSource,
11
+ description: "Disallow usage of `z.unknown()` in Zod schemas"
12
+ },
13
+ messages: { noZUnknown: "Using `z.unknown()` is not allowed. Please use a more specific schema." },
14
+ schema: []
15
+ },
16
+ defaultOptions: [],
17
+ create(context) {
18
+ const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
19
+ return {
20
+ ImportDeclaration: importDeclarationListener,
21
+ CallExpression(node) {
22
+ if (detectZodSchemaRootNode(node)?.schemaType === "unknown") context.report({
23
+ node,
24
+ messageId: "noZUnknown"
25
+ });
26
+ }
27
+ };
28
+ }
29
+ });
30
+ //#endregion
31
+ export { noUnknownSchema };
@@ -1,80 +1,64 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.preferEnumOverLiteralUnion = void 0;
4
- const utils_1 = require("@typescript-eslint/utils");
5
- const create_plugin_rule_js_1 = require("../utils/create-plugin-rule.cjs");
6
- const track_zod_schema_imports_js_1 = require("../utils/track-zod-schema-imports.cjs");
7
- const { zodImportAllowedSource, trackZodSchemaImports, } = (0, track_zod_schema_imports_js_1.createZodSchemaImportTrack)('zod');
8
- exports.preferEnumOverLiteralUnion = (0, create_plugin_rule_js_1.createZodPluginRule)({
9
- name: 'prefer-enum-over-literal-union',
10
- meta: {
11
- type: 'suggestion',
12
- fixable: 'code',
13
- docs: {
14
- zodImportAllowedSource,
15
- description: 'Prefer `z.enum()` over `z.union()` when all members are string literals.',
16
- },
17
- messages: {
18
- useEnum: 'Replace this union of string literals with `z.enum()`.',
19
- },
20
- schema: [],
21
- },
22
- defaultOptions: [],
23
- create(context) {
24
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, } = trackZodSchemaImports();
25
- return {
26
- ImportDeclaration: importDeclarationListener,
27
- CallExpression(node) {
28
- const zodSchema = detectZodSchemaRootNode(node);
29
- if ((zodSchema === null || zodSchema === void 0 ? void 0 : zodSchema.schemaType) !== 'union') {
30
- return;
31
- }
32
- const methods = collectZodChainMethods(zodSchema.node);
33
- const union = methods.find((it) => it.name === 'union');
34
- if (!union) {
35
- return;
36
- }
37
- const unionNode = union.node;
38
- const unionArgument = unionNode.arguments.at(0);
39
- if ((unionArgument === null || unionArgument === void 0 ? void 0 : unionArgument.type) !== utils_1.AST_NODE_TYPES.ArrayExpression) {
40
- return;
41
- }
42
- const zodLiteralStrings = unionArgument.elements.map((s) => {
43
- if (!s) {
44
- return null;
45
- }
46
- const maybeLiteralSchema = detectZodSchemaRootNode(s);
47
- if ((maybeLiteralSchema === null || maybeLiteralSchema === void 0 ? void 0 : maybeLiteralSchema.schemaType) !== 'literal') {
48
- return null;
49
- }
50
- const [literalArgument] = maybeLiteralSchema.node.arguments;
51
- if (literalArgument.type === utils_1.AST_NODE_TYPES.Literal &&
52
- typeof literalArgument.value === 'string') {
53
- return literalArgument.raw;
54
- }
55
- return null;
56
- });
57
- if (zodLiteralStrings.some((it) => it === null)) {
58
- return;
59
- }
60
- if (zodSchema.schemaDecl === 'named') {
61
- context.report({
62
- node,
63
- messageId: 'useEnum',
64
- });
65
- return;
66
- }
67
- context.report({
68
- node,
69
- messageId: 'useEnum',
70
- fix(fixer) {
71
- return [
72
- fixer.replaceText(unionNode.callee.property, 'enum'),
73
- fixer.replaceText(unionNode.arguments[0], `[${zodLiteralStrings.join(', ')}]`),
74
- ];
75
- },
76
- });
77
- },
78
- };
79
- },
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ const require_track_zod_schema_imports = require("../utils/track-zod-schema-imports.cjs");
4
+ let _typescript_eslint_utils = require("@typescript-eslint/utils");
5
+ //#region src/rules/prefer-enum-over-literal-union.ts
6
+ const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("zod");
7
+ const preferEnumOverLiteralUnion = require_create_plugin_rule.createZodPluginRule({
8
+ name: "prefer-enum-over-literal-union",
9
+ meta: {
10
+ type: "suggestion",
11
+ fixable: "code",
12
+ docs: {
13
+ zodImportAllowedSource,
14
+ description: "Prefer `z.enum()` over `z.union()` when all members are string literals."
15
+ },
16
+ messages: { useEnum: "Replace this union of string literals with `z.enum()`." },
17
+ schema: []
18
+ },
19
+ defaultOptions: [],
20
+ create(context) {
21
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
22
+ return {
23
+ ImportDeclaration: importDeclarationListener,
24
+ CallExpression(node) {
25
+ const zodSchema = detectZodSchemaRootNode(node);
26
+ if (zodSchema?.schemaType !== "union") return;
27
+ const union = collectZodChainMethods(zodSchema.node).find((it) => it.name === "union");
28
+ if (!union) return;
29
+ const unionNode = union.node;
30
+ const unionArgument = unionNode.arguments.at(0);
31
+ if (unionArgument?.type !== _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression) return;
32
+ /**
33
+ * Collect zod literals name that later can be used to implement the fix,
34
+ * if the item isn't a literal schema return null
35
+ */
36
+ const zodLiteralStrings = unionArgument.elements.map((s) => {
37
+ if (!s) return null;
38
+ const maybeLiteralSchema = detectZodSchemaRootNode(s);
39
+ if (maybeLiteralSchema?.schemaType !== "literal") return null;
40
+ const [literalArgument] = maybeLiteralSchema.node.arguments;
41
+ if (literalArgument.type === _typescript_eslint_utils.AST_NODE_TYPES.Literal && typeof literalArgument.value === "string") return literalArgument.raw;
42
+ return null;
43
+ });
44
+ if (zodLiteralStrings.some((it) => it === null)) return;
45
+ if (zodSchema.schemaDecl === "named") {
46
+ context.report({
47
+ node,
48
+ messageId: "useEnum"
49
+ });
50
+ return;
51
+ }
52
+ context.report({
53
+ node,
54
+ messageId: "useEnum",
55
+ fix(fixer) {
56
+ return [fixer.replaceText(unionNode.callee.property, "enum"), fixer.replaceText(unionNode.arguments[0], `[${zodLiteralStrings.join(", ")}]`)];
57
+ }
58
+ });
59
+ }
60
+ };
61
+ }
80
62
  });
63
+ //#endregion
64
+ exports.preferEnumOverLiteralUnion = preferEnumOverLiteralUnion;
@@ -0,0 +1,63 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
3
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
4
+ //#region src/rules/prefer-enum-over-literal-union.ts
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("zod");
6
+ const preferEnumOverLiteralUnion = createZodPluginRule({
7
+ name: "prefer-enum-over-literal-union",
8
+ meta: {
9
+ type: "suggestion",
10
+ fixable: "code",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Prefer `z.enum()` over `z.union()` when all members are string literals."
14
+ },
15
+ messages: { useEnum: "Replace this union of string literals with `z.enum()`." },
16
+ schema: []
17
+ },
18
+ defaultOptions: [],
19
+ create(context) {
20
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
21
+ return {
22
+ ImportDeclaration: importDeclarationListener,
23
+ CallExpression(node) {
24
+ const zodSchema = detectZodSchemaRootNode(node);
25
+ if (zodSchema?.schemaType !== "union") return;
26
+ const union = collectZodChainMethods(zodSchema.node).find((it) => it.name === "union");
27
+ if (!union) return;
28
+ const unionNode = union.node;
29
+ const unionArgument = unionNode.arguments.at(0);
30
+ if (unionArgument?.type !== AST_NODE_TYPES.ArrayExpression) return;
31
+ /**
32
+ * Collect zod literals name that later can be used to implement the fix,
33
+ * if the item isn't a literal schema return null
34
+ */
35
+ const zodLiteralStrings = unionArgument.elements.map((s) => {
36
+ if (!s) return null;
37
+ const maybeLiteralSchema = detectZodSchemaRootNode(s);
38
+ if (maybeLiteralSchema?.schemaType !== "literal") return null;
39
+ const [literalArgument] = maybeLiteralSchema.node.arguments;
40
+ if (literalArgument.type === AST_NODE_TYPES.Literal && typeof literalArgument.value === "string") return literalArgument.raw;
41
+ return null;
42
+ });
43
+ if (zodLiteralStrings.some((it) => it === null)) return;
44
+ if (zodSchema.schemaDecl === "named") {
45
+ context.report({
46
+ node,
47
+ messageId: "useEnum"
48
+ });
49
+ return;
50
+ }
51
+ context.report({
52
+ node,
53
+ messageId: "useEnum",
54
+ fix(fixer) {
55
+ return [fixer.replaceText(unionNode.callee.property, "enum"), fixer.replaceText(unionNode.arguments[0], `[${zodLiteralStrings.join(", ")}]`)];
56
+ }
57
+ });
58
+ }
59
+ };
60
+ }
61
+ });
62
+ //#endregion
63
+ export { preferEnumOverLiteralUnion };