eslint-plugin-zod 1.2.1 → 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>
@@ -66,5 +76,13 @@ _The `--fix` option on the command line automatically fixes problems reported by
66
76
 
67
77
  Requires that objects are initialized with `.strict()`.
68
78
 
79
+ <a name="user-content-eslint-plugin-zod-rules-require-strict-options"></a>
80
+ <a name="eslint-plugin-zod-rules-require-strict-options"></a>
81
+ #### Options
82
+
83
+ |configuration|format|default|description|
84
+ |---|---|---|---|
85
+ |`allowPassthrough`|boolean|`true`|Ignores objects explicitly set to `allowPassthrough()`.|
86
+
69
87
 
70
88
 
@@ -1,6 +1,6 @@
1
1
  declare const _default: {
2
2
  rules: {
3
- 'require-strict': {
3
+ 'prefer-enum': {
4
4
  create: (context: any) => {
5
5
  CallExpression(node: any): void;
6
6
  };
@@ -18,8 +18,32 @@ declare const _default: {
18
18
  type: string;
19
19
  };
20
20
  };
21
+ 'require-strict': {
22
+ create: (context: any) => {
23
+ CallExpression(node: any): void;
24
+ };
25
+ meta: {
26
+ docs: {
27
+ description: string;
28
+ url: string;
29
+ };
30
+ fixable: string;
31
+ schema: {
32
+ additionalProperties: boolean;
33
+ properties: {
34
+ allowPassthrough: {
35
+ default: boolean;
36
+ type: string;
37
+ };
38
+ };
39
+ type: string;
40
+ }[];
41
+ type: string;
42
+ };
43
+ };
21
44
  };
22
45
  rulesConfig: {
46
+ 'prefer-enum': number;
23
47
  'require-strict': number;
24
48
  };
25
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
+ };
@@ -10,7 +10,12 @@ declare const _default: {
10
10
  fixable: string;
11
11
  schema: {
12
12
  additionalProperties: boolean;
13
- properties: {};
13
+ properties: {
14
+ allowPassthrough: {
15
+ default: boolean;
16
+ type: string;
17
+ };
18
+ };
14
19
  type: string;
15
20
  }[];
16
21
  type: string;
@@ -1,14 +1,18 @@
1
1
  "use strict";
2
+ const defaultOptions = {
3
+ allowPassthrough: true,
4
+ };
2
5
  const create = (context) => {
3
6
  return {
4
7
  CallExpression(node) {
5
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
8
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
6
9
  if (((_a = node.callee.object) === null || _a === void 0 ? void 0 : _a.name) !== 'z') {
7
10
  return;
8
11
  }
9
12
  if (node.callee.property.name !== 'object') {
10
13
  return;
11
14
  }
15
+ const { allowPassthrough, } = (_b = context.options[0]) !== null && _b !== void 0 ? _b : defaultOptions;
12
16
  if (!node.parent.property) {
13
17
  context.report({
14
18
  fix: (fixer) => {
@@ -18,14 +22,18 @@ const create = (context) => {
18
22
  node,
19
23
  });
20
24
  }
21
- else if (((_c = (_b = node.parent) === null || _b === void 0 ? void 0 : _b.property) === null || _c === void 0 ? void 0 : _c.name) === 'and') {
22
- // Ignore .and() calls
23
- }
24
25
  else if (
25
26
  // z.object().strict()
26
- ((_e = (_d = node.parent) === null || _d === void 0 ? void 0 : _d.property) === null || _e === void 0 ? void 0 : _e.name) !== 'strict' &&
27
+ ((_d = (_c = node.parent) === null || _c === void 0 ? void 0 : _c.property) === null || _d === void 0 ? void 0 : _d.name) !== 'strict' &&
27
28
  // z.object().merge().strict()
28
- ((_j = (_h = (_g = (_f = node.parent) === null || _f === void 0 ? void 0 : _f.parent) === null || _g === void 0 ? void 0 : _g.parent) === null || _h === void 0 ? void 0 : _h.property) === null || _j === void 0 ? void 0 : _j.name) !== 'strict') {
29
+ ((_h = (_g = (_f = (_e = node.parent) === null || _e === void 0 ? void 0 : _e.parent) === null || _f === void 0 ? void 0 : _f.parent) === null || _g === void 0 ? void 0 : _g.property) === null || _h === void 0 ? void 0 : _h.name) !== 'strict') {
30
+ if (((_k = (_j = node.parent) === null || _j === void 0 ? void 0 : _j.property) === null || _k === void 0 ? void 0 : _k.name) === 'passthrough' && allowPassthrough) {
31
+ return;
32
+ }
33
+ if (((_m = (_l = node.parent) === null || _l === void 0 ? void 0 : _l.property) === null || _m === void 0 ? void 0 : _m.name) === 'and') {
34
+ // Ignore .and() calls
35
+ return;
36
+ }
29
37
  // As far as I can think, in cases where the property name is not-strict,
30
38
  // e.g. passthrough, we should not add a strict() call.
31
39
  context.report({
@@ -47,7 +55,12 @@ module.exports = {
47
55
  schema: [
48
56
  {
49
57
  additionalProperties: false,
50
- properties: {},
58
+ properties: {
59
+ allowPassthrough: {
60
+ default: true,
61
+ type: 'boolean',
62
+ },
63
+ },
51
64
  type: 'object',
52
65
  },
53
66
  ],
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.2.1"
49
+ "version": "1.4.0"
50
50
  }