eslint-plugin-zod-mini 1.0.0 → 1.0.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 (34) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/index.cjs +58 -0
  3. package/dist/index.d.cts +22 -0
  4. package/dist/index.d.mts +23 -0
  5. package/dist/index.mjs +58 -0
  6. package/dist/meta.cjs +11 -0
  7. package/dist/meta.mjs +8 -0
  8. package/dist/rules/consistent-import-source.cjs +58 -0
  9. package/dist/rules/consistent-import-source.mjs +57 -0
  10. package/dist/rules/consistent-import.cjs +107 -0
  11. package/dist/rules/consistent-import.mjs +106 -0
  12. package/dist/rules/consistent-object-schema-type.cjs +79 -0
  13. package/dist/rules/consistent-object-schema-type.mjs +78 -0
  14. package/dist/rules/consistent-schema-output-type-style.cjs +67 -0
  15. package/dist/rules/consistent-schema-output-type-style.mjs +66 -0
  16. package/dist/rules/consistent-schema-var-name.cjs +54 -0
  17. package/dist/rules/consistent-schema-var-name.mjs +53 -0
  18. package/dist/rules/no-any-schema.cjs +60 -0
  19. package/dist/rules/no-any-schema.mjs +59 -0
  20. package/dist/rules/no-empty-custom-schema.cjs +34 -0
  21. package/dist/rules/no-empty-custom-schema.mjs +33 -0
  22. package/dist/rules/no-unknown-schema.cjs +29 -0
  23. package/dist/rules/no-unknown-schema.mjs +28 -0
  24. package/dist/rules/prefer-meta.cjs +38 -0
  25. package/dist/rules/prefer-meta.mjs +37 -0
  26. package/dist/rules/require-brand-type-parameter.cjs +47 -0
  27. package/dist/rules/require-brand-type-parameter.mjs +46 -0
  28. package/dist/rules/require-error-message.cjs +82 -0
  29. package/dist/rules/require-error-message.mjs +81 -0
  30. package/dist/rules/schema-error-property-style.cjs +93 -0
  31. package/dist/rules/schema-error-property-style.mjs +91 -0
  32. package/dist/utils/create-plugin-rule.cjs +6 -0
  33. package/dist/utils/create-plugin-rule.mjs +6 -0
  34. package/package.json +2 -2
