|
| 1 | +# Coding Standards |
| 2 | + |
| 3 | +This document outlines coding conventions and standards for this project. |
| 4 | + |
| 5 | +## React and TypeScript |
| 6 | + |
| 7 | +### Import Conventions |
| 8 | + |
| 9 | +**React Imports**: Always import specific hooks and types from React rather than using the `React.` prefix. |
| 10 | + |
| 11 | +```typescript |
| 12 | +// ✅ Correct |
| 13 | +import { useState, useEffect, useCallback } from "react"; |
| 14 | +import type { FC, ReactNode } from "react"; |
| 15 | + |
| 16 | +export const MyComponent: FC = () => { |
| 17 | + const [count, setCount] = useState(0); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + // effect logic |
| 21 | + }, []); |
| 22 | + |
| 23 | + return <div>{count}</div>; |
| 24 | +}; |
| 25 | + |
| 26 | +// ❌ Incorrect |
| 27 | +import React from "react"; |
| 28 | + |
| 29 | +export const MyComponent: React.FC = () => { |
| 30 | + const [count, setCount] = React.useState(0); |
| 31 | + |
| 32 | + React.useEffect(() => { |
| 33 | + // effect logic |
| 34 | + }, []); |
| 35 | + |
| 36 | + return <div>{count}</div>; |
| 37 | +}; |
| 38 | +``` |
| 39 | + |
| 40 | +**Rationale**: |
| 41 | +- Improves code readability by making hook usage more explicit |
| 42 | +- Reduces verbosity and visual clutter |
| 43 | +- Aligns with modern React conventions and tree-shaking optimization |
| 44 | +- Consistent with the rest of the codebase |
| 45 | + |
| 46 | +### Type Annotations |
| 47 | + |
| 48 | +- Use explicit function component typing sparingly - TypeScript can often infer the type |
| 49 | +- When needed, import `FC` or `ReactNode` types explicitly from React |
| 50 | +- Prefer explicit return types for component props interfaces |
| 51 | + |
| 52 | +```typescript |
| 53 | +// ✅ Good - TypeScript infers the component type |
| 54 | +export const SimpleComponent = () => { |
| 55 | + return <div>Hello</div>; |
| 56 | +}; |
| 57 | + |
| 58 | +// ✅ Good - explicit typing when needed for props |
| 59 | +import type { FC, ReactNode } from "react"; |
| 60 | + |
| 61 | +interface Props { |
| 62 | + children: ReactNode; |
| 63 | + title: string; |
| 64 | +} |
| 65 | + |
| 66 | +export const ComplexComponent: FC<Props> = ({ children, title }) => { |
| 67 | + return <div>{title}{children}</div>; |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +## General Guidelines |
| 72 | + |
| 73 | +### Naming Conventions |
| 74 | +- Use PascalCase for component names and files: `ConnectionStatus.tsx` |
| 75 | +- Use PascalCase for enum types: `ConnectionState`, `BadgeVariant` |
| 76 | +- Use PascalCase for enum values: `ConnectionState.Connecting`, `BadgeVariant.Secondary` |
| 77 | +- Use camelCase for functions, variables, and hooks: `useEditorCache`, `handleClick` |
| 78 | +- Use SCREAMING_SNAKE_CASE for constants: `MAX_RETRIES`, `API_ENDPOINT` |
| 79 | + |
| 80 | +```typescript |
| 81 | +// ✅ Correct - PascalCase for enum types and values |
| 82 | +enum ConnectionState { |
| 83 | + Connecting = "connecting", |
| 84 | + Connected = "connected", |
| 85 | + Offline = "offline", |
| 86 | + Error = "error", |
| 87 | +} |
| 88 | + |
| 89 | +// ❌ Incorrect - lowercase or SCREAMING_SNAKE_CASE |
| 90 | +enum connectionState { |
| 91 | + CONNECTING = "connecting", |
| 92 | + CONNECTED = "connected", |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### File Organization |
| 97 | +- Keep React components in `src/components/` |
| 98 | +- Place tests in `__tests__/` directories mirroring the source structure |
| 99 | +- Group related components in feature-specific subdirectories |
| 100 | + |
| 101 | +### Documentation |
| 102 | +- Add JSDoc comments for complex logic or non-obvious patterns |
| 103 | +- Explain *why* something is done, not just *what* is being done |
| 104 | +- Document external state synchronization patterns (like forceUpdate mechanisms) |
| 105 | + |
| 106 | +## Code Review Checklist |
| 107 | + |
| 108 | +When reviewing or writing code, ensure: |
| 109 | +- [ ] React hooks are imported directly, not accessed via `React.` |
| 110 | +- [ ] Components follow the established import patterns |
| 111 | +- [ ] Enum types and values use PascalCase |
| 112 | +- [ ] Complex logic has explanatory comments |
| 113 | +- [ ] Tests are updated to match code changes |
| 114 | +- [ ] TypeScript types are properly defined and used |
0 commit comments