no-positional-children
A child of a scene graph or a DOM element picked by its position: actor.children[2], el.childNodes[0], el.children.item(1), container.getChildAt(0). An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: the position only holds while nothing changes the list. In PixiJS, giving a child a zIndex makes its container sort the children, and addChild, addChildAt or a removal shifts the rest. In the DOM, a whitespace text node, a comment or an element another piece of code inserts does the same. The code then changes another child, with no error. An adopter's game gave one part of each villager a zIndex, the container re-sorted, and every unselected villager lost its body. A reference kept when the child is created, or a lookup by name, still points at the right child after any of that.
✕ Rejectedno-positional-children
ts
type Part = { visible: boolean; zIndex: number };
type Actor = { readonly children: readonly Part[] };
export function partsShown(actor: Actor, selected: boolean): readonly boolean[] {
const ring = actor.children[0]?.visible === true;
const cargo = actor.children[2]?.visible === true;
return [selected && ring, cargo];
}✅ Accepted: the parts kept by name where they are made
✓ Accepted0 diagnostics
ts
type Part = { visible: boolean; zIndex: number };
type ActorParts = { readonly body: Part; readonly ring: Part; readonly cargo: Part };
export function partsShown(parts: ActorParts, selected: boolean): readonly boolean[] {
return [selected && parts.ring.visible, parts.cargo.visible];
}In PixiJS, container.getChildByLabel("ring") (v8; getChildByName before) finds a child by name. In the DOM, querySelector(".ring") or a template ref does.
What counts
- Reported: a number literal as the index into
.childrenor.childNodes, as the argument of.children.item(n)/.childNodes.item(n), and ofgetChildAt(n). - Left alone: an index that varies (
children[i]in a loop over all of them), and the only child, where a test right before says so:node.children.length === 1 && node.children[0], also in?:andif.
Why it is opt-in: without a type checker, children is any object's field, so a data tree's node.children[0] is reported too, and el.children[0] is common in DOM code that builds the element itself a line before.