|
| 1 | +# Part 1.1: Extract Pure Pattern Analyzers |
| 2 | + |
| 3 | +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. |
| 4 | +
|
| 5 | +**Goal:** Extract the pattern analysis logic from private methods into exported pure functions that are testable without file I/O. |
| 6 | + |
| 7 | +**User stories:** US-1, US-3 |
| 8 | + |
| 9 | +**Files:** |
| 10 | +- Modify: `packages/core/src/services/pattern-analysis-service.ts` |
| 11 | +- Modify: `packages/core/src/services/__tests__/pattern-analysis-service.test.ts` |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Task 1: Write tests for pure extraction functions |
| 16 | + |
| 17 | +- [ ] **Step 1: Add test block for extractImportStyleFromContent** |
| 18 | + |
| 19 | +```typescript |
| 20 | +// In pattern-analysis-service.test.ts, add: |
| 21 | +import { |
| 22 | + extractImportStyleFromContent, |
| 23 | + extractErrorHandlingFromContent, |
| 24 | + extractTypeCoverageFromSignatures, |
| 25 | +} from '../pattern-analysis-service'; |
| 26 | + |
| 27 | +describe('Pure Pattern Extractors', () => { |
| 28 | + describe('extractImportStyleFromContent', () => { |
| 29 | + it('should detect ESM imports', () => { |
| 30 | + const content = 'import { foo } from "./bar";\nimport * as baz from "baz";'; |
| 31 | + const result = extractImportStyleFromContent(content); |
| 32 | + expect(result).toEqual({ style: 'esm', importCount: 2 }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should detect CJS requires', () => { |
| 36 | + const content = 'const foo = require("bar");\nconst baz = require("baz");'; |
| 37 | + const result = extractImportStyleFromContent(content); |
| 38 | + expect(result).toEqual({ style: 'cjs', importCount: 2 }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should detect mixed imports', () => { |
| 42 | + const content = 'import { foo } from "./bar";\nconst baz = require("baz");'; |
| 43 | + const result = extractImportStyleFromContent(content); |
| 44 | + expect(result).toEqual({ style: 'mixed', importCount: 2 }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return unknown for no imports', () => { |
| 48 | + const content = 'const x = 1;'; |
| 49 | + const result = extractImportStyleFromContent(content); |
| 50 | + expect(result).toEqual({ style: 'unknown', importCount: 0 }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('extractErrorHandlingFromContent', () => { |
| 55 | + it('should detect throw style', () => { |
| 56 | + const content = 'throw new Error("oops");\nthrow new TypeError("bad");'; |
| 57 | + const result = extractErrorHandlingFromContent(content); |
| 58 | + expect(result.style).toBe('throw'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should return unknown for no error handling', () => { |
| 62 | + const content = 'const x = 1;'; |
| 63 | + const result = extractErrorHandlingFromContent(content); |
| 64 | + expect(result.style).toBe('unknown'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('extractTypeCoverageFromSignatures', () => { |
| 69 | + it('should detect full coverage', () => { |
| 70 | + const signatures = [ |
| 71 | + 'function foo(x: string): number', |
| 72 | + 'function bar(y: boolean): void', |
| 73 | + ]; |
| 74 | + const result = extractTypeCoverageFromSignatures(signatures); |
| 75 | + expect(result.coverage).toBe('full'); |
| 76 | + expect(result.annotatedCount).toBe(2); |
| 77 | + expect(result.totalCount).toBe(2); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should detect partial coverage', () => { |
| 81 | + const signatures = [ |
| 82 | + 'function foo(x: string): number', |
| 83 | + 'function bar(y)', |
| 84 | + ]; |
| 85 | + const result = extractTypeCoverageFromSignatures(signatures); |
| 86 | + expect(result.coverage).toBe('partial'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return none for empty', () => { |
| 90 | + const result = extractTypeCoverageFromSignatures([]); |
| 91 | + expect(result.coverage).toBe('none'); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +- [ ] **Step 2: Run tests to verify they fail** |
| 98 | + |
| 99 | +Run: `pnpm test -- packages/core/src/services/__tests__/pattern-analysis-service.test.ts` |
| 100 | +Expected: FAIL — functions not exported |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Task 2: Implement and export pure functions |
| 105 | + |
| 106 | +- [ ] **Step 1: Add exported pure functions to pattern-analysis-service.ts** |
| 107 | + |
| 108 | +Add before the class definition: |
| 109 | + |
| 110 | +```typescript |
| 111 | +/** |
| 112 | + * Extract import style from raw file content. Pure function — no I/O. |
| 113 | + */ |
| 114 | +export function extractImportStyleFromContent(content: string): ImportStylePattern { |
| 115 | + const esmImports = content.match(/^import\s/gm) || []; |
| 116 | + const cjsImports = content.match(/require\s*\(/g) || []; |
| 117 | + const hasESM = esmImports.length > 0; |
| 118 | + const hasCJS = cjsImports.length > 0; |
| 119 | + |
| 120 | + if (!hasESM && !hasCJS) return { style: 'unknown', importCount: 0 }; |
| 121 | + |
| 122 | + const importCount = esmImports.length + cjsImports.length; |
| 123 | + const style: ImportStylePattern['style'] = |
| 124 | + hasESM && hasCJS ? 'mixed' : hasESM ? 'esm' : 'cjs'; |
| 125 | + return { style, importCount }; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Extract error handling pattern from raw file content. Pure function — no I/O. |
| 130 | + */ |
| 131 | +export function extractErrorHandlingFromContent(content: string): ErrorHandlingPattern { |
| 132 | + const counts = { |
| 133 | + throw: [...content.matchAll(/throw\s+new\s+\w*Error/g)].length, |
| 134 | + result: [...content.matchAll(/Result<|{\s*ok:\s*(true|false)/g)].length, |
| 135 | + errorReturn: [...content.matchAll(/\)\s*:\s*\([^)]*,\s*error\)/g)].length, |
| 136 | + }; |
| 137 | + const total = counts.throw + counts.result + counts.errorReturn; |
| 138 | + if (total === 0) return { style: 'unknown', examples: [] }; |
| 139 | + |
| 140 | + const max = Math.max(counts.throw, counts.result, counts.errorReturn); |
| 141 | + const hasMultiple = Object.values(counts).filter((c) => c > 0).length > 1; |
| 142 | + let style: ErrorHandlingPattern['style'] = 'unknown'; |
| 143 | + if (hasMultiple) style = 'mixed'; |
| 144 | + else if (counts.throw === max) style = 'throw'; |
| 145 | + else if (counts.result === max) style = 'result'; |
| 146 | + else if (counts.errorReturn === max) style = 'error-return'; |
| 147 | + return { style, examples: [] }; |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Extract type coverage from function/method signatures. Pure function — no I/O. |
| 152 | + */ |
| 153 | +export function extractTypeCoverageFromSignatures(signatures: string[]): TypeAnnotationPattern { |
| 154 | + if (signatures.length === 0) return { coverage: 'none', annotatedCount: 0, totalCount: 0 }; |
| 155 | + |
| 156 | + const annotated = signatures.filter((sig) => /(\)|=>)\s*:\s*\w+/.test(sig)); |
| 157 | + const ratio = annotated.length / signatures.length; |
| 158 | + let coverage: TypeAnnotationPattern['coverage']; |
| 159 | + if (ratio >= 0.9) coverage = 'full'; |
| 160 | + else if (ratio >= 0.5) coverage = 'partial'; |
| 161 | + else if (ratio > 0) coverage = 'minimal'; |
| 162 | + else coverage = 'none'; |
| 163 | + return { coverage, annotatedCount: annotated.length, totalCount: signatures.length }; |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +- [ ] **Step 2: Run tests to verify they pass** |
| 168 | + |
| 169 | +Run: `pnpm test -- packages/core/src/services/__tests__/pattern-analysis-service.test.ts` |
| 170 | +Expected: ALL PASS (new + existing) |
| 171 | + |
| 172 | +- [ ] **Step 3: Commit** |
| 173 | + |
| 174 | +```bash |
| 175 | +git add packages/core/src/services/pattern-analysis-service.ts packages/core/src/services/__tests__/pattern-analysis-service.test.ts |
| 176 | +git commit -m "refactor(core): extract pure pattern analyzers for testability" |
| 177 | +``` |
0 commit comments