eslint-plugin-zod 4.4.0 → 4.5.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.
Files changed (54) hide show
  1. package/README.md +1 -0
  2. package/dist/index.cjs +3 -0
  3. package/dist/index.mjs +3 -0
  4. package/dist/meta.cjs +0 -1
  5. package/dist/rules/array-style.cjs +0 -1
  6. package/dist/rules/consistent-import-source.cjs +2 -26
  7. package/dist/rules/consistent-import-source.mjs +2 -25
  8. package/dist/rules/consistent-import.cjs +3 -80
  9. package/dist/rules/consistent-import.mjs +3 -79
  10. package/dist/rules/consistent-object-schema-type.cjs +2 -41
  11. package/dist/rules/consistent-object-schema-type.mjs +3 -41
  12. package/dist/rules/consistent-schema-output-type-style.cjs +3 -42
  13. package/dist/rules/consistent-schema-output-type-style.mjs +4 -42
  14. package/dist/rules/consistent-schema-var-name.cjs +2 -26
  15. package/dist/rules/consistent-schema-var-name.mjs +3 -26
  16. package/dist/rules/no-any-schema.cjs +2 -41
  17. package/dist/rules/no-any-schema.mjs +3 -41
  18. package/dist/rules/no-empty-custom-schema.cjs +2 -18
  19. package/dist/rules/no-empty-custom-schema.mjs +3 -18
  20. package/dist/rules/no-native-enum.cjs +0 -1
  21. package/dist/rules/no-number-schema-with-finite.cjs +0 -1
  22. package/dist/rules/no-number-schema-with-int.cjs +0 -1
  23. package/dist/rules/no-number-schema-with-is-finite.cjs +0 -1
  24. package/dist/rules/no-number-schema-with-is-int.cjs +0 -1
  25. package/dist/rules/no-number-schema-with-safe.cjs +0 -1
  26. package/dist/rules/no-number-schema-with-step.cjs +0 -1
  27. package/dist/rules/no-optional-and-default-together.cjs +0 -1
  28. package/dist/rules/no-promise-schema.cjs +29 -0
  29. package/dist/rules/no-promise-schema.mjs +29 -0
  30. package/dist/rules/no-schema-with-is-nullable.cjs +0 -1
  31. package/dist/rules/no-schema-with-is-optional.cjs +0 -1
  32. package/dist/rules/no-string-schema-with-uuid.cjs +0 -1
  33. package/dist/rules/no-throw-in-refine.cjs +0 -1
  34. package/dist/rules/no-transform-in-record-key.cjs +0 -1
  35. package/dist/rules/no-unknown-schema.cjs +2 -14
  36. package/dist/rules/no-unknown-schema.mjs +3 -14
  37. package/dist/rules/prefer-enum-over-literal-union.cjs +2 -39
  38. package/dist/rules/prefer-enum-over-literal-union.mjs +3 -39
  39. package/dist/rules/prefer-loose-object.cjs +0 -1
  40. package/dist/rules/prefer-meta-last.cjs +0 -1
  41. package/dist/rules/prefer-meta.cjs +0 -1
  42. package/dist/rules/prefer-strict-object.cjs +0 -1
  43. package/dist/rules/prefer-string-schema-with-trim.cjs +0 -1
  44. package/dist/rules/prefer-top-level-string-formats.cjs +0 -1
  45. package/dist/rules/prefer-trim-before-string-length-checks.cjs +0 -1
  46. package/dist/rules/require-brand-type-parameter.cjs +2 -28
  47. package/dist/rules/require-brand-type-parameter.mjs +3 -28
  48. package/dist/rules/require-error-message.cjs +2 -62
  49. package/dist/rules/require-error-message.mjs +3 -62
  50. package/dist/rules/schema-error-property-style.cjs +2 -59
  51. package/dist/rules/schema-error-property-style.mjs +3 -58
  52. package/dist/utils/create-plugin-rule.cjs +0 -1
  53. package/package.json +5 -7
  54. package/dist/_virtual/_rolldown/runtime.cjs +0 -23
