eslint-plugin-zod-core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Marco Pasqualetti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # eslint-plugin-zod-core
2
+
3
+ [![CI Status][CIBadge]][CIURL]
4
+ [![Code style: prettier][CodeStyleBadge]][CodeStyleURL]
5
+ [![Lint: eslint][lintBadge]][lintURL]
6
+ [![Open on npmx][npmVersionBadge]][npmVersionURL]
7
+ [![Open issue tracker][issuesBadge]][issuesURL]
8
+
9
+ [CIBadge]: https://img.shields.io/github/actions/workflow/status/marcalexiei/eslint-zod/ci.yml?style=for-the-badge&logo=github&event=push&label=CI
10
+ [CIURL]: https://github.com/marcalexiei/eslint-zod/actions/workflows/CI.yml/badge.svg
11
+ [CodeStyleBadge]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=for-the-badge&logo=prettier
12
+ [CodeStyleURL]: https://prettier.io
13
+ [npmVersionBadge]: https://img.shields.io/npm/v/eslint-plugin-zod-core.svg?style=for-the-badge&logo=npm
14
+ [npmVersionURL]: https://npmx.dev/package/eslint-plugin-zod-core
15
+ [lintBadge]: https://img.shields.io/badge/lint-eslint-3A33D1?logo=eslint&style=for-the-badge
16
+ [lintURL]: https://eslint.org
17
+ [issuesBadge]: https://img.shields.io/github/issues/marcalexiei/eslint-zod.svg?style=for-the-badge
18
+ [issuesURL]: https://github.com/marcalexiei/eslint-zod/issues
19
+
20
+ [ESLint](https://eslint.org) plugin that adds custom linting rules to enforce best practices when using [Zod Core](https://zod.dev) (`zod/v4/core`).
21
+
22
+ It can also work with [Oxlint](https://oxc.rs/docs/guide/usage/linter.html)!\
23
+ Find out more about [Oxlint's `jsPLugins`](https://oxc.rs/docs/guide/usage/linter/js-plugins.html).
24
+
25
+ ## Rules
26
+
27
+ <!-- begin auto-generated rules list -->
28
+
29
+ 💼 Configurations enabled in.\
30
+ ✅ Set in the `recommended` configuration.\
31
+ 🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
32
+
33
+ | Name                                | Description | 💼 | 🔧 |
34
+ | :--------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------- | :-- | :-- |
35
+ | [consistent-import](docs/rules/consistent-import.md) | Enforce a consistent import style for Zod core | ✅ | 🔧 |
36
+ | [consistent-schema-output-type-style](docs/rules/consistent-schema-output-type-style.md) | Enforce consistent use of core.infer or core.output for schema type inference | | 🔧 |
37
+
38
+ <!-- end auto-generated rules list -->
39
+
40
+ ## Installation
41
+
42
+ ### ESLint
43
+
44
+ Install `eslint` and `eslint-plugin-zod-core` using your preferred package manager:
45
+
46
+ ```shell
47
+ npm i --save-dev eslint eslint-plugin-zod-core
48
+ ```
49
+
50
+ ```shell
51
+ yarn add --dev eslint eslint-plugin-zod-core
52
+ ```
53
+
54
+ ```shell
55
+ pnpm add --save-dev eslint eslint-plugin-zod-core
56
+ ```
57
+
58
+ #### ESLint Configuration
59
+
60
+ 1. Import the plugin
61
+
62
+ ```ts
63
+ import eslintPluginZodCore from 'eslint-plugin-zod-core';
64
+ ```
65
+
66
+ 2. Add `recommended` config to your ESLint setup
67
+
68
+ ```ts
69
+ eslintPluginZodCore.configs.recommended,
70
+ ```
71
+
72
+ Here's a minimal example using the flat config format:
73
+
74
+ ```ts
75
+ // eslint.config.js
76
+ import { defineConfig } from 'eslint/config';
77
+ import eslint from '@eslint/js';
78
+ import eslintPluginZodCore from 'eslint-plugin-zod-core';
79
+
80
+ export default defineConfig(
81
+ eslint.configs.recommended,
82
+ eslintPluginZodCore.configs.recommended,
83
+ );
84
+ ```
85
+
86
+ ### Oxlint
87
+
88
+ Install `oxlint` and `eslint-plugin-zod-core` using your preferred package manager:
89
+
90
+ ```shell
91
+ npm i --save-dev oxlint eslint-plugin-zod-core
92
+ ```
93
+
94
+ ```shell
95
+ yarn add --dev oxlint eslint-plugin-zod-core
96
+ ```
97
+
98
+ ```shell
99
+ pnpm add --save-dev oxlint eslint-plugin-zod-core
100
+ ```
101
+
102
+ #### Oxlint Configuration
103
+
104
+ 1. Import the plugin
105
+
106
+ ```ts
107
+ import eslintPluginZodCore from 'eslint-plugin-zod-core';
108
+ ```
109
+
110
+ 2. Add `eslint-plugin-zod-core` to the `jsPlugins` key
111
+
112
+ ```ts
113
+ {
114
+ jsPlugins: ['eslint-plugin-zod-core'],
115
+ // ...
116
+ }
117
+ ```
118
+
119
+ 3. Add `eslintPluginZodCore.configs.recommended.rules` to your Oxlint config.\
120
+ Alternatively you can specify the rules manually
121
+
122
+ Here's a minimal example using the flat config format:
123
+
124
+ ```ts
125
+ // oxlint.config.ts
126
+ import eslintPluginZodCore from 'eslint-plugin-zod-core';
127
+ import { defineConfig } from 'oxlint';
128
+
129
+ export default defineConfig({
130
+ jsPlugins: ['eslint-plugin-zod-core'],
131
+ rules: {
132
+ ...eslintPluginZodCore.configs.recommended.rules,
133
+ },
134
+ });
135
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,30 @@
1
+ const require_meta = require("./meta.cjs");
2
+ const require_consistent_import = require("./rules/consistent-import.cjs");
3
+ const require_consistent_schema_output_type_style = require("./rules/consistent-schema-output-type-style.cjs");
4
+ //#region src/index.ts
5
+ const eslintPluginZodCore = {
6
+ meta: {
7
+ name: require_meta.PLUGIN_NAME,
8
+ version: require_meta.PLUGIN_VERSION
9
+ },
10
+ rules: {
11
+ "consistent-import": require_consistent_import.consistentImport,
12
+ "consistent-schema-output-type-style": require_consistent_schema_output_type_style.consistentSchemaOutputTypeStyle
13
+ }
14
+ };
15
+ const recommendedConfig = {
16
+ name: `${require_meta.PLUGIN_NAME}/recommended`,
17
+ files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
18
+ plugins: { "zod-core": eslintPluginZodCore },
19
+ rules: { "zod-core/consistent-import": "error" }
20
+ };
21
+ var src_default = {
22
+ ...eslintPluginZodCore,
23
+ configs: { recommended: recommendedConfig }
24
+ };
25
+ /**
26
+ * why `satisfies`?
27
+ * @see https://github.com/marcalexiei/eslint-zod/issues/49
28
+ */
29
+ //#endregion
30
+ 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,30 @@
1
+ import { PLUGIN_NAME, PLUGIN_VERSION } from "./meta.mjs";
2
+ import { consistentImport } from "./rules/consistent-import.mjs";
3
+ import { consistentSchemaOutputTypeStyle } from "./rules/consistent-schema-output-type-style.mjs";
4
+ //#region src/index.ts
5
+ const eslintPluginZodCore = {
6
+ meta: {
7
+ name: PLUGIN_NAME,
8
+ version: PLUGIN_VERSION
9
+ },
10
+ rules: {
11
+ "consistent-import": consistentImport,
12
+ "consistent-schema-output-type-style": consistentSchemaOutputTypeStyle
13
+ }
14
+ };
15
+ const recommendedConfig = {
16
+ name: `${PLUGIN_NAME}/recommended`,
17
+ files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
18
+ plugins: { "zod-core": eslintPluginZodCore },
19
+ rules: { "zod-core/consistent-import": "error" }
20
+ };
21
+ var src_default = {
22
+ ...eslintPluginZodCore,
23
+ configs: { recommended: recommendedConfig }
24
+ };
25
+ /**
26
+ * why `satisfies`?
27
+ * @see https://github.com/marcalexiei/eslint-zod/issues/49
28
+ */
29
+ //#endregion
30
+ export { src_default as default };
package/dist/meta.cjs ADDED
@@ -0,0 +1,10 @@
1
+ let node_fs = require("node:fs");
2
+ //#region src/meta.ts
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"));
4
+ function getRuleURL(ruleID) {
5
+ return `${repository.url}/blob/HEAD/${repository.directory}/docs/rules/${ruleID}.md`;
6
+ }
7
+ //#endregion
8
+ exports.PLUGIN_NAME = PLUGIN_NAME;
9
+ exports.PLUGIN_VERSION = PLUGIN_VERSION;
10
+ 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,102 @@
1
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
2
+ let _eslint_zod_utils = require("@eslint-zod/utils");
3
+ //#region src/rules/consistent-import.ts
4
+ const consistentImport = require_create_plugin_rule.createZodPluginRule({
5
+ name: "consistent-import",
6
+ meta: {
7
+ type: "problem",
8
+ docs: { description: "Enforce a consistent import style for Zod core" },
9
+ fixable: "code",
10
+ messages: {
11
+ changeImportSyntax: "Use a {{syntax}} import for Zod core.",
12
+ removeDuplicate: "Remove duplicate Zod core import; Zod core is already imported.",
13
+ convertUsage: "Update Zod core 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 core.",
19
+ type: "string",
20
+ enum: _eslint_zod_utils.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 (!_eslint_zod_utils.zodCoreImportScope.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
+ const nodesWithVariablesToUpdate = [];
47
+ let namespaceAliasName = null;
48
+ for (const specifier of nodes.flatMap((it) => it.specifiers)) {
49
+ if (!namespaceAliasName) {
50
+ namespaceAliasName = (0, _eslint_zod_utils.getNamespaceAliasNameFrom)(specifier);
51
+ if (namespaceAliasName) continue;
52
+ }
53
+ nodesWithVariablesToUpdate.push(specifier);
54
+ }
55
+ if (!namespaceAliasName) {
56
+ namespaceAliasName = "z";
57
+ if (namespaceAliasNameIndex > 0) {
58
+ namespaceAliasName = `z${namespaceAliasNameIndex}`;
59
+ namespaceAliasNameIndex += 1;
60
+ }
61
+ }
62
+ if (!(0, _eslint_zod_utils.isGroupFirstImportKindValidForSyntax)(importGroup, syntax)) context.report({
63
+ node: firstImportNode,
64
+ messageId: "changeImportSyntax",
65
+ data: { syntax },
66
+ fix(fixer) {
67
+ const importTypeKeyword = hasOnlyTypeImports ? "type " : "";
68
+ let importSpecifier;
69
+ if (syntax === "named") if (namespaceAliasName === "z") importSpecifier = "{ z }";
70
+ else importSpecifier = `{ z as ${namespaceAliasName} }`;
71
+ else importSpecifier = `* as ${namespaceAliasName}`;
72
+ const newImportText = `import ${importTypeKeyword}${importSpecifier} from ${firstImportNode.source.raw};`;
73
+ return fixer.replaceText(firstImportNode, newImportText);
74
+ }
75
+ });
76
+ const allReferences = nodesWithVariablesToUpdate.flatMap((it) => sourceCode.getDeclaredVariables(it)).flatMap((it) => it.references);
77
+ for (const ref of allReferences) {
78
+ const { identifier } = ref;
79
+ if ((0, _eslint_zod_utils.shouldIdentifierBeRenamed)(identifier)) context.report({
80
+ node: identifier,
81
+ messageId: "convertUsage",
82
+ data: { syntax },
83
+ fix(fixer) {
84
+ const newId = `${namespaceAliasName}.${identifier.name}`;
85
+ return fixer.replaceText(identifier, newId);
86
+ }
87
+ });
88
+ }
89
+ for (const extraImport of othersImportNodes) context.report({
90
+ node: extraImport,
91
+ messageId: "removeDuplicate",
92
+ fix(fixer) {
93
+ return fixer.removeRange(extraImport.range);
94
+ }
95
+ });
96
+ }
97
+ }
98
+ };
99
+ }
100
+ });
101
+ //#endregion
102
+ exports.consistentImport = consistentImport;
@@ -0,0 +1,102 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { IMPORT_SYNTAXES, getNamespaceAliasNameFrom, isGroupFirstImportKindValidForSyntax, shouldIdentifierBeRenamed, zodCoreImportScope } from "@eslint-zod/utils";
3
+ //#region src/rules/consistent-import.ts
4
+ const consistentImport = createZodPluginRule({
5
+ name: "consistent-import",
6
+ meta: {
7
+ type: "problem",
8
+ docs: { description: "Enforce a consistent import style for Zod core" },
9
+ fixable: "code",
10
+ messages: {
11
+ changeImportSyntax: "Use a {{syntax}} import for Zod core.",
12
+ removeDuplicate: "Remove duplicate Zod core import; Zod core is already imported.",
13
+ convertUsage: "Update Zod core 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 core.",
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 (!zodCoreImportScope.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
+ const nodesWithVariablesToUpdate = [];
47
+ let namespaceAliasName = null;
48
+ for (const specifier of nodes.flatMap((it) => it.specifiers)) {
49
+ if (!namespaceAliasName) {
50
+ namespaceAliasName = getNamespaceAliasNameFrom(specifier);
51
+ if (namespaceAliasName) continue;
52
+ }
53
+ nodesWithVariablesToUpdate.push(specifier);
54
+ }
55
+ if (!namespaceAliasName) {
56
+ namespaceAliasName = "z";
57
+ if (namespaceAliasNameIndex > 0) {
58
+ namespaceAliasName = `z${namespaceAliasNameIndex}`;
59
+ namespaceAliasNameIndex += 1;
60
+ }
61
+ }
62
+ if (!isGroupFirstImportKindValidForSyntax(importGroup, syntax)) context.report({
63
+ node: firstImportNode,
64
+ messageId: "changeImportSyntax",
65
+ data: { syntax },
66
+ fix(fixer) {
67
+ const importTypeKeyword = hasOnlyTypeImports ? "type " : "";
68
+ let importSpecifier;
69
+ if (syntax === "named") if (namespaceAliasName === "z") importSpecifier = "{ z }";
70
+ else importSpecifier = `{ z as ${namespaceAliasName} }`;
71
+ else importSpecifier = `* as ${namespaceAliasName}`;
72
+ const newImportText = `import ${importTypeKeyword}${importSpecifier} from ${firstImportNode.source.raw};`;
73
+ return fixer.replaceText(firstImportNode, newImportText);
74
+ }
75
+ });
76
+ const allReferences = nodesWithVariablesToUpdate.flatMap((it) => sourceCode.getDeclaredVariables(it)).flatMap((it) => it.references);
77
+ for (const ref of allReferences) {
78
+ const { identifier } = ref;
79
+ if (shouldIdentifierBeRenamed(identifier)) context.report({
80
+ node: identifier,
81
+ messageId: "convertUsage",
82
+ data: { syntax },
83
+ fix(fixer) {
84
+ const newId = `${namespaceAliasName}.${identifier.name}`;
85
+ return fixer.replaceText(identifier, newId);
86
+ }
87
+ });
88
+ }
89
+ for (const extraImport of othersImportNodes) context.report({
90
+ node: extraImport,
91
+ messageId: "removeDuplicate",
92
+ fix(fixer) {
93
+ return fixer.removeRange(extraImport.range);
94
+ }
95
+ });
96
+ }
97
+ }
98
+ };
99
+ }
100
+ });
101
+ //#endregion
102
+ export { consistentImport };
@@ -0,0 +1,66 @@
1
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
2
+ let _eslint_zod_utils = require("@eslint-zod/utils");
3
+ let _typescript_eslint_utils = require("@typescript-eslint/utils");
4
+ //#region src/rules/consistent-schema-output-type-style.ts
5
+ const ZOD_SCHEMA_TYPE_STYLES = ["infer", "output"];
6
+ const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodCoreImportScope);
7
+ const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodPluginRule({
8
+ name: "consistent-schema-output-type-style",
9
+ meta: {
10
+ type: "suggestion",
11
+ fixable: "code",
12
+ docs: { description: "Enforce consistent use of core.infer or core.output for schema type inference" },
13
+ messages: {
14
+ useInfer: "Use infer instead of output.",
15
+ useOutput: "Use output instead of infer."
16
+ },
17
+ schema: [{
18
+ type: "object",
19
+ properties: { style: {
20
+ description: "Decides which style to use for schema type inference",
21
+ type: "string",
22
+ enum: [...ZOD_SCHEMA_TYPE_STYLES]
23
+ } },
24
+ additionalProperties: false
25
+ }]
26
+ },
27
+ 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 === _typescript_eslint_utils.AST_NODE_TYPES.TSQualifiedName) {
35
+ const { left, right } = typeName;
36
+ if (left.type !== _typescript_eslint_utils.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 === _typescript_eslint_utils.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
+ }
64
+ });
65
+ //#endregion
66
+ exports.consistentSchemaOutputTypeStyle = consistentSchemaOutputTypeStyle;
@@ -0,0 +1,66 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { createZodSchemaImportTrack, zodCoreImportScope } 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(zodCoreImportScope);
7
+ const consistentSchemaOutputTypeStyle = createZodPluginRule({
8
+ name: "consistent-schema-output-type-style",
9
+ meta: {
10
+ type: "suggestion",
11
+ fixable: "code",
12
+ docs: { description: "Enforce consistent use of core.infer or core.output for schema type inference" },
13
+ messages: {
14
+ useInfer: "Use infer instead of output.",
15
+ useOutput: "Use output instead of infer."
16
+ },
17
+ schema: [{
18
+ type: "object",
19
+ properties: { style: {
20
+ description: "Decides which style to use for schema type inference",
21
+ type: "string",
22
+ enum: [...ZOD_SCHEMA_TYPE_STYLES]
23
+ } },
24
+ additionalProperties: false
25
+ }]
26
+ },
27
+ 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
+ }
64
+ });
65
+ //#endregion
66
+ export { consistentSchemaOutputTypeStyle };
@@ -0,0 +1,5 @@
1
+ const require_meta = require("../meta.cjs");
2
+ //#region src/utils/create-plugin-rule.ts
3
+ const createZodPluginRule = require("@typescript-eslint/utils").ESLintUtils.RuleCreator(require_meta.getRuleURL);
4
+ //#endregion
5
+ exports.createZodPluginRule = createZodPluginRule;
@@ -0,0 +1,6 @@
1
+ import { getRuleURL } from "../meta.mjs";
2
+ import { ESLintUtils } from "@typescript-eslint/utils";
3
+ //#region src/utils/create-plugin-rule.ts
4
+ const createZodPluginRule = ESLintUtils.RuleCreator(getRuleURL);
5
+ //#endregion
6
+ export { createZodPluginRule };
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "eslint-plugin-zod-core",
3
+ "version": "0.1.0",
4
+ "description": "ESLint plugin that adds custom linting rules to enforce best practices when using Zod core",
5
+ "keywords": [
6
+ "eslint",
7
+ "eslint-plugin",
8
+ "eslintplugin",
9
+ "oxlint",
10
+ "zod"
11
+ ],
12
+ "homepage": "https://github.com/marcalexiei/eslint-zod#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/marcalexiei/eslint-zod/issues"
15
+ },
16
+ "license": "MIT",
17
+ "author": "Marco Pasqualetti @marcalexiei",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/marcalexiei/eslint-zod",
21
+ "directory": "plugins/eslint-plugin-zod-core"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "main": "./dist/index.cjs",
28
+ "module": "./dist/index.mjs",
29
+ "types": "./dist/index.d.cts",
30
+ "exports": {
31
+ ".": {
32
+ "import": "./dist/index.mjs",
33
+ "require": "./dist/index.cjs"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "dependencies": {
41
+ "@typescript-eslint/utils": "^8.57.0",
42
+ "@eslint-zod/utils": "1.0.1"
43
+ },
44
+ "devDependencies": {
45
+ "@marcalexiei/prettier-config": "1.1.4",
46
+ "@typescript-eslint/rule-tester": "8.59.2",
47
+ "dedent": "1.7.2",
48
+ "eslint-doc-generator": "3.3.2",
49
+ "prettier": "3.8.3",
50
+ "tsdown": "0.21.10",
51
+ "vitest": "4.1.5"
52
+ },
53
+ "peerDependencies": {
54
+ "eslint": "^9 || ^10",
55
+ "oxlint": "^1.59.0",
56
+ "zod": "^4"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "eslint": {
60
+ "optional": true
61
+ },
62
+ "oxlint": {
63
+ "optional": true
64
+ },
65
+ "zod": {
66
+ "optional": true
67
+ }
68
+ },
69
+ "engines": {
70
+ "node": "^20 || ^22 || >=24"
71
+ },
72
+ "scripts": {
73
+ "build": "tsdown",
74
+ "build:docs": "pnpm run build && eslint-doc-generator",
75
+ "lint:docs": "eslint-doc-generator --check",
76
+ "test": "vitest"
77
+ }
78
+ }