@murky-web/oxlint-plugin-solid 1.5.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -16
- package/THIRD_PARTY_NOTICES.md +7 -4
- package/package.json +5 -2
- package/src/compat.mjs +3 -0
- package/src/index.mjs +23 -0
- package/src/rules/components_return_once.mjs +131 -84
- package/src/rules/event_handlers.mjs +33 -13
- package/src/rules/filename_matches_export.mjs +292 -0
- package/src/rules/function_size.mjs +189 -0
- package/src/rules/imports.mjs +5 -2
- package/src/rules/init_declarations.mjs +139 -0
- package/src/rules/jsx_no_duplicate_props.mjs +36 -8
- package/src/rules/jsx_no_script_url.mjs +3 -2
- package/src/rules/jsx_no_undef.mjs +8 -11
- package/src/rules/no_array_handlers.mjs +35 -6
- package/src/rules/no_cleanup_in_effect_apply.mjs +154 -0
- package/src/rules/no_destructure.mjs +76 -4
- package/src/rules/no_innerhtml.mjs +51 -18
- package/src/rules/no_module_scope_reactive_primitive.mjs +73 -0
- package/src/rules/no_owned_primitives_in_ref.mjs +23 -2
- package/src/rules/no_proxy_apis.mjs +41 -12
- package/src/rules/no_react_deps.mjs +394 -38
- package/src/rules/no_react_specific_props.mjs +33 -4
- package/src/rules/no_server_import_in_client.mjs +1 -1
- package/src/rules/no_setter_in_effect.mjs +145 -22
- package/src/rules/no_single_arg_create_effect.mjs +55 -0
- package/src/rules/no_unassigned_vars.mjs +99 -0
- package/src/rules/no_unknown_namespaces.mjs +96 -17
- package/src/rules/no_untracked_effect_read.mjs +65 -28
- package/src/rules/no_write_in_pure_computation.mjs +273 -0
- package/src/rules/prefer_arrow_components.mjs +30 -4
- package/src/rules/prefer_class_object.mjs +37 -5
- package/src/rules/prefer_destructuring.mjs +164 -0
- package/src/rules/prefer_for.mjs +34 -4
- package/src/rules/prefer_ref_shorthand.mjs +142 -0
- package/src/rules/prefer_show.mjs +48 -17
- package/src/rules/reactive_async.mjs +55 -8
- package/src/rules/reactivity.mjs +1160 -291
- package/src/rules/self_closing_comp.mjs +7 -2
- package/src/rules/strict_void_return.mjs +43 -13
- package/src/rules/style_prop.mjs +38 -9
- package/src/rules/typed_merge_defaults.mjs +88 -0
- package/src/rules/validate_jsx_nesting.mjs +2 -2
- package/src/utils.mjs +51 -20
package/src/rules/reactivity.mjs
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* File overview here, scroll to bottom.
|
|
3
3
|
*
|
|
4
|
+
* Ported from eslint-plugin-solid 0.20.1 (d3e946c). Differences from upstream:
|
|
5
|
+
*
|
|
6
|
+
* - Solid 2.0 only: the 1.x APIs `on`, `observable`, `mergeProps`,
|
|
7
|
+
* `createResource`, `createMutable`, `createComputed`, `createSelector`,
|
|
8
|
+
* `indexArray`, `<Index>`, `batch`, `produce`, `onMount`, `onError` and
|
|
9
|
+
* `useTransition` are not recognized, and neither are
|
|
10
|
+
* `settings.solid.version` / `moduleSources`.
|
|
11
|
+
* - `createDeferred` returns an accessor, like `createMemo`.
|
|
12
|
+
* - `createEffect`/`createRenderEffect` with a second argument (the split form)
|
|
13
|
+
* may have an async compute phase, which `reactive-async` models; the apply
|
|
14
|
+
* phase may also be an effect bundle object.
|
|
15
|
+
* - An uncalled signal passed to a DOM attribute is reported here (upstream hands
|
|
16
|
+
* that to `no-accessor-as-prop` in v2 mode, which we do not have).
|
|
17
|
+
*
|
|
4
18
|
* @link https://github.com/solidjs-community/eslint-plugin-solid/blob/main/docs/reactivity.md
|
|
5
19
|
*/
|
|
6
20
|
import { getFunctionHeadLocation } from "@eslint-community/eslint-utils";
|
|
7
21
|
import { traverse } from "estraverse";
|
|
8
22
|
|
|
9
|
-
import { findVariable, getSourceCode } from "../compat.mjs";
|
|
10
23
|
import {
|
|
24
|
+
findVariable,
|
|
25
|
+
findVariableInScope,
|
|
26
|
+
getScope,
|
|
27
|
+
getSourceCode
|
|
28
|
+
} from "../compat.mjs";
|
|
29
|
+
import {
|
|
30
|
+
createNameMatcher,
|
|
11
31
|
findParent,
|
|
12
32
|
findInScope,
|
|
13
33
|
isPropsByName,
|
|
@@ -18,12 +38,10 @@ import {
|
|
|
18
38
|
ignoreTransparentWrappers,
|
|
19
39
|
getFunctionName,
|
|
20
40
|
isJSXElementOrFragment,
|
|
21
|
-
trace
|
|
41
|
+
trace,
|
|
42
|
+
walkOwnBody
|
|
22
43
|
} from "../utils.mjs";
|
|
23
|
-
|
|
24
|
-
// `<Index>`. Their branches below are kept structurally but matched against
|
|
25
|
-
// `__removed_*` names so they can never fire; delete them once no Solid 1
|
|
26
|
-
// code is linted with this plugin any more.
|
|
44
|
+
|
|
27
45
|
class ScopeStackItem {
|
|
28
46
|
/** The node for the current scope, or program if global scope */
|
|
29
47
|
node;
|
|
@@ -170,6 +188,17 @@ const getNthDestructuredVar = (id, n, context) => {
|
|
|
170
188
|
if (el?.type === "Identifier") {
|
|
171
189
|
return findVariable(context, el);
|
|
172
190
|
}
|
|
191
|
+
} else if (id?.type === "ObjectPattern") {
|
|
192
|
+
// {0: a, '1': b, 2: c, ...rest}
|
|
193
|
+
const el = id.properties.find(
|
|
194
|
+
(p) =>
|
|
195
|
+
p.type === "Property" &&
|
|
196
|
+
p.key.type === "Literal" &&
|
|
197
|
+
(p.key.value === n || p.key.value === String(n))
|
|
198
|
+
);
|
|
199
|
+
if (el?.value.type === "Identifier") {
|
|
200
|
+
return findVariable(context, el.value);
|
|
201
|
+
}
|
|
173
202
|
}
|
|
174
203
|
return null;
|
|
175
204
|
};
|
|
@@ -179,6 +208,15 @@ const getReturnedVar = (id, context) => {
|
|
|
179
208
|
}
|
|
180
209
|
return null;
|
|
181
210
|
};
|
|
211
|
+
const getStart = (node) => node.range?.[0] ?? node.start;
|
|
212
|
+
const getEnd = (node) => node.range?.[1] ?? node.end;
|
|
213
|
+
const TIMERS = [
|
|
214
|
+
"setInterval",
|
|
215
|
+
"setTimeout",
|
|
216
|
+
"setImmediate",
|
|
217
|
+
"requestAnimationFrame",
|
|
218
|
+
"requestIdleCallback"
|
|
219
|
+
];
|
|
182
220
|
export default {
|
|
183
221
|
meta: {
|
|
184
222
|
type: "problem",
|
|
@@ -193,7 +231,7 @@ export default {
|
|
|
193
231
|
properties: {
|
|
194
232
|
customReactiveFunctions: {
|
|
195
233
|
description:
|
|
196
|
-
"List of function names to consider as reactive functions (allow signals to be safely passed as arguments). In addition, any create* or use* functions are automatically included.",
|
|
234
|
+
"List of function names to consider as reactive functions (allow signals to be safely passed as arguments). Supports exact names, '*' wildcards ('use*Store'), and regexes given as '/pattern/' strings. In addition, any create* or use* functions are automatically included.",
|
|
197
235
|
type: "array",
|
|
198
236
|
items: {
|
|
199
237
|
type: "string"
|
|
@@ -206,21 +244,27 @@ export default {
|
|
|
206
244
|
],
|
|
207
245
|
messages: {
|
|
208
246
|
noWrite:
|
|
209
|
-
"
|
|
247
|
+
"`{{expression}}` writes to the reactive variable '{{name}}' directly, so {{consequence}}. Fix: {{fix}}. Do not: copy '{{name}}' into a local and write that; the copy never updates.",
|
|
210
248
|
untrackedReactive:
|
|
211
|
-
"
|
|
249
|
+
"`{{expression}}` is read outside a tracked scope, so it is evaluated once and later changes are ignored (in a component body, Solid warns STRICT_READ_UNTRACKED in dev). Fix: {{fix}}. Do not: wrap the read in `untrack()`; the value still never updates.",
|
|
212
250
|
expectedFunctionGotExpression:
|
|
213
|
-
"
|
|
251
|
+
"'{{name}}' is read in `{{scope}}`, which is passed where Solid expects a function, so it is evaluated once and later changes are ignored. Fix: {{fix}}. Do not: wrap the read in `untrack()`; that keeps it static.",
|
|
252
|
+
handlerBinding:
|
|
253
|
+
"'{{name}}' is read where `{{attribute}}` is bound on a native element. Solid binds the handler once, so `{{expression}}` is evaluated a single time and later changes of '{{name}}' are ignored. Fix: decide inside the handler, `{{attribute}}={{{fix}}}`. Do not wrap the read in `untrack()` and do not disable this rule; neither makes the handler react.",
|
|
214
254
|
badSignal:
|
|
215
|
-
"The
|
|
255
|
+
"The accessor '{{name}}' is used without a call in {{where}}, so the code gets the function instead of its value. Fix: call it, `{{name}}()`.",
|
|
216
256
|
badUnnamedDerivedSignal:
|
|
217
|
-
"This function
|
|
257
|
+
"This function reads reactive values but is {{where}}, which is not a tracked scope, so its reads are not tracked and later changes are ignored. Fix: read the values in JSX, in `createMemo(() => …)` or in an effect's compute, and hand the results to this function{{custom}}. Do not: wrap the reads in `untrack()`.",
|
|
218
258
|
shouldDestructure:
|
|
219
|
-
"
|
|
259
|
+
"The result of `{{callee}}(…)` is not destructured, so this rule cannot see which variable holds the reactive value and does not check its reads. Fix: `{{fix}}`.",
|
|
220
260
|
shouldAssign:
|
|
221
|
-
"
|
|
261
|
+
"The result of `{{callee}}(…)` is not assigned to one variable, so this rule cannot check its reads{{destructured}}. Fix: `const {{local}} = {{callee}}(…);` and read `{{read}}` where the value is used.",
|
|
222
262
|
noAsyncTrackedScope:
|
|
223
|
-
"
|
|
263
|
+
"This `async` function is {{where}}, which tracks it synchronously, so reads after the first `await` are not dependencies and later changes are ignored. Fix: derive the async value first, `const data = createMemo(() => load(…))`, and read `data()` in a function without `async`. Do not: wrap the late reads in `untrack()`.",
|
|
264
|
+
staleCapture:
|
|
265
|
+
"'{{captured}}' copies `{{init}}` once at setup, and a returned function reads '{{captured}}' later, so it never sees a new value of '{{name}}'. Fix: read `{{init}}` inside the returned function instead of '{{captured}}'. If a one-time snapshot is intended, name it `initial{{Captured}}`. Do not: wrap the read in `untrack()`.",
|
|
266
|
+
providerValue:
|
|
267
|
+
"A context provider reads its `value` prop once, when it is created, so `{{expression}}` is evaluated once and consumers never see a new value. Fix: pass something the consumers can read later, `{{accessor}}` (or an object holding it), and read it in the consumers. Do not: wrap the read in `untrack()`."
|
|
224
268
|
}
|
|
225
269
|
},
|
|
226
270
|
defaultOptions: [
|
|
@@ -234,15 +278,68 @@ export default {
|
|
|
234
278
|
// wrapper the parameter is undefined, so the default is applied here.
|
|
235
279
|
const options = context.options[0] ?? { customReactiveFunctions: [] };
|
|
236
280
|
|
|
237
|
-
const
|
|
281
|
+
const sourceCode = getSourceCode(context);
|
|
282
|
+
/** Code as written, or `fallback` when it spans lines. */
|
|
283
|
+
const quote = (node, fallback = "…") => {
|
|
284
|
+
const text = sourceCode.getText(node);
|
|
285
|
+
return text.includes("\n") ? fallback : text;
|
|
286
|
+
};
|
|
287
|
+
const capitalize = (name) => `${name[0].toUpperCase()}${name.slice(1)}`;
|
|
288
|
+
/** The fix for a tuple primitive whose result is not destructured. */
|
|
289
|
+
const destructureFix = (id, callee) => {
|
|
290
|
+
if (matchImport("splitProps", callee)) {
|
|
291
|
+
// Solid 2.0 has no `splitProps`; `omit` takes the keys as
|
|
292
|
+
// arguments and the named props stay on `props`.
|
|
293
|
+
return 'const rest = omit(props, "a", "b")` and read `props.a';
|
|
294
|
+
}
|
|
295
|
+
const isStore = matchImport(
|
|
296
|
+
["createStore", "createOptimisticStore"],
|
|
297
|
+
callee
|
|
298
|
+
);
|
|
299
|
+
const name =
|
|
300
|
+
id?.type === "Identifier"
|
|
301
|
+
? id.name
|
|
302
|
+
: isStore
|
|
303
|
+
? "store"
|
|
304
|
+
: "value";
|
|
305
|
+
return `const [${name}, set${capitalize(name)}] = ${callee}(…)`;
|
|
306
|
+
};
|
|
307
|
+
const warnShouldDestructure = (node, init) => {
|
|
308
|
+
const callee = init.callee.name;
|
|
238
309
|
context.report({
|
|
239
310
|
node,
|
|
240
311
|
messageId: "shouldDestructure",
|
|
241
|
-
data:
|
|
312
|
+
data: {
|
|
313
|
+
callee,
|
|
314
|
+
fix: destructureFix(
|
|
315
|
+
node.type === "CallExpression" ? null : node,
|
|
316
|
+
callee
|
|
317
|
+
)
|
|
318
|
+
}
|
|
242
319
|
});
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
320
|
+
};
|
|
321
|
+
const warnShouldAssign = (node, init) => {
|
|
322
|
+
const callee = init.callee.name;
|
|
323
|
+
const local = matchImport(["createMemo", "createDeferred"], callee)
|
|
324
|
+
? "value"
|
|
325
|
+
: matchImport("merge", callee)
|
|
326
|
+
? "merged"
|
|
327
|
+
: "rest";
|
|
328
|
+
context.report({
|
|
329
|
+
node,
|
|
330
|
+
messageId: "shouldAssign",
|
|
331
|
+
data: {
|
|
332
|
+
callee,
|
|
333
|
+
destructured:
|
|
334
|
+
node.type === "ObjectPattern" ||
|
|
335
|
+
node.type === "ArrayPattern"
|
|
336
|
+
? ", and destructuring it reads each value once"
|
|
337
|
+
: "",
|
|
338
|
+
local,
|
|
339
|
+
read: local === "value" ? "value()" : `${local}.x`
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
};
|
|
246
343
|
/**
|
|
247
344
|
* Represents the lexical function stack and relevant information for
|
|
248
345
|
* each function
|
|
@@ -251,6 +348,118 @@ export default {
|
|
|
251
348
|
const { currentScope, parentScope } = scopeStack;
|
|
252
349
|
/** Tracks imports from 'solid-js', handling aliases. */
|
|
253
350
|
const { matchImport, handleImportDeclaration } = trackImports();
|
|
351
|
+
/**
|
|
352
|
+
* `customReactiveFunctions` entries may be exact names, glob-ish
|
|
353
|
+
* patterns using `*` wildcards, or regexes written as "/pattern/"
|
|
354
|
+
* strings.
|
|
355
|
+
*/
|
|
356
|
+
const matchesCustomReactive = createNameMatcher(
|
|
357
|
+
options.customReactiveFunctions ?? []
|
|
358
|
+
);
|
|
359
|
+
/**
|
|
360
|
+
* JSXExpressionContainers for `value={...}` props on context providers.
|
|
361
|
+
* Providers read `props.value` once, untracked, when created, so
|
|
362
|
+
* reactive expressions here are frozen at their initial value. Recorded
|
|
363
|
+
* so reads inside them get a specific error message instead of the
|
|
364
|
+
* generic untracked-reactivity one.
|
|
365
|
+
*/
|
|
366
|
+
const providerValueContainers = new WeakSet();
|
|
367
|
+
/**
|
|
368
|
+
* Returns whether a name resolves to the result of a `createContext()`
|
|
369
|
+
* call in scope.
|
|
370
|
+
*/
|
|
371
|
+
const isCreatedContext = (name, refNode) => {
|
|
372
|
+
const variable = findVariableInScope(
|
|
373
|
+
getScope(context, refNode),
|
|
374
|
+
name
|
|
375
|
+
);
|
|
376
|
+
const def = variable?.defs[0];
|
|
377
|
+
return Boolean(
|
|
378
|
+
def &&
|
|
379
|
+
def.node.type === "VariableDeclarator" &&
|
|
380
|
+
def.node.init?.type === "CallExpression" &&
|
|
381
|
+
def.node.init.callee.type === "Identifier" &&
|
|
382
|
+
matchImport("createContext", def.node.init.callee.name)
|
|
383
|
+
);
|
|
384
|
+
};
|
|
385
|
+
/**
|
|
386
|
+
* Detects JSX element names that are context providers:
|
|
387
|
+
* `<SomeContext.Provider>` and `<SomeProvider>` by convention, plus any
|
|
388
|
+
* name resolving to a `createContext()` call — in Solid 2.0 the context
|
|
389
|
+
* object is used directly as the provider (`<MyContext value={}>`).
|
|
390
|
+
*/
|
|
391
|
+
const isProviderElementName = (name) => {
|
|
392
|
+
if (name.type === "JSXIdentifier") {
|
|
393
|
+
return (
|
|
394
|
+
name.name.endsWith("Provider") ||
|
|
395
|
+
isCreatedContext(name.name, name)
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
if (name.type === "JSXMemberExpression") {
|
|
399
|
+
return name.property.name === "Provider";
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
};
|
|
403
|
+
/**
|
|
404
|
+
* Async/generator functions that are tracked scopes for Solid 2.0 async
|
|
405
|
+
* computations (async createMemo, function-form
|
|
406
|
+
* createSignal/createStore, etc.). Only reads before the first
|
|
407
|
+
* suspension point (`await`/`yield`) are tracked; later reads are
|
|
408
|
+
* reported.
|
|
409
|
+
*/
|
|
410
|
+
const asyncTrackedScopes = new Set();
|
|
411
|
+
const suspensionTaintCache = new Map();
|
|
412
|
+
/**
|
|
413
|
+
* If `fn` is an async computation, returns the source position after
|
|
414
|
+
* which reactive reads are no longer tracked, or null if it never
|
|
415
|
+
* suspends. When the first suspension point is inside a loop, the loop
|
|
416
|
+
* is tainted too, since later iterations read after the previous
|
|
417
|
+
* iteration's suspension.
|
|
418
|
+
*/
|
|
419
|
+
const getSuspensionTaint = (fn) => {
|
|
420
|
+
if (!isFunctionNode(fn) || !asyncTrackedScopes.has(fn)) return null;
|
|
421
|
+
if (suspensionTaintCache.has(fn)) {
|
|
422
|
+
return suspensionTaintCache.get(fn);
|
|
423
|
+
}
|
|
424
|
+
// Nested functions suspend independently.
|
|
425
|
+
let suspension = null;
|
|
426
|
+
if (!isFunctionNode(fn.body)) {
|
|
427
|
+
walkOwnBody(fn.body, (node) => {
|
|
428
|
+
if (
|
|
429
|
+
(node.type === "AwaitExpression" ||
|
|
430
|
+
node.type === "YieldExpression") &&
|
|
431
|
+
(suspension === null ||
|
|
432
|
+
getStart(node) < getStart(suspension))
|
|
433
|
+
) {
|
|
434
|
+
suspension = node;
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
let taint = null;
|
|
439
|
+
if (suspension) {
|
|
440
|
+
// the operands of the first suspension point are still evaluated synchronously
|
|
441
|
+
taint = getEnd(suspension);
|
|
442
|
+
let ancestor = suspension.parent;
|
|
443
|
+
while (ancestor && ancestor !== fn) {
|
|
444
|
+
if (
|
|
445
|
+
ancestor.type === "ForStatement" ||
|
|
446
|
+
ancestor.type === "WhileStatement" ||
|
|
447
|
+
ancestor.type === "DoWhileStatement"
|
|
448
|
+
) {
|
|
449
|
+
taint = getStart(ancestor);
|
|
450
|
+
} else if (
|
|
451
|
+
ancestor.type === "ForOfStatement" ||
|
|
452
|
+
ancestor.type === "ForInStatement"
|
|
453
|
+
) {
|
|
454
|
+
// the iterated expression is only evaluated once, before the first suspension
|
|
455
|
+
taint = getStart(ancestor.body);
|
|
456
|
+
}
|
|
457
|
+
ancestor = ancestor.parent;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
suspensionTaintCache.set(fn, taint);
|
|
461
|
+
return taint;
|
|
462
|
+
};
|
|
254
463
|
/** Workaround for #61 */
|
|
255
464
|
const markPropsOnCondition = (node, cb) => {
|
|
256
465
|
if (
|
|
@@ -299,6 +508,415 @@ export default {
|
|
|
299
508
|
);
|
|
300
509
|
}
|
|
301
510
|
};
|
|
511
|
+
/**
|
|
512
|
+
* Returns true when `fn` is directly returned by the enclosing
|
|
513
|
+
* function: either the argument of a `return` statement or the whole
|
|
514
|
+
* body of an arrow function. Deliberately strict — a function that
|
|
515
|
+
* merely appears somewhere inside a returned expression (e.g. a
|
|
516
|
+
* runWithOwner callback in a returned call) is not "returned" for our
|
|
517
|
+
* purposes.
|
|
518
|
+
*/
|
|
519
|
+
const isDirectlyReturned = (fn) =>
|
|
520
|
+
fn.parent?.type === "ReturnStatement" ||
|
|
521
|
+
(fn.parent?.type === "ArrowFunctionExpression" &&
|
|
522
|
+
fn.parent.body === fn);
|
|
523
|
+
/**
|
|
524
|
+
* Returns the parent of `fn` for the object-property exemption, looking
|
|
525
|
+
* through conditional/logical wrappers that only decide _whether_ the
|
|
526
|
+
* function is used, not _where_ it lands: `{ onDrag: props.onDrag ? (e)
|
|
527
|
+
* => props.onDrag(e) : undefined }` or `{ onDrag: props.onDrag && ((e)
|
|
528
|
+
* => props.onDrag(e)) }` classify the same as the unconditional form
|
|
529
|
+
* (#235). Functions in a conditional's _test_ are actually called to
|
|
530
|
+
* decide, so they are not looked through.
|
|
531
|
+
*/
|
|
532
|
+
const getEffectivePropertyParent = (fn) => {
|
|
533
|
+
let child = fn;
|
|
534
|
+
let parent = fn.parent ?? null;
|
|
535
|
+
while (
|
|
536
|
+
parent &&
|
|
537
|
+
((parent.type === "ConditionalExpression" &&
|
|
538
|
+
child !== parent.test) ||
|
|
539
|
+
(parent.type === "LogicalExpression" &&
|
|
540
|
+
child === parent.right))
|
|
541
|
+
) {
|
|
542
|
+
child = parent;
|
|
543
|
+
parent = parent.parent ?? null;
|
|
544
|
+
}
|
|
545
|
+
return parent;
|
|
546
|
+
};
|
|
547
|
+
const isReferenceReturned = (node, boundary) => {
|
|
548
|
+
let cursor = node.parent ?? null;
|
|
549
|
+
while (cursor && cursor !== boundary) {
|
|
550
|
+
if (cursor.type === "ReturnStatement") return true;
|
|
551
|
+
if (
|
|
552
|
+
cursor.type === "CallExpression" ||
|
|
553
|
+
cursor.type === "NewExpression" ||
|
|
554
|
+
isFunctionNode(cursor)
|
|
555
|
+
) {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
cursor = cursor.parent ?? null;
|
|
559
|
+
}
|
|
560
|
+
return false;
|
|
561
|
+
};
|
|
562
|
+
const isDeclaredClosureReturned = (closure, boundary) => {
|
|
563
|
+
const declaration =
|
|
564
|
+
closure.type === "FunctionDeclaration"
|
|
565
|
+
? closure
|
|
566
|
+
: closure.parent?.type === "VariableDeclarator" &&
|
|
567
|
+
closure.parent.init === closure
|
|
568
|
+
? closure.parent
|
|
569
|
+
: null;
|
|
570
|
+
if (!declaration) return false;
|
|
571
|
+
const variables =
|
|
572
|
+
sourceCode.scopeManager?.getDeclaredVariables(declaration) ??
|
|
573
|
+
[];
|
|
574
|
+
return variables.some((variable) =>
|
|
575
|
+
variable.references.some(
|
|
576
|
+
(reference) =>
|
|
577
|
+
!reference.init &&
|
|
578
|
+
isReferenceReturned(reference.identifier, boundary)
|
|
579
|
+
)
|
|
580
|
+
);
|
|
581
|
+
};
|
|
582
|
+
/**
|
|
583
|
+
* Returns true when `node` sits inside a nested function that escapes
|
|
584
|
+
* the `boundary` function through a `return` statement (including
|
|
585
|
+
* functions embedded in returned JSX or objects).
|
|
586
|
+
*/
|
|
587
|
+
const isCapturedInReturnedFunction = (node, boundary) => {
|
|
588
|
+
// Find the outermost function strictly inside `boundary` containing `node`.
|
|
589
|
+
let outermost = null;
|
|
590
|
+
let cursor = node;
|
|
591
|
+
while (cursor && cursor !== boundary) {
|
|
592
|
+
if (isFunctionNode(cursor)) outermost = cursor;
|
|
593
|
+
cursor = cursor.parent ?? null;
|
|
594
|
+
}
|
|
595
|
+
if (!cursor || !outermost) return false; // not under boundary, or not captured by a closure
|
|
596
|
+
// Walk from that function to `boundary`; only non-function nodes remain
|
|
597
|
+
// in between, so a ReturnStatement found here belongs to `boundary`.
|
|
598
|
+
let child = outermost;
|
|
599
|
+
let parent = child.parent ?? null;
|
|
600
|
+
while (parent && child !== boundary) {
|
|
601
|
+
if (parent.type === "ReturnStatement") return true;
|
|
602
|
+
// A function passed as a call argument doesn't escape through a
|
|
603
|
+
// `return` below it — only the call's *result* does. Synchronous
|
|
604
|
+
// callbacks (`return items.filter((item) => item.includes(q))`) run
|
|
605
|
+
// during the computation, where the capture is fresh (#223). The
|
|
606
|
+
// same goes for a called function itself (IIFEs).
|
|
607
|
+
if (
|
|
608
|
+
parent.type === "CallExpression" ||
|
|
609
|
+
parent.type === "NewExpression"
|
|
610
|
+
) {
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
if (isFunctionNode(parent)) {
|
|
614
|
+
if (
|
|
615
|
+
parent === boundary &&
|
|
616
|
+
child.type === "BlockStatement" &&
|
|
617
|
+
isDeclaredClosureReturned(outermost, boundary)
|
|
618
|
+
) {
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
return (
|
|
622
|
+
parent === boundary &&
|
|
623
|
+
parent.type === "ArrowFunctionExpression" &&
|
|
624
|
+
parent.body === child &&
|
|
625
|
+
// only an expression body is an implicit return; a closure declared as a
|
|
626
|
+
// statement in a block body is not returned (#250)
|
|
627
|
+
child.type !== "BlockStatement"
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
child = parent;
|
|
631
|
+
parent = parent.parent ?? null;
|
|
632
|
+
}
|
|
633
|
+
return false;
|
|
634
|
+
};
|
|
635
|
+
/**
|
|
636
|
+
* Detects the stale-capture footgun: a signal called at a function's
|
|
637
|
+
* setup level, its result captured in a variable, and that variable
|
|
638
|
+
* read by a function the enclosing function _returns_. The returned
|
|
639
|
+
* function pretends to be live but reads a value frozen at setup.
|
|
640
|
+
* Reports and returns true when found.
|
|
641
|
+
*/
|
|
642
|
+
const checkStaleCapture = (identifier) => {
|
|
643
|
+
const call = identifier.parent;
|
|
644
|
+
if (call?.type !== "CallExpression" || call.callee !== identifier) {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
// Find the VariableDeclarator whose init contains this call, walking up
|
|
648
|
+
// through intermediate expressions (`const t = items().length`) but
|
|
649
|
+
// never through a statement or another function.
|
|
650
|
+
let child = call;
|
|
651
|
+
while (
|
|
652
|
+
child.parent &&
|
|
653
|
+
child.parent.type !== "VariableDeclarator" &&
|
|
654
|
+
!isFunctionNode(child.parent) &&
|
|
655
|
+
!child.parent.type.endsWith("Statement")
|
|
656
|
+
) {
|
|
657
|
+
child = child.parent;
|
|
658
|
+
}
|
|
659
|
+
const declarator = child.parent;
|
|
660
|
+
if (
|
|
661
|
+
declarator?.type !== "VariableDeclarator" ||
|
|
662
|
+
declarator.init !== child
|
|
663
|
+
) {
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
const currentScopeNode = currentScope().node;
|
|
667
|
+
const capturedVars =
|
|
668
|
+
sourceCode.scopeManager?.getDeclaredVariables(declarator) ?? [];
|
|
669
|
+
for (const captured of capturedVars) {
|
|
670
|
+
// The initial/default/static naming convention opts into a one-time
|
|
671
|
+
// snapshot, same as for props reads.
|
|
672
|
+
if (/^(?:initial|default|static[A-Z])/.test(captured.name)) {
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
for (const reference of captured.references) {
|
|
676
|
+
if (reference.init) continue;
|
|
677
|
+
if (
|
|
678
|
+
isCapturedInReturnedFunction(
|
|
679
|
+
reference.identifier,
|
|
680
|
+
currentScopeNode
|
|
681
|
+
)
|
|
682
|
+
) {
|
|
683
|
+
context.report({
|
|
684
|
+
node: call,
|
|
685
|
+
messageId: "staleCapture",
|
|
686
|
+
data: {
|
|
687
|
+
name: identifier.name,
|
|
688
|
+
captured: captured.name,
|
|
689
|
+
Captured: capitalize(captured.name),
|
|
690
|
+
init: quote(declarator.init)
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return false;
|
|
698
|
+
};
|
|
699
|
+
/**
|
|
700
|
+
* Where a function sits, for messages: `passed to \`foo\``for a call
|
|
701
|
+
* argument, otherwise`fallback`. `callee` is the plain name of the
|
|
702
|
+
* function it is passed to, when it has one.
|
|
703
|
+
*/
|
|
704
|
+
const describePlacement = (fn, fallback) => {
|
|
705
|
+
const parent = fn.parent;
|
|
706
|
+
if (
|
|
707
|
+
parent?.type === "CallExpression" &&
|
|
708
|
+
parent.arguments.includes(fn)
|
|
709
|
+
) {
|
|
710
|
+
return {
|
|
711
|
+
callee:
|
|
712
|
+
parent.callee.type === "Identifier"
|
|
713
|
+
? parent.callee.name
|
|
714
|
+
: null,
|
|
715
|
+
where: `passed to \`${quote(parent.callee)}\``
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
if (parent?.type === "JSXExpressionContainer") {
|
|
719
|
+
return { callee: null, where: "a JSX child" };
|
|
720
|
+
}
|
|
721
|
+
if (parent?.type === "Property" && parent.kind === "get") {
|
|
722
|
+
return { callee: null, where: "a store getter" };
|
|
723
|
+
}
|
|
724
|
+
return { callee: null, where: fallback };
|
|
725
|
+
};
|
|
726
|
+
/**
|
|
727
|
+
* The parts of the noWrite message for a write to `identifier`: the
|
|
728
|
+
* write as written, what it breaks, and the setter call or derivation
|
|
729
|
+
* that replaces it. `kind` is "signal" for a signal or memo, "object"
|
|
730
|
+
* for props or a store, and "member" for `props.x = …`/`store.x = …`.
|
|
731
|
+
*/
|
|
732
|
+
const noWriteData = (identifier, kind) => {
|
|
733
|
+
let write = identifier;
|
|
734
|
+
while (
|
|
735
|
+
write.parent &&
|
|
736
|
+
write.type !== "AssignmentExpression" &&
|
|
737
|
+
write.type !== "UpdateExpression" &&
|
|
738
|
+
!write.type.endsWith("Statement")
|
|
739
|
+
) {
|
|
740
|
+
write = write.parent;
|
|
741
|
+
}
|
|
742
|
+
const isWrite =
|
|
743
|
+
write.type === "AssignmentExpression" ||
|
|
744
|
+
write.type === "UpdateExpression";
|
|
745
|
+
const name = identifier.name;
|
|
746
|
+
const expression = isWrite
|
|
747
|
+
? quote(write, `${name} = …`)
|
|
748
|
+
: quote(identifier);
|
|
749
|
+
// `const [value, setValue] = createX(…)` -> `setValue`.
|
|
750
|
+
const definition = findVariable(context, identifier)?.defs[0];
|
|
751
|
+
const declarator =
|
|
752
|
+
definition?.type === "Variable" ? definition.node : null;
|
|
753
|
+
const setterElement =
|
|
754
|
+
declarator?.id.type === "ArrayPattern"
|
|
755
|
+
? declarator.id.elements[1]
|
|
756
|
+
: null;
|
|
757
|
+
const setter =
|
|
758
|
+
setterElement?.type === "Identifier"
|
|
759
|
+
? setterElement.name
|
|
760
|
+
: null;
|
|
761
|
+
const right =
|
|
762
|
+
write.type === "AssignmentExpression"
|
|
763
|
+
? quote(write.right)
|
|
764
|
+
: "…";
|
|
765
|
+
if (kind === "member") {
|
|
766
|
+
const target = identifier.parent;
|
|
767
|
+
const path = sourceCode.text.slice(
|
|
768
|
+
target.object.range[1],
|
|
769
|
+
target.range[1]
|
|
770
|
+
);
|
|
771
|
+
if (setter) {
|
|
772
|
+
const operator =
|
|
773
|
+
write.type === "AssignmentExpression"
|
|
774
|
+
? write.operator
|
|
775
|
+
: "=";
|
|
776
|
+
return {
|
|
777
|
+
consequence:
|
|
778
|
+
"the write is ignored: a store is read-only outside its setter (Solid warns STORE_WRITE_OUTSIDE_SETTER in dev)",
|
|
779
|
+
expression,
|
|
780
|
+
fix: `write through the setter's draft, \`${setter}((draft) => { draft${path} ${operator} ${right}; })\``,
|
|
781
|
+
name
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
const prop =
|
|
785
|
+
!target.computed && target.property.type === "Identifier"
|
|
786
|
+
? target.property.name
|
|
787
|
+
: "value";
|
|
788
|
+
return {
|
|
789
|
+
consequence:
|
|
790
|
+
"the write goes to an object the parent owns and is read-only here",
|
|
791
|
+
expression,
|
|
792
|
+
fix: `keep a local value that follows the prop, \`const [${prop}, set${capitalize(prop)}] = createSignal(() => ${quote(target)});\`, and call \`set${capitalize(prop)}(…)\`; or have the parent pass the new value`,
|
|
793
|
+
name
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
const ownsSetter =
|
|
797
|
+
setter !== null &&
|
|
798
|
+
declarator.id.elements[0] === definition.name;
|
|
799
|
+
if (kind === "signal") {
|
|
800
|
+
// A signal, memo or derived function.
|
|
801
|
+
if (ownsSetter) {
|
|
802
|
+
let call = `${setter}(${right})`;
|
|
803
|
+
if (write.type === "UpdateExpression") {
|
|
804
|
+
call = `${setter}((value) => value ${write.operator[0]} 1)`;
|
|
805
|
+
} else if (
|
|
806
|
+
write.type === "AssignmentExpression" &&
|
|
807
|
+
write.operator !== "="
|
|
808
|
+
) {
|
|
809
|
+
call = `${setter}((value) => value ${write.operator.slice(0, -1)} ${right})`;
|
|
810
|
+
}
|
|
811
|
+
return {
|
|
812
|
+
consequence:
|
|
813
|
+
"the accessor is replaced and nothing that reads it updates",
|
|
814
|
+
expression,
|
|
815
|
+
fix: `call the setter, \`${call}\``,
|
|
816
|
+
name
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
return {
|
|
820
|
+
consequence:
|
|
821
|
+
"the accessor is replaced and nothing that reads it updates",
|
|
822
|
+
expression,
|
|
823
|
+
fix: `derive the new value instead, \`createMemo(() => …)\` over its inputs, or declare it as \`const [${name}, set${capitalize(name)}] = createSignal(() => …)\` when it needs a local override`,
|
|
824
|
+
name
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
if (ownsSetter) {
|
|
828
|
+
return {
|
|
829
|
+
consequence:
|
|
830
|
+
"the name no longer points at the store and nothing that reads it updates",
|
|
831
|
+
expression,
|
|
832
|
+
fix: `call the setter, \`${setter}(() => ${right})\``,
|
|
833
|
+
name
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
return {
|
|
837
|
+
consequence:
|
|
838
|
+
"the name no longer points at the reactive object and the reads after it lose its updates",
|
|
839
|
+
expression,
|
|
840
|
+
fix: `keep '${name}' and give the new value its own name, \`const merged = merge({ … }, ${name})\` for defaults`,
|
|
841
|
+
name
|
|
842
|
+
};
|
|
843
|
+
};
|
|
844
|
+
/**
|
|
845
|
+
* The fix for a read outside a tracked scope, with the code's names:
|
|
846
|
+
* `const x = () => <init>` for a declaration, `props.a` reads for a
|
|
847
|
+
* destructured `props`, else where the read belongs.
|
|
848
|
+
*/
|
|
849
|
+
const untrackedReadFix = (reported) => {
|
|
850
|
+
let child = reported;
|
|
851
|
+
while (
|
|
852
|
+
child.parent &&
|
|
853
|
+
child.parent.type !== "VariableDeclarator" &&
|
|
854
|
+
!isFunctionNode(child.parent) &&
|
|
855
|
+
!child.parent.type.endsWith("Statement")
|
|
856
|
+
) {
|
|
857
|
+
child = child.parent;
|
|
858
|
+
}
|
|
859
|
+
const declarator = child.parent;
|
|
860
|
+
const expression = quote(reported);
|
|
861
|
+
if (
|
|
862
|
+
declarator?.type === "VariableDeclarator" &&
|
|
863
|
+
declarator.init === child
|
|
864
|
+
) {
|
|
865
|
+
const init = quote(child);
|
|
866
|
+
if (declarator.id.type === "Identifier") {
|
|
867
|
+
const name = declarator.id.name;
|
|
868
|
+
return `make it a derived function, \`const ${name} = () => ${init};\`, and call \`${name}()\` where it is used (\`createMemo(() => ${init})\` when the work is costly, \`createSignal(() => ${init})\` for a local copy that can be edited)`;
|
|
869
|
+
}
|
|
870
|
+
if (
|
|
871
|
+
declarator.id.type === "ObjectPattern" &&
|
|
872
|
+
child === reported
|
|
873
|
+
) {
|
|
874
|
+
const reads = declarator.id.properties
|
|
875
|
+
.filter(
|
|
876
|
+
(property) =>
|
|
877
|
+
property.type === "Property" &&
|
|
878
|
+
property.key.type === "Identifier" &&
|
|
879
|
+
!property.computed
|
|
880
|
+
)
|
|
881
|
+
.map(
|
|
882
|
+
(property) =>
|
|
883
|
+
`\`${expression}.${property.key.name}\``
|
|
884
|
+
);
|
|
885
|
+
if (reads.length > 0) {
|
|
886
|
+
return `do not destructure; read ${reads.join(", ")} where each value is used`;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return `read \`${expression}\` where the value is used: in JSX, in a function \`() => ${expression}\`, in \`createMemo(() => ${expression})\` or in an effect's compute`;
|
|
891
|
+
};
|
|
892
|
+
/**
|
|
893
|
+
* The fix for a reactive read where a function is expected: wrap the
|
|
894
|
+
* expression, or pass a called function itself.
|
|
895
|
+
*/
|
|
896
|
+
const expectedFunctionFix = (scope) => {
|
|
897
|
+
const text = quote(scope);
|
|
898
|
+
if (isFunctionNode(scope)) {
|
|
899
|
+
return `pass the function itself, \`${text}\`, instead of calling it right away`;
|
|
900
|
+
}
|
|
901
|
+
// A member or a name may hold a function itself.
|
|
902
|
+
const called =
|
|
903
|
+
scope.type === "MemberExpression" || scope.type === "Identifier"
|
|
904
|
+
? ` (or \`() => ${text}()\` when \`${text}\` is itself a function)`
|
|
905
|
+
: "";
|
|
906
|
+
return `pass a function, \`() => ${text}\`${called}`;
|
|
907
|
+
};
|
|
908
|
+
/**
|
|
909
|
+
* What to pass a context provider instead of a read: the accessor of a
|
|
910
|
+
* call, a function around a property read.
|
|
911
|
+
*/
|
|
912
|
+
const providerSource = (reported) => {
|
|
913
|
+
if (reported.type === "CallExpression") {
|
|
914
|
+
return quote(reported.callee);
|
|
915
|
+
}
|
|
916
|
+
return reported.type === "MemberExpression"
|
|
917
|
+
? `() => ${quote(reported)}`
|
|
918
|
+
: quote(reported);
|
|
919
|
+
};
|
|
302
920
|
/**
|
|
303
921
|
* Inspects a specific reference of a reactive variable for correct
|
|
304
922
|
* handling.
|
|
@@ -339,19 +957,121 @@ export default {
|
|
|
339
957
|
identifier.parent?.type === "CallExpression"
|
|
340
958
|
? identifier.parent
|
|
341
959
|
: null;
|
|
960
|
+
// Reads inside a context provider's `value={...}` get a specific message; the
|
|
961
|
+
// value is read once, untracked, when the provider is created.
|
|
962
|
+
// A React-style dependency array (`createEffect(fn, [a, b])`)
|
|
963
|
+
// is no-react-deps' report, with the right fix; reporting
|
|
964
|
+
// each entry here too would suggest `() => [a, b]`.
|
|
965
|
+
const depsArray = findParent(
|
|
966
|
+
identifier,
|
|
967
|
+
(n) => n.type === "ArrayExpression"
|
|
968
|
+
);
|
|
969
|
+
if (
|
|
970
|
+
depsArray?.parent?.type === "CallExpression" &&
|
|
971
|
+
depsArray.parent.arguments.indexOf(depsArray) >= 1 &&
|
|
972
|
+
depsArray.parent.callee.type === "Identifier" &&
|
|
973
|
+
matchImport(
|
|
974
|
+
[
|
|
975
|
+
"createEffect",
|
|
976
|
+
"createRenderEffect",
|
|
977
|
+
"createMemo"
|
|
978
|
+
],
|
|
979
|
+
depsArray.parent.callee.name
|
|
980
|
+
)
|
|
981
|
+
) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
const inProviderValue = Boolean(
|
|
985
|
+
findParent(identifier, (n) =>
|
|
986
|
+
providerValueContainers.has(n)
|
|
987
|
+
)
|
|
988
|
+
);
|
|
989
|
+
// An `on*` handler on a native element is bound once; name
|
|
990
|
+
// the attribute and the fix.
|
|
991
|
+
const handlerAttribute = matchedExpression
|
|
992
|
+
? findParent(identifier, (n) => {
|
|
993
|
+
return (
|
|
994
|
+
n.type === "JSXAttribute" &&
|
|
995
|
+
n.name.type === "JSXIdentifier" &&
|
|
996
|
+
/^on[A-Z]/u.test(n.name.name) &&
|
|
997
|
+
n.parent?.name?.type === "JSXIdentifier" &&
|
|
998
|
+
isDOMElementName(n.parent.name.name)
|
|
999
|
+
);
|
|
1000
|
+
})
|
|
1001
|
+
: null;
|
|
1002
|
+
const handlerExpression =
|
|
1003
|
+
handlerAttribute?.value?.type ===
|
|
1004
|
+
"JSXExpressionContainer"
|
|
1005
|
+
? handlerAttribute.value.expression
|
|
1006
|
+
: null;
|
|
1007
|
+
const handlerText = handlerExpression
|
|
1008
|
+
? sourceCode.getText(handlerExpression)
|
|
1009
|
+
: "";
|
|
1010
|
+
// A copyable replacement with the code's own names.
|
|
1011
|
+
let handlerFix = `(event) => (${handlerText})(event)`;
|
|
1012
|
+
if (handlerExpression?.type === "ConditionalExpression") {
|
|
1013
|
+
handlerFix = `(event) => (${handlerText})?.(event)`;
|
|
1014
|
+
} else if (
|
|
1015
|
+
handlerExpression?.type === "LogicalExpression" &&
|
|
1016
|
+
handlerExpression.operator === "&&"
|
|
1017
|
+
) {
|
|
1018
|
+
const { left, right } = handlerExpression;
|
|
1019
|
+
const callee =
|
|
1020
|
+
right.type === "Identifier"
|
|
1021
|
+
? right.name
|
|
1022
|
+
: `(${sourceCode.getText(right)})`;
|
|
1023
|
+
handlerFix = `(event) => { if (${sourceCode.getText(left)}) ${callee}(event); }`;
|
|
1024
|
+
}
|
|
1025
|
+
let messageId = "untrackedReactive";
|
|
1026
|
+
if (inProviderValue) {
|
|
1027
|
+
messageId = "providerValue";
|
|
1028
|
+
} else if (handlerAttribute) {
|
|
1029
|
+
messageId = "handlerBinding";
|
|
1030
|
+
} else if (matchedExpression) {
|
|
1031
|
+
messageId = "expectedFunctionGotExpression";
|
|
1032
|
+
}
|
|
1033
|
+
const reported =
|
|
1034
|
+
parentMemberExpression ??
|
|
1035
|
+
parentCallExpression ??
|
|
1036
|
+
identifier;
|
|
1037
|
+
// What is read: the member or the call of the signal, or
|
|
1038
|
+
// the signal itself when it is passed as an argument.
|
|
1039
|
+
const read =
|
|
1040
|
+
parentMemberExpression ??
|
|
1041
|
+
(parentCallExpression?.callee === identifier
|
|
1042
|
+
? parentCallExpression
|
|
1043
|
+
: identifier);
|
|
1044
|
+
let data = {
|
|
1045
|
+
attribute: handlerAttribute?.name.name,
|
|
1046
|
+
expression: handlerText,
|
|
1047
|
+
fix: handlerFix,
|
|
1048
|
+
name: parentMemberExpression
|
|
1049
|
+
? sourceCode.getText(parentMemberExpression)
|
|
1050
|
+
: identifier.name
|
|
1051
|
+
};
|
|
1052
|
+
if (messageId === "untrackedReactive") {
|
|
1053
|
+
data = {
|
|
1054
|
+
...data,
|
|
1055
|
+
expression: quote(read, data.name),
|
|
1056
|
+
fix: untrackedReadFix(read)
|
|
1057
|
+
};
|
|
1058
|
+
} else if (messageId === "providerValue") {
|
|
1059
|
+
data = {
|
|
1060
|
+
...data,
|
|
1061
|
+
accessor: providerSource(read),
|
|
1062
|
+
expression: quote(read, data.name)
|
|
1063
|
+
};
|
|
1064
|
+
} else if (messageId === "expectedFunctionGotExpression") {
|
|
1065
|
+
data = {
|
|
1066
|
+
...data,
|
|
1067
|
+
fix: expectedFunctionFix(matchedExpression.node),
|
|
1068
|
+
scope: quote(matchedExpression.node)
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
342
1071
|
context.report({
|
|
343
|
-
node:
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
identifier,
|
|
347
|
-
messageId: matchedExpression
|
|
348
|
-
? "expectedFunctionGotExpression"
|
|
349
|
-
: "untrackedReactive",
|
|
350
|
-
data: {
|
|
351
|
-
name: parentMemberExpression
|
|
352
|
-
? sourceCode.getText(parentMemberExpression)
|
|
353
|
-
: identifier.name
|
|
354
|
-
}
|
|
1072
|
+
node: reported,
|
|
1073
|
+
messageId,
|
|
1074
|
+
data
|
|
355
1075
|
});
|
|
356
1076
|
} else {
|
|
357
1077
|
// If all of the reactive variables were declared above the current
|
|
@@ -361,6 +1081,12 @@ export default {
|
|
|
361
1081
|
if (!parentScope() || !isFunctionNode(currentScopeNode)) {
|
|
362
1082
|
throw new Error("this shouldn't happen!");
|
|
363
1083
|
}
|
|
1084
|
+
// Signal called at this function's setup level with its value
|
|
1085
|
+
// captured by a variable that a *returned* function reads: the
|
|
1086
|
+
// returned function will read a stale value forever. Report here —
|
|
1087
|
+
// treating the enclosing function as a derived signal (below) is
|
|
1088
|
+
// still correct, but no call-site discipline can fix the capture.
|
|
1089
|
+
checkStaleCapture(identifier);
|
|
364
1090
|
// If the current function doesn't have an associated variable, that's
|
|
365
1091
|
// fine, it's being used inline (i.e. anonymous arrow function). For
|
|
366
1092
|
// this to be okay, the arrow function has to be the same node as one
|
|
@@ -369,7 +1095,16 @@ export default {
|
|
|
369
1095
|
(parentScope().unnamedDerivedSignals ??= new Set()).add(
|
|
370
1096
|
currentScopeNode
|
|
371
1097
|
);
|
|
372
|
-
if (currentScopeNode
|
|
1098
|
+
if (isDirectlyReturned(currentScopeNode)) {
|
|
1099
|
+
// A function directly returned by its enclosing function is an
|
|
1100
|
+
// accessor handed to the caller — the custom-primitive contract.
|
|
1101
|
+
// The caller decides whether it lands in a tracked scope; there is
|
|
1102
|
+
// nowhere in this file for it to match a tracked scope, so
|
|
1103
|
+
// requiring one only punishes the idiomatic hook shape
|
|
1104
|
+
// (`return () => signal()`). Do nothing.
|
|
1105
|
+
} else if (
|
|
1106
|
+
currentScopeNode.type === "FunctionDeclaration"
|
|
1107
|
+
) {
|
|
373
1108
|
// get variable representing function, function node only defines one variable
|
|
374
1109
|
const functionVariable =
|
|
375
1110
|
sourceCode.scopeManager?.getDeclaredVariables(
|
|
@@ -401,7 +1136,10 @@ export default {
|
|
|
401
1136
|
} else {
|
|
402
1137
|
pushUnnamedDerivedSignal();
|
|
403
1138
|
}
|
|
404
|
-
} else if (
|
|
1139
|
+
} else if (
|
|
1140
|
+
getEffectivePropertyParent(currentScopeNode)?.type ===
|
|
1141
|
+
"Property"
|
|
1142
|
+
) {
|
|
405
1143
|
// todo make this a unique props or something--for now, just ignore (unsafe)
|
|
406
1144
|
} else {
|
|
407
1145
|
pushUnnamedDerivedSignal();
|
|
@@ -410,72 +1148,6 @@ export default {
|
|
|
410
1148
|
}
|
|
411
1149
|
};
|
|
412
1150
|
/** Performs all analysis and reporting. */
|
|
413
|
-
const asyncGapCache = new Map();
|
|
414
|
-
const ASYNC_GAP_TYPES = new Set(["AwaitExpression", "YieldExpression"]);
|
|
415
|
-
/**
|
|
416
|
-
* Where a function first suspends, or null if it never does.
|
|
417
|
-
*
|
|
418
|
-
* `yield` counts alongside `await`: an async generator driven by
|
|
419
|
-
* `action` suspends there in exactly the same way, and a read
|
|
420
|
-
* afterwards is just as detached. Nested functions are skipped — their
|
|
421
|
-
* gaps are their own, and a callback defined after an await still runs
|
|
422
|
-
* with its own tracking.
|
|
423
|
-
*/
|
|
424
|
-
const firstAsyncGapStart = (node) => {
|
|
425
|
-
if (!isFunctionNode(node) || node.async !== true) {
|
|
426
|
-
return null;
|
|
427
|
-
}
|
|
428
|
-
if (asyncGapCache.has(node)) {
|
|
429
|
-
return asyncGapCache.get(node);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
let earliest = null;
|
|
433
|
-
const pending = [node.body];
|
|
434
|
-
while (pending.length > 0) {
|
|
435
|
-
const current = pending.pop();
|
|
436
|
-
if (!current || typeof current !== "object") {
|
|
437
|
-
continue;
|
|
438
|
-
}
|
|
439
|
-
if (Array.isArray(current)) {
|
|
440
|
-
pending.push(...current);
|
|
441
|
-
continue;
|
|
442
|
-
}
|
|
443
|
-
if (typeof current.type !== "string") {
|
|
444
|
-
continue;
|
|
445
|
-
}
|
|
446
|
-
if (ASYNC_GAP_TYPES.has(current.type)) {
|
|
447
|
-
const start = Array.isArray(current.range)
|
|
448
|
-
? current.range[0]
|
|
449
|
-
: null;
|
|
450
|
-
if (
|
|
451
|
-
start !== null &&
|
|
452
|
-
(earliest === null || start < earliest)
|
|
453
|
-
) {
|
|
454
|
-
earliest = start;
|
|
455
|
-
}
|
|
456
|
-
// An await nested inside this one cannot start earlier.
|
|
457
|
-
continue;
|
|
458
|
-
}
|
|
459
|
-
for (const [key, value] of Object.entries(current)) {
|
|
460
|
-
if (key === "parent") {
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
if (
|
|
464
|
-
value &&
|
|
465
|
-
typeof value === "object" &&
|
|
466
|
-
!(
|
|
467
|
-
typeof value.type === "string" &&
|
|
468
|
-
isFunctionNode(value)
|
|
469
|
-
)
|
|
470
|
-
) {
|
|
471
|
-
pending.push(value);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
asyncGapCache.set(node, earliest);
|
|
477
|
-
return earliest;
|
|
478
|
-
};
|
|
479
1151
|
const onFunctionExit = (currentScopeNode) => {
|
|
480
1152
|
// If this function is a component, add its props as a reactive variable
|
|
481
1153
|
if (isFunctionNode(currentScopeNode)) {
|
|
@@ -501,40 +1173,27 @@ export default {
|
|
|
501
1173
|
) {
|
|
502
1174
|
return;
|
|
503
1175
|
}
|
|
504
|
-
//
|
|
505
|
-
//
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
//
|
|
511
|
-
//
|
|
512
|
-
|
|
1176
|
+
// If this scope is an async computation (Solid 2.0), reactive reads positioned
|
|
1177
|
+
// after its first suspension point resume outside the tracking window.
|
|
1178
|
+
const suspensionTaint = getSuspensionTaint(currentScopeNode);
|
|
1179
|
+
const isReadAfterSuspension = (identifier) =>
|
|
1180
|
+
suspensionTaint != null &&
|
|
1181
|
+
getStart(identifier) >= suspensionTaint;
|
|
1182
|
+
// Reads after the first `await` are reported by `reactive-async`,
|
|
1183
|
+
// which follows the runtime's UNTRACKED_READ_AFTER_AWAIT; here they
|
|
1184
|
+
// are only kept out of the tracked-scope analysis.
|
|
513
1185
|
// Iterate through all usages of (derived) signals in the current scope
|
|
514
1186
|
for (const {
|
|
515
1187
|
reference,
|
|
516
1188
|
declarationScope
|
|
517
1189
|
} of scopeStack.consumeSignalReferencesInScope()) {
|
|
518
1190
|
const identifier = reference.identifier;
|
|
519
|
-
if (
|
|
520
|
-
asyncGapStart !== null &&
|
|
521
|
-
Array.isArray(identifier.range) &&
|
|
522
|
-
identifier.range[0] > asyncGapStart
|
|
523
|
-
) {
|
|
524
|
-
context.report({
|
|
525
|
-
node: identifier,
|
|
526
|
-
messageId: "noAsyncTrackedScope",
|
|
527
|
-
data: { name: identifier.name }
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
1191
|
if (reference.isWrite()) {
|
|
531
1192
|
// don't allow reassigning signals
|
|
532
1193
|
context.report({
|
|
533
1194
|
node: identifier,
|
|
534
1195
|
messageId: "noWrite",
|
|
535
|
-
data:
|
|
536
|
-
name: identifier.name
|
|
537
|
-
}
|
|
1196
|
+
data: noWriteData(identifier, "signal")
|
|
538
1197
|
});
|
|
539
1198
|
} else if (identifier.type === "Identifier") {
|
|
540
1199
|
const reportBadSignal = (where) =>
|
|
@@ -550,8 +1209,10 @@ export default {
|
|
|
550
1209
|
(identifier.parent?.type === "ArrayExpression" &&
|
|
551
1210
|
identifier.parent.parent?.type === "CallExpression")
|
|
552
1211
|
) {
|
|
553
|
-
|
|
554
|
-
|
|
1212
|
+
if (!isReadAfterSuspension(identifier)) {
|
|
1213
|
+
// This signal is getting called properly, analyze it.
|
|
1214
|
+
handleTrackedScopes(identifier, declarationScope);
|
|
1215
|
+
}
|
|
555
1216
|
} else if (identifier.parent?.type === "TemplateLiteral") {
|
|
556
1217
|
reportBadSignal("template literals");
|
|
557
1218
|
} else if (
|
|
@@ -637,9 +1298,7 @@ export default {
|
|
|
637
1298
|
context.report({
|
|
638
1299
|
node: identifier,
|
|
639
1300
|
messageId: "noWrite",
|
|
640
|
-
data:
|
|
641
|
-
name: identifier.name
|
|
642
|
-
}
|
|
1301
|
+
data: noWriteData(identifier, "object")
|
|
643
1302
|
});
|
|
644
1303
|
} else if (
|
|
645
1304
|
identifier.parent?.type === "MemberExpression" &&
|
|
@@ -654,9 +1313,7 @@ export default {
|
|
|
654
1313
|
context.report({
|
|
655
1314
|
node: identifier,
|
|
656
1315
|
messageId: "noWrite",
|
|
657
|
-
data:
|
|
658
|
-
name: identifier.name
|
|
659
|
-
}
|
|
1316
|
+
data: noWriteData(identifier, "member")
|
|
660
1317
|
});
|
|
661
1318
|
} else if (
|
|
662
1319
|
parent.property.type === "Identifier" &&
|
|
@@ -669,21 +1326,32 @@ export default {
|
|
|
669
1326
|
// about untracked usages of these props, because the user has shown
|
|
670
1327
|
// that they understand the consequences of using a reactive
|
|
671
1328
|
// variable to initialize something else. Do nothing.
|
|
672
|
-
} else {
|
|
1329
|
+
} else if (!isReadAfterSuspension(identifier)) {
|
|
673
1330
|
// The props are the object in a property read access, which
|
|
674
1331
|
// should be under a tracked scope.
|
|
675
1332
|
handleTrackedScopes(identifier, declarationScope);
|
|
676
1333
|
}
|
|
677
1334
|
} else if (
|
|
678
|
-
identifier.
|
|
679
|
-
identifier.parent?.type === "
|
|
1335
|
+
identifier.type === "Identifier" &&
|
|
1336
|
+
(identifier.parent?.type === "AssignmentExpression" ||
|
|
1337
|
+
identifier.parent?.type === "VariableDeclarator")
|
|
680
1338
|
) {
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
1339
|
+
if (
|
|
1340
|
+
identifier.parent.type === "AssignmentExpression" &&
|
|
1341
|
+
identifier.parent.right === identifier &&
|
|
1342
|
+
identifier.parent.left.type === "MemberExpression"
|
|
1343
|
+
) {
|
|
1344
|
+
// `this.state = state` stores a *reference* to the proxy, not a
|
|
1345
|
+
// snapshot: property reads through it are still reactive at read
|
|
1346
|
+
// time. The alias escapes local analysis exactly like passing the
|
|
1347
|
+
// proxy as a call argument, which is already permitted (#184).
|
|
1348
|
+
// Do nothing.
|
|
1349
|
+
} else {
|
|
1350
|
+
// `... = props` is usually destructuring, which snapshots values instead of
|
|
1351
|
+
// staying reactive. Inside a tracked scope (or a function passed to one) the
|
|
1352
|
+
// destructuring re-runs on updates, so defer to the same analysis as reads.
|
|
1353
|
+
handleTrackedScopes(identifier, declarationScope);
|
|
1354
|
+
}
|
|
687
1355
|
}
|
|
688
1356
|
// The props are being read, but not in a MemberExpression. Since
|
|
689
1357
|
// there's a lot of possibilities here and they're generally fine,
|
|
@@ -700,9 +1368,21 @@ export default {
|
|
|
700
1368
|
matchTrackedScope(trackedScope, node)
|
|
701
1369
|
)
|
|
702
1370
|
) {
|
|
1371
|
+
const { callee, where } = describePlacement(
|
|
1372
|
+
node,
|
|
1373
|
+
"used here"
|
|
1374
|
+
);
|
|
703
1375
|
context.report({
|
|
704
1376
|
loc: getFunctionHeadLocation(node, sourceCode),
|
|
705
|
-
messageId: "badUnnamedDerivedSignal"
|
|
1377
|
+
messageId: "badUnnamedDerivedSignal",
|
|
1378
|
+
data: {
|
|
1379
|
+
// A callee that tracks its callback itself can
|
|
1380
|
+
// be declared to the rule.
|
|
1381
|
+
custom: callee
|
|
1382
|
+
? `; if \`${callee}\` calls it in a tracked scope of its own, list \`${callee}\` in the \`customReactiveFunctions\` option of \`solid/reactivity\``
|
|
1383
|
+
: "",
|
|
1384
|
+
where
|
|
1385
|
+
}
|
|
706
1386
|
});
|
|
707
1387
|
}
|
|
708
1388
|
}
|
|
@@ -713,11 +1393,11 @@ export default {
|
|
|
713
1393
|
/*
|
|
714
1394
|
* Sync array functions (forEach, map, reduce, reduceRight, flatMap),
|
|
715
1395
|
* store update fn params (ex. setState("todos", (t) => [...t.slice(0, i()),
|
|
716
|
-
* ...t.slice(i() + 1)])),
|
|
717
|
-
*
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
1396
|
+
* ...t.slice(i() + 1)])), flush, and maybe a few others don't actually
|
|
1397
|
+
* create a new scope. That is, any signal/prop accesses in these
|
|
1398
|
+
* functions act as if they happen in the enclosing function. Note that
|
|
1399
|
+
* this means whether or not the enclosing function is a tracking scope
|
|
1400
|
+
* applies to the fn param as well.
|
|
721
1401
|
*
|
|
722
1402
|
* Every time a sync callback is detected, we put that function node into a
|
|
723
1403
|
* syncCallbacks Set<FunctionNode>. The detections must happen on the entry pass
|
|
@@ -750,10 +1430,28 @@ export default {
|
|
|
750
1430
|
scopeStack.syncCallbacks.add(node.arguments[0]);
|
|
751
1431
|
}
|
|
752
1432
|
}
|
|
1433
|
+
if (
|
|
1434
|
+
// Array.from(iterable, mapFn) calls mapFn synchronously (#250)
|
|
1435
|
+
node.callee.type === "MemberExpression" &&
|
|
1436
|
+
!node.callee.computed &&
|
|
1437
|
+
node.callee.object.type === "Identifier" &&
|
|
1438
|
+
node.callee.object.name === "Array" &&
|
|
1439
|
+
node.callee.property.type === "Identifier" &&
|
|
1440
|
+
node.callee.property.name === "from" &&
|
|
1441
|
+
isFunctionNode(node.arguments[1]) &&
|
|
1442
|
+
!node.arguments[1].async
|
|
1443
|
+
) {
|
|
1444
|
+
scopeStack.syncCallbacks.add(node.arguments[1]);
|
|
1445
|
+
}
|
|
753
1446
|
if (node.callee.type === "Identifier") {
|
|
754
1447
|
if (
|
|
755
1448
|
matchImport(
|
|
756
|
-
[
|
|
1449
|
+
[
|
|
1450
|
+
"createSignal",
|
|
1451
|
+
"createStore",
|
|
1452
|
+
"createOptimistic",
|
|
1453
|
+
"createOptimisticStore"
|
|
1454
|
+
],
|
|
757
1455
|
node.callee.name
|
|
758
1456
|
) &&
|
|
759
1457
|
node.parent?.type === "VariableDeclarator"
|
|
@@ -808,40 +1506,48 @@ export default {
|
|
|
808
1506
|
init.callee.type === "Identifier"
|
|
809
1507
|
) {
|
|
810
1508
|
const { callee } = init;
|
|
811
|
-
if (
|
|
1509
|
+
if (
|
|
1510
|
+
matchImport(
|
|
1511
|
+
["createSignal", "createOptimistic"],
|
|
1512
|
+
callee.name
|
|
1513
|
+
)
|
|
1514
|
+
) {
|
|
812
1515
|
const signal = id && getNthDestructuredVar(id, 0, context);
|
|
813
1516
|
if (signal) {
|
|
814
1517
|
scopeStack.pushSignal(signal, currentScope().node);
|
|
815
1518
|
} else {
|
|
816
|
-
warnShouldDestructure(id ?? init,
|
|
1519
|
+
warnShouldDestructure(id ?? init, init);
|
|
817
1520
|
}
|
|
818
1521
|
} else if (
|
|
819
|
-
matchImport(
|
|
820
|
-
["createMemo", "createDeferred", "createProjection"],
|
|
821
|
-
callee.name
|
|
822
|
-
)
|
|
1522
|
+
matchImport(["createMemo", "createDeferred"], callee.name)
|
|
823
1523
|
) {
|
|
824
1524
|
const memo = id && getReturnedVar(id, context);
|
|
825
1525
|
// memos act like signals
|
|
826
1526
|
if (memo) {
|
|
827
1527
|
scopeStack.pushSignal(memo, currentScope().node);
|
|
828
1528
|
} else {
|
|
829
|
-
warnShouldAssign(id ?? init);
|
|
1529
|
+
warnShouldAssign(id ?? init, init);
|
|
830
1530
|
}
|
|
831
|
-
} else if (
|
|
1531
|
+
} else if (
|
|
1532
|
+
matchImport(
|
|
1533
|
+
["createStore", "createOptimisticStore"],
|
|
1534
|
+
callee.name
|
|
1535
|
+
)
|
|
1536
|
+
) {
|
|
832
1537
|
const store = id && getNthDestructuredVar(id, 0, context);
|
|
833
1538
|
// stores act like props
|
|
834
1539
|
if (store) {
|
|
835
1540
|
scopeStack.pushProps(store, currentScope().node);
|
|
836
1541
|
} else {
|
|
837
|
-
warnShouldDestructure(id ?? init,
|
|
1542
|
+
warnShouldDestructure(id ?? init, init);
|
|
838
1543
|
}
|
|
839
|
-
} else if (matchImport("merge", callee.name)) {
|
|
1544
|
+
} else if (matchImport(["merge", "omit"], callee.name)) {
|
|
1545
|
+
// omit (which replaces splitProps) also returns a single props object
|
|
840
1546
|
const merged = id && getReturnedVar(id, context);
|
|
841
1547
|
if (merged) {
|
|
842
1548
|
scopeStack.pushProps(merged, currentScope().node);
|
|
843
1549
|
} else {
|
|
844
|
-
warnShouldAssign(id ?? init);
|
|
1550
|
+
warnShouldAssign(id ?? init, init);
|
|
845
1551
|
}
|
|
846
1552
|
} else if (matchImport("splitProps", callee.name)) {
|
|
847
1553
|
// splitProps can return an unbounded array of props variables, though it's most often two
|
|
@@ -852,7 +1558,7 @@ export default {
|
|
|
852
1558
|
)
|
|
853
1559
|
.filter(Boolean);
|
|
854
1560
|
if (vars.length === 0) {
|
|
855
|
-
warnShouldDestructure(id);
|
|
1561
|
+
warnShouldDestructure(id, init);
|
|
856
1562
|
} else {
|
|
857
1563
|
vars.forEach((variable) => {
|
|
858
1564
|
scopeStack.pushProps(
|
|
@@ -868,24 +1574,11 @@ export default {
|
|
|
868
1574
|
scopeStack.pushProps(vars, currentScope().node);
|
|
869
1575
|
}
|
|
870
1576
|
}
|
|
871
|
-
} else if (
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
id && getNthDestructuredVar(id, 0, context);
|
|
877
|
-
if (resourceReturn) {
|
|
878
|
-
scopeStack.pushProps(
|
|
879
|
-
resourceReturn,
|
|
880
|
-
currentScope().node
|
|
881
|
-
);
|
|
882
|
-
}
|
|
883
|
-
} else if (
|
|
884
|
-
matchImport("__removed_createMutable", callee.name)
|
|
885
|
-
) {
|
|
886
|
-
const mutable = id && getReturnedVar(id, context);
|
|
887
|
-
if (mutable) {
|
|
888
|
-
scopeStack.pushProps(mutable, currentScope().node);
|
|
1577
|
+
} else if (matchImport("createProjection", callee.name)) {
|
|
1578
|
+
// createProjection returns a readonly derived store
|
|
1579
|
+
const projection = id && getReturnedVar(id, context);
|
|
1580
|
+
if (projection) {
|
|
1581
|
+
scopeStack.pushProps(projection, currentScope().node);
|
|
889
1582
|
}
|
|
890
1583
|
} else if (matchImport("mapArray", callee.name)) {
|
|
891
1584
|
const arg1 = init.arguments[1];
|
|
@@ -902,27 +1595,39 @@ export default {
|
|
|
902
1595
|
scopeStack.pushSignal(indexSignal);
|
|
903
1596
|
}
|
|
904
1597
|
}
|
|
905
|
-
} else if (matchImport("__removed_indexArray", callee.name)) {
|
|
906
|
-
const arg1 = init.arguments[1];
|
|
907
|
-
if (
|
|
908
|
-
isFunctionNode(arg1) &&
|
|
909
|
-
arg1.params.length >= 1 &&
|
|
910
|
-
arg1.params[0].type === "Identifier"
|
|
911
|
-
) {
|
|
912
|
-
const valueSignal = findVariable(
|
|
913
|
-
context,
|
|
914
|
-
arg1.params[0]
|
|
915
|
-
);
|
|
916
|
-
if (valueSignal) {
|
|
917
|
-
scopeStack.pushSignal(valueSignal);
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
1598
|
}
|
|
921
1599
|
}
|
|
922
1600
|
};
|
|
923
1601
|
const checkForTrackedScopes = (node) => {
|
|
924
|
-
const pushTrackedScope = (node, expect) => {
|
|
1602
|
+
const pushTrackedScope = (node, expect, allowAsync = false) => {
|
|
925
1603
|
currentScope().trackedScopes.push({ node, expect });
|
|
1604
|
+
if (
|
|
1605
|
+
expect !== "called-function" &&
|
|
1606
|
+
isFunctionNode(node) &&
|
|
1607
|
+
(node.async || node.generator)
|
|
1608
|
+
) {
|
|
1609
|
+
if (allowAsync) {
|
|
1610
|
+
// Async computations are first-class in Solid 2.0 (async createMemo,
|
|
1611
|
+
// function-form createSignal/createStore, etc.). Reads before the first
|
|
1612
|
+
// suspension point are tracked normally; reads after it get a targeted
|
|
1613
|
+
// reactive-async report on reads after the first await instead of a blanket warning.
|
|
1614
|
+
asyncTrackedScopes.add(node);
|
|
1615
|
+
} else if (node.async) {
|
|
1616
|
+
// "[Solid's] approach only tracks synchronously. If you have a
|
|
1617
|
+
// setTimeout or use an async function in your Effect the code that
|
|
1618
|
+
// executes async after the fact won't be tracked."
|
|
1619
|
+
context.report({
|
|
1620
|
+
node,
|
|
1621
|
+
messageId: "noAsyncTrackedScope",
|
|
1622
|
+
data: {
|
|
1623
|
+
where: describePlacement(
|
|
1624
|
+
node,
|
|
1625
|
+
"a tracked scope"
|
|
1626
|
+
).where
|
|
1627
|
+
}
|
|
1628
|
+
});
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
926
1631
|
};
|
|
927
1632
|
// given some expression, mark any functions within it as tracking scopes, and do not traverse
|
|
928
1633
|
// those functions
|
|
@@ -939,6 +1644,19 @@ export default {
|
|
|
939
1644
|
!(
|
|
940
1645
|
traced.parent.type === "CallExpression" &&
|
|
941
1646
|
traced.parent.callee === traced
|
|
1647
|
+
)) ||
|
|
1648
|
+
// memo accessors traced to their initializing call, e.g. passing a variable
|
|
1649
|
+
// holding a createMemo result — treat like passing a signal
|
|
1650
|
+
(traced.type === "CallExpression" &&
|
|
1651
|
+
traced.callee.type === "Identifier" &&
|
|
1652
|
+
matchImport(
|
|
1653
|
+
[
|
|
1654
|
+
"createMemo",
|
|
1655
|
+
"createDeferred",
|
|
1656
|
+
"children",
|
|
1657
|
+
"createProjection"
|
|
1658
|
+
],
|
|
1659
|
+
traced.callee.name
|
|
942
1660
|
))
|
|
943
1661
|
) {
|
|
944
1662
|
pushTrackedScope(childNode, "called-function");
|
|
@@ -981,20 +1699,13 @@ export default {
|
|
|
981
1699
|
node.parent?.type === "JSXAttribute" &&
|
|
982
1700
|
node.parent.name.name === "value" &&
|
|
983
1701
|
node.parent.parent?.type === "JSXOpeningElement" &&
|
|
984
|
-
(
|
|
985
|
-
node.parent.parent.name.name.endsWith("Provider")) ||
|
|
986
|
-
(node.parent.parent.name.type ===
|
|
987
|
-
"JSXMemberExpression" &&
|
|
988
|
-
node.parent.parent.name.property.name ===
|
|
989
|
-
"Provider"))
|
|
1702
|
+
isProviderElementName(node.parent.parent.name)
|
|
990
1703
|
) {
|
|
991
|
-
//
|
|
992
|
-
//
|
|
993
|
-
//
|
|
994
|
-
//
|
|
995
|
-
|
|
996
|
-
// TODO: add some kind of "anti- tracked scope" that still warns but enhances the error
|
|
997
|
-
// message if matched.
|
|
1704
|
+
// Context providers read `props.value` once, untracked, when they are created, so
|
|
1705
|
+
// a reactive expression here is frozen at its initial value. Don't push a tracked
|
|
1706
|
+
// scope; record the container so reads inside it get the specific `providerValue`
|
|
1707
|
+
// message instead of the generic one.
|
|
1708
|
+
providerValueContainers.add(node);
|
|
998
1709
|
} else if (
|
|
999
1710
|
node.parent?.type === "JSXAttribute" &&
|
|
1000
1711
|
node.parent.name?.type === "JSXIdentifier" &&
|
|
@@ -1057,40 +1768,20 @@ export default {
|
|
|
1057
1768
|
arguments: { 0: arg0, 1: arg1 }
|
|
1058
1769
|
} = node;
|
|
1059
1770
|
if (
|
|
1060
|
-
matchImport(
|
|
1061
|
-
[
|
|
1062
|
-
"createMemo",
|
|
1063
|
-
// An async memo: the callback may return a promise.
|
|
1064
|
-
"createDeferred",
|
|
1065
|
-
"children",
|
|
1066
|
-
"createTrackedEffect",
|
|
1067
|
-
"createProjection",
|
|
1068
|
-
"untrack",
|
|
1069
|
-
"mapArray",
|
|
1070
|
-
"isPending",
|
|
1071
|
-
"latest",
|
|
1072
|
-
"resolve",
|
|
1073
|
-
// The predicate is re-evaluated reactively until it is truthy.
|
|
1074
|
-
"until"
|
|
1075
|
-
],
|
|
1076
|
-
callee.name
|
|
1077
|
-
) ||
|
|
1078
|
-
false /* Solid 2.0 removed createResource */
|
|
1079
|
-
) {
|
|
1080
|
-
// The whole first argument is the tracked function.
|
|
1081
|
-
pushTrackedScope(arg0, "function");
|
|
1082
|
-
} else if (
|
|
1083
1771
|
matchImport(
|
|
1084
1772
|
["createEffect", "createRenderEffect"],
|
|
1085
1773
|
callee.name
|
|
1086
1774
|
)
|
|
1087
1775
|
) {
|
|
1088
1776
|
// Solid 2.0 split these into `(compute, apply)`. The
|
|
1089
|
-
// compute phase tracks
|
|
1777
|
+
// compute phase tracks and, like any computation, may
|
|
1778
|
+
// be async (`reactive-async` checks it). A lone function
|
|
1779
|
+
// is reported by no-single-arg-create-effect, whether
|
|
1780
|
+
// async or not. The apply phase runs untracked
|
|
1090
1781
|
// and may return a cleanup, so reads there poll rather
|
|
1091
1782
|
// than subscribe. `createEffect` also accepts an effect
|
|
1092
1783
|
// bundle object in place of a plain apply function.
|
|
1093
|
-
pushTrackedScope(arg0, "function");
|
|
1784
|
+
pushTrackedScope(arg0, "function", true);
|
|
1094
1785
|
if (arg1) {
|
|
1095
1786
|
if (arg1.type === "ObjectExpression") {
|
|
1096
1787
|
for (const property of arg1.properties) {
|
|
@@ -1108,50 +1799,109 @@ export default {
|
|
|
1108
1799
|
pushTrackedScope(arg1, "called-function");
|
|
1109
1800
|
}
|
|
1110
1801
|
}
|
|
1111
|
-
} else if (
|
|
1802
|
+
} else if (
|
|
1803
|
+
matchImport(
|
|
1804
|
+
[
|
|
1805
|
+
"createMemo",
|
|
1806
|
+
"children",
|
|
1807
|
+
"createDeferred",
|
|
1808
|
+
"mapArray",
|
|
1809
|
+
"createTrackedEffect",
|
|
1810
|
+
"isPending",
|
|
1811
|
+
"latest",
|
|
1812
|
+
"resolve",
|
|
1813
|
+
"deep",
|
|
1814
|
+
"repeat",
|
|
1815
|
+
"createErrorBoundary",
|
|
1816
|
+
"createLoadingBoundary",
|
|
1817
|
+
"createRevealOrder",
|
|
1818
|
+
"dynamic",
|
|
1819
|
+
"dynamicComponent",
|
|
1820
|
+
"until"
|
|
1821
|
+
],
|
|
1822
|
+
callee.name
|
|
1823
|
+
)
|
|
1824
|
+
) {
|
|
1825
|
+
// createMemo, createDeferred, dynamic, and dynamicComponent may take an
|
|
1826
|
+
// async function; only reads before its first `await` are tracked.
|
|
1827
|
+
// until(fn) tracks its predicate until it is truthy.
|
|
1828
|
+
pushTrackedScope(
|
|
1829
|
+
arg0,
|
|
1830
|
+
"function",
|
|
1831
|
+
Boolean(
|
|
1832
|
+
matchImport(
|
|
1833
|
+
[
|
|
1834
|
+
"createMemo",
|
|
1835
|
+
"createDeferred",
|
|
1836
|
+
"dynamic",
|
|
1837
|
+
"dynamicComponent"
|
|
1838
|
+
],
|
|
1839
|
+
callee.name
|
|
1840
|
+
)
|
|
1841
|
+
)
|
|
1842
|
+
);
|
|
1843
|
+
if (
|
|
1844
|
+
matchImport(
|
|
1845
|
+
[
|
|
1846
|
+
"createErrorBoundary",
|
|
1847
|
+
"createLoadingBoundary"
|
|
1848
|
+
],
|
|
1849
|
+
callee.name
|
|
1850
|
+
) &&
|
|
1851
|
+
arg1
|
|
1852
|
+
) {
|
|
1853
|
+
// Boundary fallbacks are reactive accessors; the error fallback
|
|
1854
|
+
// also receives (err, reset) arguments
|
|
1855
|
+
pushTrackedScope(arg1, "called-function");
|
|
1856
|
+
}
|
|
1857
|
+
} else if (matchImport("untrack", callee.name) && arg0) {
|
|
1858
|
+
// untrack's callback is called synchronously but explicitly NOT
|
|
1859
|
+
// tracked: reads inside are sanctioned, and an async callback is
|
|
1860
|
+
// harmless (there's no subscription to lose), so it must not get
|
|
1861
|
+
// the async-tracked-scope or read-after-await treatment (#188).
|
|
1862
|
+
pushTrackedScope(arg0, "called-function");
|
|
1863
|
+
} else if (matchImport("merge", callee.name)) {
|
|
1864
|
+
// merge wraps function sources in createMemo, so function arguments
|
|
1865
|
+
// are genuinely tracked scopes.
|
|
1866
|
+
for (const arg of node.arguments) {
|
|
1867
|
+
if (arg.type === "SpreadElement") continue;
|
|
1868
|
+
if (isFunctionNode(trace(arg, context))) {
|
|
1869
|
+
pushTrackedScope(arg, "function");
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
} else if (
|
|
1873
|
+
matchImport(
|
|
1874
|
+
[
|
|
1875
|
+
"createSignal",
|
|
1876
|
+
"createStore",
|
|
1877
|
+
"createProjection",
|
|
1878
|
+
"createOptimistic",
|
|
1879
|
+
"createOptimisticStore"
|
|
1880
|
+
],
|
|
1881
|
+
callee.name
|
|
1882
|
+
) &&
|
|
1883
|
+
isFunctionNode(arg0)
|
|
1884
|
+
) {
|
|
1885
|
+
// Function-form derived primitives: the function argument is a tracked
|
|
1886
|
+
// scope, and may be async (only reads before the first `await` are
|
|
1887
|
+
// tracked).
|
|
1888
|
+
pushTrackedScope(arg0, "function", true);
|
|
1889
|
+
} else if (matchImport("createReaction", callee.name)) {
|
|
1112
1890
|
// `createReaction(effectFn)` takes the untracked effect
|
|
1113
1891
|
// and returns the tracking function.
|
|
1114
1892
|
pushTrackedScope(arg0, "called-function");
|
|
1115
1893
|
} else if (
|
|
1116
1894
|
matchImport(
|
|
1117
|
-
["onSettled", "onFlush", "
|
|
1895
|
+
["onCleanup", "onSettled", "onFlush", "action"],
|
|
1118
1896
|
callee.name
|
|
1119
1897
|
) ||
|
|
1120
|
-
|
|
1121
|
-
// Timers
|
|
1122
|
-
"setInterval",
|
|
1123
|
-
"setTimeout",
|
|
1124
|
-
"setImmediate",
|
|
1125
|
-
"requestAnimationFrame",
|
|
1126
|
-
"requestIdleCallback"
|
|
1127
|
-
].includes(callee.name)
|
|
1898
|
+
TIMERS.includes(callee.name)
|
|
1128
1899
|
) {
|
|
1129
1900
|
// on* and timers are NOT tracked scopes. However, they
|
|
1130
1901
|
// don't need to react to updates to reactive variables; it's okay
|
|
1131
1902
|
// to poll the current value. Consider them called-function tracked
|
|
1132
1903
|
// scopes for our purposes.
|
|
1133
1904
|
pushTrackedScope(arg0, "called-function");
|
|
1134
|
-
} else if (matchImport("__removed_on", callee.name)) {
|
|
1135
|
-
// on accepts a signal or an array of signals as its first argument,
|
|
1136
|
-
// and a tracking function as its second
|
|
1137
|
-
if (arg0) {
|
|
1138
|
-
if (arg0.type === "ArrayExpression") {
|
|
1139
|
-
arg0.elements.forEach((element) => {
|
|
1140
|
-
if (
|
|
1141
|
-
element &&
|
|
1142
|
-
element?.type !== "SpreadElement"
|
|
1143
|
-
) {
|
|
1144
|
-
pushTrackedScope(element, "function");
|
|
1145
|
-
}
|
|
1146
|
-
});
|
|
1147
|
-
} else {
|
|
1148
|
-
pushTrackedScope(arg0, "function");
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
if (arg1) {
|
|
1152
|
-
// Since dependencies are known, function can be async
|
|
1153
|
-
pushTrackedScope(arg1, "called-function");
|
|
1154
|
-
}
|
|
1155
1905
|
} else if (
|
|
1156
1906
|
matchImport("createStore", callee.name) &&
|
|
1157
1907
|
arg0?.type === "ObjectExpression"
|
|
@@ -1221,7 +1971,7 @@ export default {
|
|
|
1221
1971
|
}
|
|
1222
1972
|
} else if (
|
|
1223
1973
|
/^(?:use|create)[A-Z]/.test(callee.name) ||
|
|
1224
|
-
|
|
1974
|
+
matchesCustomReactive(callee.name)
|
|
1225
1975
|
) {
|
|
1226
1976
|
// Custom hooks parameters may or may not be tracking scopes, no way to know.
|
|
1227
1977
|
// Assume all identifier/function arguments are tracked scopes, and use "called-function"
|
|
@@ -1229,6 +1979,25 @@ export default {
|
|
|
1229
1979
|
for (const arg of node.arguments) {
|
|
1230
1980
|
permissivelyTrackNode(arg);
|
|
1231
1981
|
}
|
|
1982
|
+
} else if (
|
|
1983
|
+
parentScope()?.trackedScopes.some(
|
|
1984
|
+
(trackedScope) =>
|
|
1985
|
+
(trackedScope.expect === "function" ||
|
|
1986
|
+
trackedScope.expect ===
|
|
1987
|
+
"called-function") &&
|
|
1988
|
+
trackedScope.node === currentScope().node
|
|
1989
|
+
)
|
|
1990
|
+
) {
|
|
1991
|
+
// An unknown call inside a tracked scope (e.g. a helper called in an effect
|
|
1992
|
+
// callback). Functions passed to it either run synchronously — still inside the
|
|
1993
|
+
// tracked scope — or run later, where polling current values is fine, like any
|
|
1994
|
+
// called function. Known Solid APIs that intentionally decline to track their
|
|
1995
|
+
// callbacks (like runWithOwner with an untracked owner) match earlier branches.
|
|
1996
|
+
for (const arg of node.arguments) {
|
|
1997
|
+
if (isFunctionNode(arg)) {
|
|
1998
|
+
pushTrackedScope(arg, "called-function");
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
1232
2001
|
}
|
|
1233
2002
|
} else if (node.callee.type === "MemberExpression") {
|
|
1234
2003
|
const { property } = node.callee;
|
|
@@ -1239,12 +2008,38 @@ export default {
|
|
|
1239
2008
|
) {
|
|
1240
2009
|
// Like `on*` event handlers, mark all `addEventListener` listeners as called functions.
|
|
1241
2010
|
pushTrackedScope(node.arguments[1], "called-function");
|
|
2011
|
+
} else if (
|
|
2012
|
+
property.type === "Identifier" &&
|
|
2013
|
+
node.callee.object.type === "Identifier" &&
|
|
2014
|
+
["window", "globalThis", "self"].includes(
|
|
2015
|
+
node.callee.object.name
|
|
2016
|
+
) &&
|
|
2017
|
+
TIMERS.includes(property.name)
|
|
2018
|
+
) {
|
|
2019
|
+
// `window.setTimeout` etc. behave exactly like the bare timer globals above:
|
|
2020
|
+
// callbacks are called functions, free to poll current reactive values.
|
|
2021
|
+
if (node.arguments[0]) {
|
|
2022
|
+
pushTrackedScope(
|
|
2023
|
+
node.arguments[0],
|
|
2024
|
+
"called-function"
|
|
2025
|
+
);
|
|
2026
|
+
}
|
|
2027
|
+
} else if (
|
|
2028
|
+
property.type === "Identifier" &&
|
|
2029
|
+
["then", "catch", "finally"].includes(property.name)
|
|
2030
|
+
) {
|
|
2031
|
+
// Promise callbacks run after the current tick, like timer callbacks:
|
|
2032
|
+
// they poll current values rather than subscribing, exactly as the same
|
|
2033
|
+
// code would after an `await` (#235).
|
|
2034
|
+
for (const arg of node.arguments) {
|
|
2035
|
+
if (arg.type !== "SpreadElement") {
|
|
2036
|
+
pushTrackedScope(arg, "called-function");
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
1242
2039
|
} else if (
|
|
1243
2040
|
property.type === "Identifier" &&
|
|
1244
2041
|
(/^(?:use|create)[A-Z]/.test(property.name) ||
|
|
1245
|
-
|
|
1246
|
-
property.name
|
|
1247
|
-
))
|
|
2042
|
+
matchesCustomReactive(property.name))
|
|
1248
2043
|
) {
|
|
1249
2044
|
// Handle custom hook parameters for property access custom hooks
|
|
1250
2045
|
for (const arg of node.arguments) {
|
|
@@ -1253,18 +2048,15 @@ export default {
|
|
|
1253
2048
|
}
|
|
1254
2049
|
}
|
|
1255
2050
|
} else if (node.type === "VariableDeclarator") {
|
|
1256
|
-
//
|
|
1257
|
-
// function
|
|
1258
|
-
//
|
|
2051
|
+
// createReaction returns a track function, a tracked scope expecting a
|
|
2052
|
+
// reactive function. All of the track function's references where it's
|
|
2053
|
+
// called push a tracked scope.
|
|
1259
2054
|
if (
|
|
1260
2055
|
node.init?.type === "CallExpression" &&
|
|
1261
2056
|
node.init.callee.type === "Identifier"
|
|
1262
2057
|
) {
|
|
1263
2058
|
if (
|
|
1264
|
-
matchImport(
|
|
1265
|
-
["createReactive", "createReaction"],
|
|
1266
|
-
node.init.callee.name
|
|
1267
|
-
)
|
|
2059
|
+
matchImport(["createReaction"], node.init.callee.name)
|
|
1268
2060
|
) {
|
|
1269
2061
|
const track = getReturnedVar(node.id, context);
|
|
1270
2062
|
if (track) {
|
|
@@ -1345,14 +2137,55 @@ export default {
|
|
|
1345
2137
|
CallExpression(node) {
|
|
1346
2138
|
checkForTrackedScopes(node);
|
|
1347
2139
|
checkForSyncCallbacks(node);
|
|
1348
|
-
// ensure calls to reactive primitives use the results.
|
|
2140
|
+
// ensure calls to reactive primitives use the results. Directly-returned calls
|
|
2141
|
+
// (`return createMemo(...)` or as an arrow body) hand their result to the caller,
|
|
2142
|
+
// like a custom primitive, so there is nothing to capture in this scope.
|
|
1349
2143
|
const parent =
|
|
1350
2144
|
node.parent && ignoreTransparentWrappers(node.parent, true);
|
|
1351
2145
|
if (
|
|
1352
2146
|
parent?.type !== "AssignmentExpression" &&
|
|
1353
|
-
parent?.type !== "VariableDeclarator"
|
|
2147
|
+
parent?.type !== "VariableDeclarator" &&
|
|
2148
|
+
parent?.type !== "ReturnStatement" &&
|
|
2149
|
+
!(
|
|
2150
|
+
parent?.type === "ArrowFunctionExpression" &&
|
|
2151
|
+
parent.body === node
|
|
2152
|
+
)
|
|
1354
2153
|
) {
|
|
1355
|
-
|
|
2154
|
+
if (
|
|
2155
|
+
parent?.type === "CallExpression" &&
|
|
2156
|
+
parent.arguments.includes(node)
|
|
2157
|
+
) {
|
|
2158
|
+
// A primitive call passed to a wrapper, like
|
|
2159
|
+
// `const [state, setState] = makePersisted(createSignal(false))`
|
|
2160
|
+
// (#190). The dominant ecosystem contract passes the tuple through,
|
|
2161
|
+
// so if the outermost wrapper's result is captured, analyze the
|
|
2162
|
+
// primitive as if assigned there directly. If the result isn't
|
|
2163
|
+
// captured, the tuple escaped into the wrapper — nothing can be
|
|
2164
|
+
// concluded, so stay silent rather than warn.
|
|
2165
|
+
let outer = parent;
|
|
2166
|
+
let outerParent =
|
|
2167
|
+
outer.parent &&
|
|
2168
|
+
ignoreTransparentWrappers(outer.parent, true);
|
|
2169
|
+
while (
|
|
2170
|
+
outerParent?.type === "CallExpression" &&
|
|
2171
|
+
outerParent.arguments.includes(outer)
|
|
2172
|
+
) {
|
|
2173
|
+
outer = outerParent;
|
|
2174
|
+
outerParent =
|
|
2175
|
+
outer.parent &&
|
|
2176
|
+
ignoreTransparentWrappers(outer.parent, true);
|
|
2177
|
+
}
|
|
2178
|
+
if (outerParent?.type === "VariableDeclarator") {
|
|
2179
|
+
checkForReactiveAssignment(outerParent.id, node);
|
|
2180
|
+
} else if (
|
|
2181
|
+
outerParent?.type === "AssignmentExpression" &&
|
|
2182
|
+
outerParent.left.type !== "MemberExpression"
|
|
2183
|
+
) {
|
|
2184
|
+
checkForReactiveAssignment(outerParent.left, node);
|
|
2185
|
+
}
|
|
2186
|
+
} else {
|
|
2187
|
+
checkForReactiveAssignment(null, node);
|
|
2188
|
+
}
|
|
1356
2189
|
}
|
|
1357
2190
|
},
|
|
1358
2191
|
NewExpression(node) {
|
|
@@ -1380,27 +2213,49 @@ export default {
|
|
|
1380
2213
|
node.parent.parent?.type === "JSXElement"
|
|
1381
2214
|
) {
|
|
1382
2215
|
const element = node.parent.parent;
|
|
1383
|
-
if (
|
|
1384
|
-
|
|
2216
|
+
if (
|
|
2217
|
+
element.openingElement.name.type === "JSXIdentifier" &&
|
|
2218
|
+
matchImport("For", element.openingElement.name.name)
|
|
2219
|
+
) {
|
|
2220
|
+
// <For> callback params depend on the `keyed` prop:
|
|
2221
|
+
// - absent, bare `keyed`, or `keyed={true}`: (item, index-accessor)
|
|
2222
|
+
// - `keyed={false}`: (item-accessor, index)
|
|
2223
|
+
// - `keyed={fn}`: (item-accessor, index-accessor)
|
|
2224
|
+
const keyedAttr =
|
|
2225
|
+
element.openingElement.attributes.find(
|
|
2226
|
+
(attr) =>
|
|
2227
|
+
attr.type === "JSXAttribute" &&
|
|
2228
|
+
attr.name.type === "JSXIdentifier" &&
|
|
2229
|
+
attr.name.name === "keyed"
|
|
2230
|
+
);
|
|
2231
|
+
let itemIsSignal = false;
|
|
2232
|
+
let indexIsSignal = true;
|
|
1385
2233
|
if (
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
node.params[1].type === "Identifier"
|
|
2234
|
+
keyedAttr &&
|
|
2235
|
+
keyedAttr.value?.type === "JSXExpressionContainer"
|
|
1389
2236
|
) {
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
2237
|
+
const keyedValue = keyedAttr.value.expression;
|
|
2238
|
+
if (
|
|
2239
|
+
keyedValue.type === "Literal" &&
|
|
2240
|
+
keyedValue.value === false
|
|
2241
|
+
) {
|
|
2242
|
+
itemIsSignal = true;
|
|
2243
|
+
indexIsSignal = false;
|
|
2244
|
+
} else if (
|
|
2245
|
+
!(
|
|
2246
|
+
keyedValue.type === "Literal" &&
|
|
2247
|
+
keyedValue.value === true
|
|
2248
|
+
)
|
|
2249
|
+
) {
|
|
2250
|
+
// key function: both params are accessors
|
|
2251
|
+
itemIsSignal = true;
|
|
1397
2252
|
}
|
|
1398
|
-
}
|
|
1399
|
-
|
|
2253
|
+
}
|
|
2254
|
+
if (
|
|
2255
|
+
itemIsSignal &&
|
|
1400
2256
|
node.params.length >= 1 &&
|
|
1401
2257
|
node.params[0].type === "Identifier"
|
|
1402
2258
|
) {
|
|
1403
|
-
// Mark `item` in `<Index>{(item, index) => <div />}</Index>` as a signal
|
|
1404
2259
|
const item = findVariable(context, node.params[0]);
|
|
1405
2260
|
if (item) {
|
|
1406
2261
|
scopeStack.pushSignal(
|
|
@@ -1409,6 +2264,20 @@ export default {
|
|
|
1409
2264
|
);
|
|
1410
2265
|
}
|
|
1411
2266
|
}
|
|
2267
|
+
if (
|
|
2268
|
+
indexIsSignal &&
|
|
2269
|
+
node.params.length === 2 &&
|
|
2270
|
+
node.params[1].type === "Identifier"
|
|
2271
|
+
) {
|
|
2272
|
+
// Mark `index` in `<For>{(item, index) => <div /></For>` as a signal
|
|
2273
|
+
const index = findVariable(context, node.params[1]);
|
|
2274
|
+
if (index) {
|
|
2275
|
+
scopeStack.pushSignal(
|
|
2276
|
+
index,
|
|
2277
|
+
currentScope().node
|
|
2278
|
+
);
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
1412
2281
|
}
|
|
1413
2282
|
}
|
|
1414
2283
|
},
|