Como usar
- Copie o comando e verifique o pacote antes de instalar no seu ambiente de agente.
- Consulte no original as ferramentas, credenciais e ambientes necessários.
- Descreva os dados de entrada e a entrega esperada; confira a saída com as fontes.
npx skills add https://github.com/aadityajain-dev/coding-skills --skill js-best-practicesAntes de começar
- A instalação não inclui assinatura de modelo, franquia API ou acesso aos seus arquivos.
- Estrelas e instalações indicam atividade, não um teste independente de qualidade ou segurança.
Renderizado do repositório de origem, preservando títulos, exemplos, código, tabelas, links e imagens.
JavaScript & TypeScript Best Practices
You already know JS and TS, so skip generic advice. This skill only records the rules that are easy to get wrong. It sits on top of the core principles; it doesn't repeat them.
Setup (keep it cheap)
- Core principles: if
references/coding-principles-and-best-practices/SKILL.mdisn't already in this conversation, read it once. It defines precedence, the conflict ranking, the writing workflow, the general checklist and the review format. Don't re-read it on later invocations in the same conversation. - Project profile: run
node <folder-containing-this-SKILL.md>/scripts/js-profile.mjs <dir-of-the-file-you're-working-on>. It prints the cached profile from<projectRoot>/.agents/js-project-profile.md, and only re-inspects the config (package.json, tsconfig, lint/test config) when that config has changed. Use the printed profile instead of opening those files yourself. Skip this step if the profile is already in this conversation and you haven't changed any config.
- "No package.json found" → it's a standalone snippet; state your assumptions (runtime, ESM/CJS, TS or not).
- Node unavailable → read package.json and tsconfig directly, once.
- Learn as you go: when you discover a convention the config can't show (folder layering, error model, naming, test style) or the user corrects you, add one short bullet under
## Learned conventionsin the profile. Keep that section under ~15 lines; it's read on every invocation.
The profile decides syntax availability (target/engines), module system, which libraries to reuse (don't add a second validator, HTTP client or date library) and which commands to verify with.
Rules that matter
Values & mutation
??for defaults, never||(it drops0,'',false).?.only for genuinely optional data — don't use it to hide values that should always exist.===everywhere;x == nullis the only accepted loose check.- Spread/
Object.assignare shallow →structuredClonefor deep copies.sort/reverse/splicemutate the caller's array → usetoSorted/toReversed/toSpliced/with(if the target allows) or copy first. - No money in floats (integer minor units or a decimal lib).
Number.parseInt(x, 10),Number.isNaN.BigIntfor IDs aboveMAX_SAFE_INTEGER. - Dates: date-only ISO strings parse as UTC, date-time strings without offset as local;
Datemonths are 0-based. Store/transmit UTC ISO 8601, format at the edge withIntl.
Async
- Every promise is awaited, returned or given an explicit
.catch.array.forEach(async …)never waits — usefor…of(ordered) orPromise.all(parallel). - Independent awaits run in parallel (
Promise.all/allSettled), with a concurrency cap for large batches. fetchdoesn't reject on 4xx/5xx — checkresponse.ok. Every network call getsAbortSignal.timeout(ms)or a passed-insignal.- Guard against races: stale responses overwriting newer ones, double submits, read-modify-write across an
await.
Errors
- Throw
Errorsubclasses withthis.nameset, never strings/objects; wrap with{ cause }. Map them to HTTP status/UI state in one place at the boundary. catch (err)isunknown: narrow withinstanceof, re-throw what you can't handle. An empty catch needs a comment explaining why.
TypeScript (when the profile says TS)
- No
any(useunknown+ narrowing); no!assertions;@ts-expect-error <reason>instead of@ts-ignore. - Types vanish at runtime: validate request bodies, API responses,
JSON.parse, storage and env vars with the project's schema library.asis not validation. - Model states as discriminated unions with an exhaustive
switch(neverdefault). Annotate exported signatures; let locals infer.satisfiesover widening annotations.
Node.js
- No
*Syncfs/crypto/child_process calls in request paths; stream large payloads withstream/promises. - Validate env config once at startup into a typed object; no scattered
process.envreads. node:prefix for built-ins. Log and exit onunhandledRejection/uncaughtException. HandleSIGTERMgracefully.- Commands:
execFile/spawnwith an args array, neverexecwith interpolation. Normalize and contain file paths (../traversal).
Browser DOM
- Batch layout reads, then writes — never interleave
offsetHeight/getBoundingClientRectwith style writes in a loop. Toggle classes over inline styles; insert in one go (DocumentFragment/append(...nodes)). - Throttle/debounce scroll, resize and input handlers;
{ passive: true }on scroll/touch; observers instead of polling. - Every listener, timer, observer and subscription has a teardown; one
AbortControllersignal can remove many listeners.
Security
- No untrusted data into
innerHTML/outerHTML/insertAdjacentHTML/document.write; usetextContentor sanitize (DOMPurify). Rejectjavascript:URLs inhref/src. Noeval,new Functionor string timers. - Prototype pollution: never merge/assign untrusted keys into objects; use
MaporObject.create(null)for input-keyed dictionaries. - No nested-quantifier regexes on user input (ReDoS); cap input length.
- Client bundles are public: no secrets (watch
VITE_/NEXT_PUBLIC_-style env vars).
Memory
- No unbounded module-level caches/arrays/Maps — add a size limit or TTL.
WeakMapfor metadata on objects you don't own.
Tests
- Await async assertions (
await expect(p).rejects.toThrow(X)); an un-awaited one passes silently. - Mock at boundaries (network, clock, fs) with the project's tools and fake timers; no real network, time zone or order dependence.
Writing
Follow the core workflow. Prefer the platform (fetch, structuredClone, Intl, URL, crypto.randomUUID, AbortSignal.timeout) before adding a dependency. Exported functions get JSDoc/TSDoc (params, returns, throws, side effects). Verify with the commands listed in the profile and say which you couldn't run.
Reviewing
Use the core review scope and format exactly. Skip anything ESLint/Prettier/Biome already enforces. Typical severities (adjust by impact and say why):
- 🔴 XSS sinks or
evalwith untrusted data; injection or prototype pollution; secrets in client code; floating promises that lose errors or break ordering;||defaults corrupting valid falsy values; float money; missingresponse.okon calls that matter; caller-visible mutation; stale-response races. - 🟡 Missing cleanup or timeouts; sequential independent awaits;
any/as/!on external data without runtime validation; layout thrashing; unbounded caches; missing or un-awaited tests. - 🟢
.then()chains that read better asawait; unnecessary?.; small naming fixes.