@murky-web/oxlint-plugin-solid 1.2.0 → 1.3.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 +63 -1
- package/package.json +1 -1
- package/src/index.mjs +6 -0
- package/src/rules/no_server_import_in_client.mjs +172 -0
- package/src/rules/reactive_async.mjs +237 -0
- package/src/rules/strict_void_return.mjs +201 -0
- package/src/utils.mjs +33 -0
package/README.md
CHANGED
|
@@ -26,7 +26,9 @@ Aktuell sind enthalten:
|
|
|
26
26
|
apply-Phase eines zweiphasigen `createEffect`, die dort nicht tracken) und
|
|
27
27
|
`solid/no-owned-primitives-in-ref` (meldet `createEffect`/`onCleanup` und
|
|
28
28
|
Verwandte in einem Ref-Callback -- der laeuft ohne Owner, nichts raeumt sie
|
|
29
|
-
je wieder ab)
|
|
29
|
+
je wieder ab),
|
|
30
|
+
`solid/reactive-async`, `solid/no-server-import-in-client` und
|
|
31
|
+
`solid/strict-void-return` (siehe unten)
|
|
30
32
|
- ein Test-Harness, der die exportierte Rule-Surface und echte Diagnostik
|
|
31
33
|
gegen Temp-Projekte prueft
|
|
32
34
|
|
|
@@ -42,6 +44,66 @@ Wichtig dabei:
|
|
|
42
44
|
- Zielprojekte brauchen dadurch weiter nur die kopierte Runtime unter
|
|
43
45
|
`./oxc/jsplugins/solid/`, nicht dieses Paket als direkte Dependency
|
|
44
46
|
|
|
47
|
+
## Regeln fuer das Solid-2.0-Modell
|
|
48
|
+
|
|
49
|
+
Die drei Regeln arbeiten ohne Typinformation, nur mit Syntax und Scope. Sie
|
|
50
|
+
sind in `config/oxc/linting/solid.jsonc` aktiv und ersetzen dort fuer
|
|
51
|
+
`*.tsx`/`*.jsx` die Typ-Regeln `typescript/promise-function-async` und
|
|
52
|
+
`typescript/strict-void-return`.
|
|
53
|
+
|
|
54
|
+
### `solid/reactive-async`
|
|
55
|
+
|
|
56
|
+
In einer reaktiven Berechnung liest Solid ein zurueckgegebenes Promise wie einen
|
|
57
|
+
Wert; `createMemo(() => getProduct(props.id))` braucht kein `async`. Geprueft
|
|
58
|
+
werden die Callbacks von `createMemo`, `createSignal(fn)`, `createStore(fn)`,
|
|
59
|
+
`createProjection`, `createOptimistic`, `createOptimisticStore` und die
|
|
60
|
+
Compute-Phase des zweiphasigen `createEffect`/`createRenderEffect`.
|
|
61
|
+
|
|
62
|
+
Gemeldet wird
|
|
63
|
+
|
|
64
|
+
- ein Read nach dem ersten `await` im selben Callback (Aufruf eines Signals oder
|
|
65
|
+
Memos, `props.x`, Property-Read eines Stores): Solid meldet das zur Laufzeit
|
|
66
|
+
als `UNTRACKED_READ_AFTER_AWAIT`, es wird nie eine Dependency. Lies den Wert
|
|
67
|
+
vor dem `await` in eine lokale Variable.
|
|
68
|
+
- ein `async`-Callback, dessen einziges `await` ein `return await x` ist: `async`
|
|
69
|
+
und `await` weglassen und das Promise zurueckgeben. Innerhalb von `try` bleibt
|
|
70
|
+
`return await` erlaubt, weil es das `catch` erst moeglich macht.
|
|
71
|
+
|
|
72
|
+
Grenzen: Reads in verschachtelten Funktionen, Helfern und `.then`-Callbacks
|
|
73
|
+
sieht die Regel nicht (die Laufzeit-Diagnose schon). Ob ein Name ein Signal ist,
|
|
74
|
+
folgt nur aus `createSignal`/`createMemo`/den Store-Primitiven in derselben
|
|
75
|
+
Datei und aus dem Namen `props`.
|
|
76
|
+
|
|
77
|
+
### `solid/no-server-import-in-client`
|
|
78
|
+
|
|
79
|
+
Client-Dateien (`.tsx` und `.ts` ausserhalb von `src/server/`, ohne
|
|
80
|
+
Test-Dateien) duerfen ein Modul unter `src/server/` nur importieren, wenn dessen
|
|
81
|
+
erste Anweisung `"use server"` ist. Der Pfad wird vom importierenden File aus
|
|
82
|
+
aufgeloest (relativ oder `~/` = `<Projekt>/src/`, Projektwurzel = naechstes
|
|
83
|
+
Verzeichnis mit `package.json`) und die Zieldatei gelesen. Fehlt das Ziel,
|
|
84
|
+
wird nichts gemeldet. `import type` wird ignoriert. Geprueft werden auch
|
|
85
|
+
`export ... from` und `import("...")` mit String-Literal.
|
|
86
|
+
|
|
87
|
+
Grenze: andere Aliase und Server-Code ausserhalb von `src/server/` kennt die
|
|
88
|
+
Regel nicht.
|
|
89
|
+
|
|
90
|
+
### `solid/strict-void-return`
|
|
91
|
+
|
|
92
|
+
Ohne Typen meldet sie Funktionen, deren Rueckgabewert Solid verwirft:
|
|
93
|
+
|
|
94
|
+
- ein Arrow mit Expression-Body (nicht `void x`) in einem `on*`-Attribut,
|
|
95
|
+
`on:event` oder in `ref` (auch in einem Ref-Array). Erlaubt sind Block-Body
|
|
96
|
+
und ein direkt uebergebener Setter (`ref={setInput}`).
|
|
97
|
+
- ein `onCleanup`-Callback, der einen sicher nicht-void Wert zurueckgibt
|
|
98
|
+
(Literal, Zuweisung, Objekt, ...). Ein Call wie `() => clearInterval(id)`
|
|
99
|
+
bleibt erlaubt.
|
|
100
|
+
- ein `onSettled`-Callback, der etwas anderes als eine Cleanup-Funktion
|
|
101
|
+
zurueckgibt (Literal, Zuweisung, ...).
|
|
102
|
+
|
|
103
|
+
Die Regel ist schwaecher als `typescript/strict-void-return`: sie sieht nur
|
|
104
|
+
Inline-Callbacks, keine Funktionen, die per Namen uebergeben werden, und bei
|
|
105
|
+
`onCleanup`/`onSettled` keine Calls, deren Rueckgabetyp sie nicht kennt.
|
|
106
|
+
|
|
45
107
|
## Nutzung ueber @murky-web/config
|
|
46
108
|
|
|
47
109
|
Im Zielprojekt:
|
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -13,14 +13,17 @@ import noProxyApisRule from "./rules/no_proxy_apis.mjs";
|
|
|
13
13
|
import noReactDepsRule from "./rules/no_react_deps.mjs";
|
|
14
14
|
import { noReactSpecificPropsRule } from "./rules/no_react_specific_props.mjs";
|
|
15
15
|
import noSetterInEffectRule from "./rules/no_setter_in_effect.mjs";
|
|
16
|
+
import { noServerImportInClientRule } from "./rules/no_server_import_in_client.mjs";
|
|
16
17
|
import noUnknownNamespacesRule from "./rules/no_unknown_namespaces.mjs";
|
|
17
18
|
import noUntrackedEffectReadRule from "./rules/no_untracked_effect_read.mjs";
|
|
18
19
|
import { preferArrowComponentsRule } from "./rules/prefer_arrow_components.mjs";
|
|
19
20
|
import preferClassObjectRule from "./rules/prefer_class_object.mjs";
|
|
20
21
|
import preferForRule from "./rules/prefer_for.mjs";
|
|
21
22
|
import preferShowRule from "./rules/prefer_show.mjs";
|
|
23
|
+
import { reactiveAsyncRule } from "./rules/reactive_async.mjs";
|
|
22
24
|
import reactivityRule from "./rules/reactivity.mjs";
|
|
23
25
|
import selfClosingCompRule from "./rules/self_closing_comp.mjs";
|
|
26
|
+
import { strictVoidReturnRule } from "./rules/strict_void_return.mjs";
|
|
24
27
|
import stylePropRule from "./rules/style_prop.mjs";
|
|
25
28
|
import validateJsxNestingRule from "./rules/validate_jsx_nesting.mjs";
|
|
26
29
|
|
|
@@ -45,14 +48,17 @@ const extendedPlugin = {
|
|
|
45
48
|
"no-react-deps": noReactDepsRule,
|
|
46
49
|
"no-react-specific-props": noReactSpecificPropsRule,
|
|
47
50
|
"no-setter-in-effect": noSetterInEffectRule,
|
|
51
|
+
"no-server-import-in-client": noServerImportInClientRule,
|
|
48
52
|
"no-unknown-namespaces": noUnknownNamespacesRule,
|
|
49
53
|
"no-untracked-effect-read": noUntrackedEffectReadRule,
|
|
50
54
|
"prefer-arrow-components": preferArrowComponentsRule,
|
|
51
55
|
"prefer-class-object": preferClassObjectRule,
|
|
52
56
|
"prefer-for": preferForRule,
|
|
53
57
|
"prefer-show": preferShowRule,
|
|
58
|
+
"reactive-async": reactiveAsyncRule,
|
|
54
59
|
reactivity: reactivityRule,
|
|
55
60
|
"self-closing-comp": selfClosingCompRule,
|
|
61
|
+
"strict-void-return": strictVoidReturnRule,
|
|
56
62
|
"style-prop": stylePropRule,
|
|
57
63
|
"validate-jsx-nesting": validateJsxNestingRule,
|
|
58
64
|
},
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
3
|
+
|
|
4
|
+
const SERVER_DIRECTORY = join("src", "server");
|
|
5
|
+
const EXTENSIONS = [".ts", ".tsx", ".mts", ".js", ".jsx", ".mjs"];
|
|
6
|
+
const CLIENT_FILE = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
7
|
+
const TEST_FILE = /\.(?:test|spec)\.[^/]+$/;
|
|
8
|
+
const ALIAS_PREFIX = "~/";
|
|
9
|
+
const USE_SERVER = /^(?:["']use server["'])/;
|
|
10
|
+
// Whitespace and comments may precede the directive; anything else means it is
|
|
11
|
+
// not the first statement.
|
|
12
|
+
const LEADING_TRIVIA = /^(?:\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)+/;
|
|
13
|
+
|
|
14
|
+
/** Nearest directory at or above `directory` that holds a package.json. */
|
|
15
|
+
function findProjectRoot(directory) {
|
|
16
|
+
let current = directory;
|
|
17
|
+
|
|
18
|
+
while (!existsSync(join(current, "package.json"))) {
|
|
19
|
+
const parent = dirname(current);
|
|
20
|
+
if (parent === current) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
current = parent;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return current;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isFile(path) {
|
|
31
|
+
return existsSync(path) && statSync(path).isFile();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The file an import path points at, trying the extensions the bundler would.
|
|
36
|
+
* A `.js` specifier names the `.ts` source next to it.
|
|
37
|
+
*/
|
|
38
|
+
function findModuleFile(basePath) {
|
|
39
|
+
const withoutJsExtension = basePath.replace(/\.[cm]?jsx?$/, "");
|
|
40
|
+
const candidates = [
|
|
41
|
+
basePath,
|
|
42
|
+
...EXTENSIONS.map((extension) => `${basePath}${extension}`),
|
|
43
|
+
...EXTENSIONS.map((extension) => `${withoutJsExtension}${extension}`),
|
|
44
|
+
...EXTENSIONS.map((extension) => join(basePath, `index${extension}`)),
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
return candidates.find(isFile) ?? null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function startsWithUseServer(filePath) {
|
|
51
|
+
let text = readFileSync(filePath, "utf8").replace(/^/, "");
|
|
52
|
+
|
|
53
|
+
for (;;) {
|
|
54
|
+
const trivia = LEADING_TRIVIA.exec(text);
|
|
55
|
+
if (!trivia) {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
text = text.slice(trivia[0].length);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return USE_SERVER.test(text);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isInside(path, directory) {
|
|
66
|
+
return path.startsWith(`${directory}${sep}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function resolveImport(specifier, filename, projectRoot) {
|
|
70
|
+
if (specifier.startsWith(ALIAS_PREFIX)) {
|
|
71
|
+
return resolve(
|
|
72
|
+
projectRoot,
|
|
73
|
+
"src",
|
|
74
|
+
specifier.slice(ALIAS_PREFIX.length),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (specifier.startsWith("./") || specifier.startsWith("../")) {
|
|
79
|
+
return resolve(dirname(filename), specifier);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const noServerImportInClientRule = {
|
|
86
|
+
meta: {
|
|
87
|
+
type: "problem",
|
|
88
|
+
docs: {
|
|
89
|
+
description:
|
|
90
|
+
"Disallow importing modules from src/server/ into client code unless they are use-server modules.",
|
|
91
|
+
url: "https://docs.solidjs.com/",
|
|
92
|
+
},
|
|
93
|
+
schema: [],
|
|
94
|
+
messages: {
|
|
95
|
+
serverImportInClient:
|
|
96
|
+
"'{{specifier}}' lives under src/server/ but does not start with the \"use server\" directive, so importing it into client code ships server-only code to the browser. Import a server function module instead (first statement `\"use server\";`, async exports only), and keep database clients and secrets inside it.",
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
defaultOptions: [],
|
|
100
|
+
create(context) {
|
|
101
|
+
const filename = resolve(context.filename ?? context.getFilename());
|
|
102
|
+
const projectRoot = findProjectRoot(dirname(filename));
|
|
103
|
+
|
|
104
|
+
// Server modules, tests and non-code files may import anything.
|
|
105
|
+
if (
|
|
106
|
+
projectRoot === null ||
|
|
107
|
+
!CLIENT_FILE.test(filename) ||
|
|
108
|
+
TEST_FILE.test(filename) ||
|
|
109
|
+
isInside(filename, join(projectRoot, SERVER_DIRECTORY))
|
|
110
|
+
) {
|
|
111
|
+
return {};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const serverDirectory = join(projectRoot, SERVER_DIRECTORY);
|
|
115
|
+
|
|
116
|
+
function check(sourceNode) {
|
|
117
|
+
if (
|
|
118
|
+
sourceNode?.type !== "Literal" ||
|
|
119
|
+
typeof sourceNode.value !== "string"
|
|
120
|
+
) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const basePath = resolveImport(
|
|
125
|
+
sourceNode.value,
|
|
126
|
+
filename,
|
|
127
|
+
projectRoot,
|
|
128
|
+
);
|
|
129
|
+
if (basePath === null) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const target = findModuleFile(basePath);
|
|
134
|
+
// A missing file is the resolver's problem, not this rule's.
|
|
135
|
+
if (
|
|
136
|
+
target === null ||
|
|
137
|
+
!isInside(target, serverDirectory) ||
|
|
138
|
+
startsWithUseServer(target)
|
|
139
|
+
) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
context.report({
|
|
144
|
+
node: sourceNode,
|
|
145
|
+
messageId: "serverImportInClient",
|
|
146
|
+
data: { specifier: sourceNode.value },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
ImportDeclaration(node) {
|
|
152
|
+
// Types are erased, nothing reaches the bundle.
|
|
153
|
+
if (node.importKind !== "type") {
|
|
154
|
+
check(node.source);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
ExportNamedDeclaration(node) {
|
|
158
|
+
if (node.exportKind !== "type") {
|
|
159
|
+
check(node.source);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
ExportAllDeclaration(node) {
|
|
163
|
+
if (node.exportKind !== "type") {
|
|
164
|
+
check(node.source);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
ImportExpression(node) {
|
|
168
|
+
check(node.source);
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { findVariable } from "../compat.mjs";
|
|
2
|
+
import {
|
|
3
|
+
isFunctionNode,
|
|
4
|
+
isPropsByName,
|
|
5
|
+
trackImports,
|
|
6
|
+
walkOwnBody,
|
|
7
|
+
} from "../utils.mjs";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Primitives whose first argument, when it is a function, is a reactive
|
|
11
|
+
* computation. Solid 2.0 reads a promise returned from one like a value, so
|
|
12
|
+
* the callback needs no `async` to be asynchronous.
|
|
13
|
+
*/
|
|
14
|
+
const COMPUTATION_PRIMITIVES = [
|
|
15
|
+
"createMemo",
|
|
16
|
+
"createOptimistic",
|
|
17
|
+
"createOptimisticStore",
|
|
18
|
+
"createProjection",
|
|
19
|
+
"createSignal",
|
|
20
|
+
"createStore",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/** The two-argument effects: the first function is the tracked compute phase. */
|
|
24
|
+
const EFFECT_PRIMITIVES = ["createEffect", "createRenderEffect"];
|
|
25
|
+
|
|
26
|
+
/** Primitives whose result is called to read it: `value()`. */
|
|
27
|
+
const ACCESSOR_PRIMITIVES = ["createMemo", "createOptimistic", "createSignal"];
|
|
28
|
+
|
|
29
|
+
/** Primitives whose result is read by property access: `store.items`. */
|
|
30
|
+
const STORE_PRIMITIVES = [
|
|
31
|
+
"createOptimisticStore",
|
|
32
|
+
"createProjection",
|
|
33
|
+
"createStore",
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const FIRST_ELEMENT = 0;
|
|
37
|
+
|
|
38
|
+
function getFirstBinding(declarator) {
|
|
39
|
+
if (declarator.id.type === "ArrayPattern") {
|
|
40
|
+
const element = declarator.id.elements[FIRST_ELEMENT];
|
|
41
|
+
|
|
42
|
+
return element?.type === "Identifier" ? element : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return declarator.id.type === "Identifier" ? declarator.id : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getStart(node) {
|
|
49
|
+
return node.range?.[0] ?? node.start;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function getEnd(node) {
|
|
53
|
+
return node.range?.[1] ?? node.end;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isInsideTry(node, callback) {
|
|
57
|
+
let current = node.parent;
|
|
58
|
+
|
|
59
|
+
while (current && current !== callback) {
|
|
60
|
+
if (current.type === "TryStatement") {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
current = current.parent;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** `return await x` or `async () => await x`, and nothing else awaited. */
|
|
71
|
+
function isOnlyReturnAwait(callback, awaits) {
|
|
72
|
+
if (awaits.length !== 1) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const [only] = awaits;
|
|
77
|
+
if (callback.body === only) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Inside `try`, `return await` is what makes the catch see a rejection.
|
|
82
|
+
return only.parent?.type === "ReturnStatement" && !isInsideTry(only, callback);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const reactiveAsyncRule = {
|
|
86
|
+
meta: {
|
|
87
|
+
type: "problem",
|
|
88
|
+
docs: {
|
|
89
|
+
description:
|
|
90
|
+
"Keep reads before the first await in async reactive computations and return promises without async.",
|
|
91
|
+
url: "https://docs.solidjs.com/",
|
|
92
|
+
},
|
|
93
|
+
schema: [],
|
|
94
|
+
messages: {
|
|
95
|
+
untrackedReadAfterAwait:
|
|
96
|
+
"'{{name}}' is read after an `await` in this `{{primitive}}` callback. Solid reports this as UNTRACKED_READ_AFTER_AWAIT: only reads before the first `await` are dependencies, so the result goes stale when it changes. Read it before the `await` and use the local afterwards: `const current = {{name}}; … await …`.",
|
|
97
|
+
redundantAsync:
|
|
98
|
+
"This `async` `{{primitive}}` callback only does `return await …`. Solid reads a returned promise like a value, so remove `async` and `await` and return the promise: `{{primitive}}(() => load(id()))`.",
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
defaultOptions: [],
|
|
102
|
+
create(context) {
|
|
103
|
+
const { matchImport, handleImportDeclaration } = trackImports();
|
|
104
|
+
// Identifier nodes that declare a reactive value, by what reading it
|
|
105
|
+
// looks like. Lookup goes through the scope, so a shadowing local with
|
|
106
|
+
// the same name is not mistaken for the signal.
|
|
107
|
+
const accessors = new Set();
|
|
108
|
+
const stores = new Set();
|
|
109
|
+
|
|
110
|
+
function bindingKind(identifier) {
|
|
111
|
+
const definition = findVariable(context, identifier)?.defs[0];
|
|
112
|
+
const declared = definition?.name;
|
|
113
|
+
if (accessors.has(declared)) {
|
|
114
|
+
return "accessor";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return stores.has(declared) ? "store" : null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function readName(candidate) {
|
|
121
|
+
if (
|
|
122
|
+
candidate.type === "CallExpression" &&
|
|
123
|
+
candidate.callee.type === "Identifier" &&
|
|
124
|
+
bindingKind(candidate.callee) === "accessor"
|
|
125
|
+
) {
|
|
126
|
+
return candidate.callee.name;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (
|
|
130
|
+
candidate.type === "MemberExpression" &&
|
|
131
|
+
candidate.object.type === "Identifier" &&
|
|
132
|
+
candidate.property.type === "Identifier" &&
|
|
133
|
+
!candidate.computed
|
|
134
|
+
) {
|
|
135
|
+
const { name } = candidate.object;
|
|
136
|
+
if (
|
|
137
|
+
isPropsByName(name) ||
|
|
138
|
+
bindingKind(candidate.object) === "store"
|
|
139
|
+
) {
|
|
140
|
+
return `${name}.${candidate.property.name}`;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function checkCallback(callback, primitive) {
|
|
148
|
+
const awaits = [];
|
|
149
|
+
const reads = [];
|
|
150
|
+
|
|
151
|
+
walkOwnBody(callback.body, (candidate) => {
|
|
152
|
+
if (candidate.type === "AwaitExpression") {
|
|
153
|
+
awaits.push(candidate);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const name = readName(candidate);
|
|
157
|
+
if (name !== null) {
|
|
158
|
+
reads.push({ name, node: candidate });
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (awaits.length === 0) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (callback.async === true && isOnlyReturnAwait(callback, awaits)) {
|
|
167
|
+
context.report({
|
|
168
|
+
node: callback,
|
|
169
|
+
messageId: "redundantAsync",
|
|
170
|
+
data: { primitive },
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// A read inside the awaited expression itself still runs before the
|
|
175
|
+
// await suspends, so the boundary is where the first await ends.
|
|
176
|
+
const firstAwaitEnd = Math.min(...awaits.map(getEnd));
|
|
177
|
+
for (const read of reads) {
|
|
178
|
+
if (getStart(read.node) >= firstAwaitEnd) {
|
|
179
|
+
context.report({
|
|
180
|
+
node: read.node,
|
|
181
|
+
messageId: "untrackedReadAfterAwait",
|
|
182
|
+
data: { name: read.name, primitive },
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
ImportDeclaration: handleImportDeclaration,
|
|
190
|
+
VariableDeclarator(node) {
|
|
191
|
+
if (
|
|
192
|
+
node.init?.type !== "CallExpression" ||
|
|
193
|
+
node.init.callee.type !== "Identifier"
|
|
194
|
+
) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const binding = getFirstBinding(node);
|
|
199
|
+
if (!binding) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const callee = node.init.callee.name;
|
|
204
|
+
if (matchImport(ACCESSOR_PRIMITIVES, callee)) {
|
|
205
|
+
accessors.add(binding);
|
|
206
|
+
} else if (matchImport(STORE_PRIMITIVES, callee)) {
|
|
207
|
+
stores.add(binding);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
CallExpression(node) {
|
|
211
|
+
if (node.callee.type !== "Identifier") {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const [first, second] = node.arguments;
|
|
216
|
+
if (!isFunctionNode(first)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const computation = matchImport(
|
|
221
|
+
COMPUTATION_PRIMITIVES,
|
|
222
|
+
node.callee.name,
|
|
223
|
+
);
|
|
224
|
+
if (computation) {
|
|
225
|
+
checkCallback(first, computation);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// A lone function is the whole effect, not a compute phase.
|
|
230
|
+
const effect = matchImport(EFFECT_PRIMITIVES, node.callee.name);
|
|
231
|
+
if (effect && isFunctionNode(second)) {
|
|
232
|
+
checkCallback(first, effect);
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
},
|
|
237
|
+
};
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { isFunctionNode, jsxPropName, trackImports } from "../utils.mjs";
|
|
2
|
+
|
|
3
|
+
const EVENT_HANDLER_NAME = /^on(?::|[A-Z])/;
|
|
4
|
+
const DOM_EVENT_NAME = /^on[a-z]/;
|
|
5
|
+
const DOM_ELEMENT_NAME = /^[a-z]/;
|
|
6
|
+
const CLEANUP_PRIMITIVES = ["onCleanup"];
|
|
7
|
+
const SETTLED_PRIMITIVES = ["onSettled"];
|
|
8
|
+
|
|
9
|
+
/** Values that can never be a cleanup function and are never void. */
|
|
10
|
+
const PLAIN_VALUE_TYPES = new Set([
|
|
11
|
+
"ArrayExpression",
|
|
12
|
+
"AssignmentExpression",
|
|
13
|
+
"BinaryExpression",
|
|
14
|
+
"Literal",
|
|
15
|
+
"ObjectExpression",
|
|
16
|
+
"TemplateLiteral",
|
|
17
|
+
"UpdateExpression",
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
function isVoidExpression(expression) {
|
|
21
|
+
return (
|
|
22
|
+
(expression.type === "UnaryExpression" &&
|
|
23
|
+
expression.operator === "void") ||
|
|
24
|
+
(expression.type === "Identifier" && expression.name === "undefined")
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function describe(expression) {
|
|
29
|
+
return expression.type === "CallExpression"
|
|
30
|
+
? "the call's result"
|
|
31
|
+
: "its value";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Every `return <expr>` of a function, without looking into inner ones. */
|
|
35
|
+
function collectReturns(node, into) {
|
|
36
|
+
if (node === null || typeof node !== "object") {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (node.type === "ReturnStatement" && node.argument) {
|
|
41
|
+
into.push(node.argument);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
for (const [key, value] of Object.entries(node)) {
|
|
45
|
+
if (key === "parent") {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const candidates = Array.isArray(value) ? value : [value];
|
|
50
|
+
for (const candidate of candidates) {
|
|
51
|
+
if (
|
|
52
|
+
candidate !== null &&
|
|
53
|
+
typeof candidate === "object" &&
|
|
54
|
+
typeof candidate.type === "string" &&
|
|
55
|
+
!isFunctionNode(candidate)
|
|
56
|
+
) {
|
|
57
|
+
collectReturns(candidate, into);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The callbacks a `ref` applies, including the ones in a ref array. */
|
|
64
|
+
function getRefCallbacks(expression) {
|
|
65
|
+
if (isFunctionNode(expression)) {
|
|
66
|
+
return [expression];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (expression?.type === "ArrayExpression") {
|
|
70
|
+
return expression.elements.filter((element) => isFunctionNode(element));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const strictVoidReturnRule = {
|
|
77
|
+
meta: {
|
|
78
|
+
type: "problem",
|
|
79
|
+
docs: {
|
|
80
|
+
description:
|
|
81
|
+
"Disallow returning a value from callbacks Solid ignores the return of (event handlers, ref callbacks, onCleanup, onSettled).",
|
|
82
|
+
url: "https://docs.solidjs.com/",
|
|
83
|
+
},
|
|
84
|
+
schema: [],
|
|
85
|
+
messages: {
|
|
86
|
+
handlerReturnsValue:
|
|
87
|
+
"This `{{attribute}}` callback is an expression-bodied arrow, so it returns {{what}}, which Solid ignores. Write a block body (`() => { … }`) or discard it explicitly with `void …`.",
|
|
88
|
+
cleanupReturnsValue:
|
|
89
|
+
"This `{{primitive}}` callback returns a value, which is ignored. Remove the `return`, or use a block body that does not return.",
|
|
90
|
+
settledReturnsValue:
|
|
91
|
+
"This `onSettled` callback returns a value that is not a function. Only a returned cleanup function is used; remove the `return` or return `() => { … }`.",
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
defaultOptions: [],
|
|
95
|
+
create(context) {
|
|
96
|
+
const { matchImport, handleImportDeclaration } = trackImports();
|
|
97
|
+
|
|
98
|
+
function checkHandler(callback, attribute) {
|
|
99
|
+
if (
|
|
100
|
+
!isFunctionNode(callback) ||
|
|
101
|
+
callback.body.type === "BlockStatement" ||
|
|
102
|
+
isVoidExpression(callback.body)
|
|
103
|
+
) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
context.report({
|
|
108
|
+
node: callback,
|
|
109
|
+
messageId: "handlerReturnsValue",
|
|
110
|
+
data: {
|
|
111
|
+
attribute,
|
|
112
|
+
what: describe(callback.body),
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function checkCleanup(callback, primitive) {
|
|
118
|
+
const values = [];
|
|
119
|
+
if (callback.body.type === "BlockStatement") {
|
|
120
|
+
collectReturns(callback.body, values);
|
|
121
|
+
} else {
|
|
122
|
+
values.push(callback.body);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// A call may well return nothing (`() => clearInterval(id)`); without
|
|
126
|
+
// types only values that are certainly not void are reported.
|
|
127
|
+
if (
|
|
128
|
+
values.some(
|
|
129
|
+
(value) =>
|
|
130
|
+
PLAIN_VALUE_TYPES.has(value.type) &&
|
|
131
|
+
!isVoidExpression(value),
|
|
132
|
+
)
|
|
133
|
+
) {
|
|
134
|
+
context.report({
|
|
135
|
+
node: callback,
|
|
136
|
+
messageId:
|
|
137
|
+
primitive === "onSettled"
|
|
138
|
+
? "settledReturnsValue"
|
|
139
|
+
: "cleanupReturnsValue",
|
|
140
|
+
data: { primitive },
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
ImportDeclaration: handleImportDeclaration,
|
|
147
|
+
JSXAttribute(node) {
|
|
148
|
+
const isForeignNamespace =
|
|
149
|
+
node.name.type === "JSXNamespacedName" &&
|
|
150
|
+
node.name.namespace.name !== "on";
|
|
151
|
+
if (
|
|
152
|
+
node.value?.type !== "JSXExpressionContainer" ||
|
|
153
|
+
isForeignNamespace
|
|
154
|
+
) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const name = jsxPropName(node);
|
|
159
|
+
const tagName = node.parent?.name;
|
|
160
|
+
const isDomElement =
|
|
161
|
+
tagName?.type === "JSXIdentifier" &&
|
|
162
|
+
DOM_ELEMENT_NAME.test(tagName.name);
|
|
163
|
+
|
|
164
|
+
if (name === "ref") {
|
|
165
|
+
for (const callback of getRefCallbacks(
|
|
166
|
+
node.value.expression,
|
|
167
|
+
)) {
|
|
168
|
+
checkHandler(callback, "ref");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (
|
|
175
|
+
EVENT_HANDLER_NAME.test(name) ||
|
|
176
|
+
(isDomElement && DOM_EVENT_NAME.test(name))
|
|
177
|
+
) {
|
|
178
|
+
checkHandler(node.value.expression, name);
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
CallExpression(node) {
|
|
182
|
+
if (node.callee.type !== "Identifier") {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const [callback] = node.arguments;
|
|
187
|
+
if (!isFunctionNode(callback)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const primitive = matchImport(
|
|
192
|
+
[...CLEANUP_PRIMITIVES, ...SETTLED_PRIMITIVES],
|
|
193
|
+
node.callee.name,
|
|
194
|
+
);
|
|
195
|
+
if (primitive) {
|
|
196
|
+
checkCleanup(callback, primitive);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
};
|
package/src/utils.mjs
CHANGED
|
@@ -338,3 +338,36 @@ export function jsxGetProp(props, prop) {
|
|
|
338
338
|
);
|
|
339
339
|
});
|
|
340
340
|
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Visit `node` and everything below it, stopping at nested functions.
|
|
344
|
+
*
|
|
345
|
+
* What a function does itself is a different question from what a callback it
|
|
346
|
+
* creates does later, so the rules that reason about "this body" do not look
|
|
347
|
+
* into inner functions.
|
|
348
|
+
*/
|
|
349
|
+
export function walkOwnBody(node, visit) {
|
|
350
|
+
if (node === null || typeof node !== "object") {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
visit(node);
|
|
355
|
+
|
|
356
|
+
for (const [key, value] of Object.entries(node)) {
|
|
357
|
+
if (key === "parent") {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const candidates = Array.isArray(value) ? value : [value];
|
|
362
|
+
for (const candidate of candidates) {
|
|
363
|
+
if (
|
|
364
|
+
candidate !== null &&
|
|
365
|
+
typeof candidate === "object" &&
|
|
366
|
+
typeof candidate.type === "string" &&
|
|
367
|
+
!isFunctionNode(candidate)
|
|
368
|
+
) {
|
|
369
|
+
walkOwnBody(candidate, visit);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|