package/README.md CHANGED
@@ -50,6 +50,7 @@ Find out more about [Oxlint's `jsPLugins`](https://oxc.rs/docs/guide/usage/linte
50
50
  | [no-number-schema-with-safe](docs/rules/no-number-schema-with-safe.md) | Disallow deprecated `z.number().safe()`. Use `z.int()`; `.safe()` is now identical to `.int()`. | ✅ | 🔧 | | |
51
51
  | [no-number-schema-with-step](docs/rules/no-number-schema-with-step.md) | Disallow deprecated `z.number().step()`. Use `.multipleOf()` instead. | ✅ | 🔧 | | |
52
52
  | [no-optional-and-default-together](docs/rules/no-optional-and-default-together.md) | Disallow using both `.optional()` and `.default()` on the same Zod schema | ✅ | 🔧 | | |
53
+ | [no-promise-schema](docs/rules/no-promise-schema.md) | Disallow deprecated `z.promise()` schemas. | ✅ | | | |
53
54
  | [no-schema-with-is-nullable](docs/rules/no-schema-with-is-nullable.md) | Disallow deprecated `.isNullable()` on a Zod schema; use `safeParse(null).success` instead. | ✅ | | | |
54
55
  | [no-schema-with-is-optional](docs/rules/no-schema-with-is-optional.md) | Disallow deprecated `.isOptional()` on a Zod schema; use `safeParse(undefined).success` instead. | ✅ | | | |
55
56
  | [no-string-schema-with-uuid](docs/rules/no-string-schema-with-uuid.md) | Disallow usage of `z.string().uuid()` in favor of the dedicated `z.uuid()` schema | ✅ | 🔧 | | ❌ |
package/dist/index.cjs CHANGED
@@ -15,6 +15,7 @@ const require_no_number_schema_with_is_int = require("./rules/no-number-schema-w
15
15
  const require_no_number_schema_with_safe = require("./rules/no-number-schema-with-safe.cjs");
16
16
  const require_no_number_schema_with_step = require("./rules/no-number-schema-with-step.cjs");
17
17
  const require_no_optional_and_default_together = require("./rules/no-optional-and-default-together.cjs");
18
+ const require_no_promise_schema = require("./rules/no-promise-schema.cjs");
18
19
  const require_no_schema_with_is_nullable = require("./rules/no-schema-with-is-nullable.cjs");
19
20
  const require_no_schema_with_is_optional = require("./rules/no-schema-with-is-optional.cjs");
20
21
  const require_no_string_schema_with_uuid = require("./rules/no-string-schema-with-uuid.cjs");
@@ -56,6 +57,7 @@ const eslintPluginZod = {
56
57
  "no-number-schema-with-step": require_no_number_schema_with_step.noNumberSchemaWithStep,
57
58
  "no-string-schema-with-uuid": require_no_string_schema_with_uuid.noStringSchemaWithUuid,
58
59
  "no-optional-and-default-together": require_no_optional_and_default_together.noOptionalAndDefaultTogether,
60
+ "no-promise-schema": require_no_promise_schema.noPromiseSchema,
59
61
  "no-schema-with-is-nullable": require_no_schema_with_is_nullable.noSchemaWithIsNullable,
60
62
  "no-schema-with-is-optional": require_no_schema_with_is_optional.noSchemaWithIsOptional,
61
63
  "no-throw-in-refine": require_no_throw_in_refine.noThrowInRefine,
@@ -93,6 +95,7 @@ const recommendedConfig = {
93
95
  "zod/no-number-schema-with-step": "error",
94
96
  "zod/no-string-schema-with-uuid": "error",
95
97
  "zod/no-optional-and-default-together": "error",
98
+ "zod/no-promise-schema": "error",
96
99
  "zod/no-schema-with-is-nullable": "error",
97
100
  "zod/no-schema-with-is-optional": "error",
98
101
  "zod/no-throw-in-refine": "error",
package/dist/index.mjs CHANGED
@@ -15,6 +15,7 @@ import { noNumberSchemaWithIsInt } from "./rules/no-number-schema-with-is-int.mj
15
15
  import { noNumberSchemaWithSafe } from "./rules/no-number-schema-with-safe.mjs";
16
16
  import { noNumberSchemaWithStep } from "./rules/no-number-schema-with-step.mjs";
17
17
  import { noOptionalAndDefaultTogether } from "./rules/no-optional-and-default-together.mjs";
18
+ import { noPromiseSchema } from "./rules/no-promise-schema.mjs";
18
19
  import { noSchemaWithIsNullable } from "./rules/no-schema-with-is-nullable.mjs";
19
20
  import { noSchemaWithIsOptional } from "./rules/no-schema-with-is-optional.mjs";
20
21
  import { noStringSchemaWithUuid } from "./rules/no-string-schema-with-uuid.mjs";
@@ -56,6 +57,7 @@ const eslintPluginZod = {
56
57
  "no-number-schema-with-step": noNumberSchemaWithStep,
57
58
  "no-string-schema-with-uuid": noStringSchemaWithUuid,
58
59
  "no-optional-and-default-together": noOptionalAndDefaultTogether,
60
+ "no-promise-schema": noPromiseSchema,
59
61
  "no-schema-with-is-nullable": noSchemaWithIsNullable,
60
62
  "no-schema-with-is-optional": noSchemaWithIsOptional,
61
63
  "no-throw-in-refine": noThrowInRefine,
@@ -93,6 +95,7 @@ const recommendedConfig = {
93
95
  "zod/no-number-schema-with-step": "error",
94
96
  "zod/no-string-schema-with-uuid": "error",
95
97
  "zod/no-optional-and-default-together": "error",
98
+ "zod/no-promise-schema": "error",
96
99
  "zod/no-schema-with-is-nullable": "error",
97
100
  "zod/no-schema-with-is-optional": "error",
98
101
  "zod/no-throw-in-refine": "error",
package/dist/meta.cjs CHANGED
@@ -1,4 +1,3 @@
1
- require("./_virtual/_rolldown/runtime.cjs");
2
1
  let node_fs = require("node:fs");
3
2
  //#region src/meta.ts
4
3
  const { name: PLUGIN_NAME, version: PLUGIN_VERSION, repository } = JSON.parse((0, node_fs.readFileSync)(new URL("../package.json", require("url").pathToFileURL(__filename).href), "utf8"));
@@ -1,4 +1,3 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
2
  let _eslint_zod_utils = require("@eslint-zod/utils");
4
3
  //#region src/rules/array-style.ts
@@ -1,6 +1,6 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
2
  let _eslint_zod_utils = require("@eslint-zod/utils");
3
+ let _eslint_zod_utils_rule_builders_consistent_import_source = require("@eslint-zod/utils/rule-builders/consistent-import-source");
4
4
  //#region src/rules/consistent-import-source.ts
5
5
  const consistentImportSource = require_create_plugin_rule.createZodPluginRule({
6
6
  name: "consistent-import-source",
@@ -28,31 +28,7 @@ const consistentImportSource = require_create_plugin_rule.createZodPluginRule({
28
28
  }]
29
29
  },
30
30
  defaultOptions: [{ sources: ["zod"] }],
31
- create(context, [{ sources }]) {
32
- return { ImportDeclaration(node) {
33
- const sourceValue = node.source.value;
34
- if (!_eslint_zod_utils.zodImportScope.isAllowed(sourceValue)) return;
35
- if (sources.includes(sourceValue)) return;
36
- context.report({
37
- node,
38
- messageId: "sourceNotAllowed",
39
- data: {
40
- source: sourceValue,
41
- sources: sources.map((s) => `"${s}"`).join(", ")
42
- },
43
- suggest: sources.map((it) => ({
44
- messageId: "replaceSource",
45
- data: {
46
- valid: it,
47
- invalid: sourceValue
48
- },
49
- fix(fixer) {
50
- return fixer.replaceText(node.source, node.source.raw.replace(sourceValue, it));
51
- }
52
- }))
53
- });
54
- } };
55
- }
31
+ create: (0, _eslint_zod_utils_rule_builders_consistent_import_source.buildConsistentImportSourceCreate)(_eslint_zod_utils.zodImportScope)
56
32
  });
57
33
  //#endregion
58
34
  exports.consistentImportSource = consistentImportSource;
@@ -1,5 +1,6 @@
1
1
  import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
2
  import { zodImportScope } from "@eslint-zod/utils";
3
+ import { buildConsistentImportSourceCreate } from "@eslint-zod/utils/rule-builders/consistent-import-source";
3
4
  //#region src/rules/consistent-import-source.ts
4
5
  const consistentImportSource = createZodPluginRule({
5
6
  name: "consistent-import-source",
@@ -27,31 +28,7 @@ const consistentImportSource = createZodPluginRule({
27
28
  }]
28
29
  },
29
30
  defaultOptions: [{ sources: ["zod"] }],
30
- create(context, [{ sources }]) {
31
- return { ImportDeclaration(node) {
32
- const sourceValue = node.source.value;
33
- if (!zodImportScope.isAllowed(sourceValue)) return;
34
- if (sources.includes(sourceValue)) return;
35
- context.report({
36
- node,
37
- messageId: "sourceNotAllowed",
38
- data: {
39
- source: sourceValue,
40
- sources: sources.map((s) => `"${s}"`).join(", ")
41
- },
42
- suggest: sources.map((it) => ({
43
- messageId: "replaceSource",
44
- data: {
45
- valid: it,
46
- invalid: sourceValue
47
- },
48
- fix(fixer) {
49
- return fixer.replaceText(node.source, node.source.raw.replace(sourceValue, it));
50
- }
51
- }))
52
- });
53
- } };
54
- }
31
+ create: buildConsistentImportSourceCreate(zodImportScope)
55
32
  });
56
33
  //#endregion
57
34
  export { consistentImportSource };
@@ -1,6 +1,6 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
2
  let _eslint_zod_utils = require("@eslint-zod/utils");
3
+ let _eslint_zod_utils_rule_builders_consistent_import = require("@eslint-zod/utils/rule-builders/consistent-import");
4
4
  //#region src/rules/consistent-import.ts
5
5
  const consistentImport = require_create_plugin_rule.createZodPluginRule({
6
6
  name: "consistent-import",
@@ -18,90 +18,13 @@ const consistentImport = require_create_plugin_rule.createZodPluginRule({
18
18
  properties: { syntax: {
19
19
  description: "Specifies the import syntax to use for Zod.",
20
20
  type: "string",
21
- enum: _eslint_zod_utils.IMPORT_SYNTAXES
21
+ enum: _eslint_zod_utils_rule_builders_consistent_import.IMPORT_SYNTAXES
22
22
  } },
23
23
  additionalProperties: false
24
24
  }]
25
25
  },
26
26
  defaultOptions: [{ syntax: "namespace" }],
27
- create(context, [options]) {
28
- const { syntax } = options;
29
- const { sourceCode } = context;
30
- const importGroups = {};
31
- return {
32
- ImportDeclaration(node) {
33
- const { source, importKind } = node;
34
- if (!_eslint_zod_utils.zodImportScope.isAllowed(source.value)) return;
35
- importGroups[source.value] ??= {
36
- hasOnlyTypeImports: true,
37
- nodes: []
38
- };
39
- if (importGroups[source.value].hasOnlyTypeImports && importKind === "value") importGroups[source.value].hasOnlyTypeImports = false;
40
- importGroups[source.value].nodes.push(node);
41
- },
42
- "Program:exit": function() {
43
- let namespaceAliasNameIndex = 0;
44
- for (const importGroup of Object.values(importGroups)) {
45
- const { hasOnlyTypeImports, nodes } = importGroup;
46
- const [firstImportNode, ...othersImportNodes] = nodes;
47
- /**
48
- * Variable to track all specifiers that later are used by {@link getDeclaredVariables}
49
- * to add the namespace prefix
50
- */
51
- const nodesWithVariablesToUpdate = [];
52
- let namespaceAliasName = null;
53
- for (const specifier of nodes.flatMap((it) => it.specifiers)) {
54
- if (!namespaceAliasName) {
55
- namespaceAliasName = (0, _eslint_zod_utils.getNamespaceAliasNameFrom)(specifier);
56
- if (namespaceAliasName) continue;
57
- }
58
- nodesWithVariablesToUpdate.push(specifier);
59
- }
60
- if (!namespaceAliasName) {
61
- namespaceAliasName = "z";
62
- if (namespaceAliasNameIndex > 0) {
63
- namespaceAliasName = `z${namespaceAliasNameIndex}`;
64
- namespaceAliasNameIndex += 1;
65
- }
66
- }
67
- if (!(0, _eslint_zod_utils.isGroupFirstImportKindValidForSyntax)(importGroup, syntax)) context.report({
68
- node: firstImportNode,
69
- messageId: "changeImportSyntax",
70
- data: { syntax },
71
- fix(fixer) {
72
- const importTypeKeyword = hasOnlyTypeImports ? "type " : "";
73
- let importSpecifier;
74
- if (syntax === "named") if (namespaceAliasName === "z") importSpecifier = "{ z }";
75
- else importSpecifier = `{ z as ${namespaceAliasName} }`;
76
- else importSpecifier = `* as ${namespaceAliasName}`;
77
- const newImportText = `import ${importTypeKeyword}${importSpecifier} from ${firstImportNode.source.raw};`;
78
- return fixer.replaceText(firstImportNode, newImportText);
79
- }
80
- });
81
- const allReferences = nodesWithVariablesToUpdate.flatMap((it) => sourceCode.getDeclaredVariables(it)).flatMap((it) => it.references);
82
- for (const ref of allReferences) {
83
- const { identifier } = ref;
84
- if ((0, _eslint_zod_utils.shouldIdentifierBeRenamed)(identifier)) context.report({
85
- node: identifier,
86
- messageId: "convertUsage",
87
- data: { syntax },
88
- fix(fixer) {
89
- const newId = `${namespaceAliasName}.${identifier.name}`;
90
- return fixer.replaceText(identifier, newId);
91
- }
92
- });
93
- }
94
- for (const extraImport of othersImportNodes) context.report({
95
- node: extraImport,
96
- messageId: "removeDuplicate",
97
- fix(fixer) {
98
- return fixer.removeRange(extraImport.range);
99
- }
100
- });
101
- }
102
- }
103
- };
104
- }
27
+ create: (0, _eslint_zod_utils_rule_builders_consistent_import.buildConsistentImportCreate)(_eslint_zod_utils.zodImportScope)
105
28
  });
106
29
  //#endregion
107
30
  exports.consistentImport = consistentImport;
@@ -1,5 +1,6 @@
1
1
  import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
- import { IMPORT_SYNTAXES, getNamespaceAliasNameFrom, isGroupFirstImportKindValidForSyntax, shouldIdentifierBeRenamed, zodImportScope } from "@eslint-zod/utils";
2
+ import { zodImportScope } from "@eslint-zod/utils";
3
+ import { IMPORT_SYNTAXES, buildConsistentImportCreate } from "@eslint-zod/utils/rule-builders/consistent-import";
3
4
  //#region src/rules/consistent-import.ts
4
5
  const consistentImport = createZodPluginRule({
5
6
  name: "consistent-import",
@@ -23,84 +24,7 @@ const consistentImport = createZodPluginRule({
23
24
  }]
24
25
  },
25
26
  defaultOptions: [{ syntax: "namespace" }],
26
- create(context, [options]) {
27
- const { syntax } = options;
28
- const { sourceCode } = context;
29
- const importGroups = {};
30
- return {
31
- ImportDeclaration(node) {
32
- const { source, importKind } = node;
33
- if (!zodImportScope.isAllowed(source.value)) return;
34
- importGroups[source.value] ??= {
35
- hasOnlyTypeImports: true,
36
- nodes: []
37
- };
38
- if (importGroups[source.value].hasOnlyTypeImports && importKind === "value") importGroups[source.value].hasOnlyTypeImports = false;
39
- importGroups[source.value].nodes.push(node);
40
- },
41
- "Program:exit": function() {
42
- let namespaceAliasNameIndex = 0;
43
- for (const importGroup of Object.values(importGroups)) {
44
- const { hasOnlyTypeImports, nodes } = importGroup;
45
- const [firstImportNode, ...othersImportNodes] = nodes;
46
- /**
47
- * Variable to track all specifiers that later are used by {@link getDeclaredVariables}
48
- * to add the namespace prefix
49
- */
50
- const nodesWithVariablesToUpdate = [];
51
- let namespaceAliasName = null;
52
- for (const specifier of nodes.flatMap((it) => it.specifiers)) {
53
- if (!namespaceAliasName) {
54
- namespaceAliasName = getNamespaceAliasNameFrom(specifier);
55
- if (namespaceAliasName) continue;
56
- }
57
- nodesWithVariablesToUpdate.push(specifier);
58
- }
59
- if (!namespaceAliasName) {
60
- namespaceAliasName = "z";
61
- if (namespaceAliasNameIndex > 0) {
62
- namespaceAliasName = `z${namespaceAliasNameIndex}`;
63
- namespaceAliasNameIndex += 1;
64
- }
65
- }
66
- if (!isGroupFirstImportKindValidForSyntax(importGroup, syntax)) context.report({
67
- node: firstImportNode,
68
- messageId: "changeImportSyntax",
69
- data: { syntax },
70
- fix(fixer) {
71
- const importTypeKeyword = hasOnlyTypeImports ? "type " : "";
72
- let importSpecifier;
73
- if (syntax === "named") if (namespaceAliasName === "z") importSpecifier = "{ z }";
74
- else importSpecifier = `{ z as ${namespaceAliasName} }`;
75
- else importSpecifier = `* as ${namespaceAliasName}`;
76
- const newImportText = `import ${importTypeKeyword}${importSpecifier} from ${firstImportNode.source.raw};`;
77
- return fixer.replaceText(firstImportNode, newImportText);
78
- }
79
- });
80
- const allReferences = nodesWithVariablesToUpdate.flatMap((it) => sourceCode.getDeclaredVariables(it)).flatMap((it) => it.references);
81
- for (const ref of allReferences) {
82
- const { identifier } = ref;
83
- if (shouldIdentifierBeRenamed(identifier)) context.report({
84
- node: identifier,
85
- messageId: "convertUsage",
86
- data: { syntax },
87
- fix(fixer) {
88
- const newId = `${namespaceAliasName}.${identifier.name}`;
89
- return fixer.replaceText(identifier, newId);
90
- }
91
- });
92
- }
93
- for (const extraImport of othersImportNodes) context.report({
94
- node: extraImport,
95
- messageId: "removeDuplicate",
96
- fix(fixer) {
97
- return fixer.removeRange(extraImport.range);
98
- }
99
- });
100
- }
101
- }
102
- };
103
- }
27
+ create: buildConsistentImportCreate(zodImportScope)
104
28
  });
105
29
  //#endregion
106
30
  export { consistentImport };
@@ -1,7 +1,6 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
2
  let _eslint_zod_utils = require("@eslint-zod/utils");
4
- let _typescript_eslint_utils = require("@typescript-eslint/utils");
3
+ let _eslint_zod_utils_rule_builders_consistent_object_schema_type = require("@eslint-zod/utils/rule-builders/consistent-object-schema-type");
5
4
  //#region src/rules/consistent-object-schema-type.ts
6
5
  const ZOD_OBJECT_METHODS = [
7
6
  "object",
@@ -9,7 +8,6 @@ const ZOD_OBJECT_METHODS = [
9
8
  "strictObject"
10
9
  ];
11
10
  const defaultOptions = { allow: ["object"] };
12
- const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodImportScope);
13
11
  const consistentObjectSchemaType = require_create_plugin_rule.createZodPluginRule({
14
12
  name: "consistent-object-schema-type",
15
13
  meta: {
@@ -36,44 +34,7 @@ const consistentObjectSchemaType = require_create_plugin_rule.createZodPluginRul
36
34
  }]
37
35
  },
38
36
  defaultOptions: [defaultOptions],
39
- create(context, [{ allow: allowedList }]) {
40
- const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
41
- return {
42
- ImportDeclaration: importDeclarationListener,
43
- CallExpression(node) {
44
- const schemaType = detectZodSchemaRootNode(node)?.schemaType;
45
- if (!schemaType || !ZOD_OBJECT_METHODS.includes(schemaType)) return;
46
- if (allowedList.includes(schemaType)) return;
47
- const { callee } = node;
48
- if (callee.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) {
49
- context.report({
50
- node,
51
- messageId: "consistentMethod",
52
- data: {
53
- actual: schemaType,
54
- allowedList: allowedList.join(",")
55
- }
56
- });
57
- return;
58
- }
59
- if (callee.type === _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression) context.report({
60
- node,
61
- messageId: "consistentMethod",
62
- data: {
63
- actual: schemaType,
64
- allowedList: allowedList.join(",")
65
- },
66
- suggest: allowedList.map((it) => ({
67
- messageId: "useMethod",
68
- data: { expected: it },
69
- fix(fixer) {
70
- return fixer.replaceText(callee.property, it);
71
- }
72
- }))
73
- });
74
- }
75
- };
76
- }
37
+ create: (0, _eslint_zod_utils_rule_builders_consistent_object_schema_type.buildConsistentObjectSchemaTypeCreate)(_eslint_zod_utils.zodImportScope)
77
38
  });
