@optave/codegraph 3.0.1 → 3.0.2

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.
package/src/parser.js CHANGED
@@ -38,6 +38,9 @@ function grammarPath(name) {
38
38
 
39
39
  let _initialized = false;
40
40
 
41
+ // Memoized parsers — avoids reloading WASM grammars on every createParsers() call
42
+ let _cachedParsers = null;
43
+
41
44
  // Query cache for JS/TS/TSX extractors (populated during createParsers)
42
45
  const _queryCache = new Map();
43
46
 
@@ -66,6 +69,8 @@ const TS_EXTRA_PATTERNS = [
66
69
  ];
67
70
 
68
71
  export async function createParsers() {
72
+ if (_cachedParsers) return _cachedParsers;
73
+
69
74
  if (!_initialized) {
70
75
  await Parser.init();
71
76
  _initialized = true;
@@ -94,6 +99,7 @@ export async function createParsers() {
94
99
  parsers.set(entry.id, null);
95
100
  }
96
101
  }
102
+ _cachedParsers = parsers;
97
103
  return parsers;
98
104
  }
99
105
 
@@ -104,6 +110,54 @@ export function getParser(parsers, filePath) {
104
110
  return parsers.get(entry.id) || null;
105
111
  }
106
112
 
113
+ /**
114
+ * Pre-parse files missing `_tree` via WASM so downstream phases (CFG, dataflow)
115
+ * don't each need to create parsers and re-parse independently.
116
+ * Only parses files whose extension is in SUPPORTED_EXTENSIONS.
117
+ *
118
+ * @param {Map<string, object>} fileSymbols - Map<relPath, { definitions, _tree, _langId, ... }>
119
+ * @param {string} rootDir - absolute project root
120
+ */
121
+ export async function ensureWasmTrees(fileSymbols, rootDir) {
122
+ // Check if any file needs a tree
123
+ let needsParse = false;
124
+ for (const [relPath, symbols] of fileSymbols) {
125
+ if (!symbols._tree) {
126
+ const ext = path.extname(relPath).toLowerCase();
127
+ if (_extToLang.has(ext)) {
128
+ needsParse = true;
129
+ break;
130
+ }
131
+ }
132
+ }
133
+ if (!needsParse) return;
134
+
135
+ const parsers = await createParsers();
136
+
137
+ for (const [relPath, symbols] of fileSymbols) {
138
+ if (symbols._tree) continue;
139
+ const ext = path.extname(relPath).toLowerCase();
140
+ const entry = _extToLang.get(ext);
141
+ if (!entry) continue;
142
+ const parser = parsers.get(entry.id);
143
+ if (!parser) continue;
144
+
145
+ const absPath = path.join(rootDir, relPath);
146
+ let code;
147
+ try {
148
+ code = fs.readFileSync(absPath, 'utf-8');
149
+ } catch {
150
+ continue;
151
+ }
152
+ try {
153
+ symbols._tree = parser.parse(code);
154
+ symbols._langId = entry.id;
155
+ } catch {
156
+ // skip files that fail to parse
157
+ }
158
+ }
159
+ }
160
+
107
161
  /**
108
162
  * Check whether the required WASM grammar files exist on disk.
109
163
  */