eslint-plugin-zod 1.3.0 → 1.4.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 CHANGED
@@ -12,6 +12,7 @@
12
12
  * [Installation](#user-content-eslint-plugin-zod-installation)
13
13
  * [Configuration](#user-content-eslint-plugin-zod-configuration)
14
14
  * [Rules](#user-content-eslint-plugin-zod-rules)
15
+ * [`prefer-enum`](#user-content-eslint-plugin-zod-rules-prefer-enum)
15
16
  * [`require-strict`](#user-content-eslint-plugin-zod-rules-require-strict)
16
17
 
17
18
 
@@ -44,9 +45,8 @@ npm install eslint-plugin-zod --save-dev
44
45
  "zod"
45
46
  ],
46
47
  "rules": {
47
- "zod/require-strict": [
48
- 2,
49
- ],
48
+ "zod/prefer-enum": 2,
49
+ "zod/require-strict": 2
50
50
  }
51
51
  }
52
52
 
@@ -58,6 +58,16 @@ npm install eslint-plugin-zod --save-dev
58
58
 
59
59
  <!-- Rules are sorted alphabetically. -->
60
60
 
61
+ <a name="user-content-eslint-plugin-zod-rules-prefer-enum"></a>
62
+ <a name="eslint-plugin-zod-rules-prefer-enum"></a>
63
+ ### <code>prefer-enum</code>
64
+
65
+ _The `--fix` option on the command line automatically fixes problems reported by this rule._
66
+
67
+ Prefers `z.enum` over a union of literals.
68
+
69
+
70
+
61
71
  <a name="user-content-eslint-plugin-zod-rules-require-strict"></a>
62
72
  <a name="eslint-plugin-zod-rules-require-strict"></a>
63
73
  ### <code>require-strict</code>
@@ -1,5 +1,23 @@
1
1
  declare const _default: {
2
2
  rules: {
3
+ 'prefer-enum': {
4
+ create: (context: any) => {
5
+ CallExpression(node: any): void;
6
+ };
7
+ meta: {
8
+ docs: {
9
+ description: string;
10
+ url: string;
11
+ };
12
+ fixable: string;
13
+ schema: {
14
+ additionalProperties: boolean;
15
+ properties: {};
16
+ type: string;
17
+ }[];
18
+ type: string;
19
+ };
20
+ };
3
21
  'require-strict': {
4
22
  create: (context: any) => {
5
23
  CallExpression(node: any): void;
@@ -25,6 +43,7 @@ declare const _default: {
25
43
  };
26
44
  };
27
45
  rulesConfig: {
46
+ 'prefer-enum': number;
28
47
  'require-strict': number;
29
48
  };
30
49
  };
package/dist/src/index.js CHANGED
@@ -2,13 +2,16 @@
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
+ const preferEnum_1 = __importDefault(require("./rules/preferEnum"));
5
6
  const requireStrict_1 = __importDefault(require("./rules/requireStrict"));
6
7
  const rules = {
8
+ 'prefer-enum': preferEnum_1.default,
7
9
  'require-strict': requireStrict_1.default,
8
10
  };
9
11
  module.exports = {
10
12
  rules,
11
13
  rulesConfig: {
14
+ 'prefer-enum': 0,
12
15
  'require-strict': 0,
13
16
  },
14
17
  };
@@ -0,0 +1,19 @@
1
+ declare const _default: {
2
+ create: (context: any) => {
3
+ CallExpression(node: any): void;
4
+ };
5
+ meta: {
6
+ docs: {
7
+ description: string;
8
+ url: string;
9
+ };
10
+ fixable: string;
11
+ schema: {
12
+ additionalProperties: boolean;
13
+ properties: {};
14
+ type: string;
15
+ }[];
16
+ type: string;
17
+ };
18
+ };
19
+ export = _default;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ const create = (context) => {
3
+ return {
4
+ CallExpression(node) {
5
+ var _a;
6
+ if (((_a = node.callee.object) === null || _a === void 0 ? void 0 : _a.name) !== 'z') {
7
+ return;
8
+ }
9
+ if (node.callee.property.name !== 'union') {
10
+ return;
11
+ }
12
+ const members = node.arguments[0].elements;
13
+ const allMembersAreLiterals = members.every((member) => {
14
+ return member.type === 'CallExpression' && member.callee.object.name === 'z' && member.callee.property.name === 'literal';
15
+ });
16
+ if (!allMembersAreLiterals) {
17
+ return;
18
+ }
19
+ context.report({
20
+ message: 'Use z.enum().',
21
+ node,
22
+ });
23
+ },
24
+ };
25
+ };
26
+ module.exports = {
27
+ create,
28
+ meta: {
29
+ docs: {
30
+ description: 'Prefers `z.enum` over a union of literals.',
31
+ url: 'https://github.com/gajus/eslint-plugin-zod#eslint-plugin-zod-rules-prefer-enum',
32
+ },
33
+ fixable: 'code',
34
+ schema: [
35
+ {
36
+ additionalProperties: false,
37
+ properties: {},
38
+ type: 'object',
39
+ },
40
+ ],
41
+ type: 'problem',
42
+ },
43
+ };
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "lint": "eslint ./src ./test && tsc --noEmit",
47
47
  "test": "mocha --require tsx test/**/*"
48
48
  },
49
- "version": "1.3.0"
49
+ "version": "1.4.0"
50
50
  }