78
39
  //#endregion
79
40
  exports.consistentObjectSchemaType = consistentObjectSchemaType;
@@ -1,6 +1,6 @@
1
1
  import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
- import { createZodSchemaImportTrack, zodImportScope } from "@eslint-zod/utils";
3
- import { AST_NODE_TYPES } from "@typescript-eslint/utils";
2
+ import { zodImportScope } from "@eslint-zod/utils";
3
+ import { buildConsistentObjectSchemaTypeCreate } from "@eslint-zod/utils/rule-builders/consistent-object-schema-type";
4
4
  //#region src/rules/consistent-object-schema-type.ts
5
5
  const ZOD_OBJECT_METHODS = [
6
6
  "object",
@@ -8,7 +8,6 @@ const ZOD_OBJECT_METHODS = [
8
8
  "strictObject"
9
9
  ];
10
10
  const defaultOptions = { allow: ["object"] };
11
- const { trackZodSchemaImports } = createZodSchemaImportTrack(zodImportScope);
12
11
  const consistentObjectSchemaType = createZodPluginRule({
13
12
  name: "consistent-object-schema-type",
14
13
  meta: {
@@ -35,44 +34,7 @@ const consistentObjectSchemaType = createZodPluginRule({
35
34
  }]
36
35
  },
37
36
  defaultOptions: [defaultOptions],
38
- create(context, [{ allow: allowedList }]) {
39
- const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
40
- return {
41
- ImportDeclaration: importDeclarationListener,
42
- CallExpression(node) {
43
- const schemaType = detectZodSchemaRootNode(node)?.schemaType;
44
- if (!schemaType || !ZOD_OBJECT_METHODS.includes(schemaType)) return;
45
- if (allowedList.includes(schemaType)) return;
46
- const { callee } = node;
47
- if (callee.type === AST_NODE_TYPES.Identifier) {
48
- context.report({
49
- node,
50
- messageId: "consistentMethod",
51
- data: {
52
- actual: schemaType,
53
- allowedList: allowedList.join(",")
54
- }
55
- });
56
- return;
57
- }
58
- if (callee.type === AST_NODE_TYPES.MemberExpression) context.report({
59
- node,
60
- messageId: "consistentMethod",
61
- data: {
62
- actual: schemaType,
63
- allowedList: allowedList.join(",")
64
- },
65
- suggest: allowedList.map((it) => ({
66
- messageId: "useMethod",
67
- data: { expected: it },
68
- fix(fixer) {
69
- return fixer.replaceText(callee.property, it);
70
- }
71
- }))
72
- });
73
- }
74
- };
75
- }
37
+ create: buildConsistentObjectSchemaTypeCreate(zodImportScope)
76
38
  });
77
39
  //#endregion
78
40
  export { consistentObjectSchemaType };
@@ -1,10 +1,6 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
2
  let _eslint_zod_utils = require("@eslint-zod/utils");
4
- let _typescript_eslint_utils = require("@typescript-eslint/utils");
5
- //#region src/rules/consistent-schema-output-type-style.ts
6
- const ZOD_SCHEMA_TYPE_STYLES = ["infer", "output"];
7
- const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodImportScope);
3
+ let _eslint_zod_utils_rule_builders_consistent_schema_output_type_style = require("@eslint-zod/utils/rule-builders/consistent-schema-output-type-style");
8
4
  const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodPluginRule({
9
5
  name: "consistent-schema-output-type-style",
10
6
  meta: {
@@ -20,48 +16,13 @@ const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodPlug
20
16
  properties: { style: {
21
17
  description: "Decides which style to use for schema type inference",
22
18
  type: "string",
23
- enum: [...ZOD_SCHEMA_TYPE_STYLES]
19
+ enum: [...["infer", "output"]]
24
20
  } },
25
21
  additionalProperties: false
26
22
  }]
27
23
  },
