@murky-web/oxlint-plugin-solid 1.5.0 → 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 +163 -18
- package/THIRD_PARTY_NOTICES.md +7 -4
- package/package.json +5 -2
- package/src/compat.mjs +3 -0
- package/src/index.mjs +26 -3
- package/src/rules/components_return_once.mjs +136 -89
- package/src/rules/event_handlers.mjs +55 -35
- package/src/rules/filename_matches_export.mjs +292 -0
- package/src/rules/function_size.mjs +189 -0
- package/src/rules/imports.mjs +24 -21
- package/src/rules/init_declarations.mjs +139 -0
- package/src/rules/jsx_no_duplicate_props.mjs +46 -18
- package/src/rules/jsx_no_script_url.mjs +7 -6
- package/src/rules/jsx_no_undef.mjs +35 -38
- package/src/rules/jsx_uses_vars.mjs +4 -4
- package/src/rules/no_array_handlers.mjs +38 -9
- package/src/rules/no_cleanup_in_effect_apply.mjs +154 -0
- package/src/rules/no_destructure.mjs +105 -35
- package/src/rules/no_innerhtml.mjs +64 -31
- package/src/rules/no_module_scope_reactive_primitive.mjs +73 -0
- package/src/rules/no_owned_primitives_in_ref.mjs +30 -9
- package/src/rules/no_proxy_apis.mjs +44 -15
- package/src/rules/no_react_deps.mjs +397 -41
- package/src/rules/no_react_specific_props.mjs +40 -11
- package/src/rules/no_server_import_in_client.mjs +9 -9
- package/src/rules/no_setter_in_effect.mjs +153 -30
- 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 +108 -29
- package/src/rules/no_untracked_effect_read.mjs +69 -32
- package/src/rules/no_write_in_pure_computation.mjs +273 -0
- package/src/rules/prefer_arrow_components.mjs +47 -21
- package/src/rules/prefer_class_object.mjs +52 -20
- package/src/rules/prefer_destructuring.mjs +164 -0
- package/src/rules/prefer_for.mjs +42 -12
- package/src/rules/prefer_ref_shorthand.mjs +142 -0
- package/src/rules/prefer_show.mjs +55 -24
- package/src/rules/reactive_async.mjs +64 -17
- package/src/rules/reactivity.mjs +1218 -350
- package/src/rules/self_closing_comp.mjs +23 -18
- package/src/rules/strict_void_return.mjs +50 -20
- package/src/rules/style_prop.mjs +53 -25
- package/src/rules/typed_merge_defaults.mjs +88 -0
- package/src/rules/validate_jsx_nesting.mjs +16 -16
- package/src/utils.mjs +65 -34
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const element = declarator.id.elements[GETTER_INDEX];
|
|
12
|
-
|
|
13
|
-
return element?.type === "Identifier" ? element.name : null;
|
|
14
|
-
}
|
|
1
|
+
import { getSourceCode } from "../compat.mjs";
|
|
2
|
+
import {
|
|
3
|
+
getGetterName,
|
|
4
|
+
isFunctionNode,
|
|
5
|
+
isPropsByName,
|
|
6
|
+
localName,
|
|
7
|
+
trackImports
|
|
8
|
+
} from "../utils.mjs";
|
|
15
9
|
|
|
16
10
|
/** Every name a function's parameters bind, however they are destructured. */
|
|
17
11
|
function collectParameterNames(fn, into) {
|
|
@@ -75,19 +69,43 @@ function walkPhase(node, visit) {
|
|
|
75
69
|
}
|
|
76
70
|
}
|
|
77
71
|
|
|
72
|
+
/** The apply's first parameter when it is a plain identifier. */
|
|
73
|
+
function valueName(apply) {
|
|
74
|
+
const [first] = apply.params;
|
|
75
|
+
|
|
76
|
+
return first?.type === "Identifier" ? first.name : "value";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The effect with `read` moved into its compute, which hands both values over
|
|
81
|
+
* as one object: `({ value: <the compute's expression>, local: read })`.
|
|
82
|
+
*/
|
|
83
|
+
function buildFix(sourceCode, api, compute, apply, read, local) {
|
|
84
|
+
const computed =
|
|
85
|
+
isFunctionNode(compute) &&
|
|
86
|
+
compute.body.type !== "BlockStatement" &&
|
|
87
|
+
compute.body.type !== "ObjectExpression"
|
|
88
|
+
? sourceCode.getText(compute.body)
|
|
89
|
+
: "…";
|
|
90
|
+
const quoted = computed.includes("\n") ? "…" : computed;
|
|
91
|
+
const value = valueName(apply);
|
|
92
|
+
|
|
93
|
+
return `${api}(() => ({ ${value}: ${quoted}, ${local}: ${read} }), ({ ${value}, ${local} }) => { … })`;
|
|
94
|
+
}
|
|
95
|
+
|
|
78
96
|
export default {
|
|
79
97
|
meta: {
|
|
80
98
|
type: "problem",
|
|
81
99
|
docs: {
|
|
82
100
|
description:
|
|
83
101
|
"Disallow reading reactive values in the untracked apply phase of an effect.",
|
|
84
|
-
url: "https://
|
|
102
|
+
url: "https://github.com/MurkyTheMurloc/web_dev_tools/blob/main/docs/solid-2-effects.md"
|
|
85
103
|
},
|
|
86
104
|
schema: [],
|
|
87
105
|
messages: {
|
|
88
106
|
untrackedEffectRead:
|
|
89
|
-
"
|
|
90
|
-
}
|
|
107
|
+
"`{{read}}` is read in the apply phase of `{{api}}`, which runs untracked, so a change to '{{name}}' does not re-run this effect. Fix: read it in the compute and take it as an argument, `{{fix}}`, then use `{{local}}` in place of `{{read}}`."
|
|
108
|
+
}
|
|
91
109
|
},
|
|
92
110
|
defaultOptions: [],
|
|
93
111
|
create(context) {
|
|
@@ -115,7 +133,7 @@ export default {
|
|
|
115
133
|
node.callee.type !== "Identifier" ||
|
|
116
134
|
!matchImport(
|
|
117
135
|
["createEffect", "createRenderEffect"],
|
|
118
|
-
node.callee.name
|
|
136
|
+
node.callee.name
|
|
119
137
|
)
|
|
120
138
|
) {
|
|
121
139
|
return;
|
|
@@ -132,6 +150,32 @@ export default {
|
|
|
132
150
|
// parameter, so those names are already settled values.
|
|
133
151
|
const settled = new Set();
|
|
134
152
|
collectParameterNames(apply, settled);
|
|
153
|
+
const sourceCode = getSourceCode(context);
|
|
154
|
+
const report = (candidate, name) => {
|
|
155
|
+
const read = sourceCode.getText(candidate);
|
|
156
|
+
const short = localName(name);
|
|
157
|
+
// `(title) => … props.title` would bind `title` twice.
|
|
158
|
+
const local =
|
|
159
|
+
short === valueName(apply) ? `${short}Read` : short;
|
|
160
|
+
context.report({
|
|
161
|
+
node: candidate,
|
|
162
|
+
messageId: "untrackedEffectRead",
|
|
163
|
+
data: {
|
|
164
|
+
api: node.callee.name,
|
|
165
|
+
fix: buildFix(
|
|
166
|
+
sourceCode,
|
|
167
|
+
node.callee.name,
|
|
168
|
+
compute,
|
|
169
|
+
apply,
|
|
170
|
+
read,
|
|
171
|
+
local
|
|
172
|
+
),
|
|
173
|
+
local,
|
|
174
|
+
name,
|
|
175
|
+
read
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
};
|
|
135
179
|
|
|
136
180
|
walkPhase(apply.body, (candidate) => {
|
|
137
181
|
// `signal()` — a getter call that did not come through the
|
|
@@ -142,11 +186,7 @@ export default {
|
|
|
142
186
|
getterNames.has(candidate.callee.name) &&
|
|
143
187
|
!settled.has(candidate.callee.name)
|
|
144
188
|
) {
|
|
145
|
-
|
|
146
|
-
node: candidate,
|
|
147
|
-
messageId: "untrackedEffectRead",
|
|
148
|
-
data: { name: candidate.callee.name },
|
|
149
|
-
});
|
|
189
|
+
report(candidate, candidate.callee.name);
|
|
150
190
|
return;
|
|
151
191
|
}
|
|
152
192
|
|
|
@@ -160,16 +200,13 @@ export default {
|
|
|
160
200
|
!settled.has(candidate.object.name) &&
|
|
161
201
|
candidate.property.type === "Identifier"
|
|
162
202
|
) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
name: `${candidate.object.name}.${candidate.property.name}`,
|
|
168
|
-
},
|
|
169
|
-
});
|
|
203
|
+
report(
|
|
204
|
+
candidate,
|
|
205
|
+
`${candidate.object.name}.${candidate.property.name}`
|
|
206
|
+
);
|
|
170
207
|
}
|
|
171
208
|
});
|
|
172
|
-
}
|
|
209
|
+
}
|
|
173
210
|
};
|
|
174
|
-
}
|
|
211
|
+
}
|
|
175
212
|
};
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// Ported from eslint-plugin-solid 0.20.1 (no-write-in-pure-computation).
|
|
2
|
+
// `createMemo` callbacks, the compute half of `createEffect(compute, effect)`
|
|
3
|
+
// and component bodies are pure owned scopes in Solid 2.0: writing reactive
|
|
4
|
+
// state from inside them creates update cycles and throws in dev
|
|
5
|
+
// (REACTIVE_WRITE_IN_OWNED_SCOPE). Only setter calls whose nearest enclosing
|
|
6
|
+
// function IS the pure scope are reported; a setter in a nested function (an
|
|
7
|
+
// event handler, an `onSettled` callback, the effect half) runs later, in an
|
|
8
|
+
// imperative scope.
|
|
9
|
+
//
|
|
10
|
+
// Not the same as no-setter-in-effect, which reports effects that only write
|
|
11
|
+
// and should be derivations; both stay.
|
|
12
|
+
import { findVariable, getSourceCode } from "../compat.mjs";
|
|
13
|
+
import {
|
|
14
|
+
findParent,
|
|
15
|
+
getFunctionName,
|
|
16
|
+
isFunctionNode,
|
|
17
|
+
trackImports
|
|
18
|
+
} from "../utils.mjs";
|
|
19
|
+
|
|
20
|
+
const SETTER_PRIMITIVES = ["createSignal", "createStore", "createOptimistic"];
|
|
21
|
+
|
|
22
|
+
function isOwnedWriteKey(key) {
|
|
23
|
+
return key.type === "Identifier"
|
|
24
|
+
? key.name === "ownedWrite"
|
|
25
|
+
: key.type === "Literal" && key.value === "ownedWrite";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Core skips its dev guard for signals created with `ownedWrite: true`. This
|
|
30
|
+
* only says "no" when it can prove the opt-in is absent: no options argument,
|
|
31
|
+
* or an object literal (through one level of const) that is fully readable and
|
|
32
|
+
* lacks `ownedWrite`. Anything else counts as opted in, so the rule can only
|
|
33
|
+
* under-report, never contradict core.
|
|
34
|
+
*/
|
|
35
|
+
function allowsOwnedWrite(context, init) {
|
|
36
|
+
let options = init.arguments[1];
|
|
37
|
+
if (!options) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (options.type === "Identifier") {
|
|
41
|
+
const variable = findVariable(context, options);
|
|
42
|
+
const definition = variable?.defs[0];
|
|
43
|
+
if (
|
|
44
|
+
variable?.defs.length !== 1 ||
|
|
45
|
+
definition?.type !== "Variable" ||
|
|
46
|
+
definition.node.parent?.kind !== "const" ||
|
|
47
|
+
!definition.node.init ||
|
|
48
|
+
// A const object can still be given properties later, or escape;
|
|
49
|
+
// only references that are the options argument stay conclusive.
|
|
50
|
+
variable.references.some((reference) => {
|
|
51
|
+
return (
|
|
52
|
+
reference.identifier !== definition.node.id &&
|
|
53
|
+
!(
|
|
54
|
+
reference.identifier.parent?.type ===
|
|
55
|
+
"CallExpression" &&
|
|
56
|
+
reference.identifier.parent.arguments[1] ===
|
|
57
|
+
reference.identifier
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
})
|
|
61
|
+
) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
options = definition.node.init;
|
|
65
|
+
}
|
|
66
|
+
if (options.type !== "ObjectExpression") {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return options.properties.some((property) => {
|
|
70
|
+
return (
|
|
71
|
+
property.type === "SpreadElement" ||
|
|
72
|
+
(isOwnedWriteKey(property.key) &&
|
|
73
|
+
!(
|
|
74
|
+
property.value.type === "Literal" &&
|
|
75
|
+
property.value.value === false
|
|
76
|
+
))
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Component-shaped, as in no-destructure and components-return-once: not
|
|
83
|
+
* lowercase-named, not a render prop, and not a plain call argument unless the
|
|
84
|
+
* callee is PascalCase (a HOC).
|
|
85
|
+
*/
|
|
86
|
+
function isComponent(node) {
|
|
87
|
+
const parent = node.parent;
|
|
88
|
+
return (
|
|
89
|
+
!getFunctionName(node)?.match(/^[a-z]/u) &&
|
|
90
|
+
parent?.type !== "JSXExpressionContainer" &&
|
|
91
|
+
!(
|
|
92
|
+
parent?.type === "CallExpression" &&
|
|
93
|
+
parent.arguments.includes(node) &&
|
|
94
|
+
!parent.callee.name?.match(/^[A-Z]/u)
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export default {
|
|
100
|
+
meta: {
|
|
101
|
+
type: "problem",
|
|
102
|
+
docs: {
|
|
103
|
+
description:
|
|
104
|
+
"Disallow writing signals or stores inside `createMemo`, the compute half of `createEffect`, or a component body, which are pure owned scopes.",
|
|
105
|
+
url: "https://github.com/solidjs/eslint-plugin-solid/blob/main/packages/eslint-plugin-solid/docs/no-write-in-pure-computation.md"
|
|
106
|
+
},
|
|
107
|
+
schema: [],
|
|
108
|
+
messages: {
|
|
109
|
+
writeInMemo:
|
|
110
|
+
"`{{call}}` writes state inside `{{api}}`, which must be pure: Solid throws REACTIVE_WRITE_IN_OWNED_SCOPE in dev, and the write re-runs whatever reads it. Fix: return the value from the `{{api}}` callback and read the memo where the state behind '{{name}}' was read, then drop that state; if it is also edited locally, make it a writable derived signal, `createSignal(() => …)`. Do not: wrap the write in `untrack()`; the guard is on the owner, not on tracking.",
|
|
111
|
+
writeInCompute:
|
|
112
|
+
"`{{call}}` runs in the compute half of `{{api}}`, which is tracked and must be pure: Solid throws REACTIVE_WRITE_IN_OWNED_SCOPE in dev. Fix: when the written value derives from what the compute reads, replace the state behind '{{name}}' with `createMemo(() => …)`; when the write is part of the side effect, move it to the effect half, `{{api}}(() => …, (value) => { {{call}}; })`, which runs untracked. Do not: wrap the write in `untrack()`; the guard is on the owner, not on tracking.",
|
|
113
|
+
writeInComponent:
|
|
114
|
+
"`{{call}}` runs during component setup, an owned scope that must be pure: Solid throws REACTIVE_WRITE_IN_OWNED_SCOPE in dev. Fix: create the state with this value, `createSignal(value)` (or `createSignal(() => props.x)` to follow a prop), or make the write in an event handler or in `onSettled(() => { {{call}}; })`. Do not: wrap the write in `untrack()`; the guard is on the owner, not on tracking."
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
defaultOptions: [],
|
|
118
|
+
create(context) {
|
|
119
|
+
const { matchImport, handleImportDeclaration } = trackImports();
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Pure computation callbacks, mapped to how a write in them is
|
|
123
|
+
* reported.
|
|
124
|
+
*/
|
|
125
|
+
const computations = new Map();
|
|
126
|
+
/**
|
|
127
|
+
* Setter calls in a function that may be a component; decided at its
|
|
128
|
+
* exit.
|
|
129
|
+
*/
|
|
130
|
+
const pendingComponentWrites = new Map();
|
|
131
|
+
const functionStack = [];
|
|
132
|
+
const sourceCode = getSourceCode(context);
|
|
133
|
+
|
|
134
|
+
/** The setter call as written, or `setX(…)` when it spans lines. */
|
|
135
|
+
function quote(call) {
|
|
136
|
+
const text = sourceCode.getText(call);
|
|
137
|
+
return text.includes("\n") ? `${call.callee.name}(…)` : text;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isSetter(identifier) {
|
|
141
|
+
const definition = findVariable(context, identifier)?.defs[0];
|
|
142
|
+
if (definition?.type !== "Variable" || !definition.node.init) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
const { id, init } = definition.node;
|
|
146
|
+
if (
|
|
147
|
+
id.type !== "ArrayPattern" ||
|
|
148
|
+
id.elements[1]?.type !== "Identifier" ||
|
|
149
|
+
id.elements[1].name !== identifier.name ||
|
|
150
|
+
init.type !== "CallExpression" ||
|
|
151
|
+
init.callee.type !== "Identifier"
|
|
152
|
+
) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const primitive = matchImport(SETTER_PRIMITIVES, init.callee.name);
|
|
156
|
+
if (!primitive) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
// `ownedWrite` is a signal option; store setters have no such exemption.
|
|
160
|
+
return (
|
|
161
|
+
primitive === "createStore" || !allowsOwnedWrite(context, init)
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function enterFunction() {
|
|
166
|
+
functionStack.push({ hasJSX: false });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function markJSX() {
|
|
170
|
+
const current = functionStack.at(-1);
|
|
171
|
+
if (current) {
|
|
172
|
+
current.hasJSX = true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function exitFunction(node) {
|
|
177
|
+
const { hasJSX } = functionStack.pop() ?? { hasJSX: false };
|
|
178
|
+
const pending = pendingComponentWrites.get(node);
|
|
179
|
+
if (!pending) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
pendingComponentWrites.delete(node);
|
|
183
|
+
if (!hasJSX || computations.has(node) || !isComponent(node)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
for (const call of pending) {
|
|
187
|
+
context.report({
|
|
188
|
+
data: { call: quote(call), name: call.callee.name },
|
|
189
|
+
messageId: "writeInComponent",
|
|
190
|
+
node: call
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function registerComputation(node) {
|
|
196
|
+
const memoName = matchImport(
|
|
197
|
+
["createMemo", "createDeferred"],
|
|
198
|
+
node.callee.name
|
|
199
|
+
);
|
|
200
|
+
if (memoName) {
|
|
201
|
+
if (isFunctionNode(node.arguments[0])) {
|
|
202
|
+
computations.set(node.arguments[0], {
|
|
203
|
+
api: memoName,
|
|
204
|
+
messageId: "writeInMemo"
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
const effectName = matchImport(
|
|
210
|
+
["createEffect", "createRenderEffect"],
|
|
211
|
+
node.callee.name
|
|
212
|
+
);
|
|
213
|
+
if (effectName && node.arguments.length >= 2) {
|
|
214
|
+
if (isFunctionNode(node.arguments[0])) {
|
|
215
|
+
computations.set(node.arguments[0], {
|
|
216
|
+
api: effectName,
|
|
217
|
+
messageId: "writeInCompute"
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
ImportDeclaration: handleImportDeclaration,
|
|
227
|
+
FunctionDeclaration: enterFunction,
|
|
228
|
+
FunctionExpression: enterFunction,
|
|
229
|
+
ArrowFunctionExpression: enterFunction,
|
|
230
|
+
"FunctionDeclaration:exit": exitFunction,
|
|
231
|
+
"FunctionExpression:exit": exitFunction,
|
|
232
|
+
"ArrowFunctionExpression:exit": exitFunction,
|
|
233
|
+
JSXElement: markJSX,
|
|
234
|
+
JSXFragment: markJSX,
|
|
235
|
+
CallExpression(node) {
|
|
236
|
+
if (node.callee.type !== "Identifier") {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
// Ancestors are visited first, so a computation is known
|
|
240
|
+
// before the setter calls inside it.
|
|
241
|
+
if (registerComputation(node)) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const enclosing = findParent(node, isFunctionNode);
|
|
245
|
+
if (!enclosing) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const computation = computations.get(enclosing);
|
|
249
|
+
if (!isSetter(node.callee)) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (computation) {
|
|
253
|
+
context.report({
|
|
254
|
+
data: {
|
|
255
|
+
api: computation.api,
|
|
256
|
+
call: quote(node),
|
|
257
|
+
name: node.callee.name
|
|
258
|
+
},
|
|
259
|
+
messageId: computation.messageId,
|
|
260
|
+
node
|
|
261
|
+
});
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const pending = pendingComponentWrites.get(enclosing);
|
|
265
|
+
if (pending) {
|
|
266
|
+
pending.push(node);
|
|
267
|
+
} else {
|
|
268
|
+
pendingComponentWrites.set(enclosing, [node]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
};
|
|
@@ -105,7 +105,7 @@ function getPropsParam(functionNode, sourceCode) {
|
|
|
105
105
|
if (functionNode.params.length === NO_PARAMETERS) {
|
|
106
106
|
return {
|
|
107
107
|
name: "",
|
|
108
|
-
text: ""
|
|
108
|
+
text: ""
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
111
|
|
|
@@ -117,14 +117,14 @@ function getPropsParam(functionNode, sourceCode) {
|
|
|
117
117
|
if (param.type === "Identifier") {
|
|
118
118
|
return {
|
|
119
119
|
name: param.name,
|
|
120
|
-
text: param.name
|
|
120
|
+
text: param.name
|
|
121
121
|
};
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
if (param.type === "ObjectPattern") {
|
|
125
125
|
return {
|
|
126
126
|
name: "",
|
|
127
|
-
text: sourceCode.getText(param)
|
|
127
|
+
text: sourceCode.getText(param)
|
|
128
128
|
};
|
|
129
129
|
}
|
|
130
130
|
|
|
@@ -307,11 +307,17 @@ export const preferArrowComponentsRule = {
|
|
|
307
307
|
docs: {
|
|
308
308
|
description:
|
|
309
309
|
"Prefer Solid components written as arrow consts typed with Component or ParentComponent.",
|
|
310
|
-
recommended: false
|
|
310
|
+
recommended: false
|
|
311
311
|
},
|
|
312
312
|
fixable: "code",
|
|
313
|
+
messages: {
|
|
314
|
+
preferArrow:
|
|
315
|
+
"`{{name}}` is a function declaration; an arrow const typed `{{type}}` checks its props and returned JSX against Solid's component type{{children}}. Fix: `{{fix}}`{{importFix}}.",
|
|
316
|
+
preferArrowUsedEarly:
|
|
317
|
+
"`{{name}}` is a function declaration; an arrow const typed `{{type}}` checks its props and returned JSX against Solid's component type{{children}}. It is used above its declaration, where a const is not initialized yet, so there is no autofix. Fix: move it above its first use as `{{fix}}`{{importFix}}."
|
|
318
|
+
},
|
|
313
319
|
schema: [],
|
|
314
|
-
type: "suggestion"
|
|
320
|
+
type: "suggestion"
|
|
315
321
|
},
|
|
316
322
|
create(context) {
|
|
317
323
|
const sourceCode = context.sourceCode;
|
|
@@ -361,20 +367,20 @@ export const preferArrowComponentsRule = {
|
|
|
361
367
|
const plans = candidates.map((candidate) => {
|
|
362
368
|
const localTypeName = findLocalTypeName(
|
|
363
369
|
solidImports,
|
|
364
|
-
candidate.desiredTypeName
|
|
370
|
+
candidate.desiredTypeName
|
|
365
371
|
);
|
|
366
372
|
|
|
367
373
|
return {
|
|
368
374
|
...candidate,
|
|
369
375
|
fixable: !isUsedBeforeDeclaration(
|
|
370
376
|
candidate.node,
|
|
371
|
-
firstJsxUse
|
|
377
|
+
firstJsxUse
|
|
372
378
|
),
|
|
373
379
|
resolvedTypeName:
|
|
374
380
|
localTypeName === ""
|
|
375
381
|
? candidate.desiredTypeName
|
|
376
382
|
: localTypeName,
|
|
377
|
-
needsImport: localTypeName === ""
|
|
383
|
+
needsImport: localTypeName === ""
|
|
378
384
|
};
|
|
379
385
|
});
|
|
380
386
|
|
|
@@ -397,7 +403,7 @@ export const preferArrowComponentsRule = {
|
|
|
397
403
|
referencedTypes.has(name) &&
|
|
398
404
|
findLocalTypeName(solidImports, name) === ""
|
|
399
405
|
);
|
|
400
|
-
})
|
|
406
|
+
})
|
|
401
407
|
]) {
|
|
402
408
|
if (!namesToImport.includes(name)) {
|
|
403
409
|
namesToImport.push(name);
|
|
@@ -419,7 +425,7 @@ export const preferArrowComponentsRule = {
|
|
|
419
425
|
fixer,
|
|
420
426
|
sourceCode,
|
|
421
427
|
target,
|
|
422
|
-
namesToImport.map((name) => `${typeKeyword}${name}`)
|
|
428
|
+
namesToImport.map((name) => `${typeKeyword}${name}`)
|
|
423
429
|
);
|
|
424
430
|
}
|
|
425
431
|
|
|
@@ -429,20 +435,40 @@ export const preferArrowComponentsRule = {
|
|
|
429
435
|
SOLID_IMPORT_SOURCE,
|
|
430
436
|
namesToImport,
|
|
431
437
|
undefined,
|
|
432
|
-
true
|
|
438
|
+
true
|
|
433
439
|
);
|
|
434
440
|
};
|
|
435
441
|
|
|
436
|
-
const message =
|
|
437
|
-
"Prefer export const components typed as Component<Props> or ParentComponent<Props>.";
|
|
438
442
|
for (const plan of plans) {
|
|
439
443
|
const { node } = plan;
|
|
444
|
+
const typeName = `${plan.resolvedTypeName}${getPropsSuffix(plan.typeReference)}`;
|
|
445
|
+
const exportPrefix =
|
|
446
|
+
node.parent?.type === "ExportNamedDeclaration"
|
|
447
|
+
? "export "
|
|
448
|
+
: "";
|
|
449
|
+
const data = {
|
|
450
|
+
name: node.id.name,
|
|
451
|
+
type: typeName,
|
|
452
|
+
children:
|
|
453
|
+
plan.desiredTypeName === "ParentComponent"
|
|
454
|
+
? ", and `ParentComponent` types `children`"
|
|
455
|
+
: "",
|
|
456
|
+
fix: `${exportPrefix}const ${node.id.name}: ${typeName} = (${plan.propsParam.text}) => { … };${node.parent?.type === "ExportDefaultDeclaration" ? ` export default ${node.id.name};` : ""}`,
|
|
457
|
+
importFix: plan.needsImport
|
|
458
|
+
? ` with \`import type { ${plan.desiredTypeName} } from "solid-js";\``
|
|
459
|
+
: ""
|
|
460
|
+
};
|
|
440
461
|
if (!plan.fixable) {
|
|
441
|
-
context.report({
|
|
462
|
+
context.report({
|
|
463
|
+
data,
|
|
464
|
+
messageId: "preferArrowUsedEarly",
|
|
465
|
+
node
|
|
466
|
+
});
|
|
442
467
|
continue;
|
|
443
468
|
}
|
|
444
469
|
|
|
445
470
|
context.report({
|
|
471
|
+
data,
|
|
446
472
|
fix(fixer) {
|
|
447
473
|
const exportKind = node.parent?.type;
|
|
448
474
|
const propsSuffix = getPropsSuffix(plan.typeReference);
|
|
@@ -459,7 +485,7 @@ export const preferArrowComponentsRule = {
|
|
|
459
485
|
}
|
|
460
486
|
|
|
461
487
|
const edits = [
|
|
462
|
-
fixer.replaceText(nodeToReplace, replacement)
|
|
488
|
+
fixer.replaceText(nodeToReplace, replacement)
|
|
463
489
|
];
|
|
464
490
|
if (
|
|
465
491
|
plan === carrier &&
|
|
@@ -473,8 +499,8 @@ export const preferArrowComponentsRule = {
|
|
|
473
499
|
|
|
474
500
|
return edits;
|
|
475
501
|
},
|
|
476
|
-
|
|
477
|
-
node
|
|
502
|
+
messageId: "preferArrow",
|
|
503
|
+
node
|
|
478
504
|
});
|
|
479
505
|
}
|
|
480
506
|
};
|
|
@@ -489,7 +515,7 @@ export const preferArrowComponentsRule = {
|
|
|
489
515
|
const propsParam = getPropsParam(node, sourceCode);
|
|
490
516
|
const typeReference = getTypeReferenceForProps(
|
|
491
517
|
node,
|
|
492
|
-
sourceCode
|
|
518
|
+
sourceCode
|
|
493
519
|
);
|
|
494
520
|
if (
|
|
495
521
|
propsParam === FALSE_SENTINEL ||
|
|
@@ -506,9 +532,9 @@ export const preferArrowComponentsRule = {
|
|
|
506
532
|
desiredTypeName: getDesiredSolidTypeName(usesChildren),
|
|
507
533
|
node,
|
|
508
534
|
propsParam,
|
|
509
|
-
typeReference
|
|
535
|
+
typeReference
|
|
510
536
|
});
|
|
511
|
-
}
|
|
537
|
+
}
|
|
512
538
|
};
|
|
513
|
-
}
|
|
539
|
+
}
|
|
514
540
|
};
|