@@ -0,0 +1,23 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ exports.__toESM = __toESM;
package/dist/index.cjs ADDED
@@ -0,0 +1,58 @@
1
+ const require_meta = require("./meta.cjs");
2
+ const require_consistent_import_source = require("./rules/consistent-import-source.cjs");
3
+ const require_consistent_import = require("./rules/consistent-import.cjs");
4
+ const require_consistent_object_schema_type = require("./rules/consistent-object-schema-type.cjs");
5
+ const require_consistent_schema_output_type_style = require("./rules/consistent-schema-output-type-style.cjs");
6
+ const require_consistent_schema_var_name = require("./rules/consistent-schema-var-name.cjs");
7
+ const require_no_any_schema = require("./rules/no-any-schema.cjs");
8
+ const require_no_empty_custom_schema = require("./rules/no-empty-custom-schema.cjs");
9
+ const require_no_unknown_schema = require("./rules/no-unknown-schema.cjs");
10
+ const require_prefer_meta = require("./rules/prefer-meta.cjs");
11
+ const require_require_brand_type_parameter = require("./rules/require-brand-type-parameter.cjs");
12
+ const require_require_error_message = require("./rules/require-error-message.cjs");
13
+ const require_schema_error_property_style = require("./rules/schema-error-property-style.cjs");
14
+ //#region src/index.ts
15
+ const eslintPluginZodMini = {
16
+ meta: {
17
+ name: require_meta.PLUGIN_NAME,
18
+ version: require_meta.PLUGIN_VERSION
19
+ },
20
+ rules: {
21
+ "consistent-import": require_consistent_import.consistentImport,
22
+ "consistent-import-source": require_consistent_import_source.consistentImportSource,
23
+ "consistent-object-schema-type": require_consistent_object_schema_type.consistentObjectSchemaType,
24
+ "consistent-schema-output-type-style": require_consistent_schema_output_type_style.consistentSchemaOutputTypeStyle,
25
+ "consistent-schema-var-name": require_consistent_schema_var_name.consistentSchemaVarName,
26
+ "no-any-schema": require_no_any_schema.noAnySchema,
27
+ "no-empty-custom-schema": require_no_empty_custom_schema.noEmptyCustomSchema,
28
+ "no-unknown-schema": require_no_unknown_schema.noUnknownSchema,
29
+ "prefer-meta": require_prefer_meta.preferMeta,
30
+ "require-brand-type-parameter": require_require_brand_type_parameter.requireBrandTypeParameter,
31
+ "require-error-message": require_require_error_message.requireErrorMessage,
32
+ "schema-error-property-style": require_schema_error_property_style.schemaErrorPropertyStyle
33
+ }
34
+ };
35
+ const recommendedConfig = {
36
+ name: `${require_meta.PLUGIN_NAME}/recommended`,
37
+ files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
38
+ plugins: { "zod-mini": eslintPluginZodMini },
39
+ rules: {
40
+ "zod-mini/consistent-import": "error",
41
+ "zod-mini/consistent-schema-var-name": "error",
42
+ "zod-mini/no-any-schema": "error",
43
+ "zod-mini/no-empty-custom-schema": "error",
44
+ "zod-mini/prefer-meta": "error",
45
+ "zod-mini/require-brand-type-parameter": "error",
46
+ "zod-mini/require-error-message": "error"
47
+ }
48
+ };
49
+ var src_default = {
50
+ ...eslintPluginZodMini,
51
+ configs: { recommended: recommendedConfig }
52
+ };
53
+ /**
54
+ * why `satisfies`?
55
+ * @see https://github.com/marcalexiei/eslint-zod/issues/49
56
+ */
57
+ //#endregion
58
+ module.exports = src_default;
@@ -0,0 +1,22 @@
1
+ //#region src/index.d.ts
2
+ interface CompatibleConfig {
3
+ name?: string;
4
+ rules?: object;
5
+ plugins?: Record<string, CompatiblePlugin>;
6
+ }
7
+ interface CompatiblePlugin {
8
+ meta: {
9
+ name: string;
10
+ version: string;
11
+ };
12
+ }
13
+ declare const _default: {
14
+ configs: {
15
+ recommended: CompatibleConfig;
16
+ };
17
+ meta: {
18
+ name: string;
19
+ version: string;
20
+ };
21
+ };
22
+ export = _default;
@@ -0,0 +1,23 @@
1
+ //#region src/index.d.ts
2
+ interface CompatibleConfig {
3
+ name?: string;
4
+ rules?: object;
5
+ plugins?: Record<string, CompatiblePlugin>;
6
+ }
7
+ interface CompatiblePlugin {
8
+ meta: {
9
+ name: string;
10
+ version: string;
11
+ };
12
+ }
13
+ declare const _default: {
14
+ configs: {
15
+ recommended: CompatibleConfig;
16
+ };
17
+ meta: {
18
+ name: string;
19
+ version: string;
20
+ };
21
+ };
22
+ //#endregion
23
+ export { _default as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,58 @@
1
+ import { PLUGIN_NAME, PLUGIN_VERSION } from "./meta.mjs";
2
+ import { consistentImportSource } from "./rules/consistent-import-source.mjs";
3
+ import { consistentImport } from "./rules/consistent-import.mjs";
4
+ import { consistentObjectSchemaType } from "./rules/consistent-object-schema-type.mjs";
5
+ import { consistentSchemaOutputTypeStyle } from "./rules/consistent-schema-output-type-style.mjs";
6
+ import { consistentSchemaVarName } from "./rules/consistent-schema-var-name.mjs";
7
+ import { noAnySchema } from "./rules/no-any-schema.mjs";
8
+ import { noEmptyCustomSchema } from "./rules/no-empty-custom-schema.mjs";
9
+ import { noUnknownSchema } from "./rules/no-unknown-schema.mjs";
10
+ import { preferMeta } from "./rules/prefer-meta.mjs";
11
+ import { requireBrandTypeParameter } from "./rules/require-brand-type-parameter.mjs";
12
+ import { requireErrorMessage } from "./rules/require-error-message.mjs";
13
+ import { schemaErrorPropertyStyle } from "./rules/schema-error-property-style.mjs";
14
+ //#region src/index.ts
15
+ const eslintPluginZodMini = {
16
+ meta: {
17
+ name: PLUGIN_NAME,
18
+ version: PLUGIN_VERSION
19
+ },
20
+ rules: {
21
+ "consistent-import": consistentImport,
22
+ "consistent-import-source": consistentImportSource,
23
+ "consistent-object-schema-type": consistentObjectSchemaType,
24
+ "consistent-schema-output-type-style": consistentSchemaOutputTypeStyle,
25
+ "consistent-schema-var-name": consistentSchemaVarName,
26
+ "no-any-schema": noAnySchema,
27
+ "no-empty-custom-schema": noEmptyCustomSchema,
28
+ "no-unknown-schema": noUnknownSchema,
29
+ "prefer-meta": preferMeta,
30
+ "require-brand-type-parameter": requireBrandTypeParameter,
31
+ "require-error-message": requireErrorMessage,
32
+ "schema-error-property-style": schemaErrorPropertyStyle
33
+ }
34
+ };
35
+ const recommendedConfig = {
36
+ name: `${PLUGIN_NAME}/recommended`,
37
+ files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
38
+ plugins: { "zod-mini": eslintPluginZodMini },
39
+ rules: {
40
+ "zod-mini/consistent-import": "error",
41
+ "zod-mini/consistent-schema-var-name": "error",
42
+ "zod-mini/no-any-schema": "error",
43
+ "zod-mini/no-empty-custom-schema": "error",
44
+ "zod-mini/prefer-meta": "error",
45
+ "zod-mini/require-brand-type-parameter": "error",
46
+ "zod-mini/require-error-message": "error"
47
+ }
48
+ };
49
+ var src_default = {
50
+ ...eslintPluginZodMini,
51
+ configs: { recommended: recommendedConfig }
52
+ };
53
+ /**
54
+ * why `satisfies`?
55
+ * @see https://github.com/marcalexiei/eslint-zod/issues/49
56
+ */
57
+ //#endregion
58
+ export { src_default as default };
package/dist/meta.cjs ADDED
@@ -0,0 +1,11 @@
1
+ require("./_virtual/_rolldown/runtime.cjs");
2
+ let node_fs = require("node:fs");
3
+ //#region src/meta.ts
4
+ const { name: PLUGIN_NAME, version: PLUGIN_VERSION, repository } = JSON.parse((0, node_fs.readFileSync)(new URL("../package.json", require("url").pathToFileURL(__filename).href), "utf8"));
5
+ function getRuleURL(ruleID) {
6
+ return `${repository.url}/blob/HEAD/${repository.directory}/docs/rules/${ruleID}.md`;
7
+ }
8
+ //#endregion
9
+ exports.PLUGIN_NAME = PLUGIN_NAME;
10
+ exports.PLUGIN_VERSION = PLUGIN_VERSION;
11
+ exports.getRuleURL = getRuleURL;
package/dist/meta.mjs ADDED
@@ -0,0 +1,8 @@
1
+ import { readFileSync } from "node:fs";
2
+ //#region src/meta.ts
3
+ const { name: PLUGIN_NAME, version: PLUGIN_VERSION, repository } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
4
+ function getRuleURL(ruleID) {
5
+ return `${repository.url}/blob/HEAD/${repository.directory}/docs/rules/${ruleID}.md`;
6
+ }
7
+ //#endregion
8
+ export { PLUGIN_NAME, PLUGIN_VERSION, getRuleURL };
@@ -0,0 +1,58 @@
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ let _eslint_zod_utils = require("@eslint-zod/utils");
4
+ //#region src/rules/consistent-import-source.ts
5
+ const consistentImportSource = require_create_plugin_rule.createZodMiniPluginRule({
6
+ name: "consistent-import-source",
7
+ meta: {
8
+ hasSuggestions: true,
9
+ type: "suggestion",
10
+ docs: { description: "Enforce consistent source from Zod Mini imports" },
11
+ messages: {
12
+ sourceNotAllowed: "\"{{source}}\" is not allowed. Available values are: {{sources}}",
13
+ replaceSource: "Replace \"{{invalid}}\" with \"{{valid}}\""
14
+ },
15
+ schema: [{
16
+ type: "object",
17
+ properties: { sources: {
18
+ type: "array",
19
+ description: "An array of allowed Zod import sources.",
20
+ items: {
21
+ type: "string",
22
+ enum: [..._eslint_zod_utils.zodMiniImportScope.sources]
23
+ },
24
+ minItems: 1,
25
+ uniqueItems: true
26
+ } },
27
+ additionalProperties: false
28
+ }]
29
+ },
30
+ defaultOptions: [{ sources: ["zod/mini"] }],
31
+ create(context, [{ sources }]) {
32
+ return { ImportDeclaration(node) {
33
+ const sourceValue = node.source.value;
34
+ if (!_eslint_zod_utils.zodMiniImportScope.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
+ }
56
+ });
57
+ //#endregion
58
+ exports.consistentImportSource = consistentImportSource;
@@ -0,0 +1,57 @@
1
+ import { createZodMiniPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { zodMiniImportScope } from "@eslint-zod/utils";
3
+ //#region src/rules/consistent-import-source.ts
4
+ const consistentImportSource = createZodMiniPluginRule({
5
+ name: "consistent-import-source",
6
+ meta: {
7
+ hasSuggestions: true,
8
+ type: "suggestion",
9
+ docs: { description: "Enforce consistent source from Zod Mini imports" },
10
+ messages: {
11
+ sourceNotAllowed: "\"{{source}}\" is not allowed. Available values are: {{sources}}",
12
+ replaceSource: "Replace \"{{invalid}}\" with \"{{valid}}\""
13
+ },
14
+ schema: [{
15
+ type: "object",
16
+ properties: { sources: {
17
+ type: "array",
18
+ description: "An array of allowed Zod import sources.",
19
+ items: {
20
+ type: "string",
21
+ enum: [...zodMiniImportScope.sources]
22
+ },
23
+ minItems: 1,
24
+ uniqueItems: true
25
+ } },
26
+ additionalProperties: false
27
+ }]
28
+ },
29
+ defaultOptions: [{ sources: ["zod/mini"] }],
30
+ create(context, [{ sources }]) {
31
+ return { ImportDeclaration(node) {
32
+ const sourceValue = node.source.value;
33
+ if (!zodMiniImportScope.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
+ }
55
+ });
56
+ //#endregion
57
+ export { consistentImportSource };
@@ -0,0 +1,107 @@
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ let _eslint_zod_utils = require("@eslint-zod/utils");
4
+ //#region src/rules/consistent-import.ts
5
+ const consistentImport = require_create_plugin_rule.createZodMiniPluginRule({
6
+ name: "consistent-import",
7
+ meta: {
8
+ type: "problem",
9
+ docs: { description: "Enforce a consistent import style for Zod Mini" },
10
+ fixable: "code",
11
+ messages: {
12
+ changeImportSyntax: "Use a {{syntax}} import for Zod Mini.",
13
+ removeDuplicate: "Remove duplicate Zod Mini import; Zod Mini is already imported.",
14
+ convertUsage: "Update Zod Mini usage to match the {{syntax}} import syntax."
15
+ },
16
+ schema: [{
17
+ type: "object",
18
+ properties: { syntax: {
19
+ description: "Specifies the import syntax to use for Zod Mini.",
20
+ type: "string",
21
+ enum: _eslint_zod_utils.IMPORT_SYNTAXES
22
+ } },
23
+ additionalProperties: false
24
+ }]
25
+ },
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.zodMiniImportScope.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
+ }
105
+ });
106
+ //#endregion
107
+ exports.consistentImport = consistentImport;
@@ -0,0 +1,106 @@
1
+ import { createZodMiniPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { IMPORT_SYNTAXES, getNamespaceAliasNameFrom, isGroupFirstImportKindValidForSyntax, shouldIdentifierBeRenamed, zodMiniImportScope } from "@eslint-zod/utils";
3
+ //#region src/rules/consistent-import.ts
4
+ const consistentImport = createZodMiniPluginRule({
5
+ name: "consistent-import",
6
+ meta: {
7
+ type: "problem",
8
+ docs: { description: "Enforce a consistent import style for Zod Mini" },
9
+ fixable: "code",
10
+ messages: {
11
+ changeImportSyntax: "Use a {{syntax}} import for Zod Mini.",
12
+ removeDuplicate: "Remove duplicate Zod Mini import; Zod Mini is already imported.",
13
+ convertUsage: "Update Zod Mini usage to match the {{syntax}} import syntax."
14
+ },
15
+ schema: [{
16
+ type: "object",
17
+ properties: { syntax: {
18
+ description: "Specifies the import syntax to use for Zod Mini.",
19
+ type: "string",
20
+ enum: IMPORT_SYNTAXES
21
+ } },
22
+ additionalProperties: false
23
+ }]
24
+ },
25
+ 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 (!zodMiniImportScope.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
+ }
104
+ });
105
+ //#endregion
106
+ export { consistentImport };
@@ -0,0 +1,79 @@
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ let _eslint_zod_utils = require("@eslint-zod/utils");
4
+ let _typescript_eslint_utils = require("@typescript-eslint/utils");
5
+ //#region src/rules/consistent-object-schema-type.ts
6
+ const ZOD_OBJECT_METHODS = [
7
+ "object",
8
+ "looseObject",
9
+ "strictObject"
10
+ ];
11
+ const defaultOptions = { allow: ["object"] };
12
+ const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodMiniImportScope);
13
+ const consistentObjectSchemaType = require_create_plugin_rule.createZodMiniPluginRule({
14
+ name: "consistent-object-schema-type",
15
+ meta: {
16
+ hasSuggestions: true,
17
+ type: "suggestion",
18
+ docs: { description: "Enforce consistent usage of Zod Mini schema methods" },
19
+ messages: {
20
+ consistentMethod: "Inconsistent Zod Mini object schema method '{{actual}}'. Allowed: {{allowedList}}.",
21
+ useMethod: "Replace with '{{expected}}'."
22
+ },
23
+ schema: [{
24
+ type: "object",
25
+ properties: { allow: {
26
+ type: "array",
27
+ description: "Decides which object methods are allowed",
28
+ items: {
29
+ type: "string",
30
+ enum: [...ZOD_OBJECT_METHODS]
31
+ },
32
+ minItems: 1,
33
+ uniqueItems: true
34
+ } },
35
+ additionalProperties: false
36
+ }]
37
+ },
38
+ 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
+ }
77
+ });
78
+ //#endregion
79
+ exports.consistentObjectSchemaType = consistentObjectSchemaType;