28
24
  defaultOptions: [{ style: "output" }],
29
- create(context, [{ style }]) {
30
- const { importDeclarationListener, isZodNamespace, getNamedImportOriginal, getNamedImportLocal } = trackZodSchemaImports();
31
- return {
32
- ImportDeclaration: importDeclarationListener,
33
- TSTypeReference(node) {
34
- const { typeName } = node;
35
- if (typeName.type === _typescript_eslint_utils.AST_NODE_TYPES.TSQualifiedName) {
36
- const { left, right } = typeName;
37
- if (left.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier || !isZodNamespace(left.name)) return;
38
- const usedStyle = right.name;
39
- if (usedStyle !== "infer" && usedStyle !== "output" || usedStyle === style) return;
40
- context.report({
41
- node: right,
42
- messageId: style === "infer" ? "useInfer" : "useOutput",
43
- fix(fixer) {
44
- return fixer.replaceText(right, style);
45
- }
46
- });
47
- return;
48
- }
49
- if (typeName.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) {
50
- const originalName = getNamedImportOriginal(typeName.name);
51
- if (originalName !== "infer" && originalName !== "output" || originalName === style) return;
52
- const targetLocalName = getNamedImportLocal(style);
53
- context.report({
54
- node: typeName,
55
- messageId: style === "infer" ? "useInfer" : "useOutput",
56
- fix(fixer) {
57
- if (!targetLocalName || targetLocalName === "infer") return null;
58
- return fixer.replaceText(typeName, targetLocalName);
59
- }
60
- });
61
- }
62
- }
63
- };
64
- }
25
+ create: (0, _eslint_zod_utils_rule_builders_consistent_schema_output_type_style.buildConsistentSchemaOutputTypeStyleCreate)(_eslint_zod_utils.zodImportScope)
65
26
  });
