@chungwei/oxlint-plugin 0.0.0 → 0.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 +41 -9
- package/dist/index.js +2 -0
- package/dist/rules/no-use-effect.d.ts +1 -0
- package/dist/rules/no-use-effect.js +87 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,8 +8,6 @@ Custom Oxlint rules migrated from paint-mixer-app.
|
|
|
8
8
|
npm install -D @chungwei/oxlint-plugin oxlint
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Requires Oxlint 1.79.0 or later. The rules are authored in TypeScript and published as unbundled JavaScript with TypeScript declarations. Consumers need no build step, TypeScript loader, or Vite+ runtime.
|
|
12
|
-
|
|
13
11
|
## Usage
|
|
14
12
|
|
|
15
13
|
Add the plugin and enable the rules in `oxlint.config.ts`:
|
|
@@ -22,12 +20,13 @@ export default defineConfig({
|
|
|
22
20
|
rules: {
|
|
23
21
|
"chungwei/no-em-dash": "error",
|
|
24
22
|
"chungwei/no-inline-component-props": "error",
|
|
23
|
+
"chungwei/no-use-effect": "error",
|
|
25
24
|
"chungwei/prefer-jsx-short-circuit": "error",
|
|
26
25
|
},
|
|
27
26
|
});
|
|
28
27
|
```
|
|
29
28
|
|
|
30
|
-
The same `jsPlugins` and `rules` entries work under `lint` in a Vite+ config. All rules accept no options. Enable them individually when using this plugin directly. `@chungwei/oxlint-config` installs the plugin and enables all
|
|
29
|
+
The same `jsPlugins` and `rules` entries work under `lint` in a Vite+ config. All rules accept no options. Enable them individually when using this plugin directly. `@chungwei/oxlint-config` installs the plugin and enables all its rules as errors.
|
|
31
30
|
|
|
32
31
|
The plugin is also available as both a default and a named export:
|
|
33
32
|
|
|
@@ -44,6 +43,7 @@ When replacing the app's local plugin, use the alias `custom` instead of `chungw
|
|
|
44
43
|
| --------------------------- | ---------------------------------------------------------------------------------------- | ----------------- |
|
|
45
44
|
| `no-em-dash` | Disallows em dashes in string literals, template text, JSX text, and JSX attribute text. | None |
|
|
46
45
|
| `no-inline-component-props` | Requires named types for destructured component props in `.tsx` files. | None |
|
|
46
|
+
| `no-use-effect` | Disallows direct React `useEffect` calls in components and custom hooks. | None |
|
|
47
47
|
| `prefer-jsx-short-circuit` | Prefers short-circuit rendering for JSX child ternaries with one empty branch. | Editor suggestion |
|
|
48
48
|
|
|
49
49
|
### `no-em-dash`
|
|
@@ -72,6 +72,44 @@ function Panel({ title }: PanelProps) {
|
|
|
72
72
|
|
|
73
73
|
Non-destructured props, named types, ordinary helper functions, and files other than `.tsx` are allowed. The rule does not rename or extract types automatically.
|
|
74
74
|
|
|
75
|
+
### `no-use-effect`
|
|
76
|
+
|
|
77
|
+
`useEffect` is hard to use correctly without experience with React. Choosing dependencies, avoiding stale closures, and handling setup and cleanup require understanding how Effects interact with rendering. Mistakes can cause repeated updates, stale data, or subscriptions that never get cleaned up.
|
|
78
|
+
|
|
79
|
+
This rule bans direct `useEffect` calls by default to steer developers and coding agents toward simpler patterns. Calculate derived values during render, handle user actions in event handlers, and use the project's data fetching or subscription hooks where appropriate. For example, deriving a greeting from a prop needs no Effect or extra state:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
// Reported
|
|
83
|
+
import { useEffect } from "react";
|
|
84
|
+
|
|
85
|
+
function Greeting({ name }: GreetingProps) {
|
|
86
|
+
const [greeting, setGreeting] = useState("");
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
setGreeting(`Hello, ${name}`);
|
|
89
|
+
}, [name]);
|
|
90
|
+
return <p>{greeting}</p>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Allowed
|
|
94
|
+
function Greeting({ name }: GreetingProps) {
|
|
95
|
+
const greeting = `Hello, ${name}`;
|
|
96
|
+
return <p>{greeting}</p>;
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
See React's [You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect) guide for more alternatives. Do not move side effects into render, `useMemo`, or `useLayoutEffect` to bypass this rule.
|
|
101
|
+
|
|
102
|
+
Effects are still appropriate for synchronizing with external systems. When one is necessary, use a narrow disable comment explaining why, or turn the rule off for a specific integration file through a config override:
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
// oxlint-disable-next-line chungwei/no-use-effect -- Keep the external widget synchronized with the selected value.
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
widget.setValue(value);
|
|
108
|
+
}, [widget, value]);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The rule checks components and custom hooks in JavaScript and TypeScript, recognizes React import aliases and namespace access, and reports unbound `useEffect` and `React.useEffect` calls. Shadowed names, imports from other libraries, and type-only imports are ignored. It checks direct calls only; it does not trace variable aliases, re-exports, CommonJS imports, or dynamic property access. There is no automatic fix because the replacement depends on the Effect's purpose.
|
|
112
|
+
|
|
75
113
|
### `prefer-jsx-short-circuit`
|
|
76
114
|
|
|
77
115
|
Reports a ternary directly in JSX children when one branch is JSX and the other is `null`, `false`, or unshadowed `undefined`.
|
|
@@ -85,9 +123,3 @@ Reports a ternary directly in JSX children when one branch is JSX and the other
|
|
|
85
123
|
```
|
|
86
124
|
|
|
87
125
|
Suggestions coerce conditions to booleans when needed so falsy numbers do not render as text. For an empty truthy branch, the suggestion negates the condition. The rule offers no suggestion if replacement would discard a comment. It does not automatically fix code, and does not check ternaries in attributes, callbacks, or ordinary return expressions.
|
|
88
|
-
|
|
89
|
-
## Development
|
|
90
|
-
|
|
91
|
-
From the repository root, run `pnpm check` for formatting, linting, and type checking, and `pnpm test` for rule tests. Both commands build the plugin first so the workspace config can load it. `pnpm build` compiles the TypeScript source into JavaScript and declarations in `dist`.
|
|
92
|
-
|
|
93
|
-
`pnpm --filter @chungwei/oxlint-plugin pack` runs the build automatically through `prepack`, which also runs when publishing. Tests live beside the modules they cover and are excluded from the build and published package.
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
2
2
|
import { noEmDashRule } from "./rules/no-em-dash.js";
|
|
3
3
|
import { noInlineComponentPropsRule } from "./rules/no-inline-component-props.js";
|
|
4
|
+
import { noUseEffectRule } from "./rules/no-use-effect.js";
|
|
4
5
|
import { preferJsxShortCircuitRule } from "./rules/prefer-jsx-short-circuit.js";
|
|
5
6
|
const plugin = eslintCompatPlugin({
|
|
6
7
|
meta: {
|
|
@@ -9,6 +10,7 @@ const plugin = eslintCompatPlugin({
|
|
|
9
10
|
rules: {
|
|
10
11
|
"no-em-dash": noEmDashRule,
|
|
11
12
|
"no-inline-component-props": noInlineComponentPropsRule,
|
|
13
|
+
"no-use-effect": noUseEffectRule,
|
|
12
14
|
"prefer-jsx-short-circuit": preferJsxShortCircuitRule,
|
|
13
15
|
},
|
|
14
16
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const noUseEffectRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
function unwrap(node) {
|
|
3
|
+
while (node.type === "ParenthesizedExpression" ||
|
|
4
|
+
node.type === "TSAsExpression" ||
|
|
5
|
+
node.type === "TSSatisfiesExpression" ||
|
|
6
|
+
node.type === "TSNonNullExpression" ||
|
|
7
|
+
node.type === "TSTypeAssertion" ||
|
|
8
|
+
node.type === "ChainExpression") {
|
|
9
|
+
node = node.expression;
|
|
10
|
+
}
|
|
11
|
+
return node;
|
|
12
|
+
}
|
|
13
|
+
function reactImportName(node, context) {
|
|
14
|
+
const value = unwrap(node);
|
|
15
|
+
if (value.type !== "Identifier") {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
let scope = context.sourceCode.getScope(value);
|
|
19
|
+
while (true) {
|
|
20
|
+
const binding = scope.set.get(value.name);
|
|
21
|
+
if (binding && binding.defs.length > 0) {
|
|
22
|
+
const definition = binding.defs[0];
|
|
23
|
+
if (definition.type !== "ImportBinding") {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const specifier = definition.node;
|
|
27
|
+
const declaration = specifier.parent;
|
|
28
|
+
if (declaration?.type !== "ImportDeclaration" ||
|
|
29
|
+
declaration.source.value !== "react" ||
|
|
30
|
+
declaration.importKind === "type") {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
if (specifier.type === "ImportSpecifier") {
|
|
34
|
+
if (specifier.importKind === "type") {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const name = specifier.imported.type === "Identifier"
|
|
38
|
+
? specifier.imported.name
|
|
39
|
+
: specifier.imported.value;
|
|
40
|
+
return name === "default" ? "*" : name;
|
|
41
|
+
}
|
|
42
|
+
return "*";
|
|
43
|
+
}
|
|
44
|
+
if (!scope.upper) {
|
|
45
|
+
return value.name === "React" ? "*" : value.name;
|
|
46
|
+
}
|
|
47
|
+
scope = scope.upper;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export const noUseEffectRule = defineRule({
|
|
51
|
+
meta: {
|
|
52
|
+
type: "suggestion",
|
|
53
|
+
docs: {
|
|
54
|
+
description: "Disallow direct React useEffect calls",
|
|
55
|
+
},
|
|
56
|
+
schema: [],
|
|
57
|
+
messages: {
|
|
58
|
+
noUseEffect: "Avoid useEffect. Derive values during render, handle user actions in event handlers, or use a purpose-specific data or subscription hook. For necessary external synchronization, add a narrow lint exception with a reason.",
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
createOnce(context) {
|
|
62
|
+
return {
|
|
63
|
+
CallExpression(node) {
|
|
64
|
+
const callee = unwrap(node.callee);
|
|
65
|
+
if (callee.type === "Identifier") {
|
|
66
|
+
if (reactImportName(callee, context) !== "useEffect") {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (callee.type === "MemberExpression") {
|
|
71
|
+
const name = !callee.computed && callee.property.type === "Identifier"
|
|
72
|
+
? callee.property.name
|
|
73
|
+
: callee.property.type === "Literal"
|
|
74
|
+
? callee.property.value
|
|
75
|
+
: null;
|
|
76
|
+
if (name !== "useEffect" || reactImportName(callee.object, context) !== "*") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
context.report({ node: callee, messageId: "noUseEffect" });
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
});
|
package/package.json
CHANGED