MCPcopy Create free account
hub / github.com/openai/plugins / detectFunctions

Function detectFunctions

plugins/plugin-eval/src/evaluators/typescript.js:21–70  ·  view source on GitHub ↗
(lines)

Source from the content-addressed store, hash-verified

19}
20
21function 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
72export async function analyzeTypeScriptFiles(filePaths, rootPath) {
73 const checks = [];

Callers 1

analyzeTypeScriptFilesFunction · 0.70

Calls 4

mapMethod · 0.80
filterMethod · 0.80
countMatchesFunction · 0.70
countDecisionPointsFunction · 0.70

Tested by

no test coverage detected