Rules Hub
Coding Rules Library
Prefer standard polyfill libraries
Avoid manually implementing polyfills in source code; use granular imports from established libraries like core-js to ensure correctness and maintainability.
Bad example
| 1 | // lib/polyfills/is-well-formed.ts |
| 2 | // Manually copying complex logic into the codebase |
| 3 | if (!String.prototype.isWellFormed) { |
| 4 | String.prototype.isWellFormed = function () { |
| 5 | const str = String(this); |
| 6 | // ... 30 lines of complex surrogate pair logic ... |
| 7 | return true; |
| 8 | }; |
| 9 | } |
Explanation (EN)
Copying and pasting polyfill logic into the source code is risky. It creates unmaintained code that may miss edge cases, lacks updates, and clutters the repository.
Objašnjenje (HR)
Kopiranje logike polyfill-a u izvorni kod je rizično. Stvara neodržavan kod koji može propustiti rubne slučajeve, ne prima ažuriranja i nepotrebno opterećuje repozitorij.
Good example
| 1 | // lib/polyfills.ts |
| 2 | // Import specific feature to keep bundle size low while using a battle-tested implementation |
| 3 | import 'core-js/actual/string/is-well-formed'; |
Explanation (EN)
Using a standard library like `core-js` ensures the polyfill is spec-compliant and tested. Granular imports prevent bloating the bundle size by only including what is needed.
Objašnjenje (HR)
Korištenje standardne biblioteke poput `core-js` osigurava da je polyfill usklađen sa specifikacijama i testiran. Granularni uvoz sprječava povećanje paketa uključivanjem samo onoga što je potrebno.
Notes (EN)
If a manual polyfill is strictly necessary (e.g., due to extreme bundle size constraints preventing even granular imports), you must explicitly document the source/origin of the implementation in a comment.
Bilješke (HR)
Ako je ručni polyfill strogo neophodan (npr. zbog ekstremnih ograničenja veličine paketa koja sprječavaju čak i granularni uvoz), morate u komentaru eksplicitno navesti izvor/porijeklo implementacije.