File size: 8,228 Bytes
40d7073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";
/**
 * Pattern Extraction Module - Consolidated code pattern detection
 *
 * Single source of truth for extracting functions, imports, exports, etc.
 * Used by native-worker.ts and parallel-workers.ts
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectLanguage = detectLanguage;
exports.extractFunctions = extractFunctions;
exports.extractClasses = extractClasses;
exports.extractImports = extractImports;
exports.extractExports = extractExports;
exports.extractTodos = extractTodos;
exports.extractAllPatterns = extractAllPatterns;
exports.extractFromFiles = extractFromFiles;
exports.toPatternMatches = toPatternMatches;
const fs = __importStar(require("fs"));
/**
 * Detect language from file extension
 */
function detectLanguage(file) {
    const ext = file.split('.').pop()?.toLowerCase() || '';
    const langMap = {
        ts: 'typescript', tsx: 'typescript', js: 'javascript', jsx: 'javascript',
        rs: 'rust', py: 'python', go: 'go', java: 'java', rb: 'ruby',
        cpp: 'cpp', c: 'c', h: 'c', hpp: 'cpp', cs: 'csharp',
        md: 'markdown', json: 'json', yaml: 'yaml', yml: 'yaml',
        sql: 'sql', sh: 'shell', bash: 'shell', zsh: 'shell',
    };
    return langMap[ext] || ext || 'unknown';
}
/**
 * Extract function names from content
 */
function extractFunctions(content) {
    const patterns = [
        /function\s+(\w+)/g,
        /const\s+(\w+)\s*=\s*(?:async\s*)?\([^)]*\)\s*=>/g,
        /let\s+(\w+)\s*=\s*(?:async\s*)?\([^)]*\)\s*=>/g,
        /(?:async\s+)?(?:public|private|protected)?\s+(\w+)\s*\([^)]*\)\s*[:{]/g,
        /(\w+)\s*:\s*(?:async\s*)?\([^)]*\)\s*=>/g,
        /def\s+(\w+)\s*\(/g, // Python
        /fn\s+(\w+)\s*[<(]/g, // Rust
        /func\s+(\w+)\s*\(/g, // Go
    ];
    const funcs = new Set();
    const reserved = new Set(['if', 'for', 'while', 'switch', 'catch', 'try', 'else', 'return', 'new', 'class', 'function', 'async', 'await']);
    for (const pattern of patterns) {
        const regex = new RegExp(pattern.source, pattern.flags);
        let match;
        while ((match = regex.exec(content)) !== null) {
            const name = match[1];
            if (name && !reserved.has(name) && name.length > 1) {
                funcs.add(name);
            }
        }
    }
    return Array.from(funcs);
}
/**
 * Extract class names from content
 */
function extractClasses(content) {
    const patterns = [
        /class\s+(\w+)/g,
        /interface\s+(\w+)/g,
        /type\s+(\w+)\s*=/g,
        /enum\s+(\w+)/g,
        /struct\s+(\w+)/g,
    ];
    const classes = new Set();
    for (const pattern of patterns) {
        const regex = new RegExp(pattern.source, pattern.flags);
        let match;
        while ((match = regex.exec(content)) !== null) {
            if (match[1])
                classes.add(match[1]);
        }
    }
    return Array.from(classes);
}
/**
 * Extract import statements from content
 */
function extractImports(content) {
    const patterns = [
        /import\s+.*?from\s+['"]([^'"]+)['"]/g,
        /import\s+['"]([^'"]+)['"]/g,
        /require\s*\(['"]([^'"]+)['"]\)/g,
        /from\s+(\w+)\s+import/g, // Python
        /use\s+(\w+(?:::\w+)*)/g, // Rust
    ];
    const imports = [];
    for (const pattern of patterns) {
        const regex = new RegExp(pattern.source, pattern.flags);
        let match;
        while ((match = regex.exec(content)) !== null) {
            if (match[1])
                imports.push(match[1]);
        }
    }
    return [...new Set(imports)];
}
/**
 * Extract export statements from content
 */
function extractExports(content) {
    const patterns = [
        /export\s+(?:default\s+)?(?:class|function|const|let|var|interface|type|enum)\s+(\w+)/g,
        /export\s*\{\s*([^}]+)\s*\}/g,
        /module\.exports\s*=\s*(\w+)/g,
        /exports\.(\w+)\s*=/g,
        /pub\s+(?:fn|struct|enum|type)\s+(\w+)/g, // Rust
    ];
    const exports = [];
    for (const pattern of patterns) {
        const regex = new RegExp(pattern.source, pattern.flags);
        let match;
        while ((match = regex.exec(content)) !== null) {
            if (match[1]) {
                // Handle grouped exports: export { a, b, c }
                const names = match[1].split(',').map(s => s.trim().split(/\s+as\s+/)[0].trim());
                exports.push(...names.filter(n => n && /^\w+$/.test(n)));
            }
        }
    }
    return [...new Set(exports)];
}
/**
 * Extract TODO/FIXME comments from content
 */
function extractTodos(content) {
    const pattern = /\/\/\s*(TODO|FIXME|HACK|XXX|BUG|NOTE):\s*(.+)/gi;
    const todos = [];
    let match;
    while ((match = pattern.exec(content)) !== null) {
        todos.push(`${match[1]}: ${match[2].trim()}`);
    }
    return todos;
}
/**
 * Extract all patterns from a file
 */
function extractAllPatterns(filePath, content) {
    try {
        const fileContent = content ?? (fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf-8') : '');
        return {
            file: filePath,
            language: detectLanguage(filePath),
            functions: extractFunctions(fileContent),
            classes: extractClasses(fileContent),
            imports: extractImports(fileContent),
            exports: extractExports(fileContent),
            todos: extractTodos(fileContent),
            variables: [], // Could add variable extraction if needed
        };
    }
    catch {
        return {
            file: filePath,
            language: detectLanguage(filePath),
            functions: [],
            classes: [],
            imports: [],
            exports: [],
            todos: [],
            variables: [],
        };
    }
}
/**
 * Extract patterns from multiple files
 */
function extractFromFiles(files, maxFiles = 100) {
    return files.slice(0, maxFiles).map(f => extractAllPatterns(f));
}
/**
 * Convert FilePatterns to PatternMatch array (for native-worker compatibility)
 */
function toPatternMatches(patterns) {
    const matches = [];
    for (const func of patterns.functions) {
        matches.push({ type: 'function', match: func, file: patterns.file });
    }
    for (const cls of patterns.classes) {
        matches.push({ type: 'class', match: cls, file: patterns.file });
    }
    for (const imp of patterns.imports) {
        matches.push({ type: 'import', match: imp, file: patterns.file });
    }
    for (const exp of patterns.exports) {
        matches.push({ type: 'export', match: exp, file: patterns.file });
    }
    for (const todo of patterns.todos) {
        matches.push({ type: 'todo', match: todo, file: patterns.file });
    }
    return matches;
}
exports.default = {
    detectLanguage,
    extractFunctions,
    extractClasses,
    extractImports,
    extractExports,
    extractTodos,
    extractAllPatterns,
    extractFromFiles,
    toPatternMatches,
};