@brutalway/oxlint-config-react-hook-form 0.1.0 → 0.2.1

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
@@ -1,6 +1,8 @@
1
1
  # @brutalway/oxlint-config-react-hook-form
2
2
 
3
- Oxlint config for react-hook-form with Zod. A form validates in one place, its schema, so the type of the values it hands over is the type the schema checked.
3
+ Oxlint config for react-hook-form with Zod. A form validates in one place, its schema.
4
+
5
+ ## Install
4
6
 
5
7
  ```sh
6
8
  npm install --save-dev oxlint @brutalway/oxlint-config-react-hook-form
@@ -16,7 +18,13 @@ export default defineConfig({
16
18
  });
17
19
  ```
18
20
 
19
- ## `form-conventions/zod-resolver`
21
+ ## Usage
22
+
23
+ When `form-conventions/validation-in-schema` fires, move the rule into the Zod schema, where `.min(1, 'The title is blank')` also carries the message shown to the user.
24
+
25
+ ## Rules
26
+
27
+ ### `form-conventions/zod-resolver`
20
28
 
21
29
  `useForm` takes `resolver: zodResolver(XSchema)`. A `useForm()` without options, or with another resolver, is reported:
22
30
 
@@ -24,6 +32,10 @@ export default defineConfig({
24
32
  useForm({ resolver: zodResolver(TodoDraftSchema), defaultValues: { title: '' } });
25
33
  ```
26
34
 
27
- ## `form-conventions/validation-in-schema`
35
+ ### `form-conventions/validation-in-schema`
36
+
37
+ No validation outside the schema: `register('title', { required: true })`, `useController({ rules })` and `<Controller rules={...} />` are reported.
38
+
39
+ ## Why
28
40
 
29
- No validation outside the schema: `register('title', { required: true })`, `useController({ rules })` and `<Controller rules={...} />` are reported. Move the rule into the Zod schema, where `.min(1, 'The title is blank')` also carries the message shown to the user.
41
+ With the schema as the only place that validates, the type of the values the form hands over is the type the schema checked.
@@ -1,2 +1,2 @@
1
- import type { OxlintConfig } from 'oxlint';
2
- export declare const reactHookFormConfig: OxlintConfig;
1
+ import { OxlintConfig } from "oxlint";
2
+ export declare const reactHookFormConfig: OxlintConfig;
@@ -1,9 +1,14 @@
1
- import { fileURLToPath } from 'node:url';
2
- import { defineConfig } from 'oxlint';
3
- export const reactHookFormConfig = defineConfig({
4
- jsPlugins: [{ name: 'form-conventions', specifier: fileURLToPath(new URL('./plugin.js', import.meta.url)) }],
5
- rules: {
6
- 'form-conventions/zod-resolver': 'error',
7
- 'form-conventions/validation-in-schema': 'error',
8
- },
1
+ import { fileURLToPath } from "node:url";
2
+ import { defineConfig } from "oxlint";
3
+ // src/ReactHookFormConfig.ts
4
+ const reactHookFormConfig = defineConfig({
5
+ jsPlugins: [{
6
+ name: "form-conventions",
7
+ specifier: fileURLToPath(new URL("./plugin.js", import.meta.url))
8
+ }],
9
+ rules: {
10
+ "form-conventions/zod-resolver": "error",
11
+ "form-conventions/validation-in-schema": "error"
12
+ }
9
13
  });
14
+ export { reactHookFormConfig };
package/dist/plugin.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  declare const _default: import("@oxlint/plugins").Plugin;
2
- export default _default;
2
+ export { _default as default };
package/dist/plugin.js CHANGED
@@ -1,7 +1,79 @@
1
- import { definePlugin } from '@oxlint/plugins';
2
- import { validationInSchema } from './validationInSchema.js';
3
- import { zodResolver } from './zodResolver.js';
4
- export default definePlugin({
5
- meta: { name: 'form-conventions' },
6
- rules: { 'zod-resolver': zodResolver, 'validation-in-schema': validationInSchema },
1
+ import { definePlugin, defineRule } from "@oxlint/plugins";
2
+ // src/messages.json
3
+ var outsideSchema = "Validation lives in the Zod schema of the form: move this rule into it, so there is one place to read what a field accepts.";
4
+ var noSchema = "useForm takes resolver: zodResolver(XSchema), so the type of the form is the type its schema validates.";
5
+ // src/features/validation-in-schema/validationInSchema.ts
6
+ function calleeName(callee) {
7
+ if (callee.type === "Identifier") return callee.name;
8
+ if (callee.type === "MemberExpression" && callee.property.type === "Identifier") return callee.property.name;
9
+ }
10
+ function isRulesProperty(property) {
11
+ return property.type === "Property" && property.key.type === "Identifier" && property.key.name === "rules";
12
+ }
13
+ function validationOutsideSchema(call) {
14
+ const name = calleeName(call.callee);
15
+ if (name === "register") return call.arguments.length > 1 ? [call.arguments[1] ?? call] : [];
16
+ if (name !== "useController") return [];
17
+ const [options] = call.arguments;
18
+ if (options?.type !== "ObjectExpression") return [];
19
+ return options.properties.filter((property) => isRulesProperty(property));
20
+ }
21
+ function isControllerRules(attribute) {
22
+ const element = attribute.parent;
23
+ return attribute.name.type === "JSXIdentifier" && attribute.name.name === "rules" && element.type === "JSXOpeningElement" && element.name.type === "JSXIdentifier" && element.name.name === "Controller";
24
+ }
25
+ const validationInSchema = defineRule({
26
+ meta: {
27
+ type: "problem",
28
+ docs: { description: "No validation passed to register, useController or a Controller." },
29
+ messages: { outsideSchema },
30
+ schema: []
31
+ },
32
+ create(context) {
33
+ return {
34
+ CallExpression(call) {
35
+ for (const node of validationOutsideSchema(call)) context.report({
36
+ node,
37
+ messageId: "outsideSchema"
38
+ });
39
+ },
40
+ JSXAttribute(attribute) {
41
+ if (isControllerRules(attribute)) context.report({
42
+ node: attribute,
43
+ messageId: "outsideSchema"
44
+ });
45
+ }
46
+ };
47
+ }
7
48
  });
49
+ // src/features/zod-resolver/zodResolver.ts
50
+ function resolvesWithZod(options) {
51
+ if (options?.type !== "ObjectExpression") return false;
52
+ return options.properties.some((property) => property.type === "Property" && property.key.type === "Identifier" && property.key.name === "resolver" && property.value.type === "CallExpression" && property.value.callee.type === "Identifier" && property.value.callee.name === "zodResolver");
53
+ }
54
+ const zodResolver = defineRule({
55
+ meta: {
56
+ type: "problem",
57
+ docs: { description: "Every useForm validates through zodResolver." },
58
+ messages: { noSchema },
59
+ schema: []
60
+ },
61
+ create(context) {
62
+ return { CallExpression(call) {
63
+ if (call.callee.type !== "Identifier" || call.callee.name !== "useForm" || resolvesWithZod(call.arguments[0])) return;
64
+ context.report({
65
+ node: call,
66
+ messageId: "noSchema"
67
+ });
68
+ } };
69
+ }
70
+ });
71
+ // src/plugin.ts
72
+ var plugin_default = definePlugin({
73
+ meta: { name: "form-conventions" },
74
+ rules: {
75
+ "zod-resolver": zodResolver,
76
+ "validation-in-schema": validationInSchema
77
+ }
78
+ });
79
+ export { plugin_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brutalway/oxlint-config-react-hook-form",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Oxlint config for react-hook-form: every useForm validates through zodResolver, and no validation is passed to register or a Controller.",
5
5
  "keywords": [
6
6
  "oxlint",
@@ -20,13 +20,27 @@
20
20
  "license": "MIT",
21
21
  "author": "elnop",
22
22
  "type": "module",
23
+ "engines": {
24
+ "node": ">=26"
25
+ },
23
26
  "files": [
24
- "dist",
25
- "!dist/tsconfig.tsbuildinfo"
27
+ "dist"
26
28
  ],
29
+ "imports": {
30
+ "#src/*": "./src/*"
31
+ },
27
32
  "exports": "./dist/ReactHookFormConfig.js",
28
33
  "scripts": {
29
- "test": "node --test '**/*.test.ts'"
34
+ "build": "tsdown",
35
+ "typecheck": "tsc -p .",
36
+ "lint": "oxlint --type-aware src oxlint.config.ts tsdown.config.ts",
37
+ "deps": "depcruise --output-type err-long src",
38
+ "tree": "check-tree src",
39
+ "duplicates": "jscpd src",
40
+ "check": "prettier --check . && npm run typecheck && npm run lint && npm run deps && npm run tree && npm run duplicates && npm run unused",
41
+ "test": "node --test 'src/**/*.test.ts'",
42
+ "format": "prettier --write .",
43
+ "unused": "knip"
30
44
  },
31
45
  "dependencies": {
32
46
  "@oxlint/plugins": "^1.91.0"
@@ -35,7 +49,35 @@
35
49
  "oxlint": "^1.91.0"
36
50
  },
37
51
  "devDependencies": {
38
- "@brutalway/lint-utils": "^0.1.0",
39
- "oxlint": "^1.91.0"
52
+ "@brutalway/check-tree": "0.10.0",
53
+ "@brutalway/dependency-cruiser-config": "0.10.0",
54
+ "@brutalway/lint-utils": "0.2.0",
55
+ "@brutalway/oxlint-config-node": "0.2.1",
56
+ "@brutalway/oxlint-config-node-test": "0.1.0",
57
+ "@brutalway/oxlint-config-typescript": "0.10.0",
58
+ "@brutalway/prettier-config": "0.3.0",
59
+ "@brutalway/tsconfig": "0.4.0",
60
+ "@swc/core": "1.16.22",
61
+ "@types/node": "26.6.6",
62
+ "dependency-cruiser": "18.7.2",
63
+ "jscpd": "5.4.3",
64
+ "knip": "6.44.2",
65
+ "oxlint": "1.92.0",
66
+ "oxlint-tsgolint": "7.0.2006",
67
+ "prettier": "3.9.10",
68
+ "tsdown": "0.23.7",
69
+ "typescript": "7.0.2"
70
+ },
71
+ "prettier": "@brutalway/prettier-config",
72
+ "check-tree": {
73
+ "rootEntries": true
74
+ },
75
+ "allowScripts": {
76
+ "@swc/core": false
77
+ },
78
+ "overrides": {
79
+ "eslint-plugin-no-comments": {
80
+ "minimatch": "^11.0.1"
81
+ }
40
82
  }
41
83
  }
@@ -1 +0,0 @@
1
- export declare const validationInSchema: import("@oxlint/plugins").Rule;
@@ -1,43 +0,0 @@
1
- import { defineRule } from '@oxlint/plugins';
2
- function calleeName(callee) {
3
- if (callee.type === 'Identifier')
4
- return callee.name;
5
- if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier')
6
- return callee.property.name;
7
- return undefined;
8
- }
9
- const MESSAGE = 'Validation lives in the Zod schema of the form: move this rule into it, so there is one place to read what a field accepts.';
10
- export const validationInSchema = defineRule({
11
- meta: {
12
- type: 'problem',
13
- docs: { description: 'No validation passed to register, useController or a Controller.' },
14
- messages: { outsideSchema: MESSAGE },
15
- schema: [],
16
- },
17
- create(context) {
18
- return {
19
- CallExpression(call) {
20
- const name = calleeName(call.callee);
21
- if (name === 'register' && call.arguments.length > 1)
22
- context.report({ node: call.arguments[1] ?? call, messageId: 'outsideSchema' });
23
- if (name !== 'useController')
24
- return;
25
- const [options] = call.arguments;
26
- if (options?.type !== 'ObjectExpression')
27
- return;
28
- for (const property of options.properties) {
29
- if (property.type === 'Property' && property.key.type === 'Identifier' && property.key.name === 'rules')
30
- context.report({ node: property, messageId: 'outsideSchema' });
31
- }
32
- },
33
- JSXAttribute(attribute) {
34
- const element = attribute.parent;
35
- if (attribute.name.type !== 'JSXIdentifier' || attribute.name.name !== 'rules')
36
- return;
37
- if (element.type !== 'JSXOpeningElement' || element.name.type !== 'JSXIdentifier' || element.name.name !== 'Controller')
38
- return;
39
- context.report({ node: attribute, messageId: 'outsideSchema' });
40
- },
41
- };
42
- },
43
- });
@@ -1 +0,0 @@
1
- export declare const zodResolver: import("@oxlint/plugins").Rule;
@@ -1,28 +0,0 @@
1
- import { defineRule } from '@oxlint/plugins';
2
- function resolvesWithZod(options) {
3
- if (options?.type !== 'ObjectExpression')
4
- return false;
5
- return options.properties.some((property) => property.type === 'Property' &&
6
- property.key.type === 'Identifier' &&
7
- property.key.name === 'resolver' &&
8
- property.value.type === 'CallExpression' &&
9
- property.value.callee.type === 'Identifier' &&
10
- property.value.callee.name === 'zodResolver');
11
- }
12
- export const zodResolver = defineRule({
13
- meta: {
14
- type: 'problem',
15
- docs: { description: 'Every useForm validates through zodResolver.' },
16
- messages: { noSchema: 'useForm takes resolver: zodResolver(XSchema), so the type of the form is the type its schema validates.' },
17
- schema: [],
18
- },
19
- create(context) {
20
- return {
21
- CallExpression(call) {
22
- if (call.callee.type !== 'Identifier' || call.callee.name !== 'useForm' || resolvesWithZod(call.arguments[0]))
23
- return;
24
- context.report({ node: call, messageId: 'noSchema' });
25
- },
26
- };
27
- },
28
- });