specweave 0.23.18 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/.claude-plugin/marketplace.json +93 -49
  2. package/CLAUDE.md +137 -4
  3. package/dist/src/cli/helpers/ado-area-path-mapper.d.ts +89 -0
  4. package/dist/src/cli/helpers/ado-area-path-mapper.d.ts.map +1 -0
  5. package/dist/src/cli/helpers/ado-area-path-mapper.js +213 -0
  6. package/dist/src/cli/helpers/ado-area-path-mapper.js.map +1 -0
  7. package/dist/src/cli/helpers/issue-tracker/ado-auto-discover.d.ts +29 -0
  8. package/dist/src/cli/helpers/issue-tracker/ado-auto-discover.d.ts.map +1 -0
  9. package/dist/src/cli/helpers/issue-tracker/ado-auto-discover.js +109 -0
  10. package/dist/src/cli/helpers/issue-tracker/ado-auto-discover.js.map +1 -0
  11. package/dist/src/cli/helpers/issue-tracker/ado.d.ts +1 -0
  12. package/dist/src/cli/helpers/issue-tracker/ado.d.ts.map +1 -1
  13. package/dist/src/cli/helpers/issue-tracker/ado.js +2 -0
  14. package/dist/src/cli/helpers/issue-tracker/ado.js.map +1 -1
  15. package/dist/src/cli/helpers/smart-filter.d.ts +83 -0
  16. package/dist/src/cli/helpers/smart-filter.d.ts.map +1 -0
  17. package/dist/src/cli/helpers/smart-filter.js +265 -0
  18. package/dist/src/cli/helpers/smart-filter.js.map +1 -0
  19. package/dist/src/core/qa/quality-gate-decider.d.ts +1 -1
  20. package/dist/src/core/qa/quality-gate-decider.js +2 -2
  21. package/dist/src/core/qa/quality-gate-decider.js.map +1 -1
  22. package/dist/src/core/qa/risk-calculator.d.ts +2 -2
  23. package/dist/src/core/qa/risk-calculator.js +2 -2
  24. package/dist/src/core/validators/ac-presence-validator.d.ts +56 -0
  25. package/dist/src/core/validators/ac-presence-validator.d.ts.map +1 -0
  26. package/dist/src/core/validators/ac-presence-validator.js +149 -0
  27. package/dist/src/core/validators/ac-presence-validator.js.map +1 -0
  28. package/dist/src/integrations/ado/area-path-mapper.d.ts +137 -0
  29. package/dist/src/integrations/ado/area-path-mapper.d.ts.map +1 -0
  30. package/dist/src/integrations/ado/area-path-mapper.js +267 -0
  31. package/dist/src/integrations/ado/area-path-mapper.js.map +1 -0
  32. package/dist/src/integrations/jira/filter-processor.d.ts +126 -0
  33. package/dist/src/integrations/jira/filter-processor.d.ts.map +1 -0
  34. package/dist/src/integrations/jira/filter-processor.js +207 -0
  35. package/dist/src/integrations/jira/filter-processor.js.map +1 -0
  36. package/dist/src/integrations/jira/jira-client.d.ts +13 -0
  37. package/dist/src/integrations/jira/jira-client.d.ts.map +1 -1
  38. package/dist/src/integrations/jira/jira-client.js +33 -0
  39. package/dist/src/integrations/jira/jira-client.js.map +1 -1
  40. package/dist/src/utils/ac-embedder.d.ts +63 -0
  41. package/dist/src/utils/ac-embedder.d.ts.map +1 -0
  42. package/dist/src/utils/ac-embedder.js +217 -0
  43. package/dist/src/utils/ac-embedder.js.map +1 -0
  44. package/dist/src/utils/env-manager.d.ts +86 -0
  45. package/dist/src/utils/env-manager.d.ts.map +1 -0
  46. package/dist/src/utils/env-manager.js +188 -0
  47. package/dist/src/utils/env-manager.js.map +1 -0
  48. package/package.json +1 -1
  49. package/plugins/specweave/.claude-plugin/plugin.json +1 -1
  50. package/plugins/specweave/agents/AGENTS-INDEX.md +1 -1
  51. package/plugins/specweave/agents/increment-quality-judge-v2/AGENT.md +9 -9
  52. package/plugins/specweave/commands/specweave-do.md +37 -0
  53. package/plugins/specweave/commands/specweave-done.md +159 -0
  54. package/plugins/specweave/commands/specweave-embed-acs.md +446 -0
  55. package/plugins/specweave/commands/specweave-next.md +148 -3
  56. package/plugins/specweave/commands/specweave-qa.md +2 -2
  57. package/plugins/specweave/hooks/pre-increment-start.sh +168 -0
  58. package/plugins/specweave/skills/SKILLS-INDEX.md +1 -1
  59. package/plugins/specweave-ado/.claude-plugin/plugin.json +1 -1
  60. package/plugins/specweave-ado/commands/specweave-ado-import-projects.md +331 -0
  61. package/plugins/specweave-alternatives/.claude-plugin/plugin.json +10 -0
  62. package/plugins/specweave-alternatives/commands/alternatives-analyze.md +336 -0
  63. package/plugins/specweave-alternatives/skills/architecture-alternatives/SKILL.md +651 -0
  64. package/plugins/specweave-alternatives/skills/bmad-method/SKILL.md +420 -0
  65. package/plugins/specweave-alternatives/skills/spec-kit-expert/SKILL.md +487 -0
  66. package/plugins/specweave-backend/commands/api-scaffold.md +80 -0
  67. package/plugins/specweave-backend/commands/crud-generate.md +109 -0
  68. package/plugins/specweave-backend/commands/migration-generate.md +139 -0
  69. package/plugins/specweave-confluent/commands/connector-deploy.md +154 -0
  70. package/plugins/specweave-confluent/commands/ksqldb-query.md +179 -0
  71. package/plugins/specweave-confluent/commands/schema-register.md +123 -0
  72. package/plugins/specweave-core/.claude-plugin/plugin.json +21 -0
  73. package/plugins/specweave-core/commands/architecture-review.md +288 -0
  74. package/plugins/specweave-core/commands/code-review.md +213 -0
  75. package/plugins/specweave-core/commands/refactor-plan.md +249 -0
  76. package/plugins/specweave-core/skills/code-quality/SKILL.md +157 -0
  77. package/plugins/specweave-core/skills/design-patterns/SKILL.md +244 -0
  78. package/plugins/specweave-core/skills/software-architecture/SKILL.md +83 -0
  79. package/plugins/specweave-cost-optimizer/.claude-plugin/plugin.json +22 -0
  80. package/plugins/specweave-cost-optimizer/commands/cost-analyze.md +360 -0
  81. package/plugins/specweave-cost-optimizer/commands/cost-optimize.md +480 -0
  82. package/plugins/specweave-cost-optimizer/skills/aws-cost-expert/SKILL.md +416 -0
  83. package/plugins/specweave-cost-optimizer/skills/cloud-pricing/SKILL.md +325 -0
  84. package/plugins/specweave-cost-optimizer/skills/cost-optimization/SKILL.md +337 -0
  85. package/plugins/specweave-diagrams/.claude-plugin/plugin.json +1 -1
  86. package/plugins/specweave-diagrams/commands/diagrams-generate.md +168 -0
  87. package/plugins/specweave-docs/.claude-plugin/plugin.json +10 -0
  88. package/plugins/specweave-docs/commands/docs-generate.md +441 -0
  89. package/plugins/specweave-docs/commands/docs-init.md +334 -0
  90. package/plugins/specweave-docs/skills/docusaurus/SKILL.md +581 -0
  91. package/plugins/specweave-docs/skills/spec-driven-brainstorming/SKILL.md +689 -0
  92. package/plugins/specweave-docs/skills/technical-writing/SKILL.md +1039 -0
  93. package/plugins/specweave-docs-preview/.claude-plugin/plugin.json +1 -1
  94. package/plugins/specweave-figma/.claude-plugin/plugin.json +23 -0
  95. package/plugins/specweave-figma/commands/figma-import.md +690 -0
  96. package/plugins/specweave-figma/commands/figma-to-react.md +834 -0
  97. package/plugins/specweave-figma/commands/figma-tokens.md +815 -0
  98. package/plugins/specweave-frontend/.claude-plugin/plugin.json +21 -0
  99. package/plugins/specweave-frontend/agents/frontend-architect/AGENT.md +387 -0
  100. package/plugins/specweave-frontend/agents/frontend-architect/README.md +385 -0
  101. package/plugins/specweave-frontend/agents/frontend-architect/examples.md +590 -0
  102. package/plugins/specweave-frontend/agents/frontend-architect/templates/component-template.tsx +152 -0
  103. package/plugins/specweave-frontend/agents/frontend-architect/templates/hook-template.ts +311 -0
  104. package/plugins/specweave-frontend/agents/frontend-architect/templates/page-template.tsx +228 -0
  105. package/plugins/specweave-frontend/commands/component-generate.md +510 -0
  106. package/plugins/specweave-frontend/commands/design-system-init.md +494 -0
  107. package/plugins/specweave-frontend/commands/frontend-scaffold.md +207 -0
  108. package/plugins/specweave-frontend/commands/nextjs-setup.md +396 -0
  109. package/plugins/specweave-frontend/skills/design-system-architect/SKILL.md +278 -0
  110. package/plugins/specweave-frontend/skills/frontend/SKILL.md +420 -0
  111. package/plugins/specweave-frontend/skills/nextjs/SKILL.md +546 -0
  112. package/plugins/specweave-github/.claude-plugin/plugin.json +1 -1
  113. package/plugins/specweave-github/hooks/.specweave/logs/hooks-debug.log +194 -0
  114. package/plugins/specweave-infrastructure/.claude-plugin/plugin.json +1 -1
  115. package/plugins/specweave-jira/.claude-plugin/plugin.json +1 -1
  116. package/plugins/specweave-jira/commands/import-projects.js +183 -0
  117. package/plugins/specweave-jira/commands/import-projects.md +97 -0
  118. package/plugins/specweave-jira/commands/import-projects.ts +288 -0
  119. package/plugins/specweave-jira/commands/specweave-jira-import-projects.md +298 -0
  120. package/plugins/specweave-kafka/.claude-plugin/plugin.json +1 -1
  121. package/plugins/specweave-kafka-streams/.claude-plugin/plugin.json +1 -1
  122. package/plugins/specweave-kubernetes/commands/cluster-setup.md +262 -0
  123. package/plugins/specweave-kubernetes/commands/deployment-generate.md +242 -0
  124. package/plugins/specweave-kubernetes/commands/helm-scaffold.md +333 -0
  125. package/plugins/specweave-ml/.claude-plugin/plugin.json +1 -1
  126. package/plugins/specweave-mobile/commands/app-scaffold.md +233 -0
  127. package/plugins/specweave-mobile/commands/build-config.md +256 -0
  128. package/plugins/specweave-mobile/commands/screen-generate.md +289 -0
  129. package/plugins/specweave-n8n/.claude-plugin/plugin.json +1 -1
  130. package/plugins/specweave-plugin-dev/.claude-plugin/plugin.json +13 -12
  131. package/plugins/specweave-plugin-dev/commands/plugin-create.md +333 -0
  132. package/plugins/specweave-plugin-dev/commands/plugin-publish.md +339 -0
  133. package/plugins/specweave-plugin-dev/commands/plugin-test.md +293 -0
  134. package/plugins/specweave-plugin-dev/skills/claude-sdk/SKILL.md +162 -0
  135. package/plugins/specweave-plugin-dev/skills/marketplace-publishing/SKILL.md +263 -0
  136. package/plugins/specweave-plugin-dev/skills/plugin-development/SKILL.md +316 -0
  137. package/plugins/specweave-release/.claude-plugin/plugin.json +1 -1
  138. package/plugins/specweave-release/commands/specweave-release-npm.md +110 -0
  139. package/plugins/specweave-release/hooks/.specweave/logs/dora-tracking.log +168 -0
  140. package/plugins/specweave-testing/.claude-plugin/plugin.json +21 -0
  141. package/plugins/specweave-testing/agents/qa-engineer/AGENT.md +797 -0
  142. package/plugins/specweave-testing/agents/qa-engineer/README.md +443 -0
  143. package/plugins/specweave-testing/agents/qa-engineer/templates/playwright-e2e-test.ts +470 -0
  144. package/plugins/specweave-testing/agents/qa-engineer/templates/test-data-factory.ts +507 -0
  145. package/plugins/specweave-testing/agents/qa-engineer/templates/vitest-unit-test.ts +400 -0
  146. package/plugins/specweave-testing/agents/qa-engineer/test-strategies.md +726 -0
  147. package/plugins/specweave-testing/commands/e2e-setup.md +1081 -0
  148. package/plugins/specweave-testing/commands/test-coverage.md +979 -0
  149. package/plugins/specweave-testing/commands/test-generate.md +1156 -0
  150. package/plugins/specweave-testing/commands/test-init.md +409 -0
  151. package/plugins/specweave-testing/skills/e2e-playwright/SKILL.md +769 -0
  152. package/plugins/specweave-testing/skills/tdd-expert/SKILL.md +934 -0
  153. package/plugins/specweave-testing/skills/unit-testing-expert/SKILL.md +1011 -0
  154. package/plugins/specweave-tooling/.claude-plugin/plugin.json +22 -0
  155. package/plugins/specweave-tooling/commands/specweave-tooling-skill-create.md +691 -0
  156. package/plugins/specweave-tooling/commands/specweave-tooling-skill-package.md +751 -0
  157. package/plugins/specweave-tooling/commands/specweave-tooling-skill-validate.md +858 -0
  158. package/plugins/specweave-ui/.claude-plugin/plugin.json +10 -0
  159. package/plugins/specweave-ui/commands/ui-automate.md +199 -0
  160. package/plugins/specweave-ui/commands/ui-inspect.md +70 -0
  161. package/plugins/specweave-ui/skills/browser-automation/SKILL.md +314 -0
  162. package/plugins/specweave-ui/skills/ui-testing/SKILL.md +716 -0
  163. package/plugins/specweave-ui/skills/visual-regression/SKILL.md +728 -0
  164. package/plugins/specweave/commands/check-hooks.md +0 -257
  165. package/plugins/specweave/commands/specweave-archive-increments.md +0 -82
  166. package/plugins/specweave-plugin-dev/skills/plugin-expert/SKILL.md +0 -1231
  167. /package/plugins/specweave/{agents/code-reviewer.md → skills/code-reviewer/SKILL.md} +0 -0
