5.9 KiB
5.9 KiB
Agent Guidelines for LA2NodeJS
Build/Lint/Test Commands
Development
npm run dev # Start development server
npm run build # Build production bundle
Linting & Formatting
npm run lint # Run linter (ESLint)
npm run format # Format code (Prettier)
npm run format:write # Format code in place
Testing
npm test # Run all tests
npm test -- --watch # Run tests in watch mode
npm test -- <pattern> # Run single test file or pattern
npm test <test> # Run specific test (e.g., npm test -- src/__tests__/api.test.ts)
Single Test Execution
To run a single test file:
npm test -- src/__tests__/utils.test.ts
To run specific test cases with test.only:
npm run test:watch -- -t "describe name"
To run individual test case using V8 flags (if Jest doesn't support -t):
npx jest src/__tests__/utils.test.ts -t "test description"
Code Style Guidelines
Imports
- Sort imports in this order: Node built-ins, third-party, relative
- Group same-type imports together
- Limit each import statement to a single type
- Use
import typefor type-only imports - Maximum 8-10 imports per file
- Use barrel exports only when reducing exports from a directory
Formatting
- Use Prettier with the configured
.prettierrc - Line width: 100 characters
- Tab indentation preferred
- Single quotes for strings (if configured)
- Semicolons included
- Trailing commas in multi-line objects/arrays
- No unnecessary parentheses (only for arrow functions and template literals)
Type Safety
- Enable strict TypeScript mode
- Type all function parameters and return values
- Use explicit types for async functions (
Promise<void>orPromise<T>) - Avoid
anytype; useunknownfor untrusted input - Use generic types in function signatures
- Type union/literal types as narrow as possible
- Avoid
@ts-ignore/@ts-expect-errorwithout comments
Naming Conventions
- Files: kebab-case (
example-file.ts) - Components: PascalCase or kebab-case based on framework
- Variables: camelCase for JS, snake_case if required by framework
- Constants: UPPER_SNAKE_CASE
- Classes: PascalCase
- Functions: camelCase
- Private members:
_privateprefix - Public methods with prefix
get|set|add|removefor clarity - Event names: camelCase or kebab-case
- Types/Interfaces: PascalCase
Error Handling
- Use
Errorsubclassing for domain-specific errors - Throw errors when invariants are violated
- Catch specific error types, not bare
catch - Never swallow errors without logging or rethrowing
- Use custom error types:
class NotFoundError extends Error { } - Include context in error messages (path, id, etc.)
- Avoid
process.env.NODE_ENVchecks; use runtime error detection - Use
.catch()with async functions to handle Promise rejections
Async/Await
- Prefer
async/awaitover.then()chains - Handle errors immediately after async calls
- Return early from
asyncfunctions on failure - Use
Promise.reject()explicitly overthrow new Error() - Avoid
.catch(() => void 0); instead, log or handle errors
Code Organization
- Place
typesdeclarations intypes/directory - Utility functions in
utils/ - API/service modules in
services/orcontrollers/ - Tests in matching
__tests__/or.specs/directories - Keep files under 400 lines
- Maximum 10 files in any directory
- Barrel files for logical grouping, not lazy imports
Security
- Never commit secrets to git
- Validate all external inputs (URLs, IPs, email)
- Use environment-specific config (not committed)
- Sanitize data before logging
- Validate API request payloads with Zod/schema validation
- Avoid deprecated crypto functions (use Web Crypto API)
- Use secure HTTP headers (CORS, Helmet if applicable)
File Conventions
- Use
.tsover.tsxwhen possible - Export only named exports when multiple items exported
- Index exports only for submodules/microservices
- Use
.d.tsfor public type definitions - Keep test files alongside source files
- Use
.env.localfor local overrides only
Linting Rules
- Disable ESLint rule only when necessary
- Provide comment explaining why
- Use
eslint-disable-next-line // reasonfor single-line - Use
/* eslint-disable rule */for blocks (max 5 lines) - Always re-enable rules after disabling
Documentation
- Document non-obvious code with JSDoc
- Include @param and @returns for public APIs
- Document complex algorithms with comments
- Add TODO/FIXME comments for known issues
- Link related issues in commit messages and PRs
- Document breaking changes prominently
Performance
- Avoid unnecessary array method calls
- Prefer
Object.entries()overObject.keys()for iteration - Defer non-critical initializations
- Cache expensive computations
- Use
Setfor uniqueness checks - Batch DOM operations when applicable
Testing Rules
- Test one scenario per file
- Arrange-Act-Assert pattern for tests
- Mock external dependencies
- Use
describe/itfor BDD-style tests - Order tests: Setup -> Happy Path -> Edge Cases -> Error Cases
- Include coverage reports when debugging tests
Migration Notes
- Replace
.jsxwith.tsxwhere component doesn't use DOM APIs - Use
React.FConly for strict component props (avoid for simplicity) - Use
useState,useReducerfor state with reset patterns - Use
useContextfor global state needs - Avoid
useEffectfor non-reactive state; keep side effects minimal
Common Workflows
Adding a new feature
- Create implementation file in appropriate directory
- Add types if needed in
types/ - Add tests in matching
__tests__/directory - Run
npm run lintand fix issues - Create PR with descriptive title
Debugging
- Add
console.logwith context - Use
node -eto run isolated snippets - Check browser DevTools for React errors
- Use
breakpointMode: 'inspect-brk'in package.json for debugging