@nexiq/extension-sdk 1.0.2 → 1.0.3

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.
@@ -1,4 +1,8 @@
1
- import { type DatabaseData } from "@nexiq/shared";
1
+ import {
2
+ type DatabaseData,
3
+ type EffectInfo,
4
+ type ReactDependency,
5
+ } from "@nexiq/shared";
2
6
  import {
3
7
  type GraphComboData,
4
8
  type GraphArrowData,
@@ -20,7 +24,6 @@ export const componentTask: GraphViewTask = {
20
24
  const edges: GraphArrowData[] = [...result.edges];
21
25
  const typeData = { ...result.typeData };
22
26
 
23
- const entities = batch?.entities || data.entities || [];
24
27
  const scopes = batch?.scopes || data.scopes || [];
25
28
  const symbols = batch?.symbols || data.symbols || [];
26
29
  const relations = batch?.relations || data.relations || [];
@@ -29,15 +32,73 @@ export const componentTask: GraphViewTask = {
29
32
 
30
33
  const fileMap = new Map(files.map((f) => [f.id, f.path]));
31
34
 
35
+ // Identify automatic JSX symbols and their entities to skip them and their scopes
36
+ const automaticJsxEntities = new Set<string>();
37
+ for (const symbol of symbols) {
38
+ if (symbol.name.startsWith("jsx@")) {
39
+ automaticJsxEntities.add(symbol.entity_id);
40
+ }
41
+ }
42
+
43
+ // Build Export Map: Map<filePath, Map<exportName, symbolId>>
44
+ const exportMap = new Map<string, Map<string | null, string>>();
45
+ for (const exp of data.exports) {
46
+ const scope = data.scopes.find((s) => s.id === exp.scope_id);
47
+ if (!scope) continue;
48
+ const filePath = fileMap.get(scope.file_id);
49
+ if (!filePath) continue;
50
+
51
+ if (!exportMap.has(filePath)) {
52
+ exportMap.set(filePath, new Map());
53
+ }
54
+ if (exp.symbol_id) {
55
+ exportMap
56
+ .get(filePath)!
57
+ .set(exp.is_default ? "default" : exp.name, exp.symbol_id);
58
+ }
59
+ }
60
+
61
+ // Build Redirection Map: Map<importSymbolId, actualSymbolId>
62
+ const redirectionMap = new Map<string, string>();
63
+ for (const symbol of data.symbols) {
64
+ const entity = data.entities.find((e) => e.id === symbol.entity_id);
65
+ if (entity?.kind === "import" && entity.data_json) {
66
+ try {
67
+ const impData = JSON.parse(entity.data_json);
68
+ const sourcePath = impData.source;
69
+ const importedName =
70
+ impData.type === "default" ? "default" : impData.importedName;
71
+
72
+ const targetSymbolId = exportMap.get(sourcePath)?.get(importedName);
73
+ if (targetSymbolId) {
74
+ redirectionMap.set(symbol.id, targetSymbolId);
75
+ }
76
+ } catch {
77
+ // ignore
78
+ }
79
+ }
80
+ }
81
+
32
82
  // 1. Map Scopes to Combos
33
83
  for (const scope of scopes) {
84
+ if (scope.kind === "module") continue;
34
85
  if (combos.some((c) => c.id === scope.id)) continue;
35
86
 
87
+ // Skip scopes belonging to automatic JSX elements
88
+ if (scope.entity_id && automaticJsxEntities.has(scope.entity_id))
89
+ continue;
90
+
91
+ const parentScope = data.scopes.find((s) => s.id === scope.parent_id);
92
+ const parentComboId =
93
+ parentScope && parentScope.kind !== "module"
94
+ ? scope.parent_id
95
+ : undefined;
96
+
36
97
  combos.push({
37
98
  id: scope.id,
38
99
  name: scope.kind,
39
100
  label: { text: scope.kind },
40
- combo: scope.parent_id || undefined,
101
+ combo: parentComboId || undefined,
41
102
  type: "scope",
42
103
  collapsed: true,
43
104
  });
@@ -51,16 +112,36 @@ export const componentTask: GraphViewTask = {
51
112
  )
52
113
  continue;
53
114
 
115
+ // Skip automatic JSX symbols to avoid clutter
116
+ if (symbol.name.startsWith("jsx@")) continue;
117
+
54
118
  const entity = data.entities.find((e) => e.id === symbol.entity_id);
55
119
  if (!entity) continue;
56
120
 
121
+ // Skip imports
122
+ if (entity.kind === "import") continue;
123
+
124
+ // Skip state setters (index 1 of the state array) to avoid showing both state and setter as separate nodes
125
+ if (entity.kind === "state" && symbol.path) {
126
+ try {
127
+ const pathArr = JSON.parse(symbol.path);
128
+ if (
129
+ Array.isArray(pathArr) &&
130
+ pathArr.length > 0 &&
131
+ pathArr[0] !== "0"
132
+ ) {
133
+ continue;
134
+ }
135
+ } catch {
136
+ // ignore
137
+ }
138
+ }
139
+
57
140
  const scope = data.scopes.find((s) => s.id === symbol.scope_id);
58
141
  const file = scope ? fileMap.get(scope.file_id) : undefined;
59
142
 
60
- // Check if this symbol's entity has an associated block scope (the body)
61
- const blockScope = data.scopes.find(
62
- (s) => s.entity_id === entity.id && s.kind === "block",
63
- );
143
+ // Check if this symbol's entity has an associated scope (e.g. the body)
144
+ const blockScope = data.scopes.find((s) => s.entity_id === entity.id);
64
145
 
65
146
  if (blockScope) {
66
147
  // This symbol represents a scope (e.g. function body)
@@ -72,19 +153,143 @@ export const componentTask: GraphViewTask = {
72
153
  combo.type = entity.kind;
73
154
  combo.fileName = file;
74
155
  combo.loc = { line: entity.line || 0, column: entity.column || 0 };
156
+
157
+ // Handle effects for this component/hook
158
+ if (entity.data_json) {
159
+ try {
160
+ const metadata = JSON.parse(entity.data_json);
161
+ if (metadata.effects) {
162
+ for (const effect of Object.values(
163
+ metadata.effects as Record<string, EffectInfo>,
164
+ )) {
165
+ const effectName = (effect as EffectInfo).name || "useEffect";
166
+ nodes.push({
167
+ id: effect.id,
168
+ name: effectName,
169
+ label: { text: effectName },
170
+ combo: blockScope.id,
171
+ type: "effect",
172
+ fileName: file,
173
+ loc: { line: effect.loc.line, column: effect.loc.column },
174
+ displayName: effectName,
175
+ });
176
+
177
+ if (effect.reactDeps) {
178
+ for (const dep of effect.reactDeps as ReactDependency[]) {
179
+ const targetId = redirectionMap.get(dep.id) || dep.id;
180
+ if (targetId) {
181
+ edges.push({
182
+ id: `${targetId}-${effect.id}-effect-dep`,
183
+ source: targetId,
184
+ target: effect.id,
185
+ label: "dependency",
186
+ });
187
+ }
188
+ }
189
+ }
190
+ }
191
+ }
192
+ } catch {
193
+ // ignore
194
+ }
195
+ }
75
196
  }
76
197
  } else {
77
- // Just a variable, create a node
198
+ // Handle destructuring paths to group variables and reduce clutter
199
+ let parentComboId =
200
+ scope && scope.kind !== "module" ? symbol.scope_id : undefined;
201
+
202
+ if (symbol.path && entity.kind !== "state") {
203
+ try {
204
+ const pathArr = JSON.parse(symbol.path);
205
+ if (Array.isArray(pathArr) && pathArr.length >= 1) {
206
+ // 1. Create a "source" combo for the entity (e.g., the hook call or variable)
207
+ const sourceComboId = `${symbol.scope_id}:source:${symbol.entity_id}`;
208
+ if (!combos.some((c) => c.id === sourceComboId)) {
209
+ let sourceLabel = entity.name || entity.kind;
210
+ if (entity.kind === "hook" && entity.data_json) {
211
+ try {
212
+ const meta = JSON.parse(entity.data_json);
213
+ if (meta.call?.name) sourceLabel = meta.call.name;
214
+ } catch {
215
+ // ignore
216
+ }
217
+ }
218
+
219
+ combos.push({
220
+ id: sourceComboId,
221
+ name: sourceLabel,
222
+ label: { text: sourceLabel },
223
+ combo: parentComboId,
224
+ type: "source-group",
225
+ collapsed: true,
226
+ });
227
+ }
228
+ parentComboId = sourceComboId;
229
+
230
+ // 2. Create intermediate combos for each segment of the path except the last one
231
+ for (let i = 0; i < pathArr.length - 1; i++) {
232
+ const segment = pathArr[i];
233
+ // Use a stable ID for the path combo nested under the source combo
234
+ const segmentId = `${sourceComboId}:path:${pathArr
235
+ .slice(0, i + 1)
236
+ .join("/")}`;
237
+
238
+ if (!combos.some((c) => c.id === segmentId)) {
239
+ combos.push({
240
+ id: segmentId,
241
+ name: segment.toString(),
242
+ label: { text: segment.toString() },
243
+ combo: parentComboId,
244
+ type: "path-group",
245
+ collapsed: true,
246
+ });
247
+ }
248
+ parentComboId = segmentId;
249
+ }
250
+ }
251
+ } catch {
252
+ // ignore invalid path JSON
253
+ }
254
+ }
255
+
256
+ const labelText = symbol.name;
257
+
258
+ // Handle reactDeps and special labeling for hooks
259
+ if (entity.data_json) {
260
+ try {
261
+ const metadata = JSON.parse(entity.data_json);
262
+
263
+ if (metadata.reactDeps) {
264
+ for (const dep of metadata.reactDeps as {
265
+ id: string;
266
+ name: string;
267
+ }[]) {
268
+ const targetId = redirectionMap.get(dep.id) || dep.id;
269
+ if (targetId) {
270
+ edges.push({
271
+ id: `${targetId}-${symbol.id}-react-dep`,
272
+ source: targetId,
273
+ target: symbol.id,
274
+ label: "dependency",
275
+ });
276
+ }
277
+ }
278
+ }
279
+ } catch {
280
+ // ignore
281
+ }
282
+ }
283
+
78
284
  nodes.push({
79
285
  id: symbol.id,
80
286
  name: symbol.name,
81
- label: { text: symbol.name },
82
- combo: symbol.scope_id,
287
+ label: { text: labelText },
288
+ combo: parentComboId,
83
289
  type: entity.kind,
84
290
  fileName: file,
85
291
  loc: { line: entity.line || 0, column: entity.column || 0 },
86
292
  displayName: symbol.name,
87
- radius: 10,
88
293
  });
89
294
  }
90
295
  }
@@ -98,59 +303,78 @@ export const componentTask: GraphViewTask = {
98
303
  continue;
99
304
 
100
305
  const file = fileMap.get(render.file_id);
101
- const parentCombo =
102
- render.parent_render_id ||
103
- data.scopes.find(
104
- (s) => s.entity_id === render.parent_entity_id && s.kind === "block",
105
- )?.id;
306
+ const parentScope = data.scopes.find(
307
+ (s) => s.entity_id === render.parent_entity_id,
308
+ );
309
+
310
+ const parentCombo = parentScope?.id;
311
+
312
+ // Create a "render" group combo within the parent combo if not exists
313
+ let finalParentCombo = render.parent_render_id ?? parentCombo;
314
+ if (parentCombo && !render.parent_render_id) {
315
+ const renderGroupId = `render-group-${parentCombo}`;
316
+ if (!combos.some((c) => c.id === renderGroupId)) {
317
+ combos.push({
318
+ id: renderGroupId,
319
+ name: "render",
320
+ label: { text: "render" },
321
+ combo: parentCombo,
322
+ type: "render-group",
323
+ collapsed: true,
324
+ });
325
+ }
326
+ finalParentCombo = renderGroupId;
327
+ }
328
+
329
+ const labelText = render.tag;
106
330
 
107
331
  const commonData = {
108
332
  id: render.id,
109
333
  name: render.tag,
110
- label: { text: render.tag },
111
- combo: parentCombo,
334
+ label: { text: labelText },
335
+ combo: finalParentCombo,
112
336
  type: "render",
113
337
  fileName: file,
114
338
  loc: { line: render.line || 0, column: render.column || 0 },
115
339
  displayName: render.tag,
116
340
  };
117
341
 
118
- // If it has children (renders that point to it as parent_render_id), make it a combo
119
- const hasChildren = data.renders.some(
120
- (r) => r.parent_render_id === render.id,
121
- );
342
+ // Renders are now nodes that stack within the component combo
343
+ combos.push(commonData);
122
344
 
123
- if (hasChildren) {
124
- combos.push({
125
- ...commonData,
126
- collapsed: true,
127
- });
128
- } else {
129
- nodes.push({
130
- ...commonData,
131
- radius: 10,
132
- });
133
- }
345
+ // Add edge for JSX hierarchy
346
+ // if (render.parent_render_id) {
347
+ // edges.push({
348
+ // id: `${render.parent_render_id}-${render.id}-nesting`,
349
+ // source: render.parent_render_id,
350
+ // target: render.id,
351
+ // label: "nesting",
352
+ // });
353
+ // }
134
354
 
135
- if (render.symbol_id) {
136
- edges.push({
137
- id: `${render.id}-${render.symbol_id}`,
138
- source: render.id,
139
- target: render.symbol_id,
140
- label: "reference",
141
- });
142
- }
355
+ // if (render.symbol_id) {
356
+ // const targetId =
357
+ // redirectionMap.get(render.symbol_id) || render.symbol_id;
358
+ // edges.push({
359
+ // id: `${render.id}-${targetId}`,
360
+ // source: render.id,
361
+ // target: targetId,
362
+ // label: "reference",
363
+ // });
364
+ // }
143
365
  }
144
366
 
145
367
  // 4. Handle Relations (Edges)
146
368
  for (const rel of relations) {
147
- const edgeId = `${rel.from_id}-${rel.to_id}`;
369
+ const sourceId = redirectionMap.get(rel.from_id) || rel.from_id;
370
+ const targetId = redirectionMap.get(rel.to_id) || rel.to_id;
371
+ const edgeId = `${sourceId}-${targetId}-${rel.kind}`;
148
372
  if (edges.some((e) => e.id === edgeId)) continue;
149
373
 
150
374
  edges.push({
151
375
  id: edgeId,
152
- source: rel.from_id,
153
- target: rel.to_id,
376
+ source: sourceId,
377
+ target: targetId,
154
378
  label: rel.kind,
155
379
  });
156
380
  }