Skip to main content

Rule 46 - JS: Collection access

How JavaScript reads a position out of a collection. One form per receiver type, across every JavaScript file the repository owns - shipped modules, tooling, stories, and tests alike.

Rule 45 governs the shape of a component or utility module, so it reads only those two trees. Rule 46 governs an expression, which is written the same way wherever it appears, so it reaches every tree: one read spelled two ways in two files is the drift it exists to close.

Rules​

46.1. Reading an entry counted from the end of a collection MUST take the form its receiver supports.

  • An array or a string MUST use .at(-n). receiver[receiver.length - n] and receiver.slice(-n)[0] MUST NOT be used.
  • An element's last child element MUST use lastElementChild. It names the value outright and yields null on an element with no element children, so the length guard an index read needs goes with it.
  • Any other live collection - a NodeList, or an HTMLCollection read anywhere but its last entry - keeps an index read. Neither interface carries at(), and an Array.from() written only to reach .at(-1) copies the collection for nothing.

An assignment target and an update operand keep the index read whatever the receiver is. .at() yields a value, so receiver.at(-1) = x and receiver.at(-1)++ are not expressions JavaScript accepts.

The receiver decides, and the shape of the expression does not. Most collections here are arrays by the time anything reads them: window.Semetrika._collect and window.Semetrika._focusables both return Array.from(…), each component's own collector materializes the same way, and the unit-test harness in tests/support/mock-dom.js returns arrays from both querySelectorAll and children. A live collection therefore reaches far less code than the querySelectorAll in an expression's history suggests. Read where the value was built, not where it is used.

// Correct - `_focusables` returns an array.
const last = window.Semetrika._focusables(drawer).at(-1);

// Correct - the platform names the value, and `null` covers the empty case.
const lastChild = aboveEl.lastElementChild;

// Wrong - the index spelling on an array.
const last = focusable[focusable.length - 1];

// Wrong - a copy allocated only to reach `.at(-1)`.
const last = Array.from(menu.childNodes).at(-1);

A read whose receiver is genuinely a live collection carries a // live-collection: <reason> marker, trailing or on the line directly above, following the // demo-literal: precedent of Rule 61.33; the check honors the marker and the reviewer judges the reason. Nothing in the repository carries one today - every end-relative read resolves to an array or to lastElementChild - so the marker is documented and unused, as Rule 52.4's root-level allow-list is.

The check reads one expression at a time, so it matches the self-referential spelling and not one whose bound is hoisted into a variable (const length = segments.length; segments[length - 1]). That form is review-caught, the same way Rule 44.5 leaves an attribute name read through a const undetected.

It reads a slash by the token before it rather than by grammar, so the one after a closing parenthesis is division. A regular expression in that position - if (ok) /…/.test(value) - is scanned as code, which reports a finding only where the pattern itself spells an index read.

.at() is an ES2022 method, and esbuild's ES2020 bundle target downlevels syntax without polyfilling a built-in, so a shipped .at(-1) rests on the version floor rather than on the bundle target. Every browser at or above Semetrika's floor carries it.


Enforced by

46.1 by yarn cli 46 (findEndRelativeIndexReads in packages/cli/src/rules/46.js). See enforcement.md.