|
| 1 | +# packages/core — TypeScript/JavaScript SDK |
| 2 | + |
| 3 | +## Build & Test |
| 4 | + |
| 5 | +```bash |
| 6 | +yarn build:sdk:watch # Watch mode for development |
| 7 | +yarn test:watch # Jest watch mode |
| 8 | +yarn test:sdk # SDK tests only |
| 9 | +yarn test:tools # Tools tests only |
| 10 | +``` |
| 11 | + |
| 12 | +## Code Style |
| 13 | + |
| 14 | +- **Single quotes** for strings |
| 15 | +- **Arrow functions** preferred for callbacks |
| 16 | +- **async/await** is allowed (React Native bundle size isn't a concern) |
| 17 | +- Use **optional chaining** (`?.`) and **nullish coalescing** (`??`) |
| 18 | +- Maximum line length: **120 characters** |
| 19 | +- Trailing commas: **always** |
| 20 | +- Arrow parens: **avoid** when possible (`x => x` not `(x) => x`) |
| 21 | + |
| 22 | +## Type Annotations |
| 23 | + |
| 24 | +- Explicitly type function parameters and return types |
| 25 | +- Use TypeScript strict mode conventions |
| 26 | +- Prefer `interface` over `type` for object shapes |
| 27 | +- Use `unknown` instead of `any` when possible |
| 28 | + |
| 29 | +```typescript |
| 30 | +interface UserData { |
| 31 | + id: string; |
| 32 | + name: string; |
| 33 | + email?: string; |
| 34 | +} |
| 35 | + |
| 36 | +const processUser = (user: UserData): string => { |
| 37 | + return user.email ?? 'no-email@example.com'; |
| 38 | +}; |
| 39 | +``` |
| 40 | + |
| 41 | +## Import Ordering |
| 42 | + |
| 43 | +1. External packages (e.g., `@sentry/core`, `react-native`) |
| 44 | +2. Internal absolute imports |
| 45 | +3. Relative imports |
| 46 | +4. Type imports (can be inline with `import type`) |
| 47 | + |
| 48 | +## Test Naming Convention |
| 49 | + |
| 50 | +Use `describe/it` blocks (preferred) or flat `test()` calls: |
| 51 | + |
| 52 | +```typescript |
| 53 | +describe('functionName', () => { |
| 54 | + it('returns expected value when input is valid', () => { |
| 55 | + // Arrange |
| 56 | + const input = 'test'; |
| 57 | + |
| 58 | + // Act |
| 59 | + const result = functionName(input); |
| 60 | + |
| 61 | + // Assert |
| 62 | + expect(result).toBe('expected'); |
| 63 | + }); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +## Test Code Style |
| 68 | + |
| 69 | +**Arrange-Act-Assert pattern** — always structure tests with clear sections. |
| 70 | + |
| 71 | +**Use specific Jest matchers:** |
| 72 | + |
| 73 | +```typescript |
| 74 | +// Good |
| 75 | +expect(value).toBe(true); |
| 76 | +expect(array).toHaveLength(3); |
| 77 | +expect(object).toMatchObject({ key: 'value' }); |
| 78 | +expect(fn).toThrow(Error); |
| 79 | +expect(promise).resolves.toBe('success'); |
| 80 | + |
| 81 | +// Avoid |
| 82 | +expect(value === true).toBe(true); |
| 83 | +expect(array.length).toBe(3); |
| 84 | +``` |
| 85 | + |
| 86 | +**Mock cleanup:** |
| 87 | + |
| 88 | +```typescript |
| 89 | +describe('MyComponent', () => { |
| 90 | + afterEach(() => { |
| 91 | + jest.clearAllMocks(); |
| 92 | + }); |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +## Common Patterns |
| 97 | + |
| 98 | +### Error Handling |
| 99 | + |
| 100 | +```typescript |
| 101 | +try { |
| 102 | + const result = await riskyOperation(); |
| 103 | + return result; |
| 104 | +} catch (error) { |
| 105 | + logger.error('Operation failed', error); |
| 106 | + // Don't throw - log and return fallback |
| 107 | + return fallbackValue; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### Native Bridge (JS side) |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { NativeModules } from 'react-native'; |
| 115 | + |
| 116 | +const { RNSentry } = NativeModules; |
| 117 | + |
| 118 | +export async function nativeOperation(param: string): Promise<boolean> { |
| 119 | + if (!RNSentry) { |
| 120 | + logger.warn('Native module not available'); |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + try { |
| 125 | + return await RNSentry.nativeOperation(param); |
| 126 | + } catch (error) { |
| 127 | + logger.error('Native operation failed', error); |
| 128 | + return false; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Platform-Specific Code |
| 134 | + |
| 135 | +```typescript |
| 136 | +import { Platform } from 'react-native'; |
| 137 | + |
| 138 | +const platformSpecificValue = Platform.select({ |
| 139 | + ios: 'iOS value', |
| 140 | + android: 'Android value', |
| 141 | + default: 'Default value', |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +### Mocking Native Modules in Tests |
| 146 | + |
| 147 | +```typescript |
| 148 | +jest.mock('react-native', () => ({ |
| 149 | + NativeModules: { |
| 150 | + RNSentry: { |
| 151 | + nativeOperation: jest.fn(() => Promise.resolve(true)), |
| 152 | + }, |
| 153 | + }, |
| 154 | + Platform: { |
| 155 | + OS: 'ios', |
| 156 | + }, |
| 157 | +})); |
| 158 | +``` |
0 commit comments