|
| 1 | +# Cursor Rules for @sfast/pattern-emitter-ts |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +This is a TypeScript library that extends Node.js EventEmitter with RegExp pattern matching support. |
| 5 | +- **Language**: TypeScript 5.7.2 |
| 6 | +- **Test Framework**: Jest 29.7.0 |
| 7 | +- **Code Style**: Google TypeScript Style (gts) |
| 8 | +- **Target**: Production library with 100% test coverage |
| 9 | + |
| 10 | +## Code Style & Standards |
| 11 | + |
| 12 | +### TypeScript |
| 13 | +- Use strict TypeScript mode (already configured in tsconfig.json) |
| 14 | +- Prefer explicit types over `any` (use `any` only when necessary for flexibility) |
| 15 | +- Use ES2020 features (target is ES2020) |
| 16 | +- Follow Google TypeScript Style guidelines |
| 17 | +- Use JSDoc comments for all public APIs |
| 18 | + |
| 19 | +### Naming Conventions |
| 20 | +- Classes: PascalCase (e.g., `PatternEmitter`) |
| 21 | +- Interfaces: PascalCase with `I` prefix (e.g., `IPatternEmitter`) |
| 22 | +- Types: PascalCase (e.g., `EventPattern`, `PatternListener`) |
| 23 | +- Private fields: camelCase with `_` prefix (e.g., `_listeners`) |
| 24 | +- Public methods: camelCase (e.g., `addListener`) |
| 25 | + |
| 26 | +### File Organization |
| 27 | +``` |
| 28 | +src/ |
| 29 | + index.ts # Main export |
| 30 | + patternEmitter.ts # Core implementation |
| 31 | + interface.ts # Interface definitions |
| 32 | + types.ts # Type definitions |
| 33 | + utils/ # Utility functions |
| 34 | +test/ |
| 35 | + patternEmitter.test.ts # All tests in one file |
| 36 | +``` |
| 37 | + |
| 38 | +## Development Guidelines |
| 39 | + |
| 40 | +### Testing Requirements |
| 41 | +- **CRITICAL**: Maintain 100% code coverage |
| 42 | +- Write tests for ALL new features before implementation |
| 43 | +- Test both string and RegExp event patterns |
| 44 | +- Test edge cases (empty listeners, removeAll, once, etc.) |
| 45 | +- Run `npm test` before committing |
| 46 | + |
| 47 | +### Code Quality Checks |
| 48 | +1. `npm run build` - Must compile without errors |
| 49 | +2. `npm test` - All tests must pass with 100% coverage |
| 50 | +3. `npm run lint` - No linting errors |
| 51 | +4. `npm run fix` - Auto-fix formatting issues |
| 52 | + |
| 53 | +### When Making Changes |
| 54 | + |
| 55 | +#### Adding New Features |
| 56 | +1. Update `src/interface.ts` if adding public API |
| 57 | +2. Update `src/types.ts` if adding new types |
| 58 | +3. Add JSDoc comments with `@param`, `@return`, `@example` |
| 59 | +4. Write tests first (TDD approach) |
| 60 | +5. Update README.md with usage examples |
| 61 | +6. Run `npm run docs` to regenerate documentation |
| 62 | + |
| 63 | +#### Modifying Core Logic |
| 64 | +- The `PatternEmitter` class wraps listeners with `wrapListener()` to maintain order |
| 65 | +- Listener order is preserved using a global index (`_globalListenerIndex`) |
| 66 | +- String events use native EventEmitter, RegExp events use custom `_listeners` Map |
| 67 | +- Never break backward compatibility with EventEmitter API |
| 68 | + |
| 69 | +#### Bug Fixes |
| 70 | +1. Write a failing test that reproduces the bug |
| 71 | +2. Fix the bug |
| 72 | +3. Verify test passes |
| 73 | +4. Check no other tests broke |
| 74 | + |
| 75 | +### Architecture Principles |
| 76 | + |
| 77 | +1. **Backward Compatibility**: Must remain 100% compatible with Node.js EventEmitter |
| 78 | +2. **Type Safety**: Leverage TypeScript for compile-time safety |
| 79 | +3. **Performance**: Optimize for event emission (hot path) |
| 80 | +4. **Simplicity**: Keep API surface small and intuitive |
| 81 | + |
| 82 | +### Key Implementation Details |
| 83 | + |
| 84 | +- `_actualListeners`: Maps wrapped listeners to original listeners |
| 85 | +- `_listeners`: Stores RegExp pattern listeners |
| 86 | +- `_regexMap`: Stores RegExp patterns by string representation |
| 87 | +- `wrapListener()`: Wraps each listener with an index for ordering |
| 88 | +- `getMatchingListeners()`: Collects all listeners (string + pattern) and sorts by index |
| 89 | + |
| 90 | +### Common Tasks |
| 91 | + |
| 92 | +**Add a new public method:** |
| 93 | +```typescript |
| 94 | +// 1. Add to interface |
| 95 | +interface IPatternEmitter { |
| 96 | + myNewMethod(arg: string): void; |
| 97 | +} |
| 98 | + |
| 99 | +// 2. Implement in PatternEmitter |
| 100 | +/** |
| 101 | + * Description of what it does |
| 102 | + * @param {string} arg - Description of parameter |
| 103 | + * @return {void} |
| 104 | + */ |
| 105 | +public myNewMethod(arg: string): void { |
| 106 | + // implementation |
| 107 | +} |
| 108 | + |
| 109 | +// 3. Write tests |
| 110 | +it('myNewMethod does X', () => { |
| 111 | + emitter.myNewMethod('test'); |
| 112 | + expect(...).to.equal(...); |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +**Add a new type:** |
| 117 | +```typescript |
| 118 | +// In src/types.ts |
| 119 | +export type MyNewType = string | number; |
| 120 | +``` |
| 121 | + |
| 122 | +### Testing Patterns |
| 123 | + |
| 124 | +```typescript |
| 125 | +// Use Chai assertions |
| 126 | +expect(value).to.equal(expected); |
| 127 | +expect(value).to.be.instanceOf(Class); |
| 128 | +expect(array).to.eql([...]); |
| 129 | + |
| 130 | +// Test listeners are called |
| 131 | +let called = false; |
| 132 | +emitter.on('event', () => { called = true; }); |
| 133 | +emitter.emit('event'); |
| 134 | +expect(called).to.equal(true); |
| 135 | + |
| 136 | +// Test RegExp patterns |
| 137 | +emitter.on(/^prefix/, handler); |
| 138 | +emitter.emit('prefix:something'); // Should match |
| 139 | +``` |
| 140 | + |
| 141 | +### Documentation |
| 142 | + |
| 143 | +- Use JSDoc with `@param`, `@return`, `@example` tags |
| 144 | +- Keep README.md examples up to date |
| 145 | +- Generate docs with `npm run docs` before releasing |
| 146 | + |
| 147 | +### Publishing Checklist |
| 148 | + |
| 149 | +1. Run all tests: `npm test` |
| 150 | +2. Run linter: `npm run lint` |
| 151 | +3. Update version in `package.json` |
| 152 | +4. Update `CHANGELOG.md` |
| 153 | +5. Build: `npm run build` |
| 154 | +6. Verify package contents: `npm pack --dry-run` |
| 155 | +7. Generate docs: `npm run docs` |
| 156 | +8. Commit and tag: `git tag v0.x.x` |
| 157 | + |
| 158 | +## Don't Do |
| 159 | + |
| 160 | +❌ Don't use `any` unless absolutely necessary |
| 161 | +❌ Don't break EventEmitter compatibility |
| 162 | +❌ Don't reduce test coverage below 100% |
| 163 | +❌ Don't commit without running tests |
| 164 | +❌ Don't modify `dist/` directly (it's generated) |
| 165 | +❌ Don't add dependencies (keep it zero-dependency) |
| 166 | +❌ Don't use console.log (use proper logging if needed) |
| 167 | +❌ Don't modify `.pnp.*` or `.yarn/` files |
| 168 | + |
| 169 | +## Do |
| 170 | + |
| 171 | +✅ Write tests first (TDD) |
| 172 | +✅ Use meaningful variable names |
| 173 | +✅ Add JSDoc comments for public APIs |
| 174 | +✅ Keep backward compatibility |
| 175 | +✅ Run `npm run fix` before committing |
| 176 | +✅ Update README for new features |
| 177 | +✅ Maintain 100% test coverage |
| 178 | +✅ Follow Google TypeScript Style |
| 179 | + |
| 180 | +## Useful Commands |
| 181 | + |
| 182 | +```bash |
| 183 | +npm run build # Compile TypeScript |
| 184 | +npm test # Run tests with coverage |
| 185 | +npm run lint # Check code style |
| 186 | +npm run fix # Auto-fix style issues |
| 187 | +npm run check # Run gts check |
| 188 | +npm run docs # Generate TypeDoc |
| 189 | +npm pack --dry-run # Preview npm package contents |
| 190 | +``` |
| 191 | + |
| 192 | +## Questions to Ask Before Changes |
| 193 | + |
| 194 | +1. Does this maintain EventEmitter compatibility? |
| 195 | +2. Are there tests for this change? |
| 196 | +3. Is test coverage still 100%? |
| 197 | +4. Does this follow the existing patterns in the codebase? |
| 198 | +5. Is the public API documented with JSDoc? |
| 199 | +6. Does this introduce any breaking changes? |
| 200 | + |
| 201 | +## When in Doubt |
| 202 | + |
| 203 | +- Follow existing code patterns in `src/patternEmitter.ts` |
| 204 | +- Check how EventEmitter does it |
| 205 | +- Prioritize simplicity over cleverness |
| 206 | +- Ask for clarification rather than assume |
| 207 | + |
0 commit comments