(lines)
| 19 | } |
| 20 | |
| 21 | function detectFunctions(lines) { |
| 22 | const functions = []; |
| 23 | const startMatchers = [ |
| 24 | /\b(?:async\s+)?function\s+([A-Za-z0-9_$]+)\s*\(([^)]*)\)\s*\{/, |
| 25 | /\b(?:const|let|var)\s+([A-Za-z0-9_$]+)\s*=\s*(?:async\s*)?\(([^)]*)\)\s*=>\s*\{/, |
| 26 | /^\s*(?:public|private|protected|static|readonly|\s)*(?!if\b|for\b|while\b|switch\b|catch\b)([A-Za-z0-9_$]+)\s*\(([^)]*)\)\s*\{/, |
| 27 | ]; |
| 28 | |
| 29 | for (let index = 0; index < lines.length; index += 1) { |
| 30 | const line = lines[index]; |
| 31 | const match = startMatchers.map((matcher) => matcher.exec(line)).find(Boolean); |
| 32 | if (!match) { |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | let braceBalance = countMatches(line, /\{/g) - countMatches(line, /\}/g); |
| 37 | let endIndex = index; |
| 38 | while (braceBalance > 0 && endIndex + 1 < lines.length) { |
| 39 | endIndex += 1; |
| 40 | braceBalance += countMatches(lines[endIndex], /\{/g) - countMatches(lines[endIndex], /\}/g); |
| 41 | } |
| 42 | |
| 43 | const slice = lines.slice(index, endIndex + 1); |
| 44 | const source = slice.join("\n"); |
| 45 | const params = match[2] |
| 46 | .split(",") |
| 47 | .map((item) => item.trim()) |
| 48 | .filter(Boolean).length; |
| 49 | |
| 50 | let braceDepth = 0; |
| 51 | let maxDepth = 0; |
| 52 | for (const sliceLine of slice) { |
| 53 | braceDepth += countMatches(sliceLine, /\{/g); |
| 54 | maxDepth = Math.max(maxDepth, braceDepth); |
| 55 | braceDepth -= countMatches(sliceLine, /\}/g); |
| 56 | } |
| 57 | |
| 58 | functions.push({ |
| 59 | name: match[1], |
| 60 | startLine: index + 1, |
| 61 | endLine: endIndex + 1, |
| 62 | length: slice.length, |
| 63 | params, |
| 64 | complexity: 1 + countDecisionPoints(source), |
| 65 | nesting: Math.max(0, maxDepth - 1), |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | return functions; |
| 70 | } |
| 71 | |
| 72 | export async function analyzeTypeScriptFiles(filePaths, rootPath) { |
| 73 | const checks = []; |
no test coverage detected