eslint-plugin-zod-mini 1.1.0 → 1.2.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/README.md +17 -15
- package/dist/index.cjs +5 -0
- package/dist/index.mjs +5 -0
- package/dist/meta.cjs +0 -1
- package/dist/rules/consistent-import-source.cjs +2 -26
- package/dist/rules/consistent-import-source.mjs +2 -25
- package/dist/rules/consistent-import.cjs +3 -80
- package/dist/rules/consistent-import.mjs +3 -79
- package/dist/rules/consistent-object-schema-type.cjs +2 -41
- package/dist/rules/consistent-object-schema-type.mjs +3 -41
- package/dist/rules/consistent-schema-output-type-style.cjs +3 -42
- package/dist/rules/consistent-schema-output-type-style.mjs +4 -42
- package/dist/rules/consistent-schema-var-name.cjs +2 -26
- package/dist/rules/consistent-schema-var-name.mjs +3 -26
- package/dist/rules/no-any-schema.cjs +2 -41
- package/dist/rules/no-any-schema.mjs +3 -41
- package/dist/rules/no-empty-custom-schema.cjs +2 -18
- package/dist/rules/no-empty-custom-schema.mjs +3 -18
- package/dist/rules/no-throw-in-refine.cjs +17 -0
- package/dist/rules/no-throw-in-refine.mjs +17 -0
- package/dist/rules/no-transform-in-record-key.cjs +29 -0
- package/dist/rules/no-transform-in-record-key.mjs +29 -0
- package/dist/rules/no-unknown-schema.cjs +2 -14
- package/dist/rules/no-unknown-schema.mjs +3 -14
- package/dist/rules/prefer-enum-over-literal-union.cjs +2 -2
- package/dist/rules/prefer-enum-over-literal-union.mjs +2 -1
- package/dist/rules/prefer-meta.cjs +0 -1
- package/dist/rules/require-brand-type-parameter.cjs +2 -28
- package/dist/rules/require-brand-type-parameter.mjs +3 -28
- package/dist/rules/require-error-message.cjs +2 -62
- package/dist/rules/require-error-message.mjs +3 -62
- package/dist/rules/schema-error-property-style.cjs +2 -59
- package/dist/rules/schema-error-property-style.mjs +3 -58
- package/dist/utils/create-plugin-rule.cjs +0 -1
- package/package.json +2 -4
- package/dist/_virtual/_rolldown/runtime.cjs +0 -23
package/README.md
CHANGED
|
@@ -31,21 +31,23 @@ Find out more about [Oxlint's `jsPLugins`](https://oxc.rs/docs/guide/usage/linte
|
|
|
31
31
|
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
32
32
|
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
33
33
|
|
|
34
|
-
| Name | Description
|
|
35
|
-
| :--------------------------------------------------------------------------------------- |
|
|
36
|
-
| [consistent-import](docs/rules/consistent-import.md) | Enforce a consistent import style for Zod Mini
|
|
37
|
-
| [consistent-import-source](docs/rules/consistent-import-source.md) | Enforce consistent source from Zod Mini imports
|
|
38
|
-
| [consistent-object-schema-type](docs/rules/consistent-object-schema-type.md) | Enforce consistent usage of Zod Mini schema methods
|
|
39
|
-
| [consistent-schema-output-type-style](docs/rules/consistent-schema-output-type-style.md) | Enforce consistent use of z.infer or z.output for schema type inference
|
|
40
|
-
| [consistent-schema-var-name](docs/rules/consistent-schema-var-name.md) | Enforce a consistent naming convention for Zod Mini schema variables
|
|
41
|
-
| [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod Mini schemas
|
|
42
|
-
| [no-empty-custom-schema](docs/rules/no-empty-custom-schema.md) | Disallow usage of `z.custom()` without arguments
|
|
43
|
-
| [no-
|
|
44
|
-
| [
|
|
45
|
-
| [
|
|
46
|
-
| [
|
|
47
|
-
| [
|
|
48
|
-
| [
|
|
34
|
+
| Name | Description | 💼 | 🔧 | 💡 |
|
|
35
|
+
| :--------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | :-- | :-- | :-- |
|
|
36
|
+
| [consistent-import](docs/rules/consistent-import.md) | Enforce a consistent import style for Zod Mini | ✅ | 🔧 | |
|
|
37
|
+
| [consistent-import-source](docs/rules/consistent-import-source.md) | Enforce consistent source from Zod Mini imports | | | 💡 |
|
|
38
|
+
| [consistent-object-schema-type](docs/rules/consistent-object-schema-type.md) | Enforce consistent usage of Zod Mini schema methods | | | 💡 |
|
|
39
|
+
| [consistent-schema-output-type-style](docs/rules/consistent-schema-output-type-style.md) | Enforce consistent use of z.infer or z.output for schema type inference | | 🔧 | |
|
|
40
|
+
| [consistent-schema-var-name](docs/rules/consistent-schema-var-name.md) | Enforce a consistent naming convention for Zod Mini schema variables | ✅ | | |
|
|
41
|
+
| [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod Mini schemas | ✅ | | 💡 |
|
|
42
|
+
| [no-empty-custom-schema](docs/rules/no-empty-custom-schema.md) | Disallow usage of `z.custom()` without arguments | ✅ | | |
|
|
43
|
+
| [no-throw-in-refine](docs/rules/no-throw-in-refine.md) | Disallow throwing errors directly inside Zod Mini refine callbacks | ✅ | | |
|
|
44
|
+
| [no-transform-in-record-key](docs/rules/no-transform-in-record-key.md) | Disallow transforms in z.record() key schemas, which can cause silent key mutations and data loss through key collisions | | | |
|
|
45
|
+
| [no-unknown-schema](docs/rules/no-unknown-schema.md) | Disallow usage of `z.unknown()` in Zod Mini schemas | | | |
|
|
46
|
+
| [prefer-enum-over-literal-union](docs/rules/prefer-enum-over-literal-union.md) | Prefer `z.enum()` over `z.union()` when all members are string literals. | ✅ | 🔧 | |
|
|
47
|
+
| [prefer-meta](docs/rules/prefer-meta.md) | Enforce usage of `z.meta()` over `z.describe()` | ✅ | 🔧 | |
|
|
48
|
+
| [require-brand-type-parameter](docs/rules/require-brand-type-parameter.md) | Require type parameter on `.brand()` functions | ✅ | | 💡 |
|
|
49
|
+
| [require-error-message](docs/rules/require-error-message.md) | Enforce that custom refinements include an error message | ✅ | 🔧 | |
|
|
50
|
+
| [schema-error-property-style](docs/rules/schema-error-property-style.md) | Enforce consistent style for error messages in Zod Mini schema validation (using ESQuery patterns) | | | |
|
|
49
51
|
|
|
50
52
|
<!-- end auto-generated rules list -->
|
|
51
53
|
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,8 @@ const require_consistent_schema_output_type_style = require("./rules/consistent-
|
|
|
6
6
|
const require_consistent_schema_var_name = require("./rules/consistent-schema-var-name.cjs");
|
|
7
7
|
const require_no_any_schema = require("./rules/no-any-schema.cjs");
|
|
8
8
|
const require_no_empty_custom_schema = require("./rules/no-empty-custom-schema.cjs");
|
|
9
|
+
const require_no_throw_in_refine = require("./rules/no-throw-in-refine.cjs");
|
|
10
|
+
const require_no_transform_in_record_key = require("./rules/no-transform-in-record-key.cjs");
|
|
9
11
|
const require_no_unknown_schema = require("./rules/no-unknown-schema.cjs");
|
|
10
12
|
const require_prefer_enum_over_literal_union = require("./rules/prefer-enum-over-literal-union.cjs");
|
|
11
13
|
const require_prefer_meta = require("./rules/prefer-meta.cjs");
|
|
@@ -26,6 +28,8 @@ const eslintPluginZodMini = {
|
|
|
26
28
|
"consistent-schema-var-name": require_consistent_schema_var_name.consistentSchemaVarName,
|
|
27
29
|
"no-any-schema": require_no_any_schema.noAnySchema,
|
|
28
30
|
"no-empty-custom-schema": require_no_empty_custom_schema.noEmptyCustomSchema,
|
|
31
|
+
"no-throw-in-refine": require_no_throw_in_refine.noThrowInRefine,
|
|
32
|
+
"no-transform-in-record-key": require_no_transform_in_record_key.noTransformInRecordKey,
|
|
29
33
|
"no-unknown-schema": require_no_unknown_schema.noUnknownSchema,
|
|
30
34
|
"prefer-enum-over-literal-union": require_prefer_enum_over_literal_union.preferEnumOverLiteralUnion,
|
|
31
35
|
"prefer-meta": require_prefer_meta.preferMeta,
|
|
@@ -43,6 +47,7 @@ const recommendedConfig = {
|
|
|
43
47
|
"zod-mini/consistent-schema-var-name": "error",
|
|
44
48
|
"zod-mini/no-any-schema": "error",
|
|
45
49
|
"zod-mini/no-empty-custom-schema": "error",
|
|
50
|
+
"zod-mini/no-throw-in-refine": "error",
|
|
46
51
|
"zod-mini/prefer-enum-over-literal-union": "error",
|
|
47
52
|
"zod-mini/prefer-meta": "error",
|
|
48
53
|
"zod-mini/require-brand-type-parameter": "error",
|
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,8 @@ import { consistentSchemaOutputTypeStyle } from "./rules/consistent-schema-outpu
|
|
|
6
6
|
import { consistentSchemaVarName } from "./rules/consistent-schema-var-name.mjs";
|
|
7
7
|
import { noAnySchema } from "./rules/no-any-schema.mjs";
|
|
8
8
|
import { noEmptyCustomSchema } from "./rules/no-empty-custom-schema.mjs";
|
|
9
|
+
import { noThrowInRefine } from "./rules/no-throw-in-refine.mjs";
|
|
10
|
+
import { noTransformInRecordKey } from "./rules/no-transform-in-record-key.mjs";
|
|
9
11
|
import { noUnknownSchema } from "./rules/no-unknown-schema.mjs";
|
|
10
12
|
import { preferEnumOverLiteralUnion } from "./rules/prefer-enum-over-literal-union.mjs";
|
|
11
13
|
import { preferMeta } from "./rules/prefer-meta.mjs";
|
|
@@ -26,6 +28,8 @@ const eslintPluginZodMini = {
|
|
|
26
28
|
"consistent-schema-var-name": consistentSchemaVarName,
|
|
27
29
|
"no-any-schema": noAnySchema,
|
|
28
30
|
"no-empty-custom-schema": noEmptyCustomSchema,
|
|
31
|
+
"no-throw-in-refine": noThrowInRefine,
|
|
32
|
+
"no-transform-in-record-key": noTransformInRecordKey,
|
|
29
33
|
"no-unknown-schema": noUnknownSchema,
|
|
30
34
|
"prefer-enum-over-literal-union": preferEnumOverLiteralUnion,
|
|
31
35
|
"prefer-meta": preferMeta,
|
|
@@ -43,6 +47,7 @@ const recommendedConfig = {
|
|
|
43
47
|
"zod-mini/consistent-schema-var-name": "error",
|
|
44
48
|
"zod-mini/no-any-schema": "error",
|
|
45
49
|
"zod-mini/no-empty-custom-schema": "error",
|
|
50
|
+
"zod-mini/no-throw-in-refine": "error",
|
|
46
51
|
"zod-mini/prefer-enum-over-literal-union": "error",
|
|
47
52
|
"zod-mini/prefer-meta": "error",
|
|
48
53
|
"zod-mini/require-brand-type-parameter": "error",
|
package/dist/meta.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
require("./_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
let node_fs = require("node:fs");
|
|
3
2
|
//#region src/meta.ts
|
|
4
3
|
const { name: PLUGIN_NAME, version: PLUGIN_VERSION, repository } = JSON.parse((0, node_fs.readFileSync)(new URL("../package.json", require("url").pathToFileURL(__filename).href), "utf8"));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
3
2
|
let _eslint_zod_utils = require("@eslint-zod/utils");
|
|
3
|
+
let _eslint_zod_utils_rule_builders_consistent_import_source = require("@eslint-zod/utils/rule-builders/consistent-import-source");
|
|
4
4
|
//#region src/rules/consistent-import-source.ts
|
|
5
5
|
const consistentImportSource = require_create_plugin_rule.createZodMiniPluginRule({
|
|
6
6
|
name: "consistent-import-source",
|
|
@@ -28,31 +28,7 @@ const consistentImportSource = require_create_plugin_rule.createZodMiniPluginRul
|
|
|
28
28
|
}]
|
|
29
29
|
},
|
|
30
30
|
defaultOptions: [{ sources: ["zod/mini"] }],
|
|
31
|
-
create(
|
|
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
|
-
}
|
|
31
|
+
create: (0, _eslint_zod_utils_rule_builders_consistent_import_source.buildConsistentImportSourceCreate)(_eslint_zod_utils.zodMiniImportScope)
|
|
56
32
|
});
|
|
57
33
|
//#endregion
|
|
58
34
|
exports.consistentImportSource = consistentImportSource;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createZodMiniPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
2
|
import { zodMiniImportScope } from "@eslint-zod/utils";
|
|
3
|
+
import { buildConsistentImportSourceCreate } from "@eslint-zod/utils/rule-builders/consistent-import-source";
|
|
3
4
|
//#region src/rules/consistent-import-source.ts
|
|
4
5
|
const consistentImportSource = createZodMiniPluginRule({
|
|
5
6
|
name: "consistent-import-source",
|
|
@@ -27,31 +28,7 @@ const consistentImportSource = createZodMiniPluginRule({
|
|
|
27
28
|
}]
|
|
28
29
|
},
|
|
29
30
|
defaultOptions: [{ sources: ["zod/mini"] }],
|
|
30
|
-
create(
|
|
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
|
-
}
|
|
31
|
+
create: buildConsistentImportSourceCreate(zodMiniImportScope)
|
|
55
32
|
});
|
|
56
33
|
//#endregion
|
|
57
34
|
export { consistentImportSource };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
3
2
|
let _eslint_zod_utils = require("@eslint-zod/utils");
|
|
3
|
+
let _eslint_zod_utils_rule_builders_consistent_import = require("@eslint-zod/utils/rule-builders/consistent-import");
|
|
4
4
|
//#region src/rules/consistent-import.ts
|
|
5
5
|
const consistentImport = require_create_plugin_rule.createZodMiniPluginRule({
|
|
6
6
|
name: "consistent-import",
|
|
@@ -18,90 +18,13 @@ const consistentImport = require_create_plugin_rule.createZodMiniPluginRule({
|
|
|
18
18
|
properties: { syntax: {
|
|
19
19
|
description: "Specifies the import syntax to use for Zod Mini.",
|
|
20
20
|
type: "string",
|
|
21
|
-
enum:
|
|
21
|
+
enum: _eslint_zod_utils_rule_builders_consistent_import.IMPORT_SYNTAXES
|
|
22
22
|
} },
|
|
23
23
|
additionalProperties: false
|
|
24
24
|
}]
|
|
25
25
|
},
|
|
26
26
|
defaultOptions: [{ syntax: "namespace" }],
|
|
27
|
-
create(
|
|
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
|
-
}
|
|
27
|
+
create: (0, _eslint_zod_utils_rule_builders_consistent_import.buildConsistentImportCreate)(_eslint_zod_utils.zodMiniImportScope)
|
|
105
28
|
});
|
|
106
29
|
//#endregion
|
|
107
30
|
exports.consistentImport = consistentImport;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createZodMiniPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { zodMiniImportScope } from "@eslint-zod/utils";
|
|
3
|
+
import { IMPORT_SYNTAXES, buildConsistentImportCreate } from "@eslint-zod/utils/rule-builders/consistent-import";
|
|
3
4
|
//#region src/rules/consistent-import.ts
|
|
4
5
|
const consistentImport = createZodMiniPluginRule({
|
|
5
6
|
name: "consistent-import",
|
|
@@ -23,84 +24,7 @@ const consistentImport = createZodMiniPluginRule({
|
|
|
23
24
|
}]
|
|
24
25
|
},
|
|
25
26
|
defaultOptions: [{ syntax: "namespace" }],
|
|
26
|
-
create(
|
|
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
|
-
}
|
|
27
|
+
create: buildConsistentImportCreate(zodMiniImportScope)
|
|
104
28
|
});
|
|
105
29
|
//#endregion
|
|
106
30
|
export { consistentImport };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
3
2
|
let _eslint_zod_utils = require("@eslint-zod/utils");
|
|
4
|
-
let
|
|
3
|
+
let _eslint_zod_utils_rule_builders_consistent_object_schema_type = require("@eslint-zod/utils/rule-builders/consistent-object-schema-type");
|
|
5
4
|
//#region src/rules/consistent-object-schema-type.ts
|
|
6
5
|
const ZOD_OBJECT_METHODS = [
|
|
7
6
|
"object",
|
|
@@ -9,7 +8,6 @@ const ZOD_OBJECT_METHODS = [
|
|
|
9
8
|
"strictObject"
|
|
10
9
|
];
|
|
11
10
|
const defaultOptions = { allow: ["object"] };
|
|
12
|
-
const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodMiniImportScope);
|
|
13
11
|
const consistentObjectSchemaType = require_create_plugin_rule.createZodMiniPluginRule({
|
|
14
12
|
name: "consistent-object-schema-type",
|
|
15
13
|
meta: {
|
|
@@ -36,44 +34,7 @@ const consistentObjectSchemaType = require_create_plugin_rule.createZodMiniPlugi
|
|
|
36
34
|
}]
|
|
37
35
|
},
|
|
38
36
|
defaultOptions: [defaultOptions],
|
|
39
|
-
create(
|
|
40
|
-
const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
|
|
41
|
-
return {
|
|
42
|
-
ImportDeclaration: importDeclarationListener,
|
|
43
|
-
CallExpression(node) {
|
|
44
|
-
const schemaType = detectZodSchemaRootNode(node)?.schemaType;
|
|
45
|
-
if (!schemaType || !ZOD_OBJECT_METHODS.includes(schemaType)) return;
|
|
46
|
-
if (allowedList.includes(schemaType)) return;
|
|
47
|
-
const { callee } = node;
|
|
48
|
-
if (callee.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) {
|
|
49
|
-
context.report({
|
|
50
|
-
node,
|
|
51
|
-
messageId: "consistentMethod",
|
|
52
|
-
data: {
|
|
53
|
-
actual: schemaType,
|
|
54
|
-
allowedList: allowedList.join(",")
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
if (callee.type === _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression) context.report({
|
|
60
|
-
node,
|
|
61
|
-
messageId: "consistentMethod",
|
|
62
|
-
data: {
|
|
63
|
-
actual: schemaType,
|
|
64
|
-
allowedList: allowedList.join(",")
|
|
65
|
-
},
|
|
66
|
-
suggest: allowedList.map((it) => ({
|
|
67
|
-
messageId: "useMethod",
|
|
68
|
-
data: { expected: it },
|
|
69
|
-
fix(fixer) {
|
|
70
|
-
return fixer.replaceText(callee.property, it);
|
|
71
|
-
}
|
|
72
|
-
}))
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
}
|
|
37
|
+
create: (0, _eslint_zod_utils_rule_builders_consistent_object_schema_type.buildConsistentObjectSchemaTypeCreate)(_eslint_zod_utils.zodMiniImportScope)
|
|
77
38
|
});
|
|
78
39
|
//#endregion
|
|
79
40
|
exports.consistentObjectSchemaType = consistentObjectSchemaType;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createZodMiniPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { zodMiniImportScope } from "@eslint-zod/utils";
|
|
3
|
+
import { buildConsistentObjectSchemaTypeCreate } from "@eslint-zod/utils/rule-builders/consistent-object-schema-type";
|
|
4
4
|
//#region src/rules/consistent-object-schema-type.ts
|
|
5
5
|
const ZOD_OBJECT_METHODS = [
|
|
6
6
|
"object",
|
|
@@ -8,7 +8,6 @@ const ZOD_OBJECT_METHODS = [
|
|
|
8
8
|
"strictObject"
|
|
9
9
|
];
|
|
10
10
|
const defaultOptions = { allow: ["object"] };
|
|
11
|
-
const { trackZodSchemaImports } = createZodSchemaImportTrack(zodMiniImportScope);
|
|
12
11
|
const consistentObjectSchemaType = createZodMiniPluginRule({
|
|
13
12
|
name: "consistent-object-schema-type",
|
|
14
13
|
meta: {
|
|
@@ -35,44 +34,7 @@ const consistentObjectSchemaType = createZodMiniPluginRule({
|
|
|
35
34
|
}]
|
|
36
35
|
},
|
|
37
36
|
defaultOptions: [defaultOptions],
|
|
38
|
-
create
|
|
39
|
-
const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
|
|
40
|
-
return {
|
|
41
|
-
ImportDeclaration: importDeclarationListener,
|
|
42
|
-
CallExpression(node) {
|
|
43
|
-
const schemaType = detectZodSchemaRootNode(node)?.schemaType;
|
|
44
|
-
if (!schemaType || !ZOD_OBJECT_METHODS.includes(schemaType)) return;
|
|
45
|
-
if (allowedList.includes(schemaType)) return;
|
|
46
|
-
const { callee } = node;
|
|
47
|
-
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
48
|
-
context.report({
|
|
49
|
-
node,
|
|
50
|
-
messageId: "consistentMethod",
|
|
51
|
-
data: {
|
|
52
|
-
actual: schemaType,
|
|
53
|
-
allowedList: allowedList.join(",")
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression) context.report({
|
|
59
|
-
node,
|
|
60
|
-
messageId: "consistentMethod",
|
|
61
|
-
data: {
|
|
62
|
-
actual: schemaType,
|
|
63
|
-
allowedList: allowedList.join(",")
|
|
64
|
-
},
|
|
65
|
-
suggest: allowedList.map((it) => ({
|
|
66
|
-
messageId: "useMethod",
|
|
67
|
-
data: { expected: it },
|
|
68
|
-
fix(fixer) {
|
|
69
|
-
return fixer.replaceText(callee.property, it);
|
|
70
|
-
}
|
|
71
|
-
}))
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
}
|
|
37
|
+
create: buildConsistentObjectSchemaTypeCreate(zodMiniImportScope)
|
|
76
38
|
});
|
|
77
39
|
//#endregion
|
|
78
40
|
export { consistentObjectSchemaType };
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
|
|
3
2
|
let _eslint_zod_utils = require("@eslint-zod/utils");
|
|
4
|
-
let
|
|
5
|
-
//#region src/rules/consistent-schema-output-type-style.ts
|
|
6
|
-
const ZOD_SCHEMA_TYPE_STYLES = ["infer", "output"];
|
|
7
|
-
const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodMiniImportScope);
|
|
3
|
+
let _eslint_zod_utils_rule_builders_consistent_schema_output_type_style = require("@eslint-zod/utils/rule-builders/consistent-schema-output-type-style");
|
|
8
4
|
const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodMiniPluginRule({
|
|
9
5
|
name: "consistent-schema-output-type-style",
|
|
10
6
|
meta: {
|
|
@@ -20,48 +16,13 @@ const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodMini
|
|
|
20
16
|
properties: { style: {
|
|
21
17
|
description: "Decides which style to use for schema type inference",
|
|
22
18
|
type: "string",
|
|
23
|
-
enum: [...
|
|
19
|
+
enum: [...["infer", "output"]]
|
|
24
20
|
} },
|
|
25
21
|
additionalProperties: false
|
|
26
22
|
}]
|
|
27
23
|
},
|
|
28
24
|
defaultOptions: [{ style: "output" }],
|
|
29
|
-
create(
|
|
30
|
-
const { importDeclarationListener, isZodNamespace, getNamedImportOriginal, getNamedImportLocal } = trackZodSchemaImports();
|
|
31
|
-
return {
|
|
32
|
-
ImportDeclaration: importDeclarationListener,
|
|
33
|
-
TSTypeReference(node) {
|
|
34
|
-
const { typeName } = node;
|
|
35
|
-
if (typeName.type === _typescript_eslint_utils.AST_NODE_TYPES.TSQualifiedName) {
|
|
36
|
-
const { left, right } = typeName;
|
|
37
|
-
if (left.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier || !isZodNamespace(left.name)) return;
|
|
38
|
-
const usedStyle = right.name;
|
|
39
|
-
if (usedStyle !== "infer" && usedStyle !== "output" || usedStyle === style) return;
|
|
40
|
-
context.report({
|
|
41
|
-
node: right,
|
|
42
|
-
messageId: style === "infer" ? "useInfer" : "useOutput",
|
|
43
|
-
fix(fixer) {
|
|
44
|
-
return fixer.replaceText(right, style);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (typeName.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) {
|
|
50
|
-
const originalName = getNamedImportOriginal(typeName.name);
|
|
51
|
-
if (originalName !== "infer" && originalName !== "output" || originalName === style) return;
|
|
52
|
-
const targetLocalName = getNamedImportLocal(style);
|
|
53
|
-
context.report({
|
|
54
|
-
node: typeName,
|
|
55
|
-
messageId: style === "infer" ? "useInfer" : "useOutput",
|
|
56
|
-
fix(fixer) {
|
|
57
|
-
if (!targetLocalName || targetLocalName === "infer") return null;
|
|
58
|
-
return fixer.replaceText(typeName, targetLocalName);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
}
|
|
25
|
+
create: (0, _eslint_zod_utils_rule_builders_consistent_schema_output_type_style.buildConsistentSchemaOutputTypeStyleCreate)(_eslint_zod_utils.zodMiniImportScope)
|
|
65
26
|
});
|
|
66
27
|
//#endregion
|
|
67
28
|
exports.consistentSchemaOutputTypeStyle = consistentSchemaOutputTypeStyle;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { createZodMiniPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
//#region src/rules/consistent-schema-output-type-style.ts
|
|
5
|
-
const ZOD_SCHEMA_TYPE_STYLES = ["infer", "output"];
|
|
6
|
-
const { trackZodSchemaImports } = createZodSchemaImportTrack(zodMiniImportScope);
|
|
2
|
+
import { zodMiniImportScope } from "@eslint-zod/utils";
|
|
3
|
+
import { buildConsistentSchemaOutputTypeStyleCreate } from "@eslint-zod/utils/rule-builders/consistent-schema-output-type-style";
|
|
7
4
|
const consistentSchemaOutputTypeStyle = createZodMiniPluginRule({
|
|
8
5
|
name: "consistent-schema-output-type-style",
|
|
9
6
|
meta: {
|
|
@@ -19,48 +16,13 @@ const consistentSchemaOutputTypeStyle = createZodMiniPluginRule({
|
|
|
19
16
|
properties: { style: {
|
|
20
17
|
description: "Decides which style to use for schema type inference",
|
|
21
18
|
type: "string",
|
|
22
|
-
enum: [...
|
|
19
|
+
enum: [...["infer", "output"]]
|
|
23
20
|
} },
|
|
24
21
|
additionalProperties: false
|
|
25
22
|
}]
|
|
26
23
|
},
|
|
27
24
|
defaultOptions: [{ style: "output" }],
|
|
28
|
-
create(
|
|
29
|
-
const { importDeclarationListener, isZodNamespace, getNamedImportOriginal, getNamedImportLocal } = trackZodSchemaImports();
|
|
30
|
-
return {
|
|
31
|
-
ImportDeclaration: importDeclarationListener,
|
|
32
|
-
TSTypeReference(node) {
|
|
33
|
-
const { typeName } = node;
|
|
34
|
-
if (typeName.type === AST_NODE_TYPES.TSQualifiedName) {
|
|
35
|
-
const { left, right } = typeName;
|
|
36
|
-
if (left.type !== AST_NODE_TYPES.Identifier || !isZodNamespace(left.name)) return;
|
|
37
|
-
const usedStyle = right.name;
|
|
38
|
-
if (usedStyle !== "infer" && usedStyle !== "output" || usedStyle === style) return;
|
|
39
|
-
context.report({
|
|
40
|
-
node: right,
|
|
41
|
-
messageId: style === "infer" ? "useInfer" : "useOutput",
|
|
42
|
-
fix(fixer) {
|
|
43
|
-
return fixer.replaceText(right, style);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (typeName.type === AST_NODE_TYPES.Identifier) {
|
|
49
|
-
const originalName = getNamedImportOriginal(typeName.name);
|
|
50
|
-
if (originalName !== "infer" && originalName !== "output" || originalName === style) return;
|
|
51
|
-
const targetLocalName = getNamedImportLocal(style);
|
|
52
|
-
context.report({
|
|
53
|
-
node: typeName,
|
|
54
|
-
messageId: style === "infer" ? "useInfer" : "useOutput",
|
|
55
|
-
fix(fixer) {
|
|
56
|
-
if (!targetLocalName || targetLocalName === "infer") return null;
|
|
57
|
-
return fixer.replaceText(typeName, targetLocalName);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
25
|
+
create: buildConsistentSchemaOutputTypeStyleCreate(zodMiniImportScope)
|
|
64
26
|
});
|
|
65
27
|
//#endregion
|
|
66
28
|
export { consistentSchemaOutputTypeStyle };
|