66
27
  //#endregion
67
28
  exports.consistentSchemaOutputTypeStyle = consistentSchemaOutputTypeStyle;
@@ -1,9 +1,6 @@
1
1
  import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
- import { createZodSchemaImportTrack, zodImportScope } from "@eslint-zod/utils";
3
- import { AST_NODE_TYPES } from "@typescript-eslint/utils";
4
- //#region src/rules/consistent-schema-output-type-style.ts
5
- const ZOD_SCHEMA_TYPE_STYLES = ["infer", "output"];
6
- const { trackZodSchemaImports } = createZodSchemaImportTrack(zodImportScope);
2
+ import { zodImportScope } from "@eslint-zod/utils";
3
+ import { buildConsistentSchemaOutputTypeStyleCreate } from "@eslint-zod/utils/rule-builders/consistent-schema-output-type-style";
7
4
  const consistentSchemaOutputTypeStyle = createZodPluginRule({
8
5
  name: "consistent-schema-output-type-style",
9
6
  meta: {
@@ -19,48 +16,13 @@ const consistentSchemaOutputTypeStyle = createZodPluginRule({
19
16
  properties: { style: {
20
17
  description: "Decides which style to use for schema type inference",
21
18
  type: "string",
22
- enum: [...ZOD_SCHEMA_TYPE_STYLES]
19
+ enum: [...["infer", "output"]]
23
20
  } },
24
21
  additionalProperties: false
25
22
  }]
26
23
  },
27
24
  defaultOptions: [{ style: "output" }],
28
- create(context, [{ style }]) {
29
- const { importDeclarationListener, isZodNamespace, getNamedImportOriginal, getNamedImportLocal } = trackZodSchemaImports();
30
- return {
31
- ImportDeclaration: importDeclarationListener,
32
- TSTypeReference(node) {
33
- const { typeName } = node;
34
- if (typeName.type === AST_NODE_TYPES.TSQualifiedName) {
35
- const { left, right } = typeName;
36
- if (left.type !== AST_NODE_TYPES.Identifier || !isZodNamespace(left.name)) return;
37
- const usedStyle = right.name;
38
- if (usedStyle !== "infer" && usedStyle !== "output" || usedStyle === style) return;
39
- context.report({
40
- node: right,
41
- messageId: style === "infer" ? "useInfer" : "useOutput",
42
- fix(fixer) {
43
- return fixer.replaceText(right, style);
44
- }
45
- });
46
- return;
47
- }
48
- if (typeName.type === AST_NODE_TYPES.Identifier) {
49
- const originalName = getNamedImportOriginal(typeName.name);
50
- if (originalName !== "infer" && originalName !== "output" || originalName === style) return;
51
- const targetLocalName = getNamedImportLocal(style);
52
- context.report({
53
- node: typeName,
54
- messageId: style === "infer" ? "useInfer" : "useOutput",
55
- fix(fixer) {
56
- if (!targetLocalName || targetLocalName === "infer") return null;
57
- return fixer.replaceText(typeName, targetLocalName);
58
- }
59
- });
60
- }
61
- }
62
- };
63
- }
25
+ create: buildConsistentSchemaOutputTypeStyleCreate(zodImportScope)
64
26
  });
