@murky-web/oxlint-plugin-solid 1.5.1 → 1.6.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 +161 -16
- package/THIRD_PARTY_NOTICES.md +7 -4
- package/package.json +5 -2
- package/src/compat.mjs +3 -0
- package/src/index.mjs +23 -0
- package/src/rules/components_return_once.mjs +131 -84
- package/src/rules/event_handlers.mjs +33 -13
- package/src/rules/filename_matches_export.mjs +292 -0
- package/src/rules/function_size.mjs +189 -0
- package/src/rules/imports.mjs +5 -2
- package/src/rules/init_declarations.mjs +139 -0
- package/src/rules/jsx_no_duplicate_props.mjs +36 -8
- package/src/rules/jsx_no_script_url.mjs +3 -2
- package/src/rules/jsx_no_undef.mjs +8 -11
- package/src/rules/no_array_handlers.mjs +35 -6
- package/src/rules/no_cleanup_in_effect_apply.mjs +154 -0
- package/src/rules/no_destructure.mjs +76 -4
- package/src/rules/no_innerhtml.mjs +51 -18
- package/src/rules/no_module_scope_reactive_primitive.mjs +73 -0
- package/src/rules/no_owned_primitives_in_ref.mjs +23 -2
- package/src/rules/no_proxy_apis.mjs +41 -12
- package/src/rules/no_react_deps.mjs +394 -38
- package/src/rules/no_react_specific_props.mjs +33 -4
- package/src/rules/no_server_import_in_client.mjs +1 -1
- package/src/rules/no_setter_in_effect.mjs +145 -22
- package/src/rules/no_single_arg_create_effect.mjs +55 -0
- package/src/rules/no_unassigned_vars.mjs +99 -0
- package/src/rules/no_unknown_namespaces.mjs +96 -17
- package/src/rules/no_untracked_effect_read.mjs +65 -28
- package/src/rules/no_write_in_pure_computation.mjs +273 -0
- package/src/rules/prefer_arrow_components.mjs +30 -4
- package/src/rules/prefer_class_object.mjs +37 -5
- package/src/rules/prefer_destructuring.mjs +164 -0
- package/src/rules/prefer_for.mjs +34 -4
- package/src/rules/prefer_ref_shorthand.mjs +142 -0
- package/src/rules/prefer_show.mjs +48 -17
- package/src/rules/reactive_async.mjs +55 -8
- package/src/rules/reactivity.mjs +1160 -291
- package/src/rules/self_closing_comp.mjs +7 -2
- package/src/rules/strict_void_return.mjs +43 -13
- package/src/rules/style_prop.mjs +38 -9
- package/src/rules/typed_merge_defaults.mjs +88 -0
- package/src/rules/validate_jsx_nesting.mjs +2 -2
- package/src/utils.mjs +51 -20
package/README.md
CHANGED
|
@@ -17,17 +17,37 @@ TypeScript-ESLint-Toolchain in den Baum jedes Konsumenten zu ziehen.
|
|
|
17
17
|
|
|
18
18
|
Aktuell sind enthalten:
|
|
19
19
|
|
|
20
|
-
- die
|
|
20
|
+
- die Regeln von `eslint-plugin-solid` 0.14.5; was Upstream bis 0.20.1
|
|
21
|
+
dazugebaut hat, fehlt teils (`bun upstream/run.mjs` zeigt den Abgleich)
|
|
21
22
|
- die zusaetzlichen Projektregeln `solid/prefer-arrow-components`,
|
|
22
23
|
`solid/no-setter-in-effect` (meldet Effects, die nur in ein Signal oder einen
|
|
23
24
|
Store schreiben, statt den Wert abzuleiten -- und Effects, die etwas awaiten
|
|
24
25
|
und das Ergebnis zurueckschreiben, statt es aus einer Derivation zu liefern)
|
|
25
26
|
`solid/no-untracked-effect-read` (meldet reaktive Reads in der apply-Phase
|
|
26
|
-
eines zweiphasigen `createEffect`, die dort nicht tracken)
|
|
27
|
+
eines zweiphasigen `createEffect`, die dort nicht tracken),
|
|
28
|
+
`solid/no-cleanup-in-effect-apply` (siehe unten) und
|
|
27
29
|
`solid/no-owned-primitives-in-ref` (meldet `createEffect`/`onCleanup` und
|
|
28
30
|
Verwandte in einem Ref-Callback -- der laeuft ohne Owner, nichts raeumt sie je
|
|
29
31
|
wieder ab), `solid/reactive-async`, `solid/no-server-import-in-client` und
|
|
30
32
|
`solid/strict-void-return` (siehe unten)
|
|
33
|
+
- `solid/prefer-destructuring`, ein Fork von eslints `prefer-destructuring`: er
|
|
34
|
+
verlangt Destructuring wie das Original, nur nicht aus `props`, dem ersten
|
|
35
|
+
Parameter einer Komponente und Ergebnissen von `createStore`,
|
|
36
|
+
`createProjection`, `createOptimisticStore`, `merge` und `omit`. Dort kopiert
|
|
37
|
+
Destructuring den Wert einmal und verliert die Reaktivitaet.
|
|
38
|
+
- `solid/typed-merge-defaults`: verlangt `satisfies Partial<Props>` (oder
|
|
39
|
+
`as const`) an Objekt-Literalen in `merge`/`mergeProps`. Ohne werden Literale
|
|
40
|
+
zu `string` aufgeweitet, und `as Partial<Props>` macht die Defaults wieder
|
|
41
|
+
optional. `omit` behaelt seine Typen und braucht nichts.
|
|
42
|
+
- `solid/no-module-scope-reactive-primitive` aus eslint-plugin-solid 0.20.1
|
|
43
|
+
(meldet `createSignal`, `createStore` und Verwandte auf Modul-Ebene: beim SSR
|
|
44
|
+
teilen sich alle Requests diesen Zustand) und
|
|
45
|
+
`solid/no-write-in-pure-computation` (meldet Setter in `createMemo`, in der
|
|
46
|
+
Compute-Phase von `createEffect` und im Component-Body)
|
|
47
|
+
- `solid/init-declarations`, `solid/no-unassigned-vars` und
|
|
48
|
+
`solid/prefer-ref-shorthand` fuer Refs (siehe unten)
|
|
49
|
+
- `solid/filename-matches-export` fuer Dateinamen (siehe unten)
|
|
50
|
+
- `solid/function-size` fuer die Laenge von Funktionen (siehe unten)
|
|
31
51
|
- ein Test-Harness, der die exportierte Rule-Surface und echte Diagnostik gegen
|
|
32
52
|
Temp-Projekte prueft
|
|
33
53
|
|
|
@@ -45,32 +65,41 @@ Oxlint-Konfiguration als JS-Plugin geladen:
|
|
|
45
65
|
```
|
|
46
66
|
|
|
47
67
|
Die Regeln heissen `solid/<name>`. Welche Regeln dieses Repo selbst einschaltet,
|
|
48
|
-
steht in `
|
|
68
|
+
steht in `packages/oxc-config/linting/solid.jsonc`.
|
|
49
69
|
|
|
50
70
|
## Regeln fuer das Solid-2.0-Modell
|
|
51
71
|
|
|
52
72
|
Die drei Regeln arbeiten ohne Typinformation, nur mit Syntax und Scope. Sie sind
|
|
53
|
-
in `
|
|
54
|
-
die Typ-Regeln `typescript/promise-function-async` und
|
|
73
|
+
in `packages/oxc-config/linting/solid.jsonc` aktiv und ersetzen dort fuer
|
|
74
|
+
`*.tsx`/`*.jsx` die Typ-Regeln `typescript/promise-function-async` und
|
|
55
75
|
`typescript/strict-void-return`.
|
|
56
76
|
|
|
57
77
|
### `solid/reactive-async`
|
|
58
78
|
|
|
59
79
|
In einer reaktiven Berechnung liest Solid ein zurueckgegebenes Promise wie einen
|
|
60
|
-
Wert
|
|
61
|
-
|
|
62
|
-
`
|
|
63
|
-
|
|
80
|
+
Wert. In `.tsx`/`.jsx` braucht `createMemo(() => getProduct(props.id))` deshalb
|
|
81
|
+
kein `async`; in `.ts` verlangt `typescript/promise-function-async` es, also
|
|
82
|
+
`createMemo(async () => getProduct(props.id))`, und `typescript/require-await`
|
|
83
|
+
laesst das ohne `await` zu. Geprueft werden die Callbacks von `createMemo`,
|
|
84
|
+
`createDeferred`, `createSignal(fn)`, `createStore(fn)`, `createProjection`,
|
|
85
|
+
`createOptimistic`, `createOptimisticStore`, `dynamic`, `dynamicComponent` und
|
|
86
|
+
die Compute-Phase des zweiphasigen `createEffect`/`createRenderEffect`. Die
|
|
87
|
+
empfohlene Form liest alle Inputs synchron und lagert das Async in einen Helfer
|
|
88
|
+
aus: `createMemo(() => load(id(), lang()))`.
|
|
64
89
|
|
|
65
90
|
Gemeldet wird
|
|
66
91
|
|
|
67
|
-
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
`
|
|
92
|
+
- eine Quelle (Aufruf eines Signals oder Memos, `props.x`, Property-Read eines
|
|
93
|
+
Stores), die erst nach dem ersten `await` oder `yield` im selben Callback
|
|
94
|
+
gelesen wird: Solid meldet das im Dev-Build als `UNTRACKED_READ_AFTER_AWAIT`,
|
|
95
|
+
sie wird nie eine Dependency. Wie zur Laufzeit zaehlt nur der erste Read: eine
|
|
96
|
+
Quelle, die schon vor dem `await` gelesen wurde, darf danach wieder gelesen
|
|
97
|
+
werden. Lies den Wert vor dem `await` in eine lokale Variable, nicht mit
|
|
98
|
+
`untrack()`. `solid/reactivity` meldet diese Reads nicht noch einmal.
|
|
99
|
+
- ein `async`-Callback, dessen einziges `await` ein `return await x` ist: das
|
|
100
|
+
`await` weglassen und das Promise zurueckgeben, in `.tsx` auch das `async`, in
|
|
101
|
+
`.ts` bleibt es stehen (siehe oben). Innerhalb von `try` bleibt `return await`
|
|
102
|
+
erlaubt, weil es das `catch` erst moeglich macht.
|
|
74
103
|
|
|
75
104
|
Grenzen: Reads in verschachtelten Funktionen, Helfern und `.then`-Callbacks
|
|
76
105
|
sieht die Regel nicht (die Laufzeit-Diagnose schon). Ob ein Name ein Signal ist,
|
|
@@ -149,6 +178,111 @@ Tabelle wird nicht gemeldet, der Parser laesst es dort stehen. Grenzen: ein
|
|
|
149
178
|
Elternknoten, der kein DOM-Element ist (`<Show>`, eine Komponente, ein
|
|
150
179
|
`.map()`-Callback), wird nicht durchschaut.
|
|
151
180
|
|
|
181
|
+
### `solid/no-cleanup-in-effect-apply`
|
|
182
|
+
|
|
183
|
+
Meldet `onCleanup(...)`, dessen naechste umgebende Funktion die Effect-Funktion
|
|
184
|
+
von `createEffect`/`createRenderEffect` ist (zweites Argument, oder
|
|
185
|
+
`effect`/`error` eines Bundle-Objekts): sie laeuft ohne Owner, Solid warnt
|
|
186
|
+
NO_OWNER_CLEANUP, und der Cleanup laeuft nie. Ebenso in einem `onSettled`- oder
|
|
187
|
+
`createTrackedEffect`-Callback, wo Solid CLEANUP_IN_FORBIDDEN_SCOPE wirft. Der
|
|
188
|
+
Fix ist `return () => …` aus dem Callback. Steht `onCleanup(fn)` als letzte
|
|
189
|
+
Anweisung und gibt der Callback sonst nichts zurueck, schlaegt die Regel
|
|
190
|
+
`return fn;` vor. Ref-Callbacks meldet `solid/no-owned-primitives-in-ref`.
|
|
191
|
+
|
|
192
|
+
Hintergrund zu allen Effect-Regeln:
|
|
193
|
+
[`docs/solid-2-effects.md`](../../docs/solid-2-effects.md).
|
|
194
|
+
|
|
195
|
+
### `solid/init-declarations`
|
|
196
|
+
|
|
197
|
+
Ein Fork von eslints `init-declarations` im Modus `"always"`: jedes `let` und
|
|
198
|
+
`var` braucht einen Startwert, ausser eine Variable, die ein `ref` in derselben
|
|
199
|
+
Datei zuweist. Zugewiesen heisst `ref={el}` oder `ref={el!}` an einem beliebigen
|
|
200
|
+
JSX-Element (der Compiler macht aus dem einzelnen `ref` eine Zuweisung) oder
|
|
201
|
+
`el = node` in einem Ref-Callback, direkt oder als Eintrag in `ref={[…]}`. Die
|
|
202
|
+
beiden Formen dafuer sind `let el!: HTMLDivElement;` fuer Elemente, die immer
|
|
203
|
+
gerendert werden, und `let el: Optional<HTMLDivElement>;` (typebuddy) fuer
|
|
204
|
+
bedingte. Wie in eslint bleiben `for-in`-/`for-of`-Variablen und `declare`
|
|
205
|
+
aussen vor, `const` ist nicht Sache der Regel. Ein nacktes `el` in einem
|
|
206
|
+
Ref-Array wird nie zugewiesen; das meldet `solid/prefer-ref-shorthand` am
|
|
207
|
+
Eintrag, nicht diese Regel an der Deklaration.
|
|
208
|
+
|
|
209
|
+
In `oxc-config` ist sie nur fuer `*.tsx`/`*.jsx` an und ersetzt dort
|
|
210
|
+
`eslint/init-declarations`; `.ts`-Dateien behalten eslints Regel.
|
|
211
|
+
|
|
212
|
+
### `solid/no-unassigned-vars`
|
|
213
|
+
|
|
214
|
+
Port von Upstreams `no-unassigned-vars` (0.20.1): eslints `no-unassigned-vars`,
|
|
215
|
+
nur zaehlt `ref={el}` oder `ref={el!}` als Zuweisung. Gemeldet wird ein `let`
|
|
216
|
+
oder `var` ohne Startwert, das gelesen, aber nie zugewiesen wird, also immer
|
|
217
|
+
`undefined` ist. `ref={() => el}` oder `<Comp elRef={el} />` sind nur Lesen und
|
|
218
|
+
werden gemeldet. Abweichung von Upstream: ein nacktes `el` in `ref={[…]}` meldet
|
|
219
|
+
schon `solid/prefer-ref-shorthand`, deshalb laesst diese Regel die Variable dann
|
|
220
|
+
aus.
|
|
221
|
+
|
|
222
|
+
In `oxc-config` ist sie nur fuer `*.tsx`/`*.jsx` an und ersetzt dort
|
|
223
|
+
`eslint/no-unassigned-vars`; `.ts`-Dateien behalten eslints Regel.
|
|
224
|
+
|
|
225
|
+
### `solid/prefer-ref-shorthand`
|
|
226
|
+
|
|
227
|
+
- Ein Ref-Callback, der nur sein Element speichert,
|
|
228
|
+
`ref={(node) => { el = node; }}` oder `ref={(node) => (el = node)}`, wird per
|
|
229
|
+
`--fix` zu `ref={el}`. Nur als direkter `ref`-Wert und nur, wenn `el` ein
|
|
230
|
+
Bezeichner ist (kein `this.el` oder `refs.el`).
|
|
231
|
+
- Ein nackter Bezeichner in einem Ref-Array (`ref={[autofocus, el]}`), der auf
|
|
232
|
+
ein `let`/`var` ohne Startwert zeigt, wird gemeldet: Solid schreibt nur ein
|
|
233
|
+
einzelnes `ref={el}` in eine Zuweisung um, im Array bleibt `el` `undefined`.
|
|
234
|
+
Als Vorschlag (`--fix-suggestions`) wird der Eintrag zu
|
|
235
|
+
`(node) => { el = node; }`. Direktiven-Funktionen im Array bleiben erlaubt.
|
|
236
|
+
|
|
237
|
+
### `solid/filename-matches-export`
|
|
238
|
+
|
|
239
|
+
Gilt nur fuer `.tsx`/`.jsx`. Eine Datei, die eine Komponente exportiert, heisst
|
|
240
|
+
wie diese Komponente in PascalCase (`Navigate.tsx` exportiert `Navigate`); eine
|
|
241
|
+
Datei, die nur JSX benutzt und keine Komponente exportiert, bleibt snake_case
|
|
242
|
+
(`solid_ssr_renderer.tsx` exportiert `solidSsrRenderer`). Eine Komponente ist
|
|
243
|
+
ein Export mit PascalCase-Namen, dessen Wert eine Funktion ist
|
|
244
|
+
(`export const Counter = () => …`, `export function Counter()`,
|
|
245
|
+
`export { Counter }`), oder ein solcher Default-Export
|
|
246
|
+
(`export default function Counter()`, `export default Counter`). Typ-Exporte,
|
|
247
|
+
Nicht-Funktionen wie `createContext(…)` und Re-Exporte (`export { X } from`)
|
|
248
|
+
zaehlen nicht.
|
|
249
|
+
|
|
250
|
+
- Mit Komponenten muss der Name vor dem ersten Punkt einer von ihnen sein, bei
|
|
251
|
+
einem Default-Export genau dieser. Der Rest bleibt: `slider.island.tsx` mit
|
|
252
|
+
`Slider` wird zu `Slider.island.tsx`. Helfer daneben sind erlaubt
|
|
253
|
+
(`IslandsPage.tsx` exportiert `IslandsPage` und `filterProps`).
|
|
254
|
+
- Ohne Komponente muss der Name snake_case sein.
|
|
255
|
+
- `index.tsx` ist immer erlaubt.
|
|
256
|
+
- Die Option `ignore` (Globs, Standard `["**/pages/**", "**/routes/**"]`) nimmt
|
|
257
|
+
Ordner aus, deren Dateinamen der Router vorgibt.
|
|
258
|
+
|
|
259
|
+
Ohne Autofix: eine Datei umzubenennen ist kein Lint-Fix. In `oxc-config` ist sie
|
|
260
|
+
fuer `*.tsx`/`*.jsx` an, zusammen mit `unicorn/filename-case`, das dort
|
|
261
|
+
snakeCase und pascalCase erlaubt; `.ts`-Dateien bleiben snakeCase.
|
|
262
|
+
|
|
263
|
+
### `solid/function-size`
|
|
264
|
+
|
|
265
|
+
Zaehlt wie eslints `max-lines-per-function` mit `skipBlankLines`, `skipComments`
|
|
266
|
+
und `IIFEs: true`: Leer- und reine Kommentarzeilen zaehlen nicht, eine innere
|
|
267
|
+
Funktion zaehlt fuer sich und in ihrer aeusseren mit. Optionen:
|
|
268
|
+
`{ "max": 60, "maxComponent": 80 }`.
|
|
269
|
+
|
|
270
|
+
- Komponente (PascalCase-Name, enthaelt JSX, in `.tsx`/`.jsx`): `maxComponent`.
|
|
271
|
+
Die Meldung sagt, JSX in eine Sub-Komponente in eigener PascalCase-`.tsx` und
|
|
272
|
+
zusammengehoerige Signals, Memos und Effects in ein `createX` in einer
|
|
273
|
+
snake_case-`.ts` zu ziehen, das Accessors nimmt (`createX(() => props.id)`)
|
|
274
|
+
und Accessors zurueckgibt.
|
|
275
|
+
- Primitive (`createX`/`useX`): `max`, mit einer Meldung, die nach Belang in
|
|
276
|
+
kleinere `createX` teilt.
|
|
277
|
+
- Jede andere Funktion: `max`, mit dem Hinweis auf benannte pure Helfer.
|
|
278
|
+
|
|
279
|
+
Jede Meldung nennt die Zeilen der laengsten Anweisung im Rumpf als Startpunkt
|
|
280
|
+
(ausser bei Komponenten) und sagt: Ist die Funktion eine zusammenhaengende
|
|
281
|
+
Einheit, bleibt sie, mit einem einzeiligen Kommentar darueber, warum; kein
|
|
282
|
+
`// oxlint-disable`. Ohne Autofix. In `oxc-config` steht sie auf `warn` und
|
|
283
|
+
ersetzt `eslint/max-lines-per-function`, dessen Meldung oxlint nicht anpassen
|
|
284
|
+
laesst; `*.generated.*` ist ausgenommen.
|
|
285
|
+
|
|
152
286
|
## Autofixes
|
|
153
287
|
|
|
154
288
|
`oxlint --fix` schreibt nur Code, der danach noch dasselbe tut. Wo ein Umbau das
|
|
@@ -172,8 +306,19 @@ nicht sicher kann, bleibt es bei der Meldung ohne Fix:
|
|
|
172
306
|
- `solid/imports`: ein Wert wird nie in ein `import type` verschoben, ein Typ
|
|
173
307
|
kommt in einem Wert-Import als `type Name` an und in einem `import type` ohne
|
|
174
308
|
`type`; passt keine vorhandene Deklaration, schreibt der Fix eine neue.
|
|
309
|
+
- `solid/no-react-deps`: bei `createMemo` faellt das Array per `--fix` weg, wenn
|
|
310
|
+
die Funktion jede Abhaengigkeit liest, sonst nur als Vorschlag. Bei
|
|
311
|
+
`createEffect`/`createRenderEffect` gibt es nie einen Autofix (ein Effect mit
|
|
312
|
+
einem Argument wirft MISSING_EFFECT_FN), nur den Vorschlag
|
|
313
|
+
`createEffect(() => ({ a: a(), id: props.id }), ({ a, id }) => { … })`, wenn
|
|
314
|
+
jede Abhaengigkeit sauber aufloest, und fuer `[]` den Vorschlag `onSettled`.
|
|
175
315
|
- `solid/prefer-class-object`: steht neben `classList` schon ein `class`, wird
|
|
176
316
|
nur gemeldet (der Fix haette `class` doppelt geschrieben).
|
|
317
|
+
- `solid/prefer-show`: meldet nur Zweige, die sichtbar JSX sind; ein Bezeichner
|
|
318
|
+
(`{c() ? label : "b"}`) kann Text sein, und dort ist der Ternary billiger als
|
|
319
|
+
`<Show>`. Der Ternary wird umgeschrieben, `&&` nur als Vorschlag
|
|
320
|
+
(`--fix-suggestions`): `{count() && <Badge />}` rendert bei `0` eine `0`,
|
|
321
|
+
`<Show>` nichts.
|
|
177
322
|
|
|
178
323
|
Ein typischer Fix-Fall:
|
|
179
324
|
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -7,15 +7,18 @@ https://github.com/solidjs-community/eslint-plugin-solid
|
|
|
7
7
|
Most rules of this package are ports of rules from eslint-plugin-solid,
|
|
8
8
|
rewritten as plain JavaScript on the Oxlint AST (the original port is the 0.14.5
|
|
9
9
|
state, see `meta.version` in `src/index.mjs`; `validate-jsx-nesting` was ported
|
|
10
|
-
again from 0.20.0
|
|
11
|
-
|
|
10
|
+
again from 0.20.0, `reactivity` and `no-unassigned-vars` from 0.20.1).
|
|
11
|
+
`upstream/cases.json` holds its test cases at commit d3e946c (0.20.1); the
|
|
12
|
+
package does not ship it. The rules, the helpers they share and those test cases
|
|
13
|
+
are covered by its licence:
|
|
12
14
|
|
|
13
15
|
- `components-return-once`, `event-handlers`, `imports`,
|
|
14
16
|
`jsx-no-duplicate-props`, `jsx-no-script-url`, `jsx-no-undef`,
|
|
15
17
|
`jsx-uses-vars`, `no-array-handlers`, `no-destructure`, `no-innerhtml`,
|
|
16
18
|
`no-proxy-apis`, `no-react-deps`, `no-react-specific-props`,
|
|
17
|
-
`no-unknown-namespaces`, `prefer-for`, `prefer-show`,
|
|
18
|
-
`self-closing-comp`, `style-prop`, `validate-jsx-nesting`
|
|
19
|
+
`no-unassigned-vars`, `no-unknown-namespaces`, `prefer-for`, `prefer-show`,
|
|
20
|
+
`reactivity`, `self-closing-comp`, `style-prop`, `validate-jsx-nesting`
|
|
21
|
+
(`src/rules/`)
|
|
19
22
|
- `prefer-class-object`, derived from the upstream rule `prefer-classlist`
|
|
20
23
|
- the helpers in `src/utils.mjs` and `src/compat.mjs`
|
|
21
24
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murky-web/oxlint-plugin-solid",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Local Oxlint JS plugin port for Solid rules plus Murky-specific Solid conventions.",
|
|
6
6
|
"homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/oxlint-plugin-solid",
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
"provenance": true
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
|
-
"test": "vitest run"
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"upstream:report": "bun upstream/run.mjs",
|
|
33
|
+
"upstream:baseline": "bun upstream/run.mjs --write-baseline"
|
|
32
34
|
},
|
|
33
35
|
"dependencies": {
|
|
34
36
|
"@eslint-community/eslint-utils": "^4.10.1",
|
|
@@ -41,6 +43,7 @@
|
|
|
41
43
|
"validate-html-nesting": "^1.2.4"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
46
|
+
"@babel/parser": "^7.29.0",
|
|
44
47
|
"@solidjs/web": "^2.0.3",
|
|
45
48
|
"solid-js": "^2.0.3"
|
|
46
49
|
}
|
package/src/compat.mjs
CHANGED
|
@@ -22,6 +22,9 @@ export function getScope(context, node) {
|
|
|
22
22
|
return context.sourceCode.getScope(node);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/** Look a variable up by name (or identifier node) from a given scope. */
|
|
26
|
+
export const findVariableInScope = utilsFindVariable;
|
|
27
|
+
|
|
25
28
|
export function findVariable(context, node) {
|
|
26
29
|
return utilsFindVariable(getScope(context, node), node);
|
|
27
30
|
}
|
package/src/index.mjs
CHANGED
|
@@ -1,30 +1,41 @@
|
|
|
1
1
|
import componentsReturnOnceRule from "./rules/components_return_once.mjs";
|
|
2
2
|
import eventHandlersRule from "./rules/event_handlers.mjs";
|
|
3
|
+
import filenameMatchesExportRule from "./rules/filename_matches_export.mjs";
|
|
4
|
+
import functionSizeRule from "./rules/function_size.mjs";
|
|
3
5
|
import importsRule from "./rules/imports.mjs";
|
|
6
|
+
import initDeclarationsRule from "./rules/init_declarations.mjs";
|
|
4
7
|
import jsxNoDuplicatePropsRule from "./rules/jsx_no_duplicate_props.mjs";
|
|
5
8
|
import { jsxNoScriptUrlRule } from "./rules/jsx_no_script_url.mjs";
|
|
6
9
|
import jsxNoUndefRule from "./rules/jsx_no_undef.mjs";
|
|
7
10
|
import { jsxUsesVarsRule } from "./rules/jsx_uses_vars.mjs";
|
|
8
11
|
import { noArrayHandlersRule } from "./rules/no_array_handlers.mjs";
|
|
12
|
+
import noCleanupInEffectApplyRule from "./rules/no_cleanup_in_effect_apply.mjs";
|
|
9
13
|
import noDestructureRule from "./rules/no_destructure.mjs";
|
|
10
14
|
import noInnerhtmlRule from "./rules/no_innerhtml.mjs";
|
|
15
|
+
import noModuleScopeReactivePrimitiveRule from "./rules/no_module_scope_reactive_primitive.mjs";
|
|
11
16
|
import noOwnedPrimitivesInRefRule from "./rules/no_owned_primitives_in_ref.mjs";
|
|
12
17
|
import noProxyApisRule from "./rules/no_proxy_apis.mjs";
|
|
13
18
|
import noReactDepsRule from "./rules/no_react_deps.mjs";
|
|
14
19
|
import { noReactSpecificPropsRule } from "./rules/no_react_specific_props.mjs";
|
|
15
20
|
import { noServerImportInClientRule } from "./rules/no_server_import_in_client.mjs";
|
|
16
21
|
import noSetterInEffectRule from "./rules/no_setter_in_effect.mjs";
|
|
22
|
+
import noSingleArgCreateEffectRule from "./rules/no_single_arg_create_effect.mjs";
|
|
23
|
+
import noUnassignedVarsRule from "./rules/no_unassigned_vars.mjs";
|
|
17
24
|
import noUnknownNamespacesRule from "./rules/no_unknown_namespaces.mjs";
|
|
18
25
|
import noUntrackedEffectReadRule from "./rules/no_untracked_effect_read.mjs";
|
|
26
|
+
import noWriteInPureComputationRule from "./rules/no_write_in_pure_computation.mjs";
|
|
19
27
|
import { preferArrowComponentsRule } from "./rules/prefer_arrow_components.mjs";
|
|
20
28
|
import preferClassObjectRule from "./rules/prefer_class_object.mjs";
|
|
29
|
+
import preferDestructuringRule from "./rules/prefer_destructuring.mjs";
|
|
21
30
|
import preferForRule from "./rules/prefer_for.mjs";
|
|
31
|
+
import preferRefShorthandRule from "./rules/prefer_ref_shorthand.mjs";
|
|
22
32
|
import preferShowRule from "./rules/prefer_show.mjs";
|
|
23
33
|
import { reactiveAsyncRule } from "./rules/reactive_async.mjs";
|
|
24
34
|
import reactivityRule from "./rules/reactivity.mjs";
|
|
25
35
|
import selfClosingCompRule from "./rules/self_closing_comp.mjs";
|
|
26
36
|
import { strictVoidReturnRule } from "./rules/strict_void_return.mjs";
|
|
27
37
|
import stylePropRule from "./rules/style_prop.mjs";
|
|
38
|
+
import typedMergeDefaultsRule from "./rules/typed_merge_defaults.mjs";
|
|
28
39
|
import validateJsxNestingRule from "./rules/validate_jsx_nesting.mjs";
|
|
29
40
|
|
|
30
41
|
const extendedPlugin = {
|
|
@@ -35,31 +46,43 @@ const extendedPlugin = {
|
|
|
35
46
|
rules: {
|
|
36
47
|
"components-return-once": componentsReturnOnceRule,
|
|
37
48
|
"event-handlers": eventHandlersRule,
|
|
49
|
+
"filename-matches-export": filenameMatchesExportRule,
|
|
50
|
+
"function-size": functionSizeRule,
|
|
38
51
|
imports: importsRule,
|
|
52
|
+
"init-declarations": initDeclarationsRule,
|
|
39
53
|
"jsx-no-duplicate-props": jsxNoDuplicatePropsRule,
|
|
40
54
|
"jsx-no-script-url": jsxNoScriptUrlRule,
|
|
41
55
|
"jsx-no-undef": jsxNoUndefRule,
|
|
42
56
|
"jsx-uses-vars": jsxUsesVarsRule,
|
|
43
57
|
"no-array-handlers": noArrayHandlersRule,
|
|
58
|
+
"no-cleanup-in-effect-apply": noCleanupInEffectApplyRule,
|
|
44
59
|
"no-destructure": noDestructureRule,
|
|
45
60
|
"no-innerhtml": noInnerhtmlRule,
|
|
61
|
+
"no-module-scope-reactive-primitive":
|
|
62
|
+
noModuleScopeReactivePrimitiveRule,
|
|
46
63
|
"no-owned-primitives-in-ref": noOwnedPrimitivesInRefRule,
|
|
47
64
|
"no-proxy-apis": noProxyApisRule,
|
|
48
65
|
"no-react-deps": noReactDepsRule,
|
|
49
66
|
"no-react-specific-props": noReactSpecificPropsRule,
|
|
50
67
|
"no-setter-in-effect": noSetterInEffectRule,
|
|
51
68
|
"no-server-import-in-client": noServerImportInClientRule,
|
|
69
|
+
"no-single-arg-create-effect": noSingleArgCreateEffectRule,
|
|
70
|
+
"no-unassigned-vars": noUnassignedVarsRule,
|
|
52
71
|
"no-unknown-namespaces": noUnknownNamespacesRule,
|
|
53
72
|
"no-untracked-effect-read": noUntrackedEffectReadRule,
|
|
73
|
+
"no-write-in-pure-computation": noWriteInPureComputationRule,
|
|
54
74
|
"prefer-arrow-components": preferArrowComponentsRule,
|
|
55
75
|
"prefer-class-object": preferClassObjectRule,
|
|
76
|
+
"prefer-destructuring": preferDestructuringRule,
|
|
56
77
|
"prefer-for": preferForRule,
|
|
78
|
+
"prefer-ref-shorthand": preferRefShorthandRule,
|
|
57
79
|
"prefer-show": preferShowRule,
|
|
58
80
|
"reactive-async": reactiveAsyncRule,
|
|
59
81
|
reactivity: reactivityRule,
|
|
60
82
|
"self-closing-comp": selfClosingCompRule,
|
|
61
83
|
"strict-void-return": strictVoidReturnRule,
|
|
62
84
|
"style-prop": stylePropRule,
|
|
85
|
+
"typed-merge-defaults": typedMergeDefaultsRule,
|
|
63
86
|
"validate-jsx-nesting": validateJsxNestingRule
|
|
64
87
|
}
|
|
65
88
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getSourceCode } from "../compat.mjs";
|
|
2
|
-
import { getFunctionName } from "../utils.mjs";
|
|
2
|
+
import { getFunctionName, messageCode } from "../utils.mjs";
|
|
3
3
|
const isNothing = (node) => {
|
|
4
4
|
if (!node) {
|
|
5
5
|
return true;
|
|
@@ -14,6 +14,34 @@ const isNothing = (node) => {
|
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
16
|
const getLineLength = (loc) => loc.end.line - loc.start.line + 1;
|
|
17
|
+
const EARLY_RETURN_PATTERN = "return <Show when={…} fallback={…}>…</Show>;";
|
|
18
|
+
// `if (test) return A; ... return B;` as one `<Show>`, when the `if` holds
|
|
19
|
+
// nothing but that return and both returns have a value.
|
|
20
|
+
const earlyReturnFix = (earlyReturn, lastReturn, sourceCode) => {
|
|
21
|
+
const block = earlyReturn.parent;
|
|
22
|
+
const ifStatement =
|
|
23
|
+
block.type === "BlockStatement" && block.body.length === 1
|
|
24
|
+
? block.parent
|
|
25
|
+
: block;
|
|
26
|
+
if (
|
|
27
|
+
ifStatement.type !== "IfStatement" ||
|
|
28
|
+
ifStatement.alternate ||
|
|
29
|
+
!earlyReturn.argument ||
|
|
30
|
+
!lastReturn?.argument
|
|
31
|
+
) {
|
|
32
|
+
return EARLY_RETURN_PATTERN;
|
|
33
|
+
}
|
|
34
|
+
const jsx = (node) => {
|
|
35
|
+
const text = sourceCode.getText(node);
|
|
36
|
+
return node.type === "JSXElement" || node.type === "JSXFragment"
|
|
37
|
+
? text
|
|
38
|
+
: `{${text}}`;
|
|
39
|
+
};
|
|
40
|
+
return messageCode(
|
|
41
|
+
`return <Show when={${sourceCode.getText(ifStatement.test)}} fallback={${sourceCode.getText(lastReturn.argument)}}>${jsx(earlyReturn.argument)}</Show>;`,
|
|
42
|
+
EARLY_RETURN_PATTERN
|
|
43
|
+
);
|
|
44
|
+
};
|
|
17
45
|
export default {
|
|
18
46
|
meta: {
|
|
19
47
|
type: "problem",
|
|
@@ -26,9 +54,9 @@ export default {
|
|
|
26
54
|
schema: [],
|
|
27
55
|
messages: {
|
|
28
56
|
noEarlyReturn:
|
|
29
|
-
"
|
|
57
|
+
"{{component}} returns early. A component body runs once, so it never switches branches when the condition changes (dev: `STRICT_READ_UNTRACKED`). Fix: one return with the condition in JSX, `{{fix}}` (`<Switch>`/`<Match>` for more branches). Do not: `untrack()` the condition.",
|
|
30
58
|
noConditionalReturn:
|
|
31
|
-
"
|
|
59
|
+
"{{component}} picks its return with `{{test}}`. A component body runs once, so it never switches when `{{test}}` changes. Fix: `{{fix}}`. Do not: `untrack()` the condition."
|
|
32
60
|
}
|
|
33
61
|
},
|
|
34
62
|
defaultOptions: [],
|
|
@@ -40,6 +68,51 @@ export default {
|
|
|
40
68
|
? text
|
|
41
69
|
: `{${text}}`;
|
|
42
70
|
};
|
|
71
|
+
// The autofix's replacement for `return test ? a : b`.
|
|
72
|
+
const conditionalReplacement = (argument) => {
|
|
73
|
+
const sourceCode = getSourceCode(context);
|
|
74
|
+
const { test, consequent, alternate } = argument;
|
|
75
|
+
const conditions = [{ test, consequent }];
|
|
76
|
+
let fallback = alternate;
|
|
77
|
+
while (fallback.type === "ConditionalExpression") {
|
|
78
|
+
conditions.push({
|
|
79
|
+
test: fallback.test,
|
|
80
|
+
consequent: fallback.consequent
|
|
81
|
+
});
|
|
82
|
+
fallback = fallback.alternate;
|
|
83
|
+
}
|
|
84
|
+
if (conditions.length >= 2) {
|
|
85
|
+
// we have a nested ternary, use <Switch><Match /></Switch>
|
|
86
|
+
const fallbackStr = !isNothing(fallback)
|
|
87
|
+
? ` fallback={${sourceCode.getText(fallback)}}`
|
|
88
|
+
: "";
|
|
89
|
+
return `<Switch${fallbackStr}>\n${conditions
|
|
90
|
+
.map(
|
|
91
|
+
({ test, consequent }) =>
|
|
92
|
+
`<Match when={${sourceCode.getText(test)}}>${putIntoJSX(consequent)}</Match>`
|
|
93
|
+
)
|
|
94
|
+
.join("\n")}\n</Switch>`;
|
|
95
|
+
}
|
|
96
|
+
if (isNothing(consequent)) {
|
|
97
|
+
// we have a single ternary and the consequent is nothing. Negate the condition and use a <Show>.
|
|
98
|
+
return `<Show when={!(${sourceCode.getText(test)})}>${putIntoJSX(alternate)}</Show>`;
|
|
99
|
+
}
|
|
100
|
+
if (
|
|
101
|
+
isNothing(fallback) ||
|
|
102
|
+
getLineLength(consequent.loc) >=
|
|
103
|
+
getLineLength(alternate.loc) * 1.5
|
|
104
|
+
) {
|
|
105
|
+
// we have a standard ternary, and the alternate is a bit shorter in LOC than the consequent, which
|
|
106
|
+
// should be enough to tell that it's logically a fallback instead of an equal branch.
|
|
107
|
+
const fallbackStr = !isNothing(fallback)
|
|
108
|
+
? ` fallback={${sourceCode.getText(fallback)}}`
|
|
109
|
+
: "";
|
|
110
|
+
return `<Show when={${sourceCode.getText(test)}}${fallbackStr}>${putIntoJSX(consequent)}</Show>`;
|
|
111
|
+
}
|
|
112
|
+
// we have a standard ternary, but no signal from the user as to which branch is the "fallback" and
|
|
113
|
+
// which is the children. Move the whole conditional inside a JSX fragment.
|
|
114
|
+
return `<>${putIntoJSX(argument)}</>`;
|
|
115
|
+
};
|
|
43
116
|
const currentFunction = () => functionStack[functionStack.length - 1];
|
|
44
117
|
const onFunctionEnter = (node) => {
|
|
45
118
|
let lastReturn;
|
|
@@ -72,98 +145,72 @@ export default {
|
|
|
72
145
|
currentFunction().isComponent = false;
|
|
73
146
|
}
|
|
74
147
|
if (currentFunction().isComponent) {
|
|
75
|
-
|
|
76
|
-
|
|
148
|
+
const sourceCode = getSourceCode(context);
|
|
149
|
+
const name = getFunctionName(node);
|
|
150
|
+
const component = name ? `\`${name}\`` : "This component";
|
|
151
|
+
const { lastReturn } = currentFunction();
|
|
152
|
+
// Warn on each early return; with more than one, the branches
|
|
153
|
+
// go into a <Switch>, which no single `if` shows.
|
|
154
|
+
const { earlyReturns } = currentFunction();
|
|
155
|
+
earlyReturns.forEach((earlyReturn) => {
|
|
77
156
|
context.report({
|
|
78
157
|
node: earlyReturn,
|
|
79
|
-
messageId: "noEarlyReturn"
|
|
158
|
+
messageId: "noEarlyReturn",
|
|
159
|
+
data: {
|
|
160
|
+
component,
|
|
161
|
+
fix:
|
|
162
|
+
earlyReturns.length > 1
|
|
163
|
+
? "return <Switch fallback={…}><Match when={…}>…</Match></Switch>;"
|
|
164
|
+
: earlyReturnFix(
|
|
165
|
+
earlyReturn,
|
|
166
|
+
lastReturn,
|
|
167
|
+
sourceCode
|
|
168
|
+
)
|
|
169
|
+
}
|
|
80
170
|
});
|
|
81
171
|
});
|
|
82
|
-
const argument =
|
|
172
|
+
const argument = lastReturn?.argument;
|
|
173
|
+
// What the conditional return becomes: the autofix's
|
|
174
|
+
// replacement where there is one, else the whole expression
|
|
175
|
+
// in a fragment, which keeps it reactive.
|
|
176
|
+
const conditionalData = (test, replacement) => ({
|
|
177
|
+
component,
|
|
178
|
+
test: messageCode(sourceCode.getText(test), "condition"),
|
|
179
|
+
fix: messageCode(
|
|
180
|
+
`return ${replacement};`,
|
|
181
|
+
"return <Show when={condition} fallback={…}>…</Show>;"
|
|
182
|
+
)
|
|
183
|
+
});
|
|
83
184
|
if (argument?.type === "ConditionalExpression") {
|
|
84
|
-
const sourceCode = getSourceCode(context);
|
|
85
185
|
context.report({
|
|
86
186
|
node: argument.parent,
|
|
87
187
|
messageId: "noConditionalReturn",
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
test: fallback.test,
|
|
95
|
-
consequent: fallback.consequent
|
|
96
|
-
});
|
|
97
|
-
fallback = fallback.alternate;
|
|
98
|
-
}
|
|
99
|
-
if (conditions.length >= 2) {
|
|
100
|
-
// we have a nested ternary, use <Switch><Match /></Switch>
|
|
101
|
-
const fallbackStr = !isNothing(fallback)
|
|
102
|
-
? ` fallback={${sourceCode.getText(fallback)}}`
|
|
103
|
-
: "";
|
|
104
|
-
return fixer.replaceText(
|
|
105
|
-
argument,
|
|
106
|
-
`<Switch${fallbackStr}>\n${conditions
|
|
107
|
-
.map(
|
|
108
|
-
({ test, consequent }) =>
|
|
109
|
-
`<Match when={${sourceCode.getText(test)}}>${putIntoJSX(consequent)}</Match>`
|
|
110
|
-
)
|
|
111
|
-
.join("\n")}\n</Switch>`
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
if (isNothing(consequent)) {
|
|
115
|
-
// we have a single ternary and the consequent is nothing. Negate the condition and use a <Show>.
|
|
116
|
-
return fixer.replaceText(
|
|
117
|
-
argument,
|
|
118
|
-
`<Show when={!(${sourceCode.getText(test)})}>${putIntoJSX(alternate)}</Show>`
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
if (
|
|
122
|
-
isNothing(fallback) ||
|
|
123
|
-
getLineLength(consequent.loc) >=
|
|
124
|
-
getLineLength(alternate.loc) * 1.5
|
|
125
|
-
) {
|
|
126
|
-
// we have a standard ternary, and the alternate is a bit shorter in LOC than the consequent, which
|
|
127
|
-
// should be enough to tell that it's logically a fallback instead of an equal branch.
|
|
128
|
-
const fallbackStr = !isNothing(fallback)
|
|
129
|
-
? ` fallback={${sourceCode.getText(fallback)}}`
|
|
130
|
-
: "";
|
|
131
|
-
return fixer.replaceText(
|
|
132
|
-
argument,
|
|
133
|
-
`<Show when={${sourceCode.getText(test)}}${fallbackStr}>${putIntoJSX(consequent)}</Show>`
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
// we have a standard ternary, but no signal from the user as to which branch is the "fallback" and
|
|
137
|
-
// which is the children. Move the whole conditional inside a JSX fragment.
|
|
138
|
-
return fixer.replaceText(
|
|
188
|
+
data: conditionalData(
|
|
189
|
+
argument.test,
|
|
190
|
+
conditionalReplacement(argument)
|
|
191
|
+
),
|
|
192
|
+
fix: (fixer) =>
|
|
193
|
+
fixer.replaceText(
|
|
139
194
|
argument,
|
|
140
|
-
|
|
141
|
-
)
|
|
142
|
-
}
|
|
195
|
+
conditionalReplacement(argument)
|
|
196
|
+
)
|
|
143
197
|
});
|
|
144
198
|
} else if (argument?.type === "LogicalExpression") {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
} else {
|
|
161
|
-
// we have some other kind of conditional, warn
|
|
162
|
-
context.report({
|
|
163
|
-
node: argument,
|
|
164
|
-
messageId: "noConditionalReturn"
|
|
165
|
-
});
|
|
166
|
-
}
|
|
199
|
+
// `return condition && expression` goes into a <Show />;
|
|
200
|
+
// any other operator into a fragment, without a fix.
|
|
201
|
+
const isAnd = argument.operator === "&&";
|
|
202
|
+
const replacement = isAnd
|
|
203
|
+
? `<Show when={${sourceCode.getText(argument.left)}}>${putIntoJSX(argument.right)}</Show>`
|
|
204
|
+
: `<>${putIntoJSX(argument)}</>`;
|
|
205
|
+
context.report({
|
|
206
|
+
node: argument,
|
|
207
|
+
messageId: "noConditionalReturn",
|
|
208
|
+
data: conditionalData(argument.left, replacement),
|
|
209
|
+
fix: isAnd
|
|
210
|
+
? (fixer) =>
|
|
211
|
+
fixer.replaceText(argument, replacement)
|
|
212
|
+
: undefined
|
|
213
|
+
});
|
|
167
214
|
}
|
|
168
215
|
}
|
|
169
216
|
// Pop on exit
|