eslint-plugin-zod 3.5.1 → 3.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/index.cjs +94 -83
- package/dist/index.d.cts +17 -16
- package/dist/index.d.mts +24 -0
- package/dist/index.mjs +98 -0
- package/dist/meta.cjs +11 -11
- package/dist/meta.mjs +10 -0
- package/dist/rules/array-style.cjs +88 -101
- package/dist/rules/array-style.mjs +89 -0
- package/dist/rules/consistent-import-source.cjs +59 -66
- package/dist/rules/consistent-import-source.mjs +60 -0
- package/dist/rules/consistent-import.cjs +146 -172
- package/dist/rules/consistent-import.mjs +152 -0
- package/dist/rules/consistent-object-schema-type.cjs +81 -84
- package/dist/rules/consistent-object-schema-type.mjs +81 -0
- package/dist/rules/no-any-schema.cjs +62 -67
- package/dist/rules/no-any-schema.mjs +62 -0
- package/dist/rules/no-empty-custom-schema.cjs +32 -40
- package/dist/rules/no-empty-custom-schema.mjs +34 -0
- package/dist/rules/no-number-schema-with-int.cjs +56 -62
- package/dist/rules/no-number-schema-with-int.mjs +57 -0
- package/dist/rules/no-optional-and-default-together.cjs +69 -80
- package/dist/rules/no-optional-and-default-together.mjs +71 -0
- package/dist/rules/no-string-schema-with-uuid.cjs +57 -63
- package/dist/rules/no-string-schema-with-uuid.mjs +58 -0
- package/dist/rules/no-throw-in-refine.cjs +69 -87
- package/dist/rules/no-throw-in-refine.mjs +69 -0
- package/dist/rules/no-unknown-schema.cjs +29 -35
- package/dist/rules/no-unknown-schema.mjs +31 -0
- package/dist/rules/prefer-enum-over-literal-union.cjs +63 -79
- package/dist/rules/prefer-enum-over-literal-union.mjs +63 -0
- package/dist/rules/prefer-meta-last.cjs +47 -63
- package/dist/rules/prefer-meta-last.mjs +49 -0
- package/dist/rules/prefer-meta.cjs +38 -48
- package/dist/rules/prefer-meta.mjs +40 -0
- package/dist/rules/prefer-namespace-import.cjs +110 -142
- package/dist/rules/prefer-namespace-import.mjs +116 -0
- package/dist/rules/prefer-string-schema-with-trim.cjs +45 -52
- package/dist/rules/prefer-string-schema-with-trim.mjs +47 -0
- package/dist/rules/require-brand-type-parameter.cjs +47 -60
- package/dist/rules/require-brand-type-parameter.mjs +49 -0
- package/dist/rules/require-error-message.cjs +83 -102
- package/dist/rules/require-error-message.mjs +83 -0
- package/dist/rules/require-schema-suffix.cjs +67 -78
- package/dist/rules/require-schema-suffix.mjs +67 -0
- package/dist/rules/schema-error-property-style.cjs +95 -107
- package/dist/rules/schema-error-property-style.mjs +94 -0
- package/dist/utils/build-zod-chain-replacement-fix.cjs +28 -29
- package/dist/utils/build-zod-chain-replacement-fix.mjs +30 -0
- package/dist/utils/create-plugin-rule.cjs +6 -6
- package/dist/utils/create-plugin-rule.mjs +6 -0
- package/dist/utils/detect-zod-schema-root-node.cjs +94 -96
- package/dist/utils/detect-zod-schema-root-node.mjs +101 -0
- package/dist/utils/is-zod-import-source.cjs +14 -18
- package/dist/utils/is-zod-import-source.mjs +16 -0
- package/dist/utils/track-zod-schema-imports.cjs +61 -63
- package/dist/utils/track-zod-schema-imports.mjs +64 -0
- package/package.json +14 -17
- package/dist/index.d.ts +0 -22
- package/dist/index.js +0 -84
- package/dist/meta.d.cts +0 -3
- package/dist/meta.d.ts +0 -3
- package/dist/meta.js +0 -8
- package/dist/rules/array-style.d.cts +0 -11
- package/dist/rules/array-style.d.ts +0 -11
- package/dist/rules/array-style.js +0 -99
- package/dist/rules/consistent-import-source.d.cts +0 -11
- package/dist/rules/consistent-import-source.d.ts +0 -11
- package/dist/rules/consistent-import-source.js +0 -64
- package/dist/rules/consistent-import.d.cts +0 -12
- package/dist/rules/consistent-import.d.ts +0 -12
- package/dist/rules/consistent-import.js +0 -176
- package/dist/rules/consistent-object-schema-type.d.cts +0 -13
- package/dist/rules/consistent-object-schema-type.d.ts +0 -13
- package/dist/rules/consistent-object-schema-type.js +0 -82
- package/dist/rules/no-any-schema.d.cts +0 -6
- package/dist/rules/no-any-schema.d.ts +0 -6
- package/dist/rules/no-any-schema.js +0 -65
- package/dist/rules/no-empty-custom-schema.d.cts +0 -5
- package/dist/rules/no-empty-custom-schema.d.ts +0 -5
- package/dist/rules/no-empty-custom-schema.js +0 -38
- package/dist/rules/no-number-schema-with-int.d.cts +0 -5
- package/dist/rules/no-number-schema-with-int.d.ts +0 -5
- package/dist/rules/no-number-schema-with-int.js +0 -60
- package/dist/rules/no-optional-and-default-together.d.cts +0 -12
- package/dist/rules/no-optional-and-default-together.d.ts +0 -12
- package/dist/rules/no-optional-and-default-together.js +0 -78
- package/dist/rules/no-string-schema-with-uuid.d.cts +0 -5
- package/dist/rules/no-string-schema-with-uuid.d.ts +0 -5
- package/dist/rules/no-string-schema-with-uuid.js +0 -61
- package/dist/rules/no-throw-in-refine.d.cts +0 -5
- package/dist/rules/no-throw-in-refine.d.ts +0 -5
- package/dist/rules/no-throw-in-refine.js +0 -85
- package/dist/rules/no-unknown-schema.d.cts +0 -5
- package/dist/rules/no-unknown-schema.d.ts +0 -5
- package/dist/rules/no-unknown-schema.js +0 -33
- package/dist/rules/prefer-enum-over-literal-union.d.cts +0 -5
- package/dist/rules/prefer-enum-over-literal-union.d.ts +0 -5
- package/dist/rules/prefer-enum-over-literal-union.js +0 -77
- package/dist/rules/prefer-meta-last.d.cts +0 -5
- package/dist/rules/prefer-meta-last.d.ts +0 -5
- package/dist/rules/prefer-meta-last.js +0 -61
- package/dist/rules/prefer-meta.d.cts +0 -5
- package/dist/rules/prefer-meta.d.ts +0 -5
- package/dist/rules/prefer-meta.js +0 -46
- package/dist/rules/prefer-namespace-import.d.cts +0 -5
- package/dist/rules/prefer-namespace-import.d.ts +0 -5
- package/dist/rules/prefer-namespace-import.js +0 -146
- package/dist/rules/prefer-string-schema-with-trim.d.cts +0 -5
- package/dist/rules/prefer-string-schema-with-trim.d.ts +0 -5
- package/dist/rules/prefer-string-schema-with-trim.js +0 -50
- package/dist/rules/require-brand-type-parameter.d.cts +0 -6
- package/dist/rules/require-brand-type-parameter.d.ts +0 -6
- package/dist/rules/require-brand-type-parameter.js +0 -58
- package/dist/rules/require-error-message.d.cts +0 -5
- package/dist/rules/require-error-message.d.ts +0 -5
- package/dist/rules/require-error-message.js +0 -100
- package/dist/rules/require-schema-suffix.d.cts +0 -9
- package/dist/rules/require-schema-suffix.d.ts +0 -9
- package/dist/rules/require-schema-suffix.js +0 -76
- package/dist/rules/schema-error-property-style.d.cts +0 -10
- package/dist/rules/schema-error-property-style.d.ts +0 -10
- package/dist/rules/schema-error-property-style.js +0 -102
- package/dist/utils/build-zod-chain-replacement-fix.d.cts +0 -12
- package/dist/utils/build-zod-chain-replacement-fix.d.ts +0 -12
- package/dist/utils/build-zod-chain-replacement-fix.js +0 -28
- package/dist/utils/create-plugin-rule.d.cts +0 -13
- package/dist/utils/create-plugin-rule.d.ts +0 -13
- package/dist/utils/create-plugin-rule.js +0 -3
- package/dist/utils/detect-zod-schema-root-node.d.cts +0 -10
- package/dist/utils/detect-zod-schema-root-node.d.ts +0 -10
- package/dist/utils/detect-zod-schema-root-node.js +0 -101
- package/dist/utils/is-zod-import-source.d.cts +0 -4
- package/dist/utils/is-zod-import-source.d.ts +0 -4
- package/dist/utils/is-zod-import-source.js +0 -17
- package/dist/utils/track-zod-schema-imports.d.cts +0 -18
- package/dist/utils/track-zod-schema-imports.d.ts +0 -18
- package/dist/utils/track-zod-schema-imports.js +0 -64
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
+
import { isZodImportSource } from "../utils/is-zod-import-source.mjs";
|
|
3
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
|
|
4
|
+
//#region src/rules/prefer-namespace-import.ts
|
|
5
|
+
function isGroupFirstImportTypeNamespace(group) {
|
|
6
|
+
const { hasOnlyTypeImports, nodes: [firstImportNode] } = group;
|
|
7
|
+
const { specifiers } = firstImportNode;
|
|
8
|
+
if (specifiers.length === 1 && specifiers[0].type === AST_NODE_TYPES.ImportNamespaceSpecifier) if (hasOnlyTypeImports) {
|
|
9
|
+
if (firstImportNode.importKind === "type") return true;
|
|
10
|
+
} else return true;
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
function shouldIdentifierBeRenamed(node) {
|
|
14
|
+
if (node.parent.type === AST_NODE_TYPES.ImportSpecifier) return false;
|
|
15
|
+
if (node.parent.type === AST_NODE_TYPES.MemberExpression && node.parent.object.type === AST_NODE_TYPES.Identifier && node.parent.object.name !== node.name) return false;
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* From a given specifiers retrieve the most significant to use when creating an alias import
|
|
20
|
+
*/
|
|
21
|
+
function getNamespaceAliasNameFrom(node) {
|
|
22
|
+
if (node.type === AST_NODE_TYPES.ImportDefaultSpecifier || node.type === AST_NODE_TYPES.ImportNamespaceSpecifier) return node.local.name;
|
|
23
|
+
if (node.imported.type === AST_NODE_TYPES.Identifier && node.imported.name === "z") return node.local.name;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const preferNamespaceImport = createZodPluginRule({
|
|
27
|
+
name: "prefer-namespace-import",
|
|
28
|
+
meta: {
|
|
29
|
+
type: "suggestion",
|
|
30
|
+
deprecated: { message: "Use `zod/consistent-import` with `{ syntax: 'namespace' }`" },
|
|
31
|
+
docs: {
|
|
32
|
+
zodImportAllowedSource: "all",
|
|
33
|
+
description: "Enforce importing zod as a namespace import (`import * as z from 'zod'`)"
|
|
34
|
+
},
|
|
35
|
+
fixable: "code",
|
|
36
|
+
messages: {
|
|
37
|
+
useNamespace: "Import Zod with a namespace import",
|
|
38
|
+
removeDuplicate: "Zod is already imported via namespace import",
|
|
39
|
+
convertUsage: "Convert to namespace usage"
|
|
40
|
+
},
|
|
41
|
+
schema: []
|
|
42
|
+
},
|
|
43
|
+
defaultOptions: [],
|
|
44
|
+
create(context) {
|
|
45
|
+
const { sourceCode } = context;
|
|
46
|
+
const importGroups = {};
|
|
47
|
+
return {
|
|
48
|
+
ImportDeclaration(node) {
|
|
49
|
+
const { source, importKind } = node;
|
|
50
|
+
if (!isZodImportSource(source.value, "all")) return;
|
|
51
|
+
if (!importGroups[source.value]) importGroups[source.value] = {
|
|
52
|
+
hasOnlyTypeImports: true,
|
|
53
|
+
nodes: []
|
|
54
|
+
};
|
|
55
|
+
if (importGroups[source.value].hasOnlyTypeImports && importKind === "value") importGroups[source.value].hasOnlyTypeImports = false;
|
|
56
|
+
importGroups[source.value].nodes.push(node);
|
|
57
|
+
},
|
|
58
|
+
"Program:exit": function() {
|
|
59
|
+
let namespaceAliasNameIndex = 0;
|
|
60
|
+
for (const importGroup of Object.values(importGroups)) {
|
|
61
|
+
const { hasOnlyTypeImports, nodes } = importGroup;
|
|
62
|
+
const [firstImportNode, ...othersImportNodes] = nodes;
|
|
63
|
+
/**
|
|
64
|
+
* Variable to track all specifiers that later are used by {@link getDeclaredVariables}
|
|
65
|
+
* to add the namespace prefix
|
|
66
|
+
*/
|
|
67
|
+
const nodesWithVariablesToUpdate = [];
|
|
68
|
+
let namespaceAliasName = null;
|
|
69
|
+
for (const specifier of nodes.flatMap((it) => it.specifiers)) {
|
|
70
|
+
if (!namespaceAliasName) {
|
|
71
|
+
namespaceAliasName = getNamespaceAliasNameFrom(specifier);
|
|
72
|
+
if (namespaceAliasName) continue;
|
|
73
|
+
}
|
|
74
|
+
nodesWithVariablesToUpdate.push(specifier);
|
|
75
|
+
}
|
|
76
|
+
if (!namespaceAliasName) {
|
|
77
|
+
namespaceAliasName = "z";
|
|
78
|
+
if (namespaceAliasNameIndex > 0) {
|
|
79
|
+
namespaceAliasName = `z${namespaceAliasNameIndex}`;
|
|
80
|
+
namespaceAliasNameIndex += 1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!isGroupFirstImportTypeNamespace(importGroup)) context.report({
|
|
84
|
+
node: firstImportNode,
|
|
85
|
+
messageId: "useNamespace",
|
|
86
|
+
fix(fixer) {
|
|
87
|
+
const newImportText = `import ${hasOnlyTypeImports ? "type " : ""}* as ${namespaceAliasName} from ${firstImportNode.source.raw};`;
|
|
88
|
+
return fixer.replaceText(firstImportNode, newImportText);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const allReferences = nodesWithVariablesToUpdate.flatMap((it) => sourceCode.getDeclaredVariables(it)).flatMap((it) => it.references);
|
|
92
|
+
for (const ref of allReferences) {
|
|
93
|
+
const { identifier } = ref;
|
|
94
|
+
if (shouldIdentifierBeRenamed(identifier)) context.report({
|
|
95
|
+
node: identifier,
|
|
96
|
+
messageId: "convertUsage",
|
|
97
|
+
fix(fixer) {
|
|
98
|
+
const newId = `${namespaceAliasName}.${identifier.name}`;
|
|
99
|
+
return fixer.replaceText(identifier, newId);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
for (const extraImport of othersImportNodes) context.report({
|
|
104
|
+
node: extraImport,
|
|
105
|
+
messageId: "removeDuplicate",
|
|
106
|
+
fix(fixer) {
|
|
107
|
+
return fixer.removeRange(extraImport.range);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
//#endregion
|
|
116
|
+
export { preferNamespaceImport };
|
|
@@ -1,53 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
messageId: 'addTrim',
|
|
45
|
-
fix(fixer) {
|
|
46
|
-
const lastMethod = methods.at(0);
|
|
47
|
-
return fixer.insertTextAfter(lastMethod.node, '.trim()');
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
},
|
|
1
|
+
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
2
|
+
//#region src/rules/prefer-string-schema-with-trim.ts
|
|
3
|
+
const { zodImportAllowedSource, trackZodSchemaImports } = require("../utils/track-zod-schema-imports.cjs").createZodSchemaImportTrack("zod");
|
|
4
|
+
const preferStringSchemaWithTrim = require_create_plugin_rule.createZodPluginRule({
|
|
5
|
+
name: "prefer-string-schema-with-trim",
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
fixable: "code",
|
|
9
|
+
docs: {
|
|
10
|
+
zodImportAllowedSource,
|
|
11
|
+
description: "Enforce `z.string().trim()` to prevent accidental leading/trailing whitespace"
|
|
12
|
+
},
|
|
13
|
+
messages: { addTrim: "`z.string()` schemas should use `.trim()`." },
|
|
14
|
+
schema: []
|
|
15
|
+
},
|
|
16
|
+
defaultOptions: [],
|
|
17
|
+
create(context) {
|
|
18
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
|
|
19
|
+
return {
|
|
20
|
+
ImportDeclaration: importDeclarationListener,
|
|
21
|
+
CallExpression(node) {
|
|
22
|
+
const zodSchemaMeta = detectZodSchemaRootNode(node);
|
|
23
|
+
if (zodSchemaMeta?.schemaType !== "string") return;
|
|
24
|
+
const methods = collectZodChainMethods(zodSchemaMeta.node);
|
|
25
|
+
if (methods.some((it) => it.name === "trim")) return;
|
|
26
|
+
if (zodSchemaMeta.schemaDecl === "named") {
|
|
27
|
+
context.report({
|
|
28
|
+
node,
|
|
29
|
+
messageId: "addTrim"
|
|
30
|
+
});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
context.report({
|
|
34
|
+
node,
|
|
35
|
+
messageId: "addTrim",
|
|
36
|
+
fix(fixer) {
|
|
37
|
+
const lastMethod = methods.at(0);
|
|
38
|
+
return fixer.insertTextAfter(lastMethod.node, ".trim()");
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
53
44
|
});
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.preferStringSchemaWithTrim = preferStringSchemaWithTrim;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
+
import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
|
|
3
|
+
//#region src/rules/prefer-string-schema-with-trim.ts
|
|
4
|
+
const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("zod");
|
|
5
|
+
const preferStringSchemaWithTrim = createZodPluginRule({
|
|
6
|
+
name: "prefer-string-schema-with-trim",
|
|
7
|
+
meta: {
|
|
8
|
+
type: "problem",
|
|
9
|
+
fixable: "code",
|
|
10
|
+
docs: {
|
|
11
|
+
zodImportAllowedSource,
|
|
12
|
+
description: "Enforce `z.string().trim()` to prevent accidental leading/trailing whitespace"
|
|
13
|
+
},
|
|
14
|
+
messages: { addTrim: "`z.string()` schemas should use `.trim()`." },
|
|
15
|
+
schema: []
|
|
16
|
+
},
|
|
17
|
+
defaultOptions: [],
|
|
18
|
+
create(context) {
|
|
19
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
|
|
20
|
+
return {
|
|
21
|
+
ImportDeclaration: importDeclarationListener,
|
|
22
|
+
CallExpression(node) {
|
|
23
|
+
const zodSchemaMeta = detectZodSchemaRootNode(node);
|
|
24
|
+
if (zodSchemaMeta?.schemaType !== "string") return;
|
|
25
|
+
const methods = collectZodChainMethods(zodSchemaMeta.node);
|
|
26
|
+
if (methods.some((it) => it.name === "trim")) return;
|
|
27
|
+
if (zodSchemaMeta.schemaDecl === "named") {
|
|
28
|
+
context.report({
|
|
29
|
+
node,
|
|
30
|
+
messageId: "addTrim"
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
context.report({
|
|
35
|
+
node,
|
|
36
|
+
messageId: "addTrim",
|
|
37
|
+
fix(fixer) {
|
|
38
|
+
const lastMethod = methods.at(0);
|
|
39
|
+
return fixer.insertTextAfter(lastMethod.node, ".trim()");
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
//#endregion
|
|
47
|
+
export { preferStringSchemaWithTrim };
|
|
@@ -1,61 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
suggest: [
|
|
47
|
-
{
|
|
48
|
-
messageId: 'removeBrandFunction',
|
|
49
|
-
fix(fixer) {
|
|
50
|
-
return fixer.removeRange([
|
|
51
|
-
brandCalleeNode.object.range[1],
|
|
52
|
-
brandNode.range[1],
|
|
53
|
-
]);
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
],
|
|
57
|
-
});
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
},
|
|
1
|
+
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
2
|
+
//#region src/rules/require-brand-type-parameter.ts
|
|
3
|
+
const { zodImportAllowedSource, trackZodSchemaImports } = require("../utils/track-zod-schema-imports.cjs").createZodSchemaImportTrack("all");
|
|
4
|
+
const requireBrandTypeParameter = require_create_plugin_rule.createZodPluginRule({
|
|
5
|
+
name: "require-brand-type-parameter",
|
|
6
|
+
meta: {
|
|
7
|
+
hasSuggestions: true,
|
|
8
|
+
type: "problem",
|
|
9
|
+
docs: {
|
|
10
|
+
zodImportAllowedSource,
|
|
11
|
+
description: "Require type parameter on `.brand()` functions"
|
|
12
|
+
},
|
|
13
|
+
messages: {
|
|
14
|
+
missingTypeParameter: "Type parameter is required when using `.brand()`",
|
|
15
|
+
removeBrandFunction: "Brand is a static-only construct. If not parameter is required consider removal"
|
|
16
|
+
},
|
|
17
|
+
schema: []
|
|
18
|
+
},
|
|
19
|
+
defaultOptions: [],
|
|
20
|
+
create(context) {
|
|
21
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
|
|
22
|
+
return {
|
|
23
|
+
ImportDeclaration: importDeclarationListener,
|
|
24
|
+
CallExpression(node) {
|
|
25
|
+
const zodSchemaMeta = detectZodSchemaRootNode(node);
|
|
26
|
+
if (!zodSchemaMeta) return;
|
|
27
|
+
const brandMethod = collectZodChainMethods(zodSchemaMeta.node).find((it) => it.name === "brand");
|
|
28
|
+
if (!brandMethod) return;
|
|
29
|
+
const brandNode = brandMethod.node;
|
|
30
|
+
const { typeArguments } = brandNode;
|
|
31
|
+
if (typeArguments && typeArguments.params.length > 0) return;
|
|
32
|
+
const brandCalleeNode = brandNode.callee;
|
|
33
|
+
context.report({
|
|
34
|
+
messageId: "missingTypeParameter",
|
|
35
|
+
node: brandCalleeNode.property,
|
|
36
|
+
suggest: [{
|
|
37
|
+
messageId: "removeBrandFunction",
|
|
38
|
+
fix(fixer) {
|
|
39
|
+
return fixer.removeRange([brandCalleeNode.object.range[1], brandNode.range[1]]);
|
|
40
|
+
}
|
|
41
|
+
}]
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
61
46
|
});
|
|
47
|
+
//#endregion
|
|
48
|
+
exports.requireBrandTypeParameter = requireBrandTypeParameter;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
+
import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
|
|
3
|
+
//#region src/rules/require-brand-type-parameter.ts
|
|
4
|
+
const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("all");
|
|
5
|
+
const requireBrandTypeParameter = createZodPluginRule({
|
|
6
|
+
name: "require-brand-type-parameter",
|
|
7
|
+
meta: {
|
|
8
|
+
hasSuggestions: true,
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: {
|
|
11
|
+
zodImportAllowedSource,
|
|
12
|
+
description: "Require type parameter on `.brand()` functions"
|
|
13
|
+
},
|
|
14
|
+
messages: {
|
|
15
|
+
missingTypeParameter: "Type parameter is required when using `.brand()`",
|
|
16
|
+
removeBrandFunction: "Brand is a static-only construct. If not parameter is required consider removal"
|
|
17
|
+
},
|
|
18
|
+
schema: []
|
|
19
|
+
},
|
|
20
|
+
defaultOptions: [],
|
|
21
|
+
create(context) {
|
|
22
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
|
|
23
|
+
return {
|
|
24
|
+
ImportDeclaration: importDeclarationListener,
|
|
25
|
+
CallExpression(node) {
|
|
26
|
+
const zodSchemaMeta = detectZodSchemaRootNode(node);
|
|
27
|
+
if (!zodSchemaMeta) return;
|
|
28
|
+
const brandMethod = collectZodChainMethods(zodSchemaMeta.node).find((it) => it.name === "brand");
|
|
29
|
+
if (!brandMethod) return;
|
|
30
|
+
const brandNode = brandMethod.node;
|
|
31
|
+
const { typeArguments } = brandNode;
|
|
32
|
+
if (typeArguments && typeArguments.params.length > 0) return;
|
|
33
|
+
const brandCalleeNode = brandNode.callee;
|
|
34
|
+
context.report({
|
|
35
|
+
messageId: "missingTypeParameter",
|
|
36
|
+
node: brandCalleeNode.property,
|
|
37
|
+
suggest: [{
|
|
38
|
+
messageId: "removeBrandFunction",
|
|
39
|
+
fix(fixer) {
|
|
40
|
+
return fixer.removeRange([brandCalleeNode.object.range[1], brandNode.range[1]]);
|
|
41
|
+
}
|
|
42
|
+
}]
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
//#endregion
|
|
49
|
+
export { requireBrandTypeParameter };
|
|
@@ -1,103 +1,84 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
if (messagePropertyNode && !errorPropertyNode) {
|
|
84
|
-
context.report({
|
|
85
|
-
messageId: 'preferError',
|
|
86
|
-
node: params,
|
|
87
|
-
fix(fixer) {
|
|
88
|
-
return fixer.replaceTextRange(messagePropertyNode.key.range, 'error');
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
if (!errorPropertyNode) {
|
|
94
|
-
context.report({
|
|
95
|
-
messageId: 'requireErrorMessage',
|
|
96
|
-
node: params,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
},
|
|
1
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
3
|
+
const require_track_zod_schema_imports = require("../utils/track-zod-schema-imports.cjs");
|
|
4
|
+
let _typescript_eslint_utils = require("@typescript-eslint/utils");
|
|
5
|
+
//#region src/rules/require-error-message.ts
|
|
6
|
+
const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("all");
|
|
7
|
+
const requireErrorMessage = require_create_plugin_rule.createZodPluginRule({
|
|
8
|
+
name: "require-error-message",
|
|
9
|
+
meta: {
|
|
10
|
+
type: "suggestion",
|
|
11
|
+
fixable: "code",
|
|
12
|
+
docs: {
|
|
13
|
+
zodImportAllowedSource,
|
|
14
|
+
description: "Enforce that custom refinements include an error message"
|
|
15
|
+
},
|
|
16
|
+
messages: {
|
|
17
|
+
requireErrorMessage: "Custom refinements must include an error message",
|
|
18
|
+
preferError: "Use the \"error\" property instead of the deprecated \"message\" property",
|
|
19
|
+
removeMessage: "The \"message\" property is deprecated; use \"error\""
|
|
20
|
+
},
|
|
21
|
+
schema: []
|
|
22
|
+
},
|
|
23
|
+
defaultOptions: [],
|
|
24
|
+
create(context) {
|
|
25
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
|
|
26
|
+
return {
|
|
27
|
+
ImportDeclaration: importDeclarationListener,
|
|
28
|
+
CallExpression(node) {
|
|
29
|
+
if (!detectZodSchemaRootNode(node)) return;
|
|
30
|
+
const refine = collectZodChainMethods(node).find((it) => it.name === "refine" || it.name === "custom");
|
|
31
|
+
if (!refine) return;
|
|
32
|
+
const refineNode = refine.node;
|
|
33
|
+
if (refineNode.arguments.length < 2) {
|
|
34
|
+
context.report({
|
|
35
|
+
messageId: "requireErrorMessage",
|
|
36
|
+
node: node.callee
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const [, params] = refineNode.arguments;
|
|
41
|
+
if (params.type === _typescript_eslint_utils.AST_NODE_TYPES.Literal) return;
|
|
42
|
+
if (params.type === _typescript_eslint_utils.AST_NODE_TYPES.ObjectExpression) {
|
|
43
|
+
let errorPropertyNode;
|
|
44
|
+
let messagePropertyNode;
|
|
45
|
+
for (const property of params.properties) if (property.type === _typescript_eslint_utils.AST_NODE_TYPES.Property && property.key.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) {
|
|
46
|
+
if (property.key.name === "error") errorPropertyNode = property;
|
|
47
|
+
if (property.key.name === "message") messagePropertyNode = property;
|
|
48
|
+
if (errorPropertyNode && messagePropertyNode) break;
|
|
49
|
+
}
|
|
50
|
+
if (errorPropertyNode && messagePropertyNode) {
|
|
51
|
+
context.report({
|
|
52
|
+
messageId: "removeMessage",
|
|
53
|
+
node: messagePropertyNode,
|
|
54
|
+
fix(fixer) {
|
|
55
|
+
const { sourceCode } = context;
|
|
56
|
+
const nextToken = sourceCode.getTokenAfter(messagePropertyNode);
|
|
57
|
+
let [, end] = messagePropertyNode.range;
|
|
58
|
+
if (nextToken?.value === ",") end = nextToken.range[1];
|
|
59
|
+
return fixer.removeRange([messagePropertyNode.range[0], end]);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (messagePropertyNode && !errorPropertyNode) {
|
|
65
|
+
context.report({
|
|
66
|
+
messageId: "preferError",
|
|
67
|
+
node: params,
|
|
68
|
+
fix(fixer) {
|
|
69
|
+
return fixer.replaceTextRange(messagePropertyNode.key.range, "error");
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (!errorPropertyNode) context.report({
|
|
75
|
+
messageId: "requireErrorMessage",
|
|
76
|
+
node: params
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
103
82
|
});
|
|
83
|
+
//#endregion
|
|
84
|
+
exports.requireErrorMessage = requireErrorMessage;
|