@mehshekari/eslint-plugin-gsap-motion 4.4.2 → 4.5.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 +1 -0
- package/lib/ast.mjs +24 -3
- package/lib/rules.mjs +177 -2
- package/lib/source.mjs +9 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -81,6 +81,7 @@ so a finding waived that way still fails `gsap-motion audit`.
|
|
|
81
81
|
| `unregistered-plugin` | error | A plugin imported and never registered, whose properties are silently ignored |
|
|
82
82
|
| `never-completes` | error | An `onComplete` that can never run: on a timeline holding a `repeat: -1` child, or on a tween or timeline that repeats forever |
|
|
83
83
|
| `dev-tool-shipped` | error | `GSDevTools` or `MotionPathHelper` imported statically, or unconditional `markers: true` |
|
|
84
|
+
| `ref-read-in-cleanup` | warn | `killTweensOf` given a ref's `.current` in a `useEffect` cleanup, where React has already cleared it at unmount |
|
|
84
85
|
| `dangling-listener` | warn | An event listener that is never removed, including inline handlers that cannot be |
|
|
85
86
|
| `layout-property` | warn | Animating `width`, `height`, `top`, `left`, margins or padding, which forces layout every frame |
|
|
86
87
|
| `trigger-per-item` | warn | A ScrollTrigger created per item in a loop |
|
package/lib/ast.mjs
CHANGED
|
@@ -307,7 +307,7 @@ export const contains = (outer, inner) =>
|
|
|
307
307
|
* returns `null`: an ambiguous handler is left unread rather than guessed at.
|
|
308
308
|
*/
|
|
309
309
|
export function resolveFunction(root, node) {
|
|
310
|
-
const target = unwrap(node);
|
|
310
|
+
const target = wrappedFunction(unwrap(node));
|
|
311
311
|
if (isFunction(target)) return target;
|
|
312
312
|
if (target?.type !== "Identifier") return null;
|
|
313
313
|
|
|
@@ -318,13 +318,34 @@ export function resolveFunction(root, node) {
|
|
|
318
318
|
(n.type === "VariableDeclarator" &&
|
|
319
319
|
n.id.type === "Identifier" &&
|
|
320
320
|
n.id.name === target.name &&
|
|
321
|
-
isFunction(unwrap(n.init))),
|
|
321
|
+
isFunction(wrappedFunction(unwrap(n.init)))),
|
|
322
322
|
);
|
|
323
323
|
if (declarations.length !== 1) return null;
|
|
324
324
|
const [declaration] = declarations;
|
|
325
325
|
return declaration.type === "FunctionDeclaration"
|
|
326
326
|
? declaration
|
|
327
|
-
: unwrap(declaration.init);
|
|
327
|
+
: wrappedFunction(unwrap(declaration.init));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The function inside a call that hands it back to run later, unchanged in
|
|
332
|
+
* what it does: `contextSafe(fn)`, `ctx.add(null, fn)` and `useCallback(fn, deps)`.
|
|
333
|
+
* The handler the skill recommends is wrapped in contextSafe, and following
|
|
334
|
+
* names stopped at the wrapper, so a tween per pointer event went unreported.
|
|
335
|
+
* Anything else is returned as it was.
|
|
336
|
+
*/
|
|
337
|
+
function wrappedFunction(node) {
|
|
338
|
+
if (node?.type !== "CallExpression") return node;
|
|
339
|
+
const name = calleeName(node)?.split(".").at(-1);
|
|
340
|
+
if (name === "contextSafe" || name === "useCallback") return unwrap(node.arguments[0]);
|
|
341
|
+
if (
|
|
342
|
+
name === "add" &&
|
|
343
|
+
unwrap(node.arguments[0])?.type === "Literal" &&
|
|
344
|
+
unwrap(node.arguments[0]).value === null
|
|
345
|
+
) {
|
|
346
|
+
return unwrap(node.arguments[1]);
|
|
347
|
+
}
|
|
348
|
+
return node;
|
|
328
349
|
}
|
|
329
350
|
|
|
330
351
|
const PARENTS = new WeakMap();
|
package/lib/rules.mjs
CHANGED
|
@@ -54,13 +54,15 @@ const HINT = {
|
|
|
54
54
|
stackedFrom:
|
|
55
55
|
"A `from` or `fromTo` applies its start state the moment it is built. Built more than once against the same element, the last one wins, and holds that element at its start state until the playhead reaches it — invisible, if the start state hides it. Pass `immediateRender: false`, and set the element's first state yourself with `gsap.set`.",
|
|
56
56
|
orphanTween:
|
|
57
|
-
"Nothing reverts it on unmount, and React StrictMode runs it twice in development. Move it into the useGSAP body, or wrap the handler in contextSafe. A helper counts as inside only when every use of it is a call from inside a context.",
|
|
57
|
+
"Nothing reverts it on unmount, and React StrictMode runs it twice in development. Move it into the useGSAP body, or wrap the handler in contextSafe. A helper counts as inside only when every use of it is a call from inside a context. Note that contextSafe keeps every tween it creates until the context reverts: a handler that runs often should replay a timeline built once, or call a quickTo.",
|
|
58
58
|
unmanagedInstance:
|
|
59
59
|
"Inside a context these register themselves and are reverted with it. Out here nothing is watching: move it into the useGSAP body, or keep the instance and revert/kill it from the cleanup.",
|
|
60
60
|
unrevertedContext:
|
|
61
61
|
"Keep it under a name and revert it when the component goes: `onUnmounted` in Vue, the function returned from `onMount` or `$effect` in Svelte, the `useEffect` cleanup in React, `astro:before-swap` in Astro. Everything created inside the context goes with it.",
|
|
62
62
|
tweenPerFrame:
|
|
63
63
|
"A tween per frame allocates an object per frame, and each one fights the last. Create a `gsap.quickTo()` or `quickSetter()` once outside the callback and call it inside — or, for scroll, scrub a timeline instead of writing values by hand.",
|
|
64
|
+
refReadInCleanup:
|
|
65
|
+
"So nothing is killed, and the tweens outlive the component. Capture the element in the effect body — `const el = ref.current` — and kill through that in the cleanup, or build the tweens in a useGSAP or gsap.context body, whose revert needs no ref.",
|
|
64
66
|
allocationPerFrame:
|
|
65
67
|
"Every frame's object is garbage by the next, and the collector reclaims it in a pause that lands as a dropped frame. Build it once, outside the callback — at module scope, or in a ref or useMemo — and reuse it with `.set()` or `.copy()`.",
|
|
66
68
|
layoutReadPerFrame:
|
|
@@ -96,7 +98,7 @@ const HINT = {
|
|
|
96
98
|
unregisteredPlugin:
|
|
97
99
|
"An unregistered plugin's properties are ignored without an error — the animation simply does nothing.",
|
|
98
100
|
missingReducedMotion:
|
|
99
|
-
"Reduced motion is a design, not an off switch: decide what the motion was saying and say it without the travel. Note that the reduced branch must set the END state — a `from` that never runs leaves its target invisible.",
|
|
101
|
+
"Reduced motion is a design, not an off switch: decide what the motion was saying and say it without the travel. Note that the reduced branch must set the END state — a `from` that never runs leaves its target invisible. If the branch lives in the module that calls this one, waive it here and name that caller.",
|
|
100
102
|
devToolImport:
|
|
101
103
|
"A static import ships regardless of any `if` around the call. Load it with a dynamic `import()` behind a NODE_ENV check, or bake its output in and delete it.",
|
|
102
104
|
markers: 'Gate it: `markers: process.env.NODE_ENV === "development"`.',
|
|
@@ -310,6 +312,91 @@ function debounced(handler, node) {
|
|
|
310
312
|
);
|
|
311
313
|
}
|
|
312
314
|
|
|
315
|
+
/**
|
|
316
|
+
* Whether a tween's target is a local the enclosing effect captured, and that
|
|
317
|
+
* effect's code kills that local's tweens: `const el = ref.current` in the body,
|
|
318
|
+
* `gsap.killTweensOf(el)` in the cleanup. Found on the corpus, where it was
|
|
319
|
+
* reported as an orphan.
|
|
320
|
+
*
|
|
321
|
+
* Only a captured local counts. `killTweensOf(ref.current)` in a passive
|
|
322
|
+
* cleanup kills nothing at unmount — React has already cleared the ref — and
|
|
323
|
+
* that is `ref-read-in-cleanup`'s to say.
|
|
324
|
+
*/
|
|
325
|
+
function killedThroughCapture(ast, node) {
|
|
326
|
+
if (!isGsapCall(node)) return false;
|
|
327
|
+
const target = unwrap(node.arguments[0]);
|
|
328
|
+
if (target?.type !== "Identifier") return false;
|
|
329
|
+
|
|
330
|
+
const effect = ancestorsOf(ast, node).find((fn) => {
|
|
331
|
+
if (!isFunction(fn)) return false;
|
|
332
|
+
const call = parentOf(ast, fn);
|
|
333
|
+
return (
|
|
334
|
+
call?.type === "CallExpression" &&
|
|
335
|
+
call.arguments[0] === fn &&
|
|
336
|
+
EFFECTS.has(calleeName(call)?.split(".").at(-1))
|
|
337
|
+
);
|
|
338
|
+
});
|
|
339
|
+
if (!effect) return false;
|
|
340
|
+
|
|
341
|
+
const captured = findAll(
|
|
342
|
+
effect,
|
|
343
|
+
(n) => n.type === "VariableDeclarator" && n.id.type === "Identifier" && n.id.name === target.name,
|
|
344
|
+
).length > 0;
|
|
345
|
+
if (!captured) return false;
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* In the cleanup — the function the effect returns — and nowhere else. A
|
|
349
|
+
* kill at the top of the body stops the previous run's tweens before
|
|
350
|
+
* starting new ones; it does nothing at unmount. Nine true corpus findings
|
|
351
|
+
* did exactly that, and the first version of this check let them go.
|
|
352
|
+
*/
|
|
353
|
+
const cleanups = cleanupsOf(ast, effect);
|
|
354
|
+
|
|
355
|
+
const names = (arg) => {
|
|
356
|
+
const value = unwrap(arg);
|
|
357
|
+
if (value?.type === "Identifier") return [value.name];
|
|
358
|
+
if (value?.type === "ArrayExpression") return value.elements.flatMap((e) => (e ? names(e) : []));
|
|
359
|
+
return [];
|
|
360
|
+
};
|
|
361
|
+
return cleanups.some(
|
|
362
|
+
(cleanup) =>
|
|
363
|
+
findAll(
|
|
364
|
+
cleanup,
|
|
365
|
+
(n) =>
|
|
366
|
+
n.type === "CallExpression" &&
|
|
367
|
+
calleeName(n)?.endsWith("killTweensOf") &&
|
|
368
|
+
n.arguments.some((arg) => names(arg).includes(target.name)),
|
|
369
|
+
).length > 0,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const EFFECTS = new Set(["useEffect", "useLayoutEffect"]);
|
|
374
|
+
|
|
375
|
+
/** The functions an effect callback returns: its cleanups. */
|
|
376
|
+
function cleanupsOf(ast, effect) {
|
|
377
|
+
const cleanups = findAll(
|
|
378
|
+
effect,
|
|
379
|
+
(n) => n.type === "ReturnStatement" && n.argument && scopeOf(ast, n) === effect,
|
|
380
|
+
)
|
|
381
|
+
.map((n) => unwrap(n.argument))
|
|
382
|
+
.filter(isFunction);
|
|
383
|
+
if (effect.type === "ArrowFunctionExpression" && effect.expression && isFunction(unwrap(effect.body))) {
|
|
384
|
+
cleanups.push(unwrap(effect.body));
|
|
385
|
+
}
|
|
386
|
+
return cleanups;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** Whether `node` reads a ref: a `.current` anywhere inside it. */
|
|
390
|
+
const readsRef = (node) =>
|
|
391
|
+
findAll(
|
|
392
|
+
node,
|
|
393
|
+
(n) =>
|
|
394
|
+
n.type === "MemberExpression" &&
|
|
395
|
+
!n.computed &&
|
|
396
|
+
n.property.type === "Identifier" &&
|
|
397
|
+
n.property.name === "current",
|
|
398
|
+
).length > 0;
|
|
399
|
+
|
|
313
400
|
/** The calls that add a child to a timeline. */
|
|
314
401
|
const CHILDREN = new Set(["to", "from", "fromTo", "add"]);
|
|
315
402
|
|
|
@@ -341,6 +428,22 @@ function tornDown(ast, node) {
|
|
|
341
428
|
const kept = keptUnder(ast, node);
|
|
342
429
|
if (endsUnder(kept?.name)) return true;
|
|
343
430
|
|
|
431
|
+
/**
|
|
432
|
+
* A second name for it: `active = timeline`, then `active?.kill()` in the
|
|
433
|
+
* cleanup. Found on the corpus, in a preview whose timeline was built in a
|
|
434
|
+
* helper and handed to a variable the effect owns.
|
|
435
|
+
*/
|
|
436
|
+
if (kept?.name) {
|
|
437
|
+
const aliases = findAll(ast, (n) => {
|
|
438
|
+
const from = n.type === "AssignmentExpression" ? n.right : n.type === "VariableDeclarator" ? n.init : null;
|
|
439
|
+
const to = n.type === "AssignmentExpression" ? n.left : n.type === "VariableDeclarator" ? n.id : null;
|
|
440
|
+
return unwrap(from)?.type === "Identifier" && unwrap(from).name === kept.name && to?.type === "Identifier";
|
|
441
|
+
}).map((n) => (n.type === "AssignmentExpression" ? n.left.name : n.id.name));
|
|
442
|
+
if (aliases.some(endsUnder)) return true;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (killedThroughCapture(ast, node)) return true;
|
|
446
|
+
|
|
344
447
|
const fn = scopeOf(ast, node);
|
|
345
448
|
if (!isFunction(fn)) return false;
|
|
346
449
|
|
|
@@ -886,6 +989,62 @@ export const RULES = [
|
|
|
886
989
|
});
|
|
887
990
|
},
|
|
888
991
|
},
|
|
992
|
+
{
|
|
993
|
+
id: "ref-read-in-cleanup",
|
|
994
|
+
level: "warn",
|
|
995
|
+
description:
|
|
996
|
+
"`killTweensOf` given a ref's `.current` in a `useEffect` cleanup, where React has already cleared it at unmount",
|
|
997
|
+
/**
|
|
998
|
+
* At unmount React detaches a DOM ref before it runs passive cleanups, so
|
|
999
|
+
* `ref.current` is null there and the kill finds nothing. A corpus project
|
|
1000
|
+
* did this in three cleanups, and fifteen tweens outlived their component.
|
|
1001
|
+
*
|
|
1002
|
+
* Narrow on purpose. Only `useEffect`: when a layout cleanup runs relative
|
|
1003
|
+
* to ref detachment is not something this rule is sure of. Only
|
|
1004
|
+
* `killTweensOf`, whose argument is the element: a ref that holds a timeline
|
|
1005
|
+
* — `tlRef.current?.kill()` — is not cleared by React, and is correct.
|
|
1006
|
+
*/
|
|
1007
|
+
test(file) {
|
|
1008
|
+
if (!file.isReact) return [];
|
|
1009
|
+
const effects = findAll(
|
|
1010
|
+
file.ast,
|
|
1011
|
+
(n) => n.type === "CallExpression" && calleeName(n)?.split(".").at(-1) === "useEffect",
|
|
1012
|
+
)
|
|
1013
|
+
.map((call) => unwrap(call.arguments[0]))
|
|
1014
|
+
.filter(isFunction);
|
|
1015
|
+
|
|
1016
|
+
return effects.flatMap((effect) =>
|
|
1017
|
+
cleanupsOf(file.ast, effect).flatMap((cleanup) => {
|
|
1018
|
+
/** A local the cleanup itself reads from a ref: `const root = rootRef.current`. */
|
|
1019
|
+
const fromRef = new Set(
|
|
1020
|
+
findAll(
|
|
1021
|
+
cleanup,
|
|
1022
|
+
(n) =>
|
|
1023
|
+
n.type === "VariableDeclarator" &&
|
|
1024
|
+
n.id.type === "Identifier" &&
|
|
1025
|
+
n.init &&
|
|
1026
|
+
readsRef(n.init),
|
|
1027
|
+
).map((n) => n.id.name),
|
|
1028
|
+
);
|
|
1029
|
+
const viaRef = (arg) =>
|
|
1030
|
+
readsRef(arg) ||
|
|
1031
|
+
findAll(arg, (n) => n.type === "Identifier" && fromRef.has(n.name)).length > 0;
|
|
1032
|
+
|
|
1033
|
+
return findAll(
|
|
1034
|
+
cleanup,
|
|
1035
|
+
(n) =>
|
|
1036
|
+
n.type === "CallExpression" &&
|
|
1037
|
+
calleeName(n)?.endsWith("killTweensOf") &&
|
|
1038
|
+
n.arguments.some(viaRef),
|
|
1039
|
+
).map((call) => ({
|
|
1040
|
+
index: call.start,
|
|
1041
|
+
message: "`killTweensOf` reads a ref in a useEffect cleanup, which React has already cleared at unmount.",
|
|
1042
|
+
hint: HINT.refReadInCleanup,
|
|
1043
|
+
}));
|
|
1044
|
+
}),
|
|
1045
|
+
);
|
|
1046
|
+
},
|
|
1047
|
+
},
|
|
889
1048
|
{
|
|
890
1049
|
id: "dangling-listener",
|
|
891
1050
|
level: "warn",
|
|
@@ -1685,6 +1844,22 @@ export const RULES = [
|
|
|
1685
1844
|
if (findAll(file.ast, (node) => spells(node, "prefers-reduced-motion")).length) {
|
|
1686
1845
|
return [];
|
|
1687
1846
|
}
|
|
1847
|
+
/**
|
|
1848
|
+
* The query is often written once, in a shared module, and asked by name:
|
|
1849
|
+
* `useReducedMotion()`, `prefersReducedMotion()`, `isReduced()`, or
|
|
1850
|
+
* `matchMedia(REDUCED_MOTION_QUERY)`. That is a branch too. "Reduced",
|
|
1851
|
+
* not "reduce", so an array's `.reduce()` is not one.
|
|
1852
|
+
*/
|
|
1853
|
+
const asksByName = findAll(file.ast, (node) => {
|
|
1854
|
+
if (node.type !== "CallExpression") return false;
|
|
1855
|
+
const name = calleeName(node)?.split(".").at(-1) ?? "";
|
|
1856
|
+
if (/reduced/i.test(name)) return true;
|
|
1857
|
+
return (
|
|
1858
|
+
name === "matchMedia" &&
|
|
1859
|
+
node.arguments.some((arg) => arg.type === "Identifier" && /reduced/i.test(arg.name))
|
|
1860
|
+
);
|
|
1861
|
+
});
|
|
1862
|
+
if (asksByName.length) return [];
|
|
1688
1863
|
|
|
1689
1864
|
const [first] = findAll(file.ast, (node) => {
|
|
1690
1865
|
const method = tweenMethod(node);
|
package/lib/source.mjs
CHANGED
|
@@ -241,7 +241,15 @@ export function fromText(raw, path, display = path) {
|
|
|
241
241
|
/\bfrom ["']react["']/.test(code) ||
|
|
242
242
|
/\bfrom ["']@gsap\/react["']/.test(code),
|
|
243
243
|
isClient: /^\s*["']use client["']/m.test(code),
|
|
244
|
-
|
|
244
|
+
/**
|
|
245
|
+
* GSAP itself, or a binding named `gsap` from anywhere: a project's own
|
|
246
|
+
* wrapper (`import { gsap } from "@/core/motion/gsap"`) is common, and
|
|
247
|
+
* reading only the package left orphan-tween and missing-reduced-motion
|
|
248
|
+
* blind to every file in a real project that used one.
|
|
249
|
+
*/
|
|
250
|
+
usesGsap:
|
|
251
|
+
/\bfrom ["']gsap(?:\/|["'])/.test(code) ||
|
|
252
|
+
/\bimport\s+(?!type\b)(?:gsap\b|\{[^}]*\bgsap\b[^}]*\})[^;]*?\bfrom\s*["']/.test(code),
|
|
245
253
|
};
|
|
246
254
|
}
|
|
247
255
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mehshekari/eslint-plugin-gsap-motion",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"description": "The gsap-motion audit as ESLint rules: GSAP tweens that outlive their component, per-event allocations and missing reduced-motion branches, reported in your editor.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|