@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
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Fork of eslint's init-declarations ("always") for Solid code. Every `let` and
|
|
2
|
+
// `var` needs an initial value, except a variable a `ref` assigns: Solid sets
|
|
3
|
+
// it to the element before anything reads it, and a default would be a value
|
|
4
|
+
// nobody ever sees. Assigned by a ref means `ref={el}` or `ref={el!}` (the
|
|
5
|
+
// compiler rewrites the single shorthand to an assignment) or `el = node` in a
|
|
6
|
+
// ref callback, directly or inside a `ref={[…]}` array. A bare `el` inside a
|
|
7
|
+
// ref array is never assigned; solid/prefer-ref-shorthand reports that entry,
|
|
8
|
+
// so this rule leaves its declaration alone. As in eslint, `for-in`/`for-of`
|
|
9
|
+
// variables and `declare` declarations are ignored.
|
|
10
|
+
import { findVariable } from "../compat.mjs";
|
|
11
|
+
import { isFunctionNode, jsxPropName, walkOwnBody } from "../utils.mjs";
|
|
12
|
+
|
|
13
|
+
function unwrapNonNull(node) {
|
|
14
|
+
return node?.type === "TSNonNullExpression" ? node.expression : node;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isLoopVariable(declaration) {
|
|
18
|
+
const { parent } = declaration;
|
|
19
|
+
return (
|
|
20
|
+
(parent.type === "ForInStatement" ||
|
|
21
|
+
parent.type === "ForOfStatement") &&
|
|
22
|
+
parent.left === declaration
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** `declare let x: T;`, or any declaration in a `declare module`/`global`. */
|
|
27
|
+
function isAmbient(declaration) {
|
|
28
|
+
for (let node = declaration; node; node = node.parent) {
|
|
29
|
+
if (node.declare === true) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default {
|
|
37
|
+
meta: {
|
|
38
|
+
type: "suggestion",
|
|
39
|
+
docs: {
|
|
40
|
+
description:
|
|
41
|
+
"Require an initial value for `let` and `var`, except for a variable a `ref` assigns."
|
|
42
|
+
},
|
|
43
|
+
schema: [],
|
|
44
|
+
messages: {
|
|
45
|
+
noInitialValue:
|
|
46
|
+
"`{{kind}} {{name}}` has no initial value and no `ref` assigns it, so every read before the first write gets `undefined`. Fix: give it its start value, `{{kind}} {{name}} = …;`: a real default, `= undefined` for an `Optional<T>` (`let timer: Optional<Timeout> = undefined;`) or `= null` for a `Maybe<T>`; or derive it with `const`. Only a variable a `ref` assigns may stay uninitialized (`let el!: HTMLDivElement;` with `ref={el}`). Do not: add `!` without a ref, or `= undefined` without an `Optional<T>` type (typebuddy/no-useless-undefined reports it)."
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
defaultOptions: [],
|
|
50
|
+
create(context) {
|
|
51
|
+
const refAssigned = new Set();
|
|
52
|
+
const uninitialized = [];
|
|
53
|
+
|
|
54
|
+
function markAssigned(identifier) {
|
|
55
|
+
const variable = findVariable(context, identifier);
|
|
56
|
+
if (variable) {
|
|
57
|
+
refAssigned.add(variable);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `(node) => { el = node; }`: every `x = node` in the callback. */
|
|
62
|
+
function markCallback(callback) {
|
|
63
|
+
const [param] = callback.params;
|
|
64
|
+
if (param?.type !== "Identifier") {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
walkOwnBody(callback.body, (node) => {
|
|
68
|
+
if (
|
|
69
|
+
node.type === "AssignmentExpression" &&
|
|
70
|
+
node.operator === "=" &&
|
|
71
|
+
node.left.type === "Identifier" &&
|
|
72
|
+
node.right.type === "Identifier" &&
|
|
73
|
+
node.right.name === param.name
|
|
74
|
+
) {
|
|
75
|
+
markAssigned(node.left);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function markEntry(entry, inArray) {
|
|
81
|
+
const node = unwrapNonNull(entry);
|
|
82
|
+
if (node?.type === "Identifier") {
|
|
83
|
+
// In an array: never assigned, but prefer-ref-shorthand
|
|
84
|
+
// reports the entry, which is where the fix goes.
|
|
85
|
+
markAssigned(node);
|
|
86
|
+
} else if (node && isFunctionNode(node)) {
|
|
87
|
+
markCallback(node);
|
|
88
|
+
} else if (!inArray && node?.type === "ArrayExpression") {
|
|
89
|
+
for (const element of node.elements) {
|
|
90
|
+
markEntry(element, true);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
JSXAttribute(node) {
|
|
97
|
+
if (
|
|
98
|
+
jsxPropName(node) === "ref" &&
|
|
99
|
+
node.value?.type === "JSXExpressionContainer"
|
|
100
|
+
) {
|
|
101
|
+
markEntry(node.value.expression, false);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
VariableDeclaration(node) {
|
|
105
|
+
if (
|
|
106
|
+
(node.kind !== "let" && node.kind !== "var") ||
|
|
107
|
+
isLoopVariable(node) ||
|
|
108
|
+
isAmbient(node)
|
|
109
|
+
) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
for (const declarator of node.declarations) {
|
|
113
|
+
if (
|
|
114
|
+
declarator.init === null &&
|
|
115
|
+
declarator.id.type === "Identifier"
|
|
116
|
+
) {
|
|
117
|
+
uninitialized.push(declarator);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
"Program:exit"() {
|
|
122
|
+
for (const declarator of uninitialized) {
|
|
123
|
+
const variable = findVariable(context, declarator.id);
|
|
124
|
+
if (variable && refAssigned.has(variable)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
context.report({
|
|
128
|
+
data: {
|
|
129
|
+
kind: declarator.parent.kind,
|
|
130
|
+
name: declarator.id.name
|
|
131
|
+
},
|
|
132
|
+
messageId: "noInitialValue",
|
|
133
|
+
node: declarator
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getSourceCode } from "../compat.mjs";
|
|
2
|
+
import { jsxGetAllProps, messageCode } from "../utils.mjs";
|
|
2
3
|
export default {
|
|
3
4
|
meta: {
|
|
4
5
|
type: "problem",
|
|
@@ -21,11 +22,12 @@ export default {
|
|
|
21
22
|
}
|
|
22
23
|
],
|
|
23
24
|
messages: {
|
|
24
|
-
noDuplicateProps:
|
|
25
|
+
noDuplicateProps:
|
|
26
|
+
"`{{name}}` is passed twice. The later value overwrites the earlier one, and as two JSX attributes TypeScript rejects it (TS17001). Fix: keep the one `{{name}}` you mean and delete the other.",
|
|
25
27
|
noDuplicateClass:
|
|
26
|
-
"
|
|
28
|
+
"`class` is passed twice. One value overwrites the other, and as two JSX attributes TypeScript rejects it (TS17001). Fix: one `class` with both values in the array form, `{{fix}}`.",
|
|
27
29
|
noDuplicateChildren:
|
|
28
|
-
"
|
|
30
|
+
"{{used}} each set the element's content, so one replaces the other. Fix: keep one of them: JSX children for markup, `textContent` for plain text, `innerHTML` for trusted HTML."
|
|
29
31
|
}
|
|
30
32
|
},
|
|
31
33
|
defaultOptions: [],
|
|
@@ -34,7 +36,24 @@ export default {
|
|
|
34
36
|
JSXOpeningElement(node) {
|
|
35
37
|
const ignoreCase = context.options[0]?.ignoreCase ?? false;
|
|
36
38
|
const props = new Set();
|
|
39
|
+
// The first node of each name, for the merged `class` fix.
|
|
40
|
+
const firstNodes = new Map();
|
|
41
|
+
const sourceCode = getSourceCode(context);
|
|
42
|
+
// The value of the prop whose name is `nameNode`: an
|
|
43
|
+
// attribute's string or expression, or a spread property's.
|
|
44
|
+
const valueText = (nameNode) => {
|
|
45
|
+
const { value } = nameNode.parent;
|
|
46
|
+
if (!value) {
|
|
47
|
+
return "true";
|
|
48
|
+
}
|
|
49
|
+
return sourceCode.getText(
|
|
50
|
+
value.type === "JSXExpressionContainer"
|
|
51
|
+
? value.expression
|
|
52
|
+
: value
|
|
53
|
+
);
|
|
54
|
+
};
|
|
37
55
|
const checkPropName = (name, node) => {
|
|
56
|
+
const written = name;
|
|
38
57
|
if (ignoreCase || name.startsWith("on")) {
|
|
39
58
|
name = name
|
|
40
59
|
.toLowerCase()
|
|
@@ -47,8 +66,17 @@ export default {
|
|
|
47
66
|
messageId:
|
|
48
67
|
name === "class"
|
|
49
68
|
? "noDuplicateClass"
|
|
50
|
-
: "noDuplicateProps"
|
|
69
|
+
: "noDuplicateProps",
|
|
70
|
+
data: {
|
|
71
|
+
name: written,
|
|
72
|
+
fix: messageCode(
|
|
73
|
+
`class={[${valueText(firstNodes.get(name))}, ${valueText(node)}]}`,
|
|
74
|
+
'class={["card", { active: active() }]}'
|
|
75
|
+
)
|
|
76
|
+
}
|
|
51
77
|
});
|
|
78
|
+
} else {
|
|
79
|
+
firstNodes.set(name, node);
|
|
52
80
|
}
|
|
53
81
|
props.add(name);
|
|
54
82
|
};
|
|
@@ -64,10 +92,10 @@ export default {
|
|
|
64
92
|
const hasTextContent =
|
|
65
93
|
props.has("textContent") || props.has("textContent");
|
|
66
94
|
const used = [
|
|
67
|
-
hasChildrenProp && "`
|
|
95
|
+
hasChildrenProp && "`children`",
|
|
68
96
|
hasChildren && "JSX children",
|
|
69
|
-
hasInnerHTML && "`
|
|
70
|
-
hasTextContent && "`
|
|
97
|
+
hasInnerHTML && "`innerHTML`",
|
|
98
|
+
hasTextContent && "`textContent`"
|
|
71
99
|
].filter(Boolean);
|
|
72
100
|
if (used.length > 1) {
|
|
73
101
|
context.report({
|
|
@@ -18,7 +18,7 @@ export const jsxNoScriptUrlRule = {
|
|
|
18
18
|
},
|
|
19
19
|
messages: {
|
|
20
20
|
noJSURL:
|
|
21
|
-
|
|
21
|
+
'`{{name}}` holds a `javascript:` URL, which runs its text as code when followed; text from input makes it a script injection (XSS). Fix: run the code in an event handler, `<button type="button" onClick={() => …}>`, and give a link a real `href`. Do not: assemble the URL at run time to get past this rule.'
|
|
22
22
|
},
|
|
23
23
|
schema: [],
|
|
24
24
|
type: "problem"
|
|
@@ -47,7 +47,8 @@ export const jsxNoScriptUrlRule = {
|
|
|
47
47
|
) {
|
|
48
48
|
context.report({
|
|
49
49
|
messageId: "noJSURL",
|
|
50
|
-
node: node.value
|
|
50
|
+
node: node.value,
|
|
51
|
+
data: { name: node.name.name }
|
|
51
52
|
});
|
|
52
53
|
}
|
|
53
54
|
}
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { getScope, getSourceCode } from "../compat.mjs";
|
|
2
|
-
import {
|
|
3
|
-
isDOMElementName,
|
|
4
|
-
formatList,
|
|
5
|
-
appendImports,
|
|
6
|
-
insertImports
|
|
7
|
-
} from "../utils.mjs";
|
|
2
|
+
import { isDOMElementName, appendImports, insertImports } from "../utils.mjs";
|
|
8
3
|
// Currently all of the control flow components are from 'solid-js'.
|
|
9
4
|
// Solid 2.0 dropped `Index` (use `<For keyed={false}>`) and renamed the
|
|
10
5
|
// async/error boundaries. All of these are exported from `solid-js`.
|
|
@@ -55,10 +50,12 @@ export default {
|
|
|
55
50
|
}
|
|
56
51
|
],
|
|
57
52
|
messages: {
|
|
58
|
-
undefined:
|
|
53
|
+
undefined:
|
|
54
|
+
"`{{identifier}}` is used in JSX but not defined in this module, so rendering it throws a ReferenceError. Fix: import `{{identifier}}` or fix its spelling.",
|
|
59
55
|
customDirectiveUndefined:
|
|
60
|
-
"
|
|
61
|
-
autoImport:
|
|
56
|
+
"The directive `{{identifier}}` in `use:{{identifier}}` is not defined in this module, so applying it throws a ReferenceError. Fix: import `{{identifier}}` or fix its spelling.",
|
|
57
|
+
autoImport:
|
|
58
|
+
'Used in JSX but not imported: `{{imports}}`. Rendering throws a ReferenceError. Fix: `import { {{imports}} } from "{{source}}";`, or add them to the existing `{{source}}` import.'
|
|
62
59
|
}
|
|
63
60
|
},
|
|
64
61
|
defaultOptions: [],
|
|
@@ -188,7 +185,7 @@ export default {
|
|
|
188
185
|
node: importNode,
|
|
189
186
|
messageId: "autoImport",
|
|
190
187
|
data: {
|
|
191
|
-
imports:
|
|
188
|
+
imports: missingComponents.join(", "), // "Show, For, Switch"
|
|
192
189
|
source: SOURCE_MODULE
|
|
193
190
|
},
|
|
194
191
|
fix: (fixer) => {
|
|
@@ -205,7 +202,7 @@ export default {
|
|
|
205
202
|
node: programNode,
|
|
206
203
|
messageId: "autoImport",
|
|
207
204
|
data: {
|
|
208
|
-
imports:
|
|
205
|
+
imports: missingComponents.join(", "),
|
|
209
206
|
source: SOURCE_MODULE
|
|
210
207
|
},
|
|
211
208
|
fix: (fixer) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getSourceCode } from "../compat.mjs";
|
|
2
|
+
import { isDOMElementName, messageCode, trace } from "../utils.mjs";
|
|
2
3
|
|
|
3
4
|
export const noArrayHandlersRule = {
|
|
4
5
|
meta: {
|
|
@@ -8,7 +9,7 @@ export const noArrayHandlersRule = {
|
|
|
8
9
|
},
|
|
9
10
|
messages: {
|
|
10
11
|
noArrayHandlers:
|
|
11
|
-
"
|
|
12
|
+
"`{{name}}` gets the array `{{expression}}`. Solid calls `handler(data, event)` for `[handler, data]`, but the JSX types take `data` as `any`, so a wrong argument passes the type check. Fix: `{{name}}={{{fix}}}`."
|
|
12
13
|
},
|
|
13
14
|
schema: [],
|
|
14
15
|
type: "problem"
|
|
@@ -32,15 +33,43 @@ export const noArrayHandlersRule = {
|
|
|
32
33
|
node.name.type === "JSXIdentifier" &&
|
|
33
34
|
/^on[a-zA-Z]/.test(node.name.name);
|
|
34
35
|
|
|
36
|
+
const traced =
|
|
37
|
+
node.value?.type === "JSXExpressionContainer"
|
|
38
|
+
? trace(node.value.expression, context)
|
|
39
|
+
: null;
|
|
35
40
|
if (
|
|
36
41
|
(isNamespacedHandler || isNormalEventHandler) &&
|
|
37
|
-
|
|
38
|
-
trace(node.value.expression, context).type ===
|
|
39
|
-
"ArrayExpression"
|
|
42
|
+
traced?.type === "ArrayExpression"
|
|
40
43
|
) {
|
|
44
|
+
const sourceCode = getSourceCode(context);
|
|
45
|
+
const [handler, data] = traced.elements.map(
|
|
46
|
+
(element, index) => {
|
|
47
|
+
if (!element || element.type === "SpreadElement") {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const text = sourceCode.getText(element);
|
|
51
|
+
// An inline function is called as `(fn)(...)`.
|
|
52
|
+
return index === 0 &&
|
|
53
|
+
element.type !== "Identifier" &&
|
|
54
|
+
element.type !== "MemberExpression"
|
|
55
|
+
? `(${text})`
|
|
56
|
+
: text;
|
|
57
|
+
}
|
|
58
|
+
);
|
|
41
59
|
context.report({
|
|
42
60
|
messageId: "noArrayHandlers",
|
|
43
|
-
node
|
|
61
|
+
node,
|
|
62
|
+
data: {
|
|
63
|
+
name: sourceCode.getText(node.name),
|
|
64
|
+
expression: messageCode(
|
|
65
|
+
sourceCode.getText(node.value.expression),
|
|
66
|
+
"[handler, data]"
|
|
67
|
+
),
|
|
68
|
+
fix: messageCode(
|
|
69
|
+
`(event) => ${handler ?? "handler"}(${data ?? "data"}, event)`,
|
|
70
|
+
"(event) => handler(data, event)"
|
|
71
|
+
)
|
|
72
|
+
}
|
|
44
73
|
});
|
|
45
74
|
}
|
|
46
75
|
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// `onCleanup` registers on the current owner. An effect function runs
|
|
2
|
+
// imperatively with no owner (NO_OWNER_CLEANUP: the cleanup never runs), and
|
|
3
|
+
// `onSettled` and `createTrackedEffect` callbacks forbid it
|
|
4
|
+
// (CLEANUP_IN_FORBIDDEN_SCOPE). All of them take a returned cleanup instead.
|
|
5
|
+
// Ref callbacks also throw CLEANUP_IN_FORBIDDEN_SCOPE; no-owned-primitives-in-ref
|
|
6
|
+
// reports those.
|
|
7
|
+
import { getSourceCode } from "../compat.mjs";
|
|
8
|
+
import { isFunctionNode, trackImports, walkOwnBody } from "../utils.mjs";
|
|
9
|
+
|
|
10
|
+
const EFFECTS = ["createEffect", "createRenderEffect"];
|
|
11
|
+
const FORBIDDEN = ["onSettled", "createTrackedEffect"];
|
|
12
|
+
const BUNDLE_KEYS = new Set(["effect", "error"]);
|
|
13
|
+
|
|
14
|
+
/** The call `fn` is passed to, and whether `fn` arrives as a bundle member. */
|
|
15
|
+
function callOf(fn) {
|
|
16
|
+
const { parent } = fn;
|
|
17
|
+
if (parent?.type === "CallExpression") {
|
|
18
|
+
return { call: parent, index: parent.arguments.indexOf(fn) };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// `createEffect(compute, { effect: (v) => …, error: (e) => … })`
|
|
22
|
+
if (
|
|
23
|
+
parent?.type === "Property" &&
|
|
24
|
+
parent.value === fn &&
|
|
25
|
+
!parent.computed &&
|
|
26
|
+
BUNDLE_KEYS.has(parent.key.name ?? parent.key.value) &&
|
|
27
|
+
parent.parent.parent?.type === "CallExpression"
|
|
28
|
+
) {
|
|
29
|
+
const call = parent.parent.parent;
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
bundle: true,
|
|
33
|
+
call,
|
|
34
|
+
index: call.arguments.indexOf(parent.parent)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** True when `fn` itself returns something (nested functions aside). */
|
|
42
|
+
function hasReturn(fn) {
|
|
43
|
+
if (fn.body.type !== "BlockStatement") {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
let found = false;
|
|
47
|
+
walkOwnBody(fn.body, (node) => {
|
|
48
|
+
found ||= node.type === "ReturnStatement";
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default {
|
|
55
|
+
meta: {
|
|
56
|
+
type: "problem",
|
|
57
|
+
docs: {
|
|
58
|
+
description:
|
|
59
|
+
"Disallow `onCleanup` in an effect function, an `onSettled` callback or a `createTrackedEffect` callback; return the cleanup instead.",
|
|
60
|
+
url: "https://github.com/MurkyTheMurloc/web_dev_tools/blob/main/docs/solid-2-effects.md"
|
|
61
|
+
},
|
|
62
|
+
hasSuggestions: true,
|
|
63
|
+
schema: [],
|
|
64
|
+
messages: {
|
|
65
|
+
cleanupInEffectFn:
|
|
66
|
+
"`{{name}}` is called in the effect function of `{{api}}`, which runs without an owner, so the cleanup never runs and whatever it releases leaks (Solid warns NO_OWNER_CLEANUP in dev). Fix: return the cleanup from the effect function, `return {{cleanup}};`; it runs before the next run and on disposal. Do not: move `{{name}}` into the compute function (a compute function only reads), wrap it in `runWithOwner`, or disable the rule.",
|
|
67
|
+
cleanupInForbiddenScope:
|
|
68
|
+
"`{{name}}` is called in the `{{api}}` callback, where Solid 2.0 forbids it and throws CLEANUP_IN_FORBIDDEN_SCOPE in dev. Fix: return the cleanup from the callback, `return {{cleanup}};`; it runs when the owner is disposed. Do not: wrap `{{name}}` in `runWithOwner` or a nested function, or disable the rule.",
|
|
69
|
+
returnCleanup: "Return the cleanup instead."
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
defaultOptions: [],
|
|
73
|
+
create(context) {
|
|
74
|
+
const { matchImport, handleImportDeclaration } = trackImports();
|
|
75
|
+
const sourceCode = getSourceCode(context);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
ImportDeclaration: handleImportDeclaration,
|
|
79
|
+
CallExpression(node) {
|
|
80
|
+
if (
|
|
81
|
+
node.callee.type !== "Identifier" ||
|
|
82
|
+
!matchImport("onCleanup", node.callee.name)
|
|
83
|
+
) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let fn = node.parent;
|
|
88
|
+
while (fn && !isFunctionNode(fn)) {
|
|
89
|
+
fn = fn.parent;
|
|
90
|
+
}
|
|
91
|
+
const target = fn && callOf(fn);
|
|
92
|
+
if (
|
|
93
|
+
target?.call.callee.type !== "Identifier" ||
|
|
94
|
+
target.call.arguments.some(
|
|
95
|
+
(arg) => arg.type === "SpreadElement"
|
|
96
|
+
)
|
|
97
|
+
) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const api = target.call.callee.name;
|
|
102
|
+
const isEffectFn =
|
|
103
|
+
matchImport(EFFECTS, api) && target.index === 1;
|
|
104
|
+
const isForbidden =
|
|
105
|
+
matchImport(FORBIDDEN, api) &&
|
|
106
|
+
target.index === 0 &&
|
|
107
|
+
!target.bundle;
|
|
108
|
+
if (!isEffectFn && !isForbidden) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const [argument] = node.arguments;
|
|
113
|
+
const cleanup = argument
|
|
114
|
+
? sourceCode.getText(argument)
|
|
115
|
+
: "() => …";
|
|
116
|
+
const statement = node.parent;
|
|
117
|
+
// `onCleanup(fn)` as the last statement of a callback that
|
|
118
|
+
// returns nothing else becomes `return fn`.
|
|
119
|
+
const canFix =
|
|
120
|
+
argument &&
|
|
121
|
+
statement.type === "ExpressionStatement" &&
|
|
122
|
+
statement.parent === fn.body &&
|
|
123
|
+
fn.body.body.at(-1) === statement &&
|
|
124
|
+
!hasReturn(fn);
|
|
125
|
+
|
|
126
|
+
context.report({
|
|
127
|
+
data: {
|
|
128
|
+
api,
|
|
129
|
+
cleanup: cleanup.includes("\n")
|
|
130
|
+
? "() => { … }"
|
|
131
|
+
: cleanup,
|
|
132
|
+
name: node.callee.name
|
|
133
|
+
},
|
|
134
|
+
messageId: isEffectFn
|
|
135
|
+
? "cleanupInEffectFn"
|
|
136
|
+
: "cleanupInForbiddenScope",
|
|
137
|
+
node,
|
|
138
|
+
suggest: canFix
|
|
139
|
+
? [
|
|
140
|
+
{
|
|
141
|
+
messageId: "returnCleanup",
|
|
142
|
+
fix: (fixer) =>
|
|
143
|
+
fixer.replaceText(
|
|
144
|
+
statement,
|
|
145
|
+
`return ${cleanup};`
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
: []
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
};
|
|
@@ -110,6 +110,69 @@ const getPropertyInfo = (prop) => {
|
|
|
110
110
|
return null;
|
|
111
111
|
}
|
|
112
112
|
};
|
|
113
|
+
const MAX_LISTED_READS = 4;
|
|
114
|
+
const MAX_SETUP_LENGTH = 160;
|
|
115
|
+
// The parts of the noDestructure message that name the code: the pattern as
|
|
116
|
+
// written, the parameter to take instead, the `props.x` reads, and the setup
|
|
117
|
+
// line for defaults and a rest. A fixable plan carries the exact line its fix
|
|
118
|
+
// inserts. Without a fix (a default that reads another prop, a computed key)
|
|
119
|
+
// the line cannot be written for the code, so the message names the helpers.
|
|
120
|
+
const describeDestructure = (sourceCode, plan) => {
|
|
121
|
+
const { props } = plan;
|
|
122
|
+
const end = props.typeAnnotation?.range[0] ?? props.range[1];
|
|
123
|
+
const written = sourceCode.text.slice(props.range[0], end).trim();
|
|
124
|
+
const properties = props.properties.filter(
|
|
125
|
+
(property) => property.type === "Property"
|
|
126
|
+
);
|
|
127
|
+
const rest = props.properties.find(
|
|
128
|
+
(property) => property.type === "RestElement"
|
|
129
|
+
);
|
|
130
|
+
const hasDefaults = properties.some(
|
|
131
|
+
(property) => property.value.type === "AssignmentPattern"
|
|
132
|
+
);
|
|
133
|
+
const propsName = plan.propsName ?? "props";
|
|
134
|
+
const param = plan.origProps ?? (hasDefaults ? "_props" : propsName);
|
|
135
|
+
const access = (property) =>
|
|
136
|
+
property.key.type === "Identifier" && !property.computed
|
|
137
|
+
? `\`${propsName}.${property.key.name}\``
|
|
138
|
+
: `\`${propsName}[${sourceCode.getText(property.key)}]\``;
|
|
139
|
+
const listed = properties.slice(0, MAX_LISTED_READS).map(access);
|
|
140
|
+
if (properties.length > MAX_LISTED_READS) {
|
|
141
|
+
listed.push("…");
|
|
142
|
+
}
|
|
143
|
+
let reads = listed.join(", ");
|
|
144
|
+
if (listed.length === 0) {
|
|
145
|
+
// `({ ...rest })` is the whole props object under another name.
|
|
146
|
+
reads =
|
|
147
|
+
rest?.argument.type === "Identifier"
|
|
148
|
+
? `\`${propsName}\` in place of \`${rest.argument.name}\``
|
|
149
|
+
: `\`${propsName}.<name>\``;
|
|
150
|
+
}
|
|
151
|
+
const setup = plan.fixable
|
|
152
|
+
? (plan.lineToInsert ?? "")
|
|
153
|
+
.split("\n")
|
|
154
|
+
.map((line) => line.trim())
|
|
155
|
+
.filter(Boolean)
|
|
156
|
+
.join(" ")
|
|
157
|
+
: "";
|
|
158
|
+
const helpers = [
|
|
159
|
+
hasDefaults && `\`merge({ … }, ${param})\` for the defaults`,
|
|
160
|
+
rest && listed.length > 0 && `\`omit(${propsName}, …)\` for the rest`
|
|
161
|
+
].filter(Boolean);
|
|
162
|
+
let setupText = "";
|
|
163
|
+
if (setup !== "" && setup.length <= MAX_SETUP_LENGTH && listed.length > 0) {
|
|
164
|
+
setupText = `, after \`${setup}\` as the first line of the body`;
|
|
165
|
+
} else if (helpers.length > 0) {
|
|
166
|
+
setupText = `, with ${helpers.join(" and ")}`;
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
param,
|
|
170
|
+
pattern: written.includes("\n") ? "{ … }" : written,
|
|
171
|
+
reads,
|
|
172
|
+
setup: setupText
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
113
176
|
export default {
|
|
114
177
|
meta: {
|
|
115
178
|
type: "problem",
|
|
@@ -122,8 +185,9 @@ export default {
|
|
|
122
185
|
schema: [],
|
|
123
186
|
messages: {
|
|
124
187
|
noDestructure:
|
|
125
|
-
"Destructuring component props
|
|
126
|
-
missingImport:
|
|
188
|
+
"Destructuring `{{pattern}}` in the parameter list reads each prop once, so later changes from the parent never reach this component. Fix: take `{{param}}` as the parameter and read {{reads}} where each value is used{{setup}}. Do not: copy the props into locals in the body; that reads them once too.",
|
|
189
|
+
missingImport:
|
|
190
|
+
'`{{name}}` is called but not imported, so the call throws a ReferenceError. Fix: `import { {{specifier}} } from "solid-js";`.'
|
|
127
191
|
// noWriteToProps: "Component props are readonly, writing to props is not supported.",
|
|
128
192
|
}
|
|
129
193
|
},
|
|
@@ -530,22 +594,30 @@ export default {
|
|
|
530
594
|
context.report({
|
|
531
595
|
node: identifier,
|
|
532
596
|
messageId: "missingImport",
|
|
533
|
-
data: {
|
|
597
|
+
data: {
|
|
598
|
+
name: identifier.name,
|
|
599
|
+
specifier: importSpecifierText(
|
|
600
|
+
generatedImport(identifier.name)
|
|
601
|
+
)
|
|
602
|
+
},
|
|
534
603
|
fix: (fixer) => importFix(fixer, danglingImports)
|
|
535
604
|
});
|
|
536
605
|
}
|
|
537
606
|
const [carrier] = plans.filter((plan) => plan.fixable);
|
|
538
607
|
plans.forEach((plan) => {
|
|
608
|
+
const data = describeDestructure(sourceCode, plan);
|
|
539
609
|
if (!plan.fixable) {
|
|
540
610
|
context.report({
|
|
541
611
|
node: plan.props,
|
|
542
|
-
messageId: "noDestructure"
|
|
612
|
+
messageId: "noDestructure",
|
|
613
|
+
data
|
|
543
614
|
});
|
|
544
615
|
return;
|
|
545
616
|
}
|
|
546
617
|
context.report({
|
|
547
618
|
node: plan.props,
|
|
548
619
|
messageId: "noDestructure",
|
|
620
|
+
data,
|
|
549
621
|
fix: (fixer) =>
|
|
550
622
|
fixDestructure(
|
|
551
623
|
plan,
|