@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
|
@@ -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,18 +69,42 @@ 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
|
-
"
|
|
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}}`."
|
|
90
108
|
}
|
|
91
109
|
},
|
|
92
110
|
defaultOptions: [],
|
|
@@ -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,13 +200,10 @@ 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
|
}
|
|
@@ -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
|
+
};
|
|
@@ -310,6 +310,12 @@ export const preferArrowComponentsRule = {
|
|
|
310
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
320
|
type: "suggestion"
|
|
315
321
|
},
|
|
@@ -433,16 +439,36 @@ export const preferArrowComponentsRule = {
|
|
|
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);
|
|
@@ -473,7 +499,7 @@ export const preferArrowComponentsRule = {
|
|
|
473
499
|
|
|
474
500
|
return edits;
|
|
475
501
|
},
|
|
476
|
-
|
|
502
|
+
messageId: "preferArrow",
|
|
477
503
|
node
|
|
478
504
|
});
|
|
479
505
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getSourceCode } from "../compat.mjs";
|
|
2
|
+
import { jsxPropName, messageCode } from "../utils.mjs";
|
|
2
3
|
const DEFAULT_CLASSNAMES = ["cn", "clsx", "classnames"];
|
|
3
4
|
export default {
|
|
4
5
|
meta: {
|
|
@@ -30,14 +31,26 @@ export default {
|
|
|
30
31
|
],
|
|
31
32
|
messages: {
|
|
32
33
|
classlistRemoved:
|
|
33
|
-
"
|
|
34
|
+
"`{{ name }}` was removed in Solid 2.0; on a native element it is a compile error. `class` takes the object itself. Fix: `{{ fix }}`.",
|
|
34
35
|
preferClassObject:
|
|
35
|
-
"
|
|
36
|
+
"`{{ classnames }}(…)` turns the object into a string, which replaces the whole `class` attribute on every change. Solid 2.0's `class` takes the object and toggles single classes. Fix: `{{ fix }}`."
|
|
36
37
|
}
|
|
37
38
|
},
|
|
38
39
|
defaultOptions: [],
|
|
39
40
|
create(context) {
|
|
40
41
|
const classnames = context.options[0]?.classnames ?? DEFAULT_CLASSNAMES;
|
|
42
|
+
const sourceCode = getSourceCode(context);
|
|
43
|
+
// An attribute's value as an expression: `"a"` or the braced code.
|
|
44
|
+
const valueText = (attribute) => {
|
|
45
|
+
if (!attribute.value) {
|
|
46
|
+
return "true";
|
|
47
|
+
}
|
|
48
|
+
return sourceCode.getText(
|
|
49
|
+
attribute.value.type === "JSXExpressionContainer"
|
|
50
|
+
? attribute.value.expression
|
|
51
|
+
: attribute.value
|
|
52
|
+
);
|
|
53
|
+
};
|
|
41
54
|
return {
|
|
42
55
|
JSXAttribute(node) {
|
|
43
56
|
const name = jsxPropName(node);
|
|
@@ -50,14 +63,27 @@ export default {
|
|
|
50
63
|
// With a `class` attribute next to it, renaming would
|
|
51
64
|
// write the attribute twice (a parse error in TSX); the
|
|
52
65
|
// two have to be merged by hand.
|
|
53
|
-
const
|
|
66
|
+
const classAttribute = node.parent.attributes.find(
|
|
54
67
|
(attribute) =>
|
|
55
68
|
attribute.type === "JSXAttribute" &&
|
|
56
69
|
jsxPropName(attribute) === "class"
|
|
57
70
|
);
|
|
71
|
+
const hasClassAttribute = classAttribute !== undefined;
|
|
58
72
|
context.report({
|
|
59
73
|
node,
|
|
60
74
|
messageId: "classlistRemoved",
|
|
75
|
+
data: {
|
|
76
|
+
name,
|
|
77
|
+
// Next to a `class`, both go into the array form.
|
|
78
|
+
fix: messageCode(
|
|
79
|
+
hasClassAttribute
|
|
80
|
+
? `class={[${valueText(classAttribute)}, ${valueText(node)}]}`
|
|
81
|
+
: `class={${valueText(node)}}`,
|
|
82
|
+
hasClassAttribute
|
|
83
|
+
? 'class={["card", { active: active() }]}'
|
|
84
|
+
: "class={{ active: active() }}"
|
|
85
|
+
)
|
|
86
|
+
},
|
|
61
87
|
fix: hasClassAttribute
|
|
62
88
|
? null
|
|
63
89
|
: (fixer) => fixer.replaceText(node.name, "class")
|
|
@@ -84,7 +110,13 @@ export default {
|
|
|
84
110
|
context.report({
|
|
85
111
|
node,
|
|
86
112
|
messageId: "preferClassObject",
|
|
87
|
-
data: {
|
|
113
|
+
data: {
|
|
114
|
+
classnames: expr.callee.name,
|
|
115
|
+
fix: messageCode(
|
|
116
|
+
`class={${sourceCode.getText(expr.arguments[0])}}`,
|
|
117
|
+
"class={{ active: active() }}"
|
|
118
|
+
)
|
|
119
|
+
},
|
|
88
120
|
fix: (fixer) => {
|
|
89
121
|
const objectRange = expr.arguments[0].range;
|
|
90
122
|
return [
|