@murky-web/oxlint-plugin-solid 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -18
- package/THIRD_PARTY_NOTICES.md +7 -4
- package/package.json +5 -2
- package/src/compat.mjs +3 -0
- package/src/index.mjs +26 -3
- package/src/rules/components_return_once.mjs +136 -89
- package/src/rules/event_handlers.mjs +55 -35
- package/src/rules/filename_matches_export.mjs +292 -0
- package/src/rules/function_size.mjs +189 -0
- package/src/rules/imports.mjs +24 -21
- package/src/rules/init_declarations.mjs +139 -0
- package/src/rules/jsx_no_duplicate_props.mjs +46 -18
- package/src/rules/jsx_no_script_url.mjs +7 -6
- package/src/rules/jsx_no_undef.mjs +35 -38
- package/src/rules/jsx_uses_vars.mjs +4 -4
- package/src/rules/no_array_handlers.mjs +38 -9
- package/src/rules/no_cleanup_in_effect_apply.mjs +154 -0
- package/src/rules/no_destructure.mjs +105 -35
- package/src/rules/no_innerhtml.mjs +64 -31
- package/src/rules/no_module_scope_reactive_primitive.mjs +73 -0
- package/src/rules/no_owned_primitives_in_ref.mjs +30 -9
- package/src/rules/no_proxy_apis.mjs +44 -15
- package/src/rules/no_react_deps.mjs +397 -41
- package/src/rules/no_react_specific_props.mjs +40 -11
- package/src/rules/no_server_import_in_client.mjs +9 -9
- package/src/rules/no_setter_in_effect.mjs +153 -30
- package/src/rules/no_single_arg_create_effect.mjs +55 -0
- package/src/rules/no_unassigned_vars.mjs +99 -0
- package/src/rules/no_unknown_namespaces.mjs +108 -29
- package/src/rules/no_untracked_effect_read.mjs +69 -32
- package/src/rules/no_write_in_pure_computation.mjs +273 -0
- package/src/rules/prefer_arrow_components.mjs +47 -21
- package/src/rules/prefer_class_object.mjs +52 -20
- package/src/rules/prefer_destructuring.mjs +164 -0
- package/src/rules/prefer_for.mjs +42 -12
- package/src/rules/prefer_ref_shorthand.mjs +142 -0
- package/src/rules/prefer_show.mjs +55 -24
- package/src/rules/reactive_async.mjs +64 -17
- package/src/rules/reactivity.mjs +1218 -350
- package/src/rules/self_closing_comp.mjs +23 -18
- package/src/rules/strict_void_return.mjs +50 -20
- package/src/rules/style_prop.mjs +53 -25
- package/src/rules/typed_merge_defaults.mjs +88 -0
- package/src/rules/validate_jsx_nesting.mjs +16 -16
- package/src/utils.mjs +65 -34
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,
|
|
@@ -19,11 +39,9 @@ import {
|
|
|
19
39
|
getFunctionName,
|
|
20
40
|
isJSXElementOrFragment,
|
|
21
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;
|
|
@@ -49,10 +67,10 @@ class ScopeStack extends Array {
|
|
|
49
67
|
pushSignal(variable, declarationScope = this.currentScope().node) {
|
|
50
68
|
this.signals.push({
|
|
51
69
|
references: variable.references.filter(
|
|
52
|
-
(reference) => !reference.init
|
|
70
|
+
(reference) => !reference.init
|
|
53
71
|
),
|
|
54
72
|
variable,
|
|
55
|
-
declarationScope
|
|
73
|
+
declarationScope
|
|
56
74
|
});
|
|
57
75
|
}
|
|
58
76
|
/**
|
|
@@ -68,7 +86,7 @@ class ScopeStack extends Array {
|
|
|
68
86
|
} else {
|
|
69
87
|
foundSignal.declarationScope = this.findDeepestDeclarationScope(
|
|
70
88
|
foundSignal.declarationScope,
|
|
71
|
-
declarationScope
|
|
89
|
+
declarationScope
|
|
72
90
|
);
|
|
73
91
|
}
|
|
74
92
|
}
|
|
@@ -76,10 +94,10 @@ class ScopeStack extends Array {
|
|
|
76
94
|
pushProps(variable, declarationScope = this.currentScope().node) {
|
|
77
95
|
this.props.push({
|
|
78
96
|
references: variable.references.filter(
|
|
79
|
-
(reference) => !reference.init
|
|
97
|
+
(reference) => !reference.init
|
|
80
98
|
),
|
|
81
99
|
variable,
|
|
82
|
-
declarationScope
|
|
100
|
+
declarationScope
|
|
83
101
|
});
|
|
84
102
|
}
|
|
85
103
|
/** Function callbacks that run synchronously and don't create a new scope. */
|
|
@@ -92,14 +110,14 @@ class ScopeStack extends Array {
|
|
|
92
110
|
*consumeSignalReferencesInScope() {
|
|
93
111
|
yield* this.consumeReferencesInScope(this.signals);
|
|
94
112
|
this.signals = this.signals.filter(
|
|
95
|
-
(variable) => variable.references.length !== 0
|
|
113
|
+
(variable) => variable.references.length !== 0
|
|
96
114
|
);
|
|
97
115
|
}
|
|
98
116
|
/** Iterate through and remove the props references in the current scope. */
|
|
99
117
|
*consumePropsReferencesInScope() {
|
|
100
118
|
yield* this.consumeReferencesInScope(this.props);
|
|
101
119
|
this.props = this.props.filter(
|
|
102
|
-
(variable) => variable.references.length !== 0
|
|
120
|
+
(variable) => variable.references.length !== 0
|
|
103
121
|
);
|
|
104
122
|
}
|
|
105
123
|
*consumeReferencesInScope(variables) {
|
|
@@ -116,7 +134,7 @@ class ScopeStack extends Array {
|
|
|
116
134
|
});
|
|
117
135
|
yield* inScope.map((reference) => ({
|
|
118
136
|
reference,
|
|
119
|
-
declarationScope: variable.declarationScope
|
|
137
|
+
declarationScope: variable.declarationScope
|
|
120
138
|
}));
|
|
121
139
|
// I don't think this is needed! Just a perf optimization
|
|
122
140
|
variable.references = notInScope;
|
|
@@ -143,7 +161,7 @@ class ScopeStack extends Array {
|
|
|
143
161
|
isReferenceInCurrentScope(reference) {
|
|
144
162
|
let parentFunction = findParent(
|
|
145
163
|
reference.identifier,
|
|
146
|
-
isProgramOrFunctionNode
|
|
164
|
+
isProgramOrFunctionNode
|
|
147
165
|
);
|
|
148
166
|
while (
|
|
149
167
|
isFunctionNode(parentFunction) &&
|
|
@@ -151,7 +169,7 @@ class ScopeStack extends Array {
|
|
|
151
169
|
) {
|
|
152
170
|
parentFunction = findParent(
|
|
153
171
|
parentFunction,
|
|
154
|
-
isProgramOrFunctionNode
|
|
172
|
+
isProgramOrFunctionNode
|
|
155
173
|
);
|
|
156
174
|
}
|
|
157
175
|
return parentFunction === this.currentScope().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,13 +208,22 @@ 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",
|
|
185
223
|
docs: {
|
|
186
224
|
description:
|
|
187
225
|
"Enforce that reactivity (props, signals, memos, etc.) is properly used, so changes in those values will be tracked and update the view as expected.",
|
|
188
|
-
url: "https://github.com/solidjs-community/eslint-plugin-solid/blob/main/packages/eslint-plugin-solid/docs/reactivity.md"
|
|
226
|
+
url: "https://github.com/solidjs-community/eslint-plugin-solid/blob/main/packages/eslint-plugin-solid/docs/reactivity.md"
|
|
189
227
|
},
|
|
190
228
|
schema: [
|
|
191
229
|
{
|
|
@@ -193,40 +231,46 @@ 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
|
-
type: "string"
|
|
237
|
+
type: "string"
|
|
200
238
|
},
|
|
201
|
-
default: []
|
|
202
|
-
}
|
|
239
|
+
default: []
|
|
240
|
+
}
|
|
203
241
|
},
|
|
204
|
-
additionalProperties: false
|
|
205
|
-
}
|
|
242
|
+
additionalProperties: false
|
|
243
|
+
}
|
|
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
|
-
"
|
|
224
|
-
|
|
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()`."
|
|
268
|
+
}
|
|
225
269
|
},
|
|
226
270
|
defaultOptions: [
|
|
227
271
|
{
|
|
228
|
-
customReactiveFunctions: []
|
|
229
|
-
}
|
|
272
|
+
customReactiveFunctions: []
|
|
273
|
+
}
|
|
230
274
|
],
|
|
231
275
|
create(context) {
|
|
232
276
|
// `RuleCreator` used to merge `defaultOptions` into `context.options`
|
|
@@ -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 (
|
|
@@ -275,7 +484,7 @@ export default {
|
|
|
275
484
|
return;
|
|
276
485
|
}
|
|
277
486
|
markPropsOnCondition(node, (props) =>
|
|
278
|
-
isPropsByName(props.name)
|
|
487
|
+
isPropsByName(props.name)
|
|
279
488
|
);
|
|
280
489
|
}
|
|
281
490
|
scopeStack.push(new ScopeStackItem(node));
|
|
@@ -294,10 +503,419 @@ export default {
|
|
|
294
503
|
findInScope(
|
|
295
504
|
node,
|
|
296
505
|
currentScope().node,
|
|
297
|
-
(node) => node === trackedScope.node
|
|
298
|
-
)
|
|
506
|
+
(node) => node === trackedScope.node
|
|
507
|
+
)
|
|
508
|
+
);
|
|
509
|
+
}
|
|
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"
|
|
299
628
|
);
|
|
629
|
+
}
|
|
630
|
+
child = parent;
|
|
631
|
+
parent = parent.parent ?? null;
|
|
300
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);
|
|
301
919
|
};
|
|
302
920
|
/**
|
|
303
921
|
* Inspects a specific reference of a reactive variable for correct
|
|
@@ -308,15 +926,15 @@ export default {
|
|
|
308
926
|
// Check if the call falls outside any tracked scopes in the current scope
|
|
309
927
|
if (
|
|
310
928
|
!currentScope().trackedScopes.find((trackedScope) =>
|
|
311
|
-
matchTrackedScope(trackedScope, identifier)
|
|
929
|
+
matchTrackedScope(trackedScope, identifier)
|
|
312
930
|
)
|
|
313
931
|
) {
|
|
314
932
|
const matchedExpression = currentScope().trackedScopes.find(
|
|
315
933
|
(trackedScope) =>
|
|
316
934
|
matchTrackedScope(
|
|
317
935
|
{ ...trackedScope, expect: "expression" },
|
|
318
|
-
identifier
|
|
319
|
-
)
|
|
936
|
+
identifier
|
|
937
|
+
)
|
|
320
938
|
);
|
|
321
939
|
if (declarationScope === currentScopeNode) {
|
|
322
940
|
// If the reactivity is not contained in a tracked scope, and any of
|
|
@@ -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,24 +1081,39 @@ 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
|
|
367
1093
|
// of the tracked scopes, as we can't easily find references.
|
|
368
1094
|
const pushUnnamedDerivedSignal = () =>
|
|
369
1095
|
(parentScope().unnamedDerivedSignals ??= new Set()).add(
|
|
370
|
-
currentScopeNode
|
|
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(
|
|
376
|
-
currentScopeNode
|
|
1111
|
+
currentScopeNode
|
|
377
1112
|
)?.[0];
|
|
378
1113
|
if (functionVariable) {
|
|
379
1114
|
scopeStack.pushUniqueSignal(
|
|
380
1115
|
functionVariable,
|
|
381
|
-
declarationScope
|
|
1116
|
+
declarationScope // use declaration scope of a signal contained in this function
|
|
382
1117
|
);
|
|
383
1118
|
} else {
|
|
384
1119
|
pushUnnamedDerivedSignal();
|
|
@@ -390,18 +1125,21 @@ export default {
|
|
|
390
1125
|
// for nameless or arrow function expressions, use the declared variable it's assigned to
|
|
391
1126
|
const functionVariable =
|
|
392
1127
|
sourceCode.scopeManager?.getDeclaredVariables(
|
|
393
|
-
declarator
|
|
1128
|
+
declarator
|
|
394
1129
|
)?.[0];
|
|
395
1130
|
if (functionVariable) {
|
|
396
1131
|
// use declaration scope of a signal contained in this scope, not the function itself
|
|
397
1132
|
scopeStack.pushUniqueSignal(
|
|
398
1133
|
functionVariable,
|
|
399
|
-
declarationScope
|
|
1134
|
+
declarationScope
|
|
400
1135
|
);
|
|
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,47 +1173,34 @@ 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
|
-
declarationScope
|
|
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) =>
|
|
541
1200
|
context.report({
|
|
542
1201
|
node: identifier,
|
|
543
1202
|
messageId: "badSignal",
|
|
544
|
-
data: { name: identifier.name, where }
|
|
1203
|
+
data: { name: identifier.name, where }
|
|
545
1204
|
});
|
|
546
1205
|
if (
|
|
547
1206
|
// This allows both calling a signal and calling a function with a signal.
|
|
@@ -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 (
|
|
@@ -573,7 +1234,7 @@ export default {
|
|
|
573
1234
|
"|",
|
|
574
1235
|
"^",
|
|
575
1236
|
"&",
|
|
576
|
-
"in"
|
|
1237
|
+
"in"
|
|
577
1238
|
].includes(identifier.parent.operator)
|
|
578
1239
|
) {
|
|
579
1240
|
// We're in an arithmetic/comparison expression where using an uncalled signal wouldn't make sense
|
|
@@ -597,7 +1258,7 @@ export default {
|
|
|
597
1258
|
(trackedScope) =>
|
|
598
1259
|
trackedScope.node === identifier &&
|
|
599
1260
|
(trackedScope.expect === "function" ||
|
|
600
|
-
trackedScope.expect === "called-function")
|
|
1261
|
+
trackedScope.expect === "called-function")
|
|
601
1262
|
)
|
|
602
1263
|
) {
|
|
603
1264
|
// If the signal is in a JSXExpressionContainer that's also marked as a "function" or "called-function" tracked scope,
|
|
@@ -615,7 +1276,7 @@ export default {
|
|
|
615
1276
|
elementOrAttribute.parent.name.type ===
|
|
616
1277
|
"JSXIdentifier" &&
|
|
617
1278
|
isDOMElementName(
|
|
618
|
-
elementOrAttribute.parent.name.name
|
|
1279
|
+
elementOrAttribute.parent.name.name
|
|
619
1280
|
))
|
|
620
1281
|
) {
|
|
621
1282
|
reportBadSignal("JSX");
|
|
@@ -629,7 +1290,7 @@ export default {
|
|
|
629
1290
|
// Do a similar thing with all usages of props in the current function
|
|
630
1291
|
for (const {
|
|
631
1292
|
reference,
|
|
632
|
-
declarationScope
|
|
1293
|
+
declarationScope
|
|
633
1294
|
} of scopeStack.consumePropsReferencesInScope()) {
|
|
634
1295
|
const identifier = reference.identifier;
|
|
635
1296
|
if (reference.isWrite()) {
|
|
@@ -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,14 +1313,12 @@ 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" &&
|
|
663
1320
|
/^(?:initial|default|static[A-Z])/.test(
|
|
664
|
-
parent.property.name
|
|
1321
|
+
parent.property.name
|
|
665
1322
|
)
|
|
666
1323
|
) {
|
|
667
1324
|
// We're using a prop with a name that starts with `initial` or
|
|
@@ -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,
|
|
@@ -697,12 +1365,24 @@ export default {
|
|
|
697
1365
|
for (const node of unnamedDerivedSignals) {
|
|
698
1366
|
if (
|
|
699
1367
|
!currentScope().trackedScopes.find((trackedScope) =>
|
|
700
|
-
matchTrackedScope(trackedScope, node)
|
|
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
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
|
|
@@ -743,18 +1423,36 @@ export default {
|
|
|
743
1423
|
!node.callee.computed &&
|
|
744
1424
|
node.callee.object.type !== "ObjectExpression" &&
|
|
745
1425
|
/^(?:forEach|map|flatMap|reduce|reduceRight|find|findIndex|filter|every|some)$/.test(
|
|
746
|
-
node.callee.property.name
|
|
1426
|
+
node.callee.property.name
|
|
747
1427
|
)
|
|
748
1428
|
) {
|
|
749
1429
|
// These common array methods (or likely array methods) take synchronous callbacks
|
|
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
|
-
[
|
|
757
|
-
|
|
1449
|
+
[
|
|
1450
|
+
"createSignal",
|
|
1451
|
+
"createStore",
|
|
1452
|
+
"createOptimistic",
|
|
1453
|
+
"createOptimisticStore"
|
|
1454
|
+
],
|
|
1455
|
+
node.callee.name
|
|
758
1456
|
) &&
|
|
759
1457
|
node.parent?.type === "VariableDeclarator"
|
|
760
1458
|
) {
|
|
@@ -764,7 +1462,7 @@ export default {
|
|
|
764
1462
|
const setter = getNthDestructuredVar(
|
|
765
1463
|
node.parent.id,
|
|
766
1464
|
1,
|
|
767
|
-
context
|
|
1465
|
+
context
|
|
768
1466
|
);
|
|
769
1467
|
if (setter) {
|
|
770
1468
|
for (const reference of setter.references) {
|
|
@@ -808,56 +1506,64 @@ 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
|
|
848
1554
|
if (id?.type === "ArrayPattern") {
|
|
849
1555
|
const vars = id.elements
|
|
850
1556
|
.map((_, i) =>
|
|
851
|
-
getNthDestructuredVar(id, i, context)
|
|
1557
|
+
getNthDestructuredVar(id, i, context)
|
|
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(
|
|
859
1565
|
variable,
|
|
860
|
-
currentScope().node
|
|
1566
|
+
currentScope().node
|
|
861
1567
|
);
|
|
862
1568
|
});
|
|
863
1569
|
}
|
|
@@ -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];
|
|
@@ -896,33 +1589,45 @@ export default {
|
|
|
896
1589
|
) {
|
|
897
1590
|
const indexSignal = findVariable(
|
|
898
1591
|
context,
|
|
899
|
-
arg1.params[1]
|
|
1592
|
+
arg1.params[1]
|
|
900
1593
|
);
|
|
901
1594
|
if (indexSignal) {
|
|
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,13 +1644,26 @@ 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");
|
|
945
1663
|
this.skip(); // poor-man's `findInScope`: don't enter child scopes
|
|
946
1664
|
}
|
|
947
1665
|
},
|
|
948
|
-
fallback: "iteration"
|
|
1666
|
+
fallback: "iteration" // Don't crash when encounter unknown node.
|
|
949
1667
|
});
|
|
950
1668
|
};
|
|
951
1669
|
if (node.type === "JSXExpressionContainer") {
|
|
@@ -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" &&
|
|
@@ -1030,7 +1741,7 @@ export default {
|
|
|
1030
1741
|
} else if (node.type === "NewExpression") {
|
|
1031
1742
|
const {
|
|
1032
1743
|
callee,
|
|
1033
|
-
arguments: { 0: arg0 }
|
|
1744
|
+
arguments: { 0: arg0 }
|
|
1034
1745
|
} = node;
|
|
1035
1746
|
if (
|
|
1036
1747
|
callee.type === "Identifier" &&
|
|
@@ -1041,7 +1752,7 @@ export default {
|
|
|
1041
1752
|
"MutationObserver",
|
|
1042
1753
|
"PerformanceObserver",
|
|
1043
1754
|
"ReportingObserver",
|
|
1044
|
-
"ResizeObserver"
|
|
1755
|
+
"ResizeObserver"
|
|
1045
1756
|
].includes(callee.name)
|
|
1046
1757
|
) {
|
|
1047
1758
|
// Observers callbacks are NOT tracked scopes. However, they
|
|
@@ -1054,43 +1765,23 @@ export default {
|
|
|
1054
1765
|
if (node.callee.type === "Identifier") {
|
|
1055
1766
|
const {
|
|
1056
1767
|
callee,
|
|
1057
|
-
arguments: { 0: arg0, 1: arg1 }
|
|
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
|
-
callee.name
|
|
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) {
|
|
@@ -1100,7 +1791,7 @@ export default {
|
|
|
1100
1791
|
) {
|
|
1101
1792
|
pushTrackedScope(
|
|
1102
1793
|
property.value,
|
|
1103
|
-
"called-function"
|
|
1794
|
+
"called-function"
|
|
1104
1795
|
);
|
|
1105
1796
|
}
|
|
1106
1797
|
}
|
|
@@ -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", "
|
|
1118
|
-
callee.name
|
|
1895
|
+
["onCleanup", "onSettled", "onFlush", "action"],
|
|
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"
|
|
@@ -1185,19 +1935,18 @@ export default {
|
|
|
1185
1935
|
"Identifier" &&
|
|
1186
1936
|
matchImport(
|
|
1187
1937
|
"getOwner",
|
|
1188
|
-
decl.node.init.callee.name
|
|
1938
|
+
decl.node.init.callee.name
|
|
1189
1939
|
)
|
|
1190
1940
|
) {
|
|
1191
1941
|
// Check if the function in which getOwner() is called is a tracked scope. If the scopeStack
|
|
1192
1942
|
// has moved on from that scope already, assume it's tracked, since that's less intrusive.
|
|
1193
1943
|
const ownerFunction = findParent(
|
|
1194
1944
|
decl.node,
|
|
1195
|
-
isProgramOrFunctionNode
|
|
1945
|
+
isProgramOrFunctionNode
|
|
1196
1946
|
);
|
|
1197
1947
|
const scopeStackIndex =
|
|
1198
1948
|
scopeStack.findIndex(
|
|
1199
|
-
({ node }) =>
|
|
1200
|
-
ownerFunction === node,
|
|
1949
|
+
({ node }) => ownerFunction === node
|
|
1201
1950
|
);
|
|
1202
1951
|
if (
|
|
1203
1952
|
(scopeStackIndex >= 1 &&
|
|
@@ -1208,7 +1957,7 @@ export default {
|
|
|
1208
1957
|
trackedScope.expect ===
|
|
1209
1958
|
"function" &&
|
|
1210
1959
|
trackedScope.node ===
|
|
1211
|
-
ownerFunction
|
|
1960
|
+
ownerFunction
|
|
1212
1961
|
)) ||
|
|
1213
1962
|
scopeStackIndex === 0
|
|
1214
1963
|
) {
|
|
@@ -1222,7 +1971,7 @@ export default {
|
|
|
1222
1971
|
}
|
|
1223
1972
|
} else if (
|
|
1224
1973
|
/^(?:use|create)[A-Z]/.test(callee.name) ||
|
|
1225
|
-
|
|
1974
|
+
matchesCustomReactive(callee.name)
|
|
1226
1975
|
) {
|
|
1227
1976
|
// Custom hooks parameters may or may not be tracking scopes, no way to know.
|
|
1228
1977
|
// Assume all identifier/function arguments are tracked scopes, and use "called-function"
|
|
@@ -1230,6 +1979,25 @@ export default {
|
|
|
1230
1979
|
for (const arg of node.arguments) {
|
|
1231
1980
|
permissivelyTrackNode(arg);
|
|
1232
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
|
+
}
|
|
1233
2001
|
}
|
|
1234
2002
|
} else if (node.callee.type === "MemberExpression") {
|
|
1235
2003
|
const { property } = node.callee;
|
|
@@ -1240,12 +2008,38 @@ export default {
|
|
|
1240
2008
|
) {
|
|
1241
2009
|
// Like `on*` event handlers, mark all `addEventListener` listeners as called functions.
|
|
1242
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
|
+
}
|
|
1243
2039
|
} else if (
|
|
1244
2040
|
property.type === "Identifier" &&
|
|
1245
2041
|
(/^(?:use|create)[A-Z]/.test(property.name) ||
|
|
1246
|
-
|
|
1247
|
-
property.name,
|
|
1248
|
-
))
|
|
2042
|
+
matchesCustomReactive(property.name))
|
|
1249
2043
|
) {
|
|
1250
2044
|
// Handle custom hook parameters for property access custom hooks
|
|
1251
2045
|
for (const arg of node.arguments) {
|
|
@@ -1254,18 +2048,15 @@ export default {
|
|
|
1254
2048
|
}
|
|
1255
2049
|
}
|
|
1256
2050
|
} else if (node.type === "VariableDeclarator") {
|
|
1257
|
-
//
|
|
1258
|
-
// function
|
|
1259
|
-
//
|
|
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.
|
|
1260
2054
|
if (
|
|
1261
2055
|
node.init?.type === "CallExpression" &&
|
|
1262
2056
|
node.init.callee.type === "Identifier"
|
|
1263
2057
|
) {
|
|
1264
2058
|
if (
|
|
1265
|
-
matchImport(
|
|
1266
|
-
["createReactive", "createReaction"],
|
|
1267
|
-
node.init.callee.name,
|
|
1268
|
-
)
|
|
2059
|
+
matchImport(["createReaction"], node.init.callee.name)
|
|
1269
2060
|
) {
|
|
1270
2061
|
const track = getReturnedVar(node.id, context);
|
|
1271
2062
|
if (track) {
|
|
@@ -1290,7 +2081,7 @@ export default {
|
|
|
1290
2081
|
if (isFunctionNode(node.init.arguments[0])) {
|
|
1291
2082
|
pushTrackedScope(
|
|
1292
2083
|
node.init.arguments[0],
|
|
1293
|
-
"called-function"
|
|
2084
|
+
"called-function"
|
|
1294
2085
|
);
|
|
1295
2086
|
}
|
|
1296
2087
|
}
|
|
@@ -1327,7 +2118,7 @@ export default {
|
|
|
1327
2118
|
if (variable)
|
|
1328
2119
|
scopeStack.pushProps(
|
|
1329
2120
|
variable,
|
|
1330
|
-
currentScope().node
|
|
2121
|
+
currentScope().node
|
|
1331
2122
|
);
|
|
1332
2123
|
}
|
|
1333
2124
|
}
|
|
@@ -1346,14 +2137,55 @@ export default {
|
|
|
1346
2137
|
CallExpression(node) {
|
|
1347
2138
|
checkForTrackedScopes(node);
|
|
1348
2139
|
checkForSyncCallbacks(node);
|
|
1349
|
-
// 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.
|
|
1350
2143
|
const parent =
|
|
1351
2144
|
node.parent && ignoreTransparentWrappers(node.parent, true);
|
|
1352
2145
|
if (
|
|
1353
2146
|
parent?.type !== "AssignmentExpression" &&
|
|
1354
|
-
parent?.type !== "VariableDeclarator"
|
|
2147
|
+
parent?.type !== "VariableDeclarator" &&
|
|
2148
|
+
parent?.type !== "ReturnStatement" &&
|
|
2149
|
+
!(
|
|
2150
|
+
parent?.type === "ArrowFunctionExpression" &&
|
|
2151
|
+
parent.body === node
|
|
2152
|
+
)
|
|
1355
2153
|
) {
|
|
1356
|
-
|
|
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
|
+
}
|
|
1357
2189
|
}
|
|
1358
2190
|
},
|
|
1359
2191
|
NewExpression(node) {
|
|
@@ -1381,32 +2213,68 @@ export default {
|
|
|
1381
2213
|
node.parent.parent?.type === "JSXElement"
|
|
1382
2214
|
) {
|
|
1383
2215
|
const element = node.parent.parent;
|
|
1384
|
-
if (
|
|
1385
|
-
|
|
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;
|
|
1386
2233
|
if (
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
node.params[1].type === "Identifier"
|
|
2234
|
+
keyedAttr &&
|
|
2235
|
+
keyedAttr.value?.type === "JSXExpressionContainer"
|
|
1390
2236
|
) {
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
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;
|
|
1398
2252
|
}
|
|
1399
|
-
}
|
|
1400
|
-
|
|
2253
|
+
}
|
|
2254
|
+
if (
|
|
2255
|
+
itemIsSignal &&
|
|
1401
2256
|
node.params.length >= 1 &&
|
|
1402
2257
|
node.params[0].type === "Identifier"
|
|
1403
2258
|
) {
|
|
1404
|
-
// Mark `item` in `<Index>{(item, index) => <div />}</Index>` as a signal
|
|
1405
2259
|
const item = findVariable(context, node.params[0]);
|
|
1406
2260
|
if (item) {
|
|
1407
2261
|
scopeStack.pushSignal(
|
|
1408
2262
|
item,
|
|
1409
|
-
currentScope().node
|
|
2263
|
+
currentScope().node
|
|
2264
|
+
);
|
|
2265
|
+
}
|
|
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
|
|
1410
2278
|
);
|
|
1411
2279
|
}
|
|
1412
2280
|
}
|
|
@@ -1432,7 +2300,7 @@ export default {
|
|
|
1432
2300
|
if (scopeStack.length) {
|
|
1433
2301
|
currentScope().hasJSX = true;
|
|
1434
2302
|
}
|
|
1435
|
-
}
|
|
2303
|
+
}
|
|
1436
2304
|
};
|
|
1437
|
-
}
|
|
2305
|
+
}
|
|
1438
2306
|
};
|