eslint-plugin-zod-core 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
2
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");
3
4
  //#region src/rules/consistent-import.ts
4
5
  const consistentImport = require_create_plugin_rule.createZodPluginRule({
5
6
  name: "consistent-import",
@@ -17,86 +18,13 @@ const consistentImport = require_create_plugin_rule.createZodPluginRule({
17
18
  properties: { syntax: {
18
19
  description: "Specifies the import syntax to use for Zod core.",
19
20
  type: "string",
20
- enum: _eslint_zod_utils.IMPORT_SYNTAXES
21
+ enum: _eslint_zod_utils_rule_builders_consistent_import.IMPORT_SYNTAXES
21
22
  } },
22
23
  additionalProperties: false
23
24
  }]
24
25
  },
25
26
  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
- }
27
+ create: (0, _eslint_zod_utils_rule_builders_consistent_import.buildConsistentImportCreate)(_eslint_zod_utils.zodCoreImportScope)
100
28
  });
101
29
  //#endregion
102
30
  exports.consistentImport = consistentImport;
@@ -1,5 +1,6 @@
1
1
  import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
- import { IMPORT_SYNTAXES, getNamespaceAliasNameFrom, isGroupFirstImportKindValidForSyntax, shouldIdentifierBeRenamed, zodCoreImportScope } from "@eslint-zod/utils";
2
+ import { zodCoreImportScope } 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 = createZodPluginRule({
5
6
  name: "consistent-import",
@@ -23,80 +24,7 @@ const consistentImport = createZodPluginRule({
23
24
  }]
24
25
  },
25
26
  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
- }
27
+ create: buildConsistentImportCreate(zodCoreImportScope)
100
28
  });
101
29
  //#endregion
102
30
  export { consistentImport };
@@ -1,9 +1,6 @@
1
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
2
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);
3
+ let _eslint_zod_utils_rule_builders_consistent_schema_output_type_style = require("@eslint-zod/utils/rule-builders/consistent-schema-output-type-style");
7
4
  const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodPluginRule({
8
5
  name: "consistent-schema-output-type-style",
9
6
  meta: {
@@ -19,48 +16,13 @@ const consistentSchemaOutputTypeStyle = require_create_plugin_rule.createZodPlug
19
16
  properties: { style: {
20
17
  description: "Decides which style to use for schema type inference",
21
18
  type: "string",
22
- enum: [...ZOD_SCHEMA_TYPE_STYLES]
19
+ enum: [...["infer", "output"]]
23
20
  } },
24
21
  additionalProperties: false
25
22
  }]
26
23
  },
27
24
  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
- }
25
+ create: (0, _eslint_zod_utils_rule_builders_consistent_schema_output_type_style.buildConsistentSchemaOutputTypeStyleCreate)(_eslint_zod_utils.zodCoreImportScope)
64
26
  });
65
27
  //#endregion
66
28
  exports.consistentSchemaOutputTypeStyle = consistentSchemaOutputTypeStyle;
@@ -1,9 +1,6 @@
1
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);
2
+ import { zodCoreImportScope } from "@eslint-zod/utils";
3
+ import { buildConsistentSchemaOutputTypeStyleCreate } from "@eslint-zod/utils/rule-builders/consistent-schema-output-type-style";
7
4
  const consistentSchemaOutputTypeStyle = createZodPluginRule({
8
5
  name: "consistent-schema-output-type-style",
9
6
  meta: {
@@ -19,48 +16,13 @@ const consistentSchemaOutputTypeStyle = createZodPluginRule({
19
16
  properties: { style: {
20
17
  description: "Decides which style to use for schema type inference",
21
18
  type: "string",
22
- enum: [...ZOD_SCHEMA_TYPE_STYLES]
19
+ enum: [...["infer", "output"]]
23
20
  } },
24
21
  additionalProperties: false
25
22
  }]
26
23
  },
27
24
  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
- }
25
+ create: buildConsistentSchemaOutputTypeStyleCreate(zodCoreImportScope)
64
26
  });
65
27
  //#endregion
66
28
  export { consistentSchemaOutputTypeStyle };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zod-core",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "ESLint plugin that adds custom linting rules to enforce best practices when using Zod core",
5
5
  "keywords": [
6
6
  "eslint",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@typescript-eslint/utils": "^8.57.0",
42
- "@eslint-zod/utils": "1.3.0"
42
+ "@eslint-zod/utils": "2.0.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@marcalexiei/prettier-config": "2.0.0",