eslint-plugin-zod-utils 1.0.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 +21 -0
- package/README.md +102 -0
- package/dist/index.cjs +255 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +227 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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,102 @@
|
|
|
1
|
+
# eslint-plugin-zod-utils
|
|
2
|
+
|
|
3
|
+
ESLint utilities for safer Zod schema usage.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save-dev eslint-plugin-zod-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Flat config:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import zodUtils from "eslint-plugin-zod-utils";
|
|
17
|
+
|
|
18
|
+
export default [
|
|
19
|
+
zodUtils.configs.recommended,
|
|
20
|
+
];
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or configure the rule directly:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import zodUtils from "eslint-plugin-zod-utils";
|
|
27
|
+
|
|
28
|
+
export default [
|
|
29
|
+
{
|
|
30
|
+
plugins: {
|
|
31
|
+
"zod-utils": zodUtils,
|
|
32
|
+
},
|
|
33
|
+
rules: {
|
|
34
|
+
"zod-utils/no-inline-zod-schema": "error",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Legacy eslintrc:
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"extends": ["plugin:zod-utils/legacy-recommended"]
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Rules
|
|
49
|
+
|
|
50
|
+
### `zod-utils/no-inline-zod-schema`
|
|
51
|
+
|
|
52
|
+
Requires Zod schemas to be created as module-level variable declarations. This avoids recreating schemas inside functions, callbacks, render paths, hooks, or argument lists.
|
|
53
|
+
|
|
54
|
+
This rule was inspired by the motivation behind [`babel-plugin-zod-hoist`](https://github.com/gajus/babel-plugin-zod-hoist#motivation), which documents the cost of repeatedly initializing equivalent Zod schemas and the benefits of hoisting them.
|
|
55
|
+
|
|
56
|
+
Invalid:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { z } from "zod";
|
|
60
|
+
|
|
61
|
+
function parseUser(input: unknown) {
|
|
62
|
+
const UserSchema = z.object({
|
|
63
|
+
id: z.string(),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return UserSchema.parse(input);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { z } from "zod";
|
|
72
|
+
|
|
73
|
+
const parser = createParser(z.object({
|
|
74
|
+
id: z.string(),
|
|
75
|
+
}));
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Valid:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { z } from "zod";
|
|
82
|
+
|
|
83
|
+
const UserSchema = z.object({
|
|
84
|
+
id: z.string(),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
function parseUser(input: unknown) {
|
|
88
|
+
return UserSchema.parse(input);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The rule understands `import { z } from "zod"`, `import * as zod from "zod"`, aliased `z` imports, and direct named schema factories such as `import { object, string } from "zod"`.
|
|
93
|
+
|
|
94
|
+
## Development
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
npm test
|
|
98
|
+
npm run typecheck
|
|
99
|
+
npm run lint
|
|
100
|
+
npm run build
|
|
101
|
+
npm pack --dry-run
|
|
102
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
configs: () => configs,
|
|
24
|
+
default: () => index_default,
|
|
25
|
+
rules: () => rules
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/rules/no-inline-zod-schema.ts
|
|
30
|
+
var import_utils = require("@typescript-eslint/utils");
|
|
31
|
+
var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
|
|
32
|
+
"any",
|
|
33
|
+
"array",
|
|
34
|
+
"base64",
|
|
35
|
+
"base64url",
|
|
36
|
+
"bigint",
|
|
37
|
+
"boolean",
|
|
38
|
+
"cidrv4",
|
|
39
|
+
"cidrv6",
|
|
40
|
+
"cuid",
|
|
41
|
+
"cuid2",
|
|
42
|
+
"custom",
|
|
43
|
+
"date",
|
|
44
|
+
"discriminatedUnion",
|
|
45
|
+
"e164",
|
|
46
|
+
"email",
|
|
47
|
+
"emoji",
|
|
48
|
+
"enum",
|
|
49
|
+
"file",
|
|
50
|
+
"function",
|
|
51
|
+
"instanceof",
|
|
52
|
+
"intersection",
|
|
53
|
+
"ipv4",
|
|
54
|
+
"ipv6",
|
|
55
|
+
"jwt",
|
|
56
|
+
"ksuid",
|
|
57
|
+
"lazy",
|
|
58
|
+
"literal",
|
|
59
|
+
"map",
|
|
60
|
+
"nan",
|
|
61
|
+
"nanoid",
|
|
62
|
+
"nativeEnum",
|
|
63
|
+
"never",
|
|
64
|
+
"null",
|
|
65
|
+
"nullable",
|
|
66
|
+
"number",
|
|
67
|
+
"object",
|
|
68
|
+
"optional",
|
|
69
|
+
"preprocess",
|
|
70
|
+
"promise",
|
|
71
|
+
"record",
|
|
72
|
+
"set",
|
|
73
|
+
"string",
|
|
74
|
+
"stringbool",
|
|
75
|
+
"symbol",
|
|
76
|
+
"templateLiteral",
|
|
77
|
+
"tuple",
|
|
78
|
+
"undefined",
|
|
79
|
+
"union",
|
|
80
|
+
"unknown",
|
|
81
|
+
"ulid",
|
|
82
|
+
"url",
|
|
83
|
+
"uuid",
|
|
84
|
+
"uuidv4",
|
|
85
|
+
"uuidv6",
|
|
86
|
+
"uuidv7",
|
|
87
|
+
"void",
|
|
88
|
+
"xid"
|
|
89
|
+
]);
|
|
90
|
+
var ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["z", "coerce", "iso"]);
|
|
91
|
+
function findVariable(scope, name) {
|
|
92
|
+
let currentScope = scope;
|
|
93
|
+
while (currentScope) {
|
|
94
|
+
const variable = currentScope.set?.get(name) ?? currentScope.variables?.find((candidate) => candidate.name === name);
|
|
95
|
+
if (variable) {
|
|
96
|
+
return variable;
|
|
97
|
+
}
|
|
98
|
+
currentScope = currentScope.upper ?? null;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
function getRootIdentifier(node) {
|
|
103
|
+
if (!node) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
switch (node.type) {
|
|
107
|
+
case "Identifier":
|
|
108
|
+
return node;
|
|
109
|
+
case "MemberExpression":
|
|
110
|
+
return getRootIdentifier(node.object);
|
|
111
|
+
case "CallExpression":
|
|
112
|
+
return getRootIdentifier(node.callee);
|
|
113
|
+
case "ChainExpression":
|
|
114
|
+
return getRootIdentifier(node.expression);
|
|
115
|
+
case "TSAsExpression":
|
|
116
|
+
case "TSNonNullExpression":
|
|
117
|
+
case "TSSatisfiesExpression":
|
|
118
|
+
case "TSTypeAssertion":
|
|
119
|
+
return getRootIdentifier(node.expression);
|
|
120
|
+
default:
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function isZodImportSpecifier(node) {
|
|
125
|
+
if (node.type !== "ImportNamespaceSpecifier" && node.type !== "ImportSpecifier") {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const declaration = node.parent;
|
|
129
|
+
if (declaration?.type !== "ImportDeclaration") {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
if (declaration.source.value !== "zod") {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (node.type === "ImportNamespaceSpecifier") {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
if (node.importKind === "type" || declaration.importKind === "type") {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
const importedName = node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
|
|
142
|
+
return ZOD_NAMESPACE_IMPORTS.has(importedName) || ZOD_FACTORY_IMPORTS.has(importedName);
|
|
143
|
+
}
|
|
144
|
+
function unwrapExpression(node) {
|
|
145
|
+
let current = node;
|
|
146
|
+
while (current.parent?.type === "TSAsExpression" || current.parent?.type === "TSNonNullExpression" || current.parent?.type === "TSSatisfiesExpression" || current.parent?.type === "TSTypeAssertion") {
|
|
147
|
+
current = current.parent;
|
|
148
|
+
}
|
|
149
|
+
return current;
|
|
150
|
+
}
|
|
151
|
+
function isModuleLevelVariableInitializer(node) {
|
|
152
|
+
const expression = unwrapExpression(node);
|
|
153
|
+
if (expression.parent?.type !== "VariableDeclarator") {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
if (expression.parent.init !== expression) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
const declaration = expression.parent.parent;
|
|
160
|
+
if (declaration.type !== "VariableDeclaration") {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const container = declaration.parent;
|
|
164
|
+
return container.type === "Program" || container.type === "ExportNamedDeclaration" && container.parent.type === "Program";
|
|
165
|
+
}
|
|
166
|
+
var noInlineZodSchema = import_utils.ESLintUtils.RuleCreator(
|
|
167
|
+
(ruleName2) => `https://www.npmjs.com/package/eslint-plugin-zod-utils#${ruleName2}`
|
|
168
|
+
)({
|
|
169
|
+
name: "no-inline-zod-schema",
|
|
170
|
+
meta: {
|
|
171
|
+
type: "problem",
|
|
172
|
+
docs: {
|
|
173
|
+
description: "Disallow creating Zod schemas outside module scope."
|
|
174
|
+
},
|
|
175
|
+
messages: {
|
|
176
|
+
inlineSchema: "Create Zod schemas at module scope instead of inline inside functions, callbacks, classes, or arguments."
|
|
177
|
+
},
|
|
178
|
+
schema: []
|
|
179
|
+
},
|
|
180
|
+
defaultOptions: [],
|
|
181
|
+
create(context) {
|
|
182
|
+
const sourceCode = context.sourceCode;
|
|
183
|
+
function isZodSchemaCall(node) {
|
|
184
|
+
const root = getRootIdentifier(node.callee);
|
|
185
|
+
if (!root) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const scope = sourceCode.getScope(root);
|
|
189
|
+
const variable = findVariable(scope, root.name);
|
|
190
|
+
return variable?.defs.some((definition) => isZodImportSpecifier(definition.node)) ?? false;
|
|
191
|
+
}
|
|
192
|
+
function hasZodCallAncestor(node) {
|
|
193
|
+
let current = node.parent;
|
|
194
|
+
while (current) {
|
|
195
|
+
if (current.type === "CallExpression" && isZodSchemaCall(current)) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
current = current.parent;
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
CallExpression(node) {
|
|
204
|
+
if (!isZodSchemaCall(node)) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (hasZodCallAncestor(node)) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (isModuleLevelVariableInitializer(node)) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
context.report({
|
|
214
|
+
node,
|
|
215
|
+
messageId: "inlineSchema"
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// src/index.ts
|
|
223
|
+
var ruleName = "zod-utils/no-inline-zod-schema";
|
|
224
|
+
var rules = {
|
|
225
|
+
"no-inline-zod-schema": noInlineZodSchema
|
|
226
|
+
};
|
|
227
|
+
var plugin = {
|
|
228
|
+
meta: {
|
|
229
|
+
name: "eslint-plugin-zod-utils",
|
|
230
|
+
version: "0.1.0"
|
|
231
|
+
},
|
|
232
|
+
rules,
|
|
233
|
+
configs: {}
|
|
234
|
+
};
|
|
235
|
+
plugin.configs.recommended = {
|
|
236
|
+
plugins: {
|
|
237
|
+
"zod-utils": plugin
|
|
238
|
+
},
|
|
239
|
+
rules: {
|
|
240
|
+
[ruleName]: "error"
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
plugin.configs["legacy-recommended"] = {
|
|
244
|
+
plugins: ["zod-utils"],
|
|
245
|
+
rules: {
|
|
246
|
+
[ruleName]: "error"
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
var configs = plugin.configs;
|
|
250
|
+
var index_default = plugin;
|
|
251
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
252
|
+
0 && (module.exports = {
|
|
253
|
+
configs,
|
|
254
|
+
rules
|
|
255
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
|
|
2
|
+
|
|
3
|
+
declare const ruleName = "zod-utils/no-inline-zod-schema";
|
|
4
|
+
declare const rules: {
|
|
5
|
+
"no-inline-zod-schema": _typescript_eslint_utils_ts_eslint.RuleModule<"inlineSchema", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener> & {
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
type Plugin = {
|
|
10
|
+
meta: {
|
|
11
|
+
name: string;
|
|
12
|
+
version: string;
|
|
13
|
+
};
|
|
14
|
+
rules: typeof rules;
|
|
15
|
+
configs: {
|
|
16
|
+
recommended?: {
|
|
17
|
+
plugins: {
|
|
18
|
+
"zod-utils": Plugin;
|
|
19
|
+
};
|
|
20
|
+
rules: Record<typeof ruleName, "error">;
|
|
21
|
+
};
|
|
22
|
+
"legacy-recommended"?: {
|
|
23
|
+
plugins: ["zod-utils"];
|
|
24
|
+
rules: Record<typeof ruleName, "error">;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare const plugin: Plugin;
|
|
29
|
+
declare const configs: {
|
|
30
|
+
recommended?: {
|
|
31
|
+
plugins: {
|
|
32
|
+
"zod-utils": Plugin;
|
|
33
|
+
};
|
|
34
|
+
rules: Record<typeof ruleName, "error">;
|
|
35
|
+
};
|
|
36
|
+
"legacy-recommended"?: {
|
|
37
|
+
plugins: ["zod-utils"];
|
|
38
|
+
rules: Record<typeof ruleName, "error">;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { configs, plugin as default, rules };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
|
|
2
|
+
|
|
3
|
+
declare const ruleName = "zod-utils/no-inline-zod-schema";
|
|
4
|
+
declare const rules: {
|
|
5
|
+
"no-inline-zod-schema": _typescript_eslint_utils_ts_eslint.RuleModule<"inlineSchema", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener> & {
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
type Plugin = {
|
|
10
|
+
meta: {
|
|
11
|
+
name: string;
|
|
12
|
+
version: string;
|
|
13
|
+
};
|
|
14
|
+
rules: typeof rules;
|
|
15
|
+
configs: {
|
|
16
|
+
recommended?: {
|
|
17
|
+
plugins: {
|
|
18
|
+
"zod-utils": Plugin;
|
|
19
|
+
};
|
|
20
|
+
rules: Record<typeof ruleName, "error">;
|
|
21
|
+
};
|
|
22
|
+
"legacy-recommended"?: {
|
|
23
|
+
plugins: ["zod-utils"];
|
|
24
|
+
rules: Record<typeof ruleName, "error">;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare const plugin: Plugin;
|
|
29
|
+
declare const configs: {
|
|
30
|
+
recommended?: {
|
|
31
|
+
plugins: {
|
|
32
|
+
"zod-utils": Plugin;
|
|
33
|
+
};
|
|
34
|
+
rules: Record<typeof ruleName, "error">;
|
|
35
|
+
};
|
|
36
|
+
"legacy-recommended"?: {
|
|
37
|
+
plugins: ["zod-utils"];
|
|
38
|
+
rules: Record<typeof ruleName, "error">;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { configs, plugin as default, rules };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// src/rules/no-inline-zod-schema.ts
|
|
2
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
3
|
+
var ZOD_FACTORY_IMPORTS = /* @__PURE__ */ new Set([
|
|
4
|
+
"any",
|
|
5
|
+
"array",
|
|
6
|
+
"base64",
|
|
7
|
+
"base64url",
|
|
8
|
+
"bigint",
|
|
9
|
+
"boolean",
|
|
10
|
+
"cidrv4",
|
|
11
|
+
"cidrv6",
|
|
12
|
+
"cuid",
|
|
13
|
+
"cuid2",
|
|
14
|
+
"custom",
|
|
15
|
+
"date",
|
|
16
|
+
"discriminatedUnion",
|
|
17
|
+
"e164",
|
|
18
|
+
"email",
|
|
19
|
+
"emoji",
|
|
20
|
+
"enum",
|
|
21
|
+
"file",
|
|
22
|
+
"function",
|
|
23
|
+
"instanceof",
|
|
24
|
+
"intersection",
|
|
25
|
+
"ipv4",
|
|
26
|
+
"ipv6",
|
|
27
|
+
"jwt",
|
|
28
|
+
"ksuid",
|
|
29
|
+
"lazy",
|
|
30
|
+
"literal",
|
|
31
|
+
"map",
|
|
32
|
+
"nan",
|
|
33
|
+
"nanoid",
|
|
34
|
+
"nativeEnum",
|
|
35
|
+
"never",
|
|
36
|
+
"null",
|
|
37
|
+
"nullable",
|
|
38
|
+
"number",
|
|
39
|
+
"object",
|
|
40
|
+
"optional",
|
|
41
|
+
"preprocess",
|
|
42
|
+
"promise",
|
|
43
|
+
"record",
|
|
44
|
+
"set",
|
|
45
|
+
"string",
|
|
46
|
+
"stringbool",
|
|
47
|
+
"symbol",
|
|
48
|
+
"templateLiteral",
|
|
49
|
+
"tuple",
|
|
50
|
+
"undefined",
|
|
51
|
+
"union",
|
|
52
|
+
"unknown",
|
|
53
|
+
"ulid",
|
|
54
|
+
"url",
|
|
55
|
+
"uuid",
|
|
56
|
+
"uuidv4",
|
|
57
|
+
"uuidv6",
|
|
58
|
+
"uuidv7",
|
|
59
|
+
"void",
|
|
60
|
+
"xid"
|
|
61
|
+
]);
|
|
62
|
+
var ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["z", "coerce", "iso"]);
|
|
63
|
+
function findVariable(scope, name) {
|
|
64
|
+
let currentScope = scope;
|
|
65
|
+
while (currentScope) {
|
|
66
|
+
const variable = currentScope.set?.get(name) ?? currentScope.variables?.find((candidate) => candidate.name === name);
|
|
67
|
+
if (variable) {
|
|
68
|
+
return variable;
|
|
69
|
+
}
|
|
70
|
+
currentScope = currentScope.upper ?? null;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function getRootIdentifier(node) {
|
|
75
|
+
if (!node) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
switch (node.type) {
|
|
79
|
+
case "Identifier":
|
|
80
|
+
return node;
|
|
81
|
+
case "MemberExpression":
|
|
82
|
+
return getRootIdentifier(node.object);
|
|
83
|
+
case "CallExpression":
|
|
84
|
+
return getRootIdentifier(node.callee);
|
|
85
|
+
case "ChainExpression":
|
|
86
|
+
return getRootIdentifier(node.expression);
|
|
87
|
+
case "TSAsExpression":
|
|
88
|
+
case "TSNonNullExpression":
|
|
89
|
+
case "TSSatisfiesExpression":
|
|
90
|
+
case "TSTypeAssertion":
|
|
91
|
+
return getRootIdentifier(node.expression);
|
|
92
|
+
default:
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function isZodImportSpecifier(node) {
|
|
97
|
+
if (node.type !== "ImportNamespaceSpecifier" && node.type !== "ImportSpecifier") {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const declaration = node.parent;
|
|
101
|
+
if (declaration?.type !== "ImportDeclaration") {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (declaration.source.value !== "zod") {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (node.type === "ImportNamespaceSpecifier") {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (node.importKind === "type" || declaration.importKind === "type") {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const importedName = node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
|
|
114
|
+
return ZOD_NAMESPACE_IMPORTS.has(importedName) || ZOD_FACTORY_IMPORTS.has(importedName);
|
|
115
|
+
}
|
|
116
|
+
function unwrapExpression(node) {
|
|
117
|
+
let current = node;
|
|
118
|
+
while (current.parent?.type === "TSAsExpression" || current.parent?.type === "TSNonNullExpression" || current.parent?.type === "TSSatisfiesExpression" || current.parent?.type === "TSTypeAssertion") {
|
|
119
|
+
current = current.parent;
|
|
120
|
+
}
|
|
121
|
+
return current;
|
|
122
|
+
}
|
|
123
|
+
function isModuleLevelVariableInitializer(node) {
|
|
124
|
+
const expression = unwrapExpression(node);
|
|
125
|
+
if (expression.parent?.type !== "VariableDeclarator") {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (expression.parent.init !== expression) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
const declaration = expression.parent.parent;
|
|
132
|
+
if (declaration.type !== "VariableDeclaration") {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const container = declaration.parent;
|
|
136
|
+
return container.type === "Program" || container.type === "ExportNamedDeclaration" && container.parent.type === "Program";
|
|
137
|
+
}
|
|
138
|
+
var noInlineZodSchema = ESLintUtils.RuleCreator(
|
|
139
|
+
(ruleName2) => `https://www.npmjs.com/package/eslint-plugin-zod-utils#${ruleName2}`
|
|
140
|
+
)({
|
|
141
|
+
name: "no-inline-zod-schema",
|
|
142
|
+
meta: {
|
|
143
|
+
type: "problem",
|
|
144
|
+
docs: {
|
|
145
|
+
description: "Disallow creating Zod schemas outside module scope."
|
|
146
|
+
},
|
|
147
|
+
messages: {
|
|
148
|
+
inlineSchema: "Create Zod schemas at module scope instead of inline inside functions, callbacks, classes, or arguments."
|
|
149
|
+
},
|
|
150
|
+
schema: []
|
|
151
|
+
},
|
|
152
|
+
defaultOptions: [],
|
|
153
|
+
create(context) {
|
|
154
|
+
const sourceCode = context.sourceCode;
|
|
155
|
+
function isZodSchemaCall(node) {
|
|
156
|
+
const root = getRootIdentifier(node.callee);
|
|
157
|
+
if (!root) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
const scope = sourceCode.getScope(root);
|
|
161
|
+
const variable = findVariable(scope, root.name);
|
|
162
|
+
return variable?.defs.some((definition) => isZodImportSpecifier(definition.node)) ?? false;
|
|
163
|
+
}
|
|
164
|
+
function hasZodCallAncestor(node) {
|
|
165
|
+
let current = node.parent;
|
|
166
|
+
while (current) {
|
|
167
|
+
if (current.type === "CallExpression" && isZodSchemaCall(current)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
current = current.parent;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
CallExpression(node) {
|
|
176
|
+
if (!isZodSchemaCall(node)) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (hasZodCallAncestor(node)) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (isModuleLevelVariableInitializer(node)) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
context.report({
|
|
186
|
+
node,
|
|
187
|
+
messageId: "inlineSchema"
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// src/index.ts
|
|
195
|
+
var ruleName = "zod-utils/no-inline-zod-schema";
|
|
196
|
+
var rules = {
|
|
197
|
+
"no-inline-zod-schema": noInlineZodSchema
|
|
198
|
+
};
|
|
199
|
+
var plugin = {
|
|
200
|
+
meta: {
|
|
201
|
+
name: "eslint-plugin-zod-utils",
|
|
202
|
+
version: "0.1.0"
|
|
203
|
+
},
|
|
204
|
+
rules,
|
|
205
|
+
configs: {}
|
|
206
|
+
};
|
|
207
|
+
plugin.configs.recommended = {
|
|
208
|
+
plugins: {
|
|
209
|
+
"zod-utils": plugin
|
|
210
|
+
},
|
|
211
|
+
rules: {
|
|
212
|
+
[ruleName]: "error"
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
plugin.configs["legacy-recommended"] = {
|
|
216
|
+
plugins: ["zod-utils"],
|
|
217
|
+
rules: {
|
|
218
|
+
[ruleName]: "error"
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var configs = plugin.configs;
|
|
222
|
+
var index_default = plugin;
|
|
223
|
+
export {
|
|
224
|
+
configs,
|
|
225
|
+
index_default as default,
|
|
226
|
+
rules
|
|
227
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-zod-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "ESLint utilities for safer Zod schema usage.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @typescript-eslint/utils",
|
|
23
|
+
"lint": "eslint .",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"prepack": "npm run lint && npm run typecheck && npm test && npm run build"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"eslint",
|
|
30
|
+
"eslintplugin",
|
|
31
|
+
"eslint-plugin",
|
|
32
|
+
"zod",
|
|
33
|
+
"typescript"
|
|
34
|
+
],
|
|
35
|
+
"author": "",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"eslint": ">=8.57.0 || >=9.0.0 || >=10.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@eslint/js": "^10.0.1",
|
|
42
|
+
"@types/node": "^25.9.1",
|
|
43
|
+
"@typescript-eslint/parser": "^8.60.1",
|
|
44
|
+
"@typescript-eslint/rule-tester": "^8.60.1",
|
|
45
|
+
"eslint": "^10.4.1",
|
|
46
|
+
"tsup": "^8.5.1",
|
|
47
|
+
"typescript": "^6.0.3",
|
|
48
|
+
"typescript-eslint": "^8.60.1",
|
|
49
|
+
"vitest": "^4.1.8"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@typescript-eslint/utils": "^8.60.1"
|
|
53
|
+
}
|
|
54
|
+
}
|