@@ -0,0 +1,249 @@
1
+ # /specweave-core:refactor-plan
2
+
3
+ Generate comprehensive refactoring plans with risk assessment, step-by-step execution, and rollback strategies.
4
+
5
+ You are an expert software architect who plans safe, incremental refactoring strategies.
6
+
7
+ ## Your Task
8
+
9
+ Create detailed refactoring plans that minimize risk while improving code quality.
10
+
11
+ ### 1. Refactoring Patterns
12
+
13
+ **Extract Method**:
14
+ ```typescript
15
+ // Before: Long function with multiple responsibilities
16
+ function processOrder(order: Order) {
17
+ // 50 lines of validation
18
+ // 30 lines of calculation
19
+ // 40 lines of persistence
20
+ }
21
+
22
+ // After: Small, focused functions
23
+ function processOrder(order: Order) {
24
+ validateOrder(order);
25
+ const total = calculateTotal(order);
26
+ saveOrder(order, total);
27
+ }
28
+ ```
29
+
30
+ **Extract Class**:
31
+ ```typescript
32
+ // Before: God class
33
+ class User {
34
+ // User properties
35
+ // Email sending logic
36
+ // Password hashing logic
37
+ // Notification logic
38
+ }
39
+
40
+ // After: Single responsibility
41
+ class User { /* core user data */ }
42
+ class EmailService { /* email logic */ }
43
+ class PasswordHasher { /* password logic */ }
44
+ class NotificationService { /* notifications */ }
45
+ ```
46
+
47
+ **Replace Conditional with Polymorphism**:
48
+ ```typescript
49
+ // Before: Switch statements
50
+ function calculateShipping(type: string, weight: number) {
51
+ switch (type) {
52
+ case 'express': return weight * 5;
53
+ case 'standard': return weight * 2;
54
+ case 'economy': return weight * 1;
55
+ }
56
+ }
57
+
58
+ // After: Strategy pattern
59
+ interface ShippingStrategy {
60
+ calculate(weight: number): number;
61
+ }
62
+
63
+ class ExpressShipping implements ShippingStrategy {
64
+ calculate(weight: number) { return weight * 5; }
65
+ }
66
+ ```
67
+
68
+ **Introduce Parameter Object**:
69
+ ```typescript
70
+ // Before: Long parameter lists
71
+ function createUser(name: string, email: string, age: number, address: string, phone: string)
72
+
73
+ // After: Parameter object
74
+ interface UserData {
75
+ name: string;
76
+ email: string;
77
+ age: number;
78
+ address: string;
79
+ phone: string;
80
+ }
81
+
82
+ function createUser(data: UserData)
83
+ ```
84
+
85
+ ### 2. Refactoring Plan Template
86
+
87
+ ```markdown
88
+ ## Refactoring Plan: Extract Payment Processing
89
+
90
+ ### Current State
91
+ - 500-line PaymentController with mixed responsibilities
92
+ - Tightly coupled to database layer
93
+ - No unit tests (integration tests only)
94
+ - Duplicate payment validation logic in 3 places
95
+
96
+ ### Target State
97
+ - Payment domain services (validation, processing, reconciliation)
98
+ - Repository pattern for data access
99
+ - 80%+ unit test coverage
100
+ - Single source of truth for validation
101
+
102
+ ### Risk Assessment
103
+ **Risk Level**: Medium
104
+
105
+ **Risks**:
106
+ 1. Breaking existing payment flows (HIGH IMPACT)
107
+ 2. Race conditions in concurrent payments (MEDIUM)
108
+ 3. Performance regression (LOW)
109
+
110
+ **Mitigation**:
111
+ 1. Feature flag + parallel run (old + new code)
112
+ 2. Transaction isolation + row-level locking
113
+ 3. Performance testing before rollout
114
+
115
+ ### Prerequisites
116
+ - [ ] 100% integration test coverage of current behavior
117
+ - [ ] Performance baseline established
118
+ - [ ] Database migrations prepared
119
+ - [ ] Rollback plan documented
120
+ - [ ] Team review and approval
121
+
122
+ ### Execution Steps (10 steps, 2 weeks)
123
+
124
+ **Week 1: Preparation + Safe Extractions**
125
+
126
+ **Step 1** (Day 1): Add comprehensive integration tests
127
+ - Test all payment flows
128
+ - Test error scenarios
129
+ - Test edge cases (concurrent payments, retries)
130
+
131
+ **Step 2** (Day 2): Extract validation logic
132
+ - Create PaymentValidator class
133
+ - Move validation from controller
134
+ - Tests: unit tests for validator
135
+ - **Safe**: Pure functions, no state changes
136
+
137
+ **Step 3** (Day 3): Extract payment processing
138
+ - Create PaymentProcessor service
139
+ - Move core processing logic
140
+ - Tests: unit tests with mocks
141
+ - **Safe**: Existing controller calls new service
142
+
143
+ **Week 2: Risky Changes + Rollout**
144
+
145
+ **Step 7** (Day 8): Introduce repository pattern
146
+ - Create PaymentRepository interface
147
+ - Implement with existing DB calls
148
+ - **Risk**: Data access changes
149
+ - **Mitigation**: Feature flag, parallel run
150
+
151
+ **Step 10** (Day 10): Final cutover
152
+ - Enable new code for 100% traffic
153
+ - Monitor for 48 hours
154
+ - Remove old code if stable
155
+
156
+ ### Testing Strategy
157
+ - Unit tests: 80%+ coverage
158
+ - Integration tests: All payment flows
159
+ - Load testing: 2x expected peak traffic
160
+ - Canary deployment: 1% → 10% → 50% → 100%
161
+
162
+ ### Rollback Plan
163
+ - Feature flag: Instant rollback to old code
164
+ - Database: Backward-compatible migrations
165
+ - Monitoring: Alert on error rate > 0.1%
166
+ - Rollback trigger: Any payment processing failure
167
+
168
+ ### Success Metrics
169
+ - ✅ All tests passing
170
+ - ✅ Code coverage > 80%
171
+ - ✅ Performance within 10% of baseline
172
+ - ✅ Zero production incidents
173
+ - ✅ Team velocity unchanged
174
+ ```
175
+
176
+ ### 3. Safety Strategies
177
+
178
+ **Strangler Fig Pattern**:
179
+ ```typescript
180
+ // Gradually replace old system
181
+ class PaymentController {
182
+ async processPayment(order: Order) {
183
+ if (featureFlags.newPaymentFlow) {
184
+ return newPaymentService.process(order); // New code
185
+ } else {
186
+ return legacyPaymentLogic(order); // Old code
187
+ }
188
+ }
189
+ }
190
+ ```
191
+
192
+ **Parallel Run**:
193
+ ```typescript
194
+ // Run both old and new, compare results
195
+ async function processPayment(order: Order) {
196
+ const oldResult = await legacyPayment(order);
197
+
198
+ // Run new code in background, don't block
199
+ backgroundTask(async () => {
200
+ const newResult = await newPayment(order);
201
+ if (!isEqual(oldResult, newResult)) {
202
+ logger.warn('Payment results differ', { old: oldResult, new: newResult });
203
+ }
204
+ });
205
+
206
+ return oldResult; // Use old result (safe)
207
+ }
208
+ ```
209
+
210
+ ### 4. Code Smell Prioritization
211
+
212
+ **High Priority** (Security/Bugs):
213
+ - Null reference errors
214
+ - Memory leaks
215
+ - Race conditions
216
+ - Security vulnerabilities
217
+
218
+ **Medium Priority** (Maintainability):
219
+ - God classes (> 500 lines)
220
+ - Long functions (> 50 lines)
221
+ - Cyclomatic complexity > 10
222
+ - Code duplication (> 5 instances)
223
+
224
+ **Low Priority** (Cleanup):
225
+ - Dead code
226
+ - Unused imports
227
+ - TODOs
228
+ - Console.log statements
229
+
230
+ ### 5. Workflow
231
+
232
+ 1. **Identify Target**: What needs refactoring?
233
+ 2. **Assess Risk**: Impact analysis
234
+ 3. **Create Plan**: Step-by-step breakdown
235
+ 4. **Get Buy-in**: Team review
236
+ 5. **Prepare**: Tests, baselines, migrations
237
+ 6. **Execute**: Incremental changes
238
+ 7. **Monitor**: Metrics, alerts
239
+ 8. **Document**: Update architecture docs
240
+
241
+ ## When to Use
242
+
243
+ - Planning large-scale refactoring
244
+ - Improving legacy codebases
245
+ - Reducing technical debt
246
+ - Preparing for new features
247
+ - Post-incident code improvements
248
+
249
+ Refactor safely with detailed planning!
@@ -0,0 +1,157 @@
1
+ ---
2
+ name: code-quality
3
+ description: Expert code quality engineering covering clean code principles, SOLID, DRY, KISS, YAGNI, code smells, refactoring patterns, static analysis, linting, code coverage, mutation testing, and software craftsmanship. Activates for code quality, clean code, SOLID principles, code smells, refactoring, technical debt, code review, linting, eslint, prettier, static analysis, code coverage.
4
+ ---
5
+
6
+ # Code Quality Expert
7
+
8
+ Master of clean code principles, SOLID, and software craftsmanship.
9
+
10
+ ## SOLID Principles
11
+
12
+ **Single Responsibility**:
13
+ ```typescript
14
+ // ❌ Bad: Multiple responsibilities
15
+ class User {
16
+ save() { /* database logic */ }
17
+ sendEmail() { /* email logic */ }
18
+ hashPassword() { /* crypto logic */ }
19
+ }
20
+
21
+ // ✅ Good: Single responsibility
22
+ class User { /* user data only */ }
23
+ class UserRepository { save(user: User) {} }
24
+ class EmailService { send(to: string, message: string) {} }
25
+ class PasswordHasher { hash(password: string) {} }
26
+ ```
27
+
28
+ **Open/Closed**:
29
+ ```typescript
30
+ // ✅ Open for extension, closed for modification
31
+ interface PaymentMethod {
32
+ processPayment(amount: number): Promise<void>;
33
+ }
34
+
35
+ class CreditCardPayment implements PaymentMethod {
36
+ async processPayment(amount: number) { /* ... */ }
37
+ }
38
+
39
+ class PayPalPayment implements PaymentMethod {
40
+ async processPayment(amount: number) { /* ... */ }
41
+ }
42
+
43
+ // Add new payment methods without modifying existing code
44
+ ```
45
+
46
+ **Liskov Substitution**:
47
+ ```typescript
48
+ // Subtypes must be substitutable for base types
49
+ class Bird {
50
+ fly() { /* ... */ }
51
+ }
52
+
53
+ // ❌ Bad: Penguin can't fly, violates LSP
54
+ class Penguin extends Bird {
55
+ fly() { throw new Error('Cannot fly'); }
56
+ }
57
+
58
+ // ✅ Good: Proper abstraction
59
+ interface Bird {}
60
+ interface FlyingBird extends Bird { fly(): void; }
61
+ class Sparrow implements FlyingBird { fly() {} }
62
+ class Penguin implements Bird {} // No fly method
63
+ ```
64
+
65
+ **Interface Segregation**:
66
+ ```typescript
67
+ // ❌ Bad: Fat interface
68
+ interface Worker {
69
+ work(): void;
70
+ eat(): void;
71
+ sleep(): void;
72
+ }
73
+
74
+ // ✅ Good: Segregated interfaces
75
+ interface Workable { work(): void; }
76
+ interface Eatable { eat(): void; }
77
+ interface Sleepable { sleep(): void; }
78
+
79
+ class Human implements Workable, Eatable, Sleepable {}
80
+ class Robot implements Workable {} // Doesn't need eat/sleep
81
+ ```
82
+
83
+ **Dependency Inversion**:
84
+ ```typescript
85
+ // ❌ Bad: High-level depends on low-level
86
+ class EmailService {
87
+ private smtp = new SMTPClient(); // Direct dependency
88
+ }
89
+
90
+ // ✅ Good: Depend on abstraction
91
+ interface EmailClient {
92
+ send(to: string, message: string): Promise<void>;
93
+ }
94
+
95
+ class EmailService {
96
+ constructor(private client: EmailClient) {}
97
+ }
98
+ ```
99
+
100
+ ## Clean Code Principles
101
+
102
+ **DRY (Don't Repeat Yourself)**:
103
+ ```typescript
104
+ // ❌ Duplication
105
+ function validateEmail(email: string) {
106
+ return /^\S+@\S+\.\S+$/.test(email);
107
+ }
108
+ function validateUserEmail(email: string) {
109
+ return /^\S+@\S+\.\S+$/.test(email); // Duplicate
110
+ }
111
+
112
+ // ✅ Single source of truth
113
+ const EMAIL_REGEX = /^\S+@\S+\.\S+$/;
114
+ const isValidEmail = (email: string) => EMAIL_REGEX.test(email);
115
+ ```
116
+
117
+ **KISS (Keep It Simple)**:
118
+ ```typescript
119
+ // ❌ Over-engineered
120
+ class AdvancedCalculatorFactoryBuilderSingleton {
121
+ private static instance: AdvancedCalculatorFactoryBuilderSingleton;
122
+ // 50 lines of unnecessary abstraction
123
+ }
124
+
125
+ // ✅ Simple
126
+ const add = (a: number, b: number) => a + b;
127
+ ```
128
+
129
+ **YAGNI (You Aren't Gonna Need It)**:
130
+ ```typescript
131
+ // ❌ Premature abstraction
132
+ class User {
133
+ futureFeature1() {} // Not used yet
134
+ futureFeature2() {} // Not used yet
135
+ futureFeature3() {} // Not used yet
136
+ }
137
+
138
+ // ✅ Only what's needed now
139
+ class User {
140
+ getCurrentFeatures() {} // Actually used
141
+ }
142
+ ```
143
+
144
+ ## Code Smells
145
+
146
+ **Long Method**: > 50 lines → Extract methods
147
+ **Large Class**: > 300 lines → Extract classes
148
+ **Long Parameter List**: > 3 params → Parameter object
149
+ **Primitive Obsession**: Use value objects
150
+ **Data Clumps**: Group related data
151
+ **Switch Statements**: Replace with polymorphism
152
+
153
+ ## Testing Strategies
154
+
155
+ **Test Coverage**: 80%+ for critical paths
156
+ **Mutation Testing**: Ensure test quality
157
+ **Test Pyramid**: Many unit, few integration, minimal E2E
@@ -0,0 +1,244 @@
1
+ ---
2
+ name: design-patterns
3
+ description: Expert knowledge of Gang of Four (GoF) design patterns including creational (Singleton, Factory, Builder, Prototype), structural (Adapter, Decorator, Proxy, Facade), and behavioral (Strategy, Observer, Command, Template Method, Chain of Responsibility). Modern TypeScript/JavaScript implementations with real-world use cases. Activates for design patterns, factory pattern, singleton, strategy pattern, observer pattern, decorator pattern, adapter pattern, builder pattern, proxy pattern, facade pattern, template method.
4
+ ---
5
+
6
+ # Design Patterns Expert
7
+
8
+ Master of GoF design patterns with modern TypeScript implementations.
9
+
10
+ ## Creational Patterns
11
+
12
+ **Factory Pattern**:
13
+ ```typescript
14
+ interface Animal {
15
+ speak(): string;
16
+ }
17
+
18
+ class Dog implements Animal {
19
+ speak() { return 'Woof!'; }
20
+ }
21
+
22
+ class Cat implements Animal {
23
+ speak() { return 'Meow!'; }
24
+ }
25
+
26
+ class AnimalFactory {
27
+ static create(type: 'dog' | 'cat'): Animal {
28
+ switch (type) {
29
+ case 'dog': return new Dog();
30
+ case 'cat': return new Cat();
31
+ }
32
+ }
33
+ }
34
+ ```
35
+
36
+ **Singleton Pattern**:
37
+ ```typescript
38
+ class Database {
39
+ private static instance: Database;
40
+
41
+ private constructor() {} // Private constructor
42
+
43
+ static getInstance(): Database {
44
+ if (!Database.instance) {
45
+ Database.instance = new Database();
46
+ }
47
+ return Database.instance;
48
+ }
49
+ }
50
+ ```
51
+
52
+ **Builder Pattern**:
53
+ ```typescript
54
+ class UserBuilder {
55
+ private user: Partial<User> = {};
56
+
57
+ setName(name: string) {
58
+ this.user.name = name;
59
+ return this;
60
+ }
61
+
62
+ setEmail(email: string) {
63
+ this.user.email = email;
64
+ return this;
65
+ }
66
+
67
+ build(): User {
68
+ if (!this.user.name || !this.user.email) {
69
+ throw new Error('Missing required fields');
70
+ }
71
+ return this.user as User;
72
+ }
73
+ }
74
+
75
+ // Usage
76
+ const user = new UserBuilder()
77
+ .setName('John')
78
+ .setEmail('john@example.com')
79
+ .build();
80
+ ```
81
+
82
+ ## Structural Patterns
83
+
84
+ **Adapter Pattern**:
85
+ ```typescript
86
+ // Old API
87
+ class OldLogger {
88
+ logMessage(message: string) { console.log(message); }
89
+ }
90
+
91
+ // New interface
92
+ interface Logger {
93
+ log(level: string, message: string): void;
94
+ }
95
+
96
+ // Adapter
97
+ class LoggerAdapter implements Logger {
98
+ constructor(private oldLogger: OldLogger) {}
99
+
100
+ log(level: string, message: string) {
101
+ this.oldLogger.logMessage(`[${level}] ${message}`);
102
+ }
103
+ }
104
+ ```
105
+
106
+ **Decorator Pattern**:
107
+ ```typescript
108
+ interface Coffee {
109
+ cost(): number;
110
+ description(): string;
111
+ }
112
+
113
+ class SimpleCoffee implements Coffee {
114
+ cost() { return 5; }
115
+ description() { return 'Simple coffee'; }
116
+ }
117
+
118
+ class MilkDecorator implements Coffee {
119
+ constructor(private coffee: Coffee) {}
120
+
121
+ cost() { return this.coffee.cost() + 2; }
122
+ description() { return this.coffee.description() + ', milk'; }
123
+ }
124
+
125
+ // Usage
126
+ let coffee: Coffee = new SimpleCoffee();
127
+ coffee = new MilkDecorator(coffee);
128
+ coffee.cost(); // 7
129
+ ```
130
+
131
+ **Proxy Pattern**:
132
+ ```typescript
133
+ class RealImage {
134
+ constructor(private filename: string) {
135
+ this.loadFromDisk();
136
+ }
137
+
138
+ private loadFromDisk() {
139
+ console.log('Loading:', this.filename);
140
+ }
141
+
142
+ display() {
143
+ console.log('Displaying:', this.filename);
144
+ }
145
+ }
146
+
147
+ class ProxyImage {
148
+ private realImage?: RealImage;
149
+
150
+ constructor(private filename: string) {}
151
+
152
+ display() {
153
+ if (!this.realImage) {
154
+ this.realImage = new RealImage(this.filename); // Lazy loading
155
+ }
156
+ this.realImage.display();
157
+ }
158
+ }
159
+ ```
160
+
161
+ ## Behavioral Patterns
162
+
163
+ **Strategy Pattern**:
164
+ ```typescript
165
+ interface SortStrategy {
166
+ sort(data: number[]): number[];
167
+ }
168
+
169
+ class QuickSort implements SortStrategy {
170
+ sort(data: number[]) { /* quicksort */ return data; }
171
+ }
172
+
173
+ class MergeSort implements SortStrategy {
174
+ sort(data: number[]) { /* mergesort */ return data; }
175
+ }
176
+
177
+ class Sorter {
178
+ constructor(private strategy: SortStrategy) {}
179
+
180
+ setStrategy(strategy: SortStrategy) {
181
+ this.strategy = strategy;
182
+ }
183
+
184
+ sort(data: number[]) {
185
+ return this.strategy.sort(data);
186
+ }
187
+ }
188
+ ```
189
+
190
+ **Observer Pattern**:
191
+ ```typescript
192
+ interface Observer {
193
+ update(data: any): void;
194
+ }
195
+
196
+ class Subject {
197
+ private observers: Observer[] = [];
198
+
199
+ attach(observer: Observer) {
200
+ this.observers.push(observer);
201
+ }
202
+
203
+ notify(data: any) {
204
+ this.observers.forEach(o => o.update(data));
205
+ }
206
+ }
207
+
208
+ class EmailObserver implements Observer {
209
+ update(data: any) {
210
+ console.log('Sending email:', data);
211
+ }
212
+ }
213
+ ```
214
+
215
+ **Command Pattern**:
216
+ ```typescript
217
+ interface Command {
218
+ execute(): void;
219
+ undo(): void;
220
+ }
221
+
222
+ class SaveCommand implements Command {
223
+ constructor(private editor: Editor) {}
224
+
225
+ execute() { this.editor.save(); }
226
+ undo() { this.editor.restore(); }
227
+ }
228
+
229
+ class CommandInvoker {
230
+ private history: Command[] = [];
231
+
232
+ execute(command: Command) {
233
+ command.execute();
234
+ this.history.push(command);
235
+ }
236
+
237
+ undo() {
238
+ const command = this.history.pop();
239
+ command?.undo();
240
+ }
241
+ }
242
+ ```
243
+
244
+ Apply design patterns to solve common problems elegantly!