65
27
  //#endregion
66
28
  export { consistentSchemaOutputTypeStyle };
@@ -1,9 +1,7 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
2
  let _eslint_zod_utils = require("@eslint-zod/utils");
4
- let _typescript_eslint_utils = require("@typescript-eslint/utils");
3
+ let _eslint_zod_utils_rule_builders_consistent_schema_var_name = require("@eslint-zod/utils/rule-builders/consistent-schema-var-name");
5
4
  //#region src/rules/consistent-schema-var-name.ts
6
- const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodImportScope);
7
5
  const consistentSchemaVarName = require_create_plugin_rule.createZodPluginRule({
8
6
  name: "consistent-schema-var-name",
9
7
  meta: {
@@ -26,29 +24,7 @@ const consistentSchemaVarName = require_create_plugin_rule.createZodPluginRule({
26
24
  }]
27
25
  },
28
26
  defaultOptions: [{ after: "Schema" }],
29
- create(context, [{ before = "", after = "" }]) {
30
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
31
- return {
32
- ImportDeclaration: importDeclarationListener,
33
- VariableDeclarator(node) {
34
- const initNode = node.init;
35
- if (initNode?.type !== _typescript_eslint_utils.AST_NODE_TYPES.CallExpression || !detectZodSchemaRootNode(initNode)) return;
36
- const chainMethods = collectZodChainMethods(initNode).map((it) => it.name);
37
- if (_eslint_zod_utils.ZOD_NON_SCHEMA_PRODUCING_METHODS.some((it) => chainMethods.includes(it))) return;
38
- if (node.id.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return;
39
- const { name } = node.id;
40
- const validPrefix = !before || name.startsWith(before);
41
- const validSuffix = !after || name.endsWith(after);
42
- if (validPrefix && validSuffix) return;
43
- const expected = (validPrefix ? "" : before) + name + (validSuffix ? "" : after);
44
- context.report({
45
- node,
46
- messageId: "invalidName",
47
- data: { expected }
48
- });
49
- }
50
- };
51
- }
27
+ create: (0, _eslint_zod_utils_rule_builders_consistent_schema_var_name.buildConsistentSchemaVarNameCreate)(_eslint_zod_utils.zodImportScope)
52
28
  });
53
29
  //#endregion
54
30
  exports.consistentSchemaVarName = consistentSchemaVarName;