Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Scope DOM/element queries to the relevant container, not the whole document
When you already have a reference to the specific element/container, query within it instead of searching the entire document.
Bad example
| 1 | function getImageSrc($: CheerioAPI, $container: Cheerio<Element>) { |
| 2 | // searches the entire document — may match a different item's image |
| 3 | return $('div.image-container img').attr('src'); |
| 4 | } |
Explanation (EN)
For a page with multiple similar containers (e.g. a gallery or a list of cards), this returns the first match anywhere in the document, which is not necessarily the one that belongs to the item currently being processed.
Objašnjenje (HR)
Na stranici s više sličnih kontejnera (npr. galerija ili lista kartica) ovo vraća prvi pogodak bilo gdje u dokumentu, što nije nužno onaj koji pripada elementu koji se trenutno obrađuje.
Good example
| 1 | function getImageSrc($: CheerioAPI, $container: Cheerio<Element>) { |
| 2 | // scoped to the container we already identified |
| 3 | return $container.find('img.image-container').attr('src'); |
| 4 | } |
Explanation (EN)
Scoping the query to the already-identified container guarantees the match belongs to the right item, even when multiple similar elements exist on the same page.
Objašnjenje (HR)
Ograničavanjem upita na već identificirani kontejner osigurava se da pogodak pripada ispravnom elementu, čak i kad na istoj stranici postoji više sličnih elemenata.
Exceptions / Tradeoffs (EN)
A document-wide query is fine when there is genuinely only one instance of the element on the page.
Iznimke / Tradeoffi (HR)
Upit po cijelom dokumentu je u redu kad na stranici zaista postoji samo jedna instanca tog elementa.