vue-hook-optimizer 0.0.77 → 0.0.79

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/dist/index.mjs DELETED
@@ -1,2500 +0,0 @@
1
- // src/analyze/options.ts
2
- import _traverse2 from "@babel/traverse";
3
- import { babelParse as babelParse2 } from "@vue/compiler-sfc";
4
-
5
- // src/analyze/setupScript.ts
6
- import _traverse from "@babel/traverse";
7
- import { babelParse } from "@vue/compiler-sfc";
8
-
9
- // src/analyze/utils.ts
10
- var NodeType = /* @__PURE__ */ ((NodeType3) => {
11
- NodeType3["var"] = "var";
12
- NodeType3["fun"] = "fun";
13
- return NodeType3;
14
- })(NodeType || {});
15
- var NodeCollection = class {
16
- lineOffset = 0;
17
- addInfo = true;
18
- constructor(_lineOffset = 0, _addInfo = true) {
19
- this.lineOffset = _lineOffset;
20
- this.addInfo = _addInfo;
21
- }
22
- nodes = /* @__PURE__ */ new Map();
23
- addNode(label, node, options = { isComputed: false, isMethod: false, comment: "" }) {
24
- if (this.nodes.has(label)) {
25
- return;
26
- }
27
- if (!options.isComputed && (node.type === "VariableDeclarator" && [
28
- "ArrowFunctionExpression",
29
- "FunctionDeclaration",
30
- "FunctionExpression"
31
- ].includes(node.init?.type || "") || node.type === "ObjectProperty" && [
32
- "ArrowFunctionExpression",
33
- "FunctionDeclaration",
34
- "FunctionExpression"
35
- ].includes(node.value?.type || "") || node.type === "FunctionDeclaration" || node.type === "ObjectMethod" || node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression") || options.isMethod) {
36
- this.nodes.set(label, {
37
- label,
38
- type: "fun" /* fun */,
39
- ...this.addInfo ? {
40
- info: {
41
- line: (node.loc?.start.line || 1) - 1 + this.lineOffset,
42
- column: node.loc?.start.column || 0,
43
- ...options.comment ? { comment: options.comment } : {}
44
- }
45
- } : {}
46
- });
47
- } else {
48
- this.nodes.set(label, {
49
- label,
50
- type: "var" /* var */,
51
- ...this.addInfo ? {
52
- info: {
53
- line: (node.loc?.start.line || 1) - 1 + this.lineOffset,
54
- column: node.loc?.start.column || 0,
55
- ...options.comment ? { comment: options.comment } : {}
56
- }
57
- } : {}
58
- });
59
- }
60
- }
61
- addTypedNode(label, node) {
62
- this.nodes.set(label, {
63
- label,
64
- type: node.type,
65
- ...this.addInfo ? {
66
- info: {
67
- ...node.info || {}
68
- }
69
- } : {}
70
- });
71
- }
72
- getNode(label) {
73
- return this.nodes.get(label);
74
- }
75
- map(graph) {
76
- const nodes = new Set(Array.from(graph.nodes).map((node) => {
77
- return this.nodes.get(node);
78
- }).filter((node) => !!node));
79
- const edges = new Map(Array.from(graph.edges).map(([from, to]) => {
80
- const labelMap = /* @__PURE__ */ new Map();
81
- for (const item of to) {
82
- const node = this.nodes.get(item.label);
83
- if (!node) {
84
- continue;
85
- }
86
- const existing = labelMap.get(item.label);
87
- if (!existing || existing.type === "get" && item.type === "set") {
88
- labelMap.set(item.label, { node, type: item.type });
89
- }
90
- }
91
- const items = Array.from(labelMap.values());
92
- return [this.nodes.get(from), new Set(items)];
93
- }));
94
- return {
95
- nodes,
96
- edges
97
- };
98
- }
99
- };
100
- function getComment(node) {
101
- let comment = "";
102
- node.leadingComments?.forEach((_comment) => {
103
- if (_comment.loc.end.line > node.loc.start.line) {
104
- return;
105
- }
106
- if (_comment.value.trim().startsWith("*")) {
107
- comment += `${_comment.value.trim().replace(/^\s*\*+\s*\**/gm, "").trim()}
108
- `;
109
- }
110
- });
111
- node.trailingComments?.forEach((_comment) => {
112
- if (_comment.loc.end.line > node.loc.start.line) {
113
- return;
114
- }
115
- if (_comment.value.trim().startsWith("*")) {
116
- comment += `${_comment.value.trim().replace(/^\s*\*+\s*\**/gm, "").trim()}
117
- `;
118
- } else {
119
- comment += `${_comment.value.trim()}
120
- `;
121
- }
122
- });
123
- return comment.trim();
124
- }
125
- function isWritingNode(path) {
126
- const assignParent = path.findParent((p) => p.isAssignmentExpression());
127
- if (assignParent) {
128
- const leftNode = assignParent.node.left;
129
- if (leftNode.start != null && path.node.start >= leftNode.start && path.node.end <= leftNode.end) {
130
- return true;
131
- }
132
- }
133
- const updateParent = path.findParent((p) => p.isUpdateExpression());
134
- if (updateParent) {
135
- const argNode = updateParent.node.argument;
136
- if (argNode.start != null && path.node.start >= argNode.start && path.node.end <= argNode.end) {
137
- return true;
138
- }
139
- }
140
- return false;
141
- }
142
- function isCallingNode(path) {
143
- const parent = path.parentPath;
144
- if (parent && parent.isCallExpression()) {
145
- return parent.node.callee === path.node;
146
- }
147
- return false;
148
- }
149
- function getRelationType(path) {
150
- if (path.node.type === "Identifier" && isCallingNode(path)) {
151
- return "call";
152
- }
153
- if (isWritingNode(path)) {
154
- return "set";
155
- }
156
- return "get";
157
- }
158
-
159
- // src/analyze/setupScript.ts
160
- var traverse = _traverse.default?.default || _traverse.default || _traverse;
161
- var ignoreFunctionsName = ["defineProps", "defineEmits", "withDefaults"];
162
- var watchHooks = [
163
- "watch",
164
- // from `@vueuse/core`
165
- "watchArray",
166
- "watchAtMost",
167
- "watchDebounced",
168
- "watchDeep",
169
- "watchIgnorable",
170
- "watchImmediate",
171
- "watchOnce",
172
- "watchPausable",
173
- "watchThrottled",
174
- "watchTriggerable",
175
- "watchWithFilter"
176
- ];
177
- function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
178
- const spread = _spread || [];
179
- const nodeCollection = new NodeCollection(_lineOffset);
180
- const graph = {
181
- nodes: /* @__PURE__ */ new Set(),
182
- edges: /* @__PURE__ */ new Map(),
183
- spread: /* @__PURE__ */ new Map()
184
- };
185
- traverse(ast, {
186
- VariableDeclaration(path) {
187
- path.node.declarations.forEach((declaration) => {
188
- if (declaration.id.type === "ArrayPattern") {
189
- declaration.id.elements.forEach((element) => {
190
- if (element?.type === "Identifier") {
191
- const name = element.name;
192
- const binding = path.scope.getBinding(name);
193
- if (binding && (path.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path.parent) && !(declaration.init?.type === "CallExpression" && declaration.init?.callee.type === "Identifier" && ignoreFunctionsName.includes(declaration.init?.callee.name))) {
194
- graph.nodes.add(name);
195
- nodeCollection.addNode(name, element, {
196
- comment: getComment(path.node)
197
- });
198
- if (!graph.edges.get(name)) {
199
- graph.edges.set(name, /* @__PURE__ */ new Set());
200
- }
201
- }
202
- }
203
- if (element?.type === "RestElement" && element.argument.type === "Identifier") {
204
- const name = element.argument.name;
205
- const binding = path.scope.getBinding(name);
206
- if (binding && (path.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path.parent) && !(declaration.init?.type === "CallExpression" && declaration.init?.callee.type === "Identifier" && ignoreFunctionsName.includes(declaration.init?.callee.name))) {
207
- graph.nodes.add(name);
208
- nodeCollection.addNode(name, element.argument, {
209
- comment: getComment(path.node)
210
- });
211
- if (!graph.edges.get(name)) {
212
- graph.edges.set(name, /* @__PURE__ */ new Set());
213
- }
214
- }
215
- }
216
- });
217
- }
218
- if (declaration.id.type === "ObjectPattern") {
219
- declaration.id.properties.forEach((property) => {
220
- if (property.type === "ObjectProperty" && property.value.type === "Identifier") {
221
- const name = property.value.name;
222
- const binding = path.scope.getBinding(name);
223
- if (binding && (path.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path.parent) && !(declaration.init?.type === "CallExpression" && declaration.init?.callee.type === "Identifier" && ignoreFunctionsName.includes(declaration.init?.callee.name))) {
224
- graph.nodes.add(name);
225
- nodeCollection.addNode(name, property.value, {
226
- comment: getComment(property)
227
- });
228
- if (!graph.edges.get(name)) {
229
- graph.edges.set(name, /* @__PURE__ */ new Set());
230
- }
231
- }
232
- }
233
- if (property.type === "RestElement" && property.argument.type === "Identifier") {
234
- const name = property.argument.name;
235
- const binding = path.scope.getBinding(name);
236
- if (binding && (path.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path.parent) && !(declaration.init?.type === "CallExpression" && declaration.init?.callee.type === "Identifier" && ignoreFunctionsName.includes(declaration.init?.callee.name))) {
237
- graph.nodes.add(name);
238
- nodeCollection.addNode(name, property.argument, {
239
- comment: getComment(property)
240
- });
241
- if (!graph.edges.get(name)) {
242
- graph.edges.set(name, /* @__PURE__ */ new Set());
243
- }
244
- }
245
- }
246
- });
247
- }
248
- if (declaration.id?.type === "Identifier") {
249
- const name = declaration.id.name;
250
- const binding = path.scope.getBinding(name);
251
- if (binding && (path.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path.parent) && !(declaration.init?.type === "CallExpression" && declaration.init?.callee.type === "Identifier" && ignoreFunctionsName.includes(declaration.init?.callee.name))) {
252
- graph.nodes.add(name);
253
- nodeCollection.addNode(name, declaration, {
254
- comment: getComment(path.node)
255
- });
256
- if (!graph.edges.get(name)) {
257
- graph.edges.set(name, /* @__PURE__ */ new Set());
258
- }
259
- if (spread.includes(name)) {
260
- if (declaration.init?.type === "ObjectExpression") {
261
- declaration.init?.properties.forEach((prop) => {
262
- if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
263
- const keyName = prop.key.name;
264
- graph.nodes.add(keyName);
265
- nodeCollection.addNode(keyName, prop, {
266
- comment: getComment(prop)
267
- });
268
- if (!graph.edges.get(keyName)) {
269
- graph.edges.set(keyName, /* @__PURE__ */ new Set());
270
- }
271
- if (graph.spread.has(name)) {
272
- graph.spread.get(name)?.add(keyName);
273
- } else {
274
- graph.spread.set(name, /* @__PURE__ */ new Set([keyName]));
275
- }
276
- } else if (prop.type === "SpreadElement") {
277
- console.warn("not support spread in spread");
278
- }
279
- });
280
- }
281
- if (declaration.init?.type === "CallExpression" && declaration.init?.callee.type === "Identifier" && declaration.init?.callee.name === "reactive") {
282
- const arg = declaration.init?.arguments[0];
283
- if (arg.type === "ObjectExpression") {
284
- arg.properties.forEach((prop) => {
285
- if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
286
- const keyName = prop.key.name;
287
- graph.nodes.add(keyName);
288
- nodeCollection.addNode(keyName, prop, {
289
- comment: getComment(prop)
290
- });
291
- if (!graph.edges.get(keyName)) {
292
- graph.edges.set(keyName, /* @__PURE__ */ new Set());
293
- }
294
- if (graph.spread.has(name)) {
295
- graph.spread.get(name)?.add(keyName);
296
- } else {
297
- graph.spread.set(name, /* @__PURE__ */ new Set([keyName]));
298
- }
299
- } else if (prop.type === "SpreadElement") {
300
- console.warn("not support spread in spread");
301
- }
302
- });
303
- }
304
- }
305
- }
306
- }
307
- }
308
- });
309
- },
310
- FunctionDeclaration(path) {
311
- const name = path.node.id?.name;
312
- if (name) {
313
- const binding = path.scope.getBinding(name);
314
- if (binding && (path.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path.parent)) {
315
- graph.nodes.add(name);
316
- nodeCollection.addNode(name, path.node.id, {
317
- isMethod: true,
318
- comment: getComment(path.node)
319
- });
320
- if (!graph.edges.get(name)) {
321
- graph.edges.set(name, /* @__PURE__ */ new Set());
322
- }
323
- }
324
- }
325
- }
326
- }, parentScope, parentPath);
327
- function traverseHooks(node, patentScope) {
328
- if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.type === "Identifier" || node.type === "CallExpression" && node.callee.type === "Identifier") {
329
- const hookName = (() => {
330
- if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.type === "Identifier") {
331
- return node.expression.callee.name;
332
- }
333
- if (node.type === "CallExpression" && node.callee.type === "Identifier") {
334
- return node.callee.name;
335
- }
336
- })() || "";
337
- if (!hookName) {
338
- return;
339
- }
340
- const hookBinding = patentScope.getBinding(hookName);
341
- if (!(hookBinding === void 0 || hookBinding?.scope.block.type === "Program" || parentScope === hookBinding?.scope)) {
342
- return;
343
- }
344
- const expression = node.type === "ExpressionStatement" ? node.expression : node;
345
- const watchArgs = /* @__PURE__ */ new Set();
346
- if (hookName === "provide") {
347
- traverse(expression, {
348
- Identifier(path1) {
349
- const binding = path1.scope.getBinding(path1.node.name);
350
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
351
- const _node = nodeCollection.getNode(path1.node.name);
352
- if (_node?.info?.used) {
353
- _node?.info?.used?.add(hookName);
354
- } else if (_node) {
355
- _node.info = {
356
- ..._node?.info,
357
- used: /* @__PURE__ */ new Set([hookName])
358
- };
359
- }
360
- }
361
- }
362
- }, patentScope, node);
363
- } else if (watchHooks.includes(hookName)) {
364
- if (expression.arguments[0].type === "Identifier") {
365
- const binding = patentScope.getBinding(expression.arguments[0].name);
366
- if (graph.nodes.has(expression.arguments[0].name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
367
- watchArgs.add(expression.arguments[0]);
368
- }
369
- } else {
370
- traverse(expression.arguments[0], {
371
- Identifier(path1) {
372
- const binding = path1.scope.getBinding(path1.node.name);
373
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
374
- watchArgs.add(path1.node);
375
- }
376
- }
377
- }, patentScope, node);
378
- }
379
- } else if (hookName === "useEffect" && expression.arguments[1].type === "ArrayExpression") {
380
- traverse(expression.arguments[1], {
381
- Identifier(path1) {
382
- const binding = path1.scope.getBinding(path1.node.name);
383
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
384
- watchArgs.add(path1.node);
385
- }
386
- }
387
- }, patentScope, node);
388
- }
389
- expression.arguments.forEach((argNode, index) => {
390
- if (watchHooks.includes(hookName) && index === 0 && argNode.type === "Identifier") {
391
- const _node = nodeCollection.getNode(argNode.name);
392
- if (_node?.info?.used) {
393
- _node?.info?.used?.add(hookName);
394
- } else if (_node) {
395
- _node.info = {
396
- ..._node?.info,
397
- used: /* @__PURE__ */ new Set([hookName])
398
- };
399
- }
400
- return;
401
- }
402
- if (argNode.type === "Identifier") {
403
- const binding = patentScope.getBinding(argNode.name);
404
- if (graph.nodes.has(argNode.name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
405
- const _node = nodeCollection.getNode(argNode.name);
406
- if (_node?.info?.used) {
407
- _node?.info?.used?.add(hookName);
408
- } else if (_node) {
409
- _node.info = {
410
- ..._node?.info,
411
- used: /* @__PURE__ */ new Set([hookName])
412
- };
413
- }
414
- }
415
- } else {
416
- traverse(argNode, {
417
- Identifier(path1) {
418
- const binding = path1.scope.getBinding(path1.node.name);
419
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
420
- if ([...watchHooks, "useEffect"].includes(hookName) && watchArgs.size > 0) {
421
- const watchArgsNames = Array.from(watchArgs).map((arg) => arg.name);
422
- watchArgs.forEach((watchArg) => {
423
- if (!watchArgsNames.includes(path1.node.name)) {
424
- graph.edges.get(watchArg.name)?.add({
425
- label: path1.node.name,
426
- type: getRelationType(path1)
427
- });
428
- }
429
- });
430
- }
431
- const _node = nodeCollection.getNode(path1.node.name);
432
- if (_node?.info?.used) {
433
- _node?.info?.used?.add(hookName);
434
- } else if (_node) {
435
- _node.info = {
436
- ..._node?.info,
437
- used: /* @__PURE__ */ new Set([hookName])
438
- };
439
- }
440
- }
441
- }
442
- }, patentScope, node);
443
- }
444
- });
445
- }
446
- }
447
- traverse(ast, {
448
- FunctionDeclaration(path) {
449
- const name = path.node.id?.name;
450
- if (name && graph.nodes.has(name)) {
451
- traverse(path.node.body, {
452
- Identifier(path1) {
453
- const binding = path1.scope.getBinding(path1.node.name);
454
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
455
- graph.edges.get(name)?.add({
456
- label: path1.node.name,
457
- type: getRelationType(path1)
458
- });
459
- }
460
- },
461
- MemberExpression(path1) {
462
- if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
463
- const binding = path1.scope.getBinding(path1.node.object.name);
464
- if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
465
- graph.edges.get(name)?.add({
466
- label: path1.node.property.name,
467
- type: getRelationType(path1)
468
- });
469
- }
470
- }
471
- }
472
- }, path.scope, path);
473
- }
474
- },
475
- VariableDeclarator(path) {
476
- if (path.node.init) {
477
- if (path.node.id.type === "ArrayPattern") {
478
- path.node.id.elements.forEach((element) => {
479
- if (element?.type === "Identifier") {
480
- const name = element.name;
481
- if (name && graph.nodes.has(name) && path.node.init?.type === "CallExpression") {
482
- traverse(path.node.init, {
483
- Identifier(path1) {
484
- const binding = path1.scope.getBinding(path1.node.name);
485
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
486
- graph.edges.get(name)?.add({
487
- label: path1.node.name,
488
- type: getRelationType(path1)
489
- });
490
- }
491
- },
492
- MemberExpression(path1) {
493
- if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
494
- const binding = path1.scope.getBinding(path1.node.object.name);
495
- if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
496
- graph.edges.get(name)?.add({
497
- label: path1.node.property.name,
498
- type: getRelationType(path1)
499
- });
500
- }
501
- }
502
- }
503
- }, path.scope, path);
504
- }
505
- }
506
- });
507
- } else if (path.node.id.type === "ObjectPattern") {
508
- path.node.id.properties.forEach((property) => {
509
- if (property.type === "ObjectProperty" && property.value.type === "Identifier") {
510
- const name = property.value.name;
511
- if (name && graph.nodes.has(name) && path.node.init) {
512
- traverse(path.node.init, {
513
- Identifier(path1) {
514
- const binding = path1.scope.getBinding(path1.node.name);
515
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
516
- graph.edges.get(name)?.add({
517
- label: path1.node.name,
518
- type: getRelationType(path1)
519
- });
520
- }
521
- },
522
- MemberExpression(path1) {
523
- if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
524
- const binding = path1.scope.getBinding(path1.node.object.name);
525
- if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
526
- graph.edges.get(name)?.add({
527
- label: path1.node.property.name,
528
- type: getRelationType(path1)
529
- });
530
- }
531
- }
532
- }
533
- }, path.scope, path);
534
- }
535
- }
536
- });
537
- } else if ([
538
- "CallExpression",
539
- "ArrowFunctionExpression",
540
- "FunctionDeclaration"
541
- ].includes(path.node.init.type) && path.node.id.type === "Identifier") {
542
- if (path.node.init.type === "CallExpression" && path.node.init.callee.type === "Identifier" && [...watchHooks, "watchEffect"].includes(path.node.init.callee.name)) {
543
- traverseHooks(path.node.init, path.scope);
544
- }
545
- const name = path.node.id?.name;
546
- if (name && graph.nodes.has(name)) {
547
- traverse(path.node.init, {
548
- Identifier(path1) {
549
- const binding = path1.scope.getBinding(path1.node.name);
550
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
551
- graph.edges.get(name)?.add({
552
- label: path1.node.name,
553
- type: getRelationType(path1)
554
- });
555
- }
556
- },
557
- MemberExpression(path1) {
558
- if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
559
- const binding = path1.scope.getBinding(path1.node.object.name);
560
- if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
561
- graph.edges.get(name)?.add({
562
- label: path1.node.property.name,
563
- type: getRelationType(path1)
564
- });
565
- }
566
- }
567
- }
568
- }, path.scope, path);
569
- }
570
- } else if (path.node.id.type === "Identifier") {
571
- const name = path.node.id.name;
572
- if (path.node.init.type === "Identifier") {
573
- const binding = path.scope.getBinding(path.node.init.name);
574
- if (graph.nodes.has(path.node.init.name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
575
- graph.edges.get(name)?.add({
576
- label: path.node.init.name,
577
- type: getRelationType(path)
578
- });
579
- }
580
- } else {
581
- traverse(path.node.init, {
582
- Identifier(path1) {
583
- const binding = path1.scope.getBinding(path1.node.name);
584
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
585
- graph.edges.get(name)?.add({
586
- label: path1.node.name,
587
- type: getRelationType(path1)
588
- });
589
- }
590
- }
591
- }, path.scope, path);
592
- }
593
- }
594
- }
595
- },
596
- ObjectMethod(path) {
597
- if (path.node.key.type === "Identifier" && graph.nodes.has(path.node.key.name)) {
598
- const name = path.node.key.name;
599
- traverse(path.node.body, {
600
- Identifier(path1) {
601
- const binding = path1.scope.getBinding(path1.node.name);
602
- if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
603
- graph.edges.get(name)?.add({
604
- label: path1.node.name,
605
- type: getRelationType(path1)
606
- });
607
- }
608
- },
609
- MemberExpression(path1) {
610
- if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
611
- const binding = path1.scope.getBinding(path1.node.object.name);
612
- if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
613
- graph.edges.get(name)?.add({
614
- label: path1.node.property.name,
615
- type: getRelationType(path1)
616
- });
617
- }
618
- }
619
- }
620
- }, path.scope, path);
621
- }
622
- },
623
- ObjectProperty(path) {
624
- if (path.node.key.type === "Identifier" && graph.nodes.has(path.node.key.name)) {
625
- const name = path.node.key.name;
626
- traverse(path.node.value, {
627
- MemberExpression(path1) {
628
- if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
629
- const binding = path1.scope.getBinding(path1.node.object.name);
630
- if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
631
- graph.edges.get(name)?.add({
632
- label: path1.node.property.name,
633
- type: getRelationType(path1)
634
- });
635
- }
636
- }
637
- }
638
- }, path.scope, path);
639
- }
640
- },
641
- ExpressionStatement(path) {
642
- if (path.type === "ExpressionStatement" && path.node.expression.type === "CallExpression" && path.node.expression.callee.type === "Identifier") {
643
- const name = path.node.expression.callee.name;
644
- if (graph.nodes.has(name) && path.scope.block.type === "Program") {
645
- const _node = nodeCollection.getNode(name);
646
- if (_node?.info?.used) {
647
- _node?.info?.used?.add("Call Expression");
648
- } else if (_node) {
649
- _node.info = {
650
- ..._node?.info,
651
- used: /* @__PURE__ */ new Set(["Call Expression"])
652
- };
653
- }
654
- } else {
655
- traverseHooks(path.node.expression, path.scope);
656
- }
657
- }
658
- if (path.type === "ExpressionStatement" && path.node.expression.type === "AssignmentExpression" && path.node.expression.right.type === "CallExpression" && path.node.expression.right.callee.type === "Identifier") {
659
- traverseHooks(path.node.expression.right, path.scope);
660
- }
661
- }
662
- }, parentScope, parentPath);
663
- return {
664
- graph,
665
- nodeCollection
666
- };
667
- }
668
- function analyze(content, lineOffset = 0, jsx = false) {
669
- const ast = babelParse(content, { sourceType: "module", plugins: [
670
- "typescript",
671
- ...jsx ? ["jsx"] : []
672
- ] });
673
- const { graph, nodeCollection } = processSetup(ast, void 0, void 0, void 0, lineOffset);
674
- return nodeCollection.map(graph);
675
- }
676
-
677
- // src/analyze/options.ts
678
- var traverse2 = _traverse2.default?.default || _traverse2.default || _traverse2;
679
- var vueLifeCycleHooks = [
680
- "beforeCreate",
681
- "created",
682
- "beforeMount",
683
- "mounted",
684
- "beforeUpdate",
685
- "updated",
686
- "beforeDestroy",
687
- "destroyed",
688
- "activated",
689
- "deactivated",
690
- "errorCaptured",
691
- "renderTracked",
692
- "renderTriggered",
693
- "provide"
694
- ];
695
- function analyze2(content, lineOffset = 0, jsx = false) {
696
- const ast = babelParse2(content, { sourceType: "module", plugins: [
697
- "typescript",
698
- ...jsx ? ["jsx"] : []
699
- ] });
700
- let nodeCollection = new NodeCollection(lineOffset);
701
- const tNodes = /* @__PURE__ */ new Map();
702
- const graph = {
703
- nodes: /* @__PURE__ */ new Set(),
704
- edges: /* @__PURE__ */ new Map()
705
- };
706
- const nodesUsedInTemplate = /* @__PURE__ */ new Set();
707
- function process(node, path) {
708
- traverse2(node, {
709
- ObjectProperty(path1) {
710
- if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
711
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "data" && (path1.node.value.type === "ArrowFunctionExpression" || path1.node.value.type === "FunctionExpression")) {
712
- const dataNode = path1.node.value;
713
- traverse2(dataNode, {
714
- ReturnStatement(path2) {
715
- if (path2.parent === dataNode.body) {
716
- if (path2.node.argument?.type === "ObjectExpression") {
717
- path2.node.argument.properties.forEach((prop) => {
718
- if (prop.type === "ObjectProperty") {
719
- if (prop.key.type === "Identifier") {
720
- const name = prop.key.name;
721
- graph.nodes.add(name);
722
- tNodes.set(name, prop.key);
723
- nodeCollection.addNode(name, prop, {
724
- comment: getComment(prop)
725
- });
726
- if (!graph.edges.get(name)) {
727
- graph.edges.set(name, /* @__PURE__ */ new Set());
728
- }
729
- }
730
- }
731
- });
732
- }
733
- }
734
- }
735
- }, path1.scope, path1);
736
- }
737
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "computed") {
738
- const computedNode = path1.node;
739
- if (computedNode.value.type === "ObjectExpression") {
740
- computedNode.value.properties.forEach((prop) => {
741
- if (prop.type === "ObjectProperty" || prop.type === "ObjectMethod") {
742
- if (prop.key.type === "Identifier") {
743
- const name = prop.key.name;
744
- graph.nodes.add(name);
745
- tNodes.set(name, prop.key);
746
- nodeCollection.addNode(name, prop, {
747
- isComputed: true,
748
- comment: getComment(prop)
749
- });
750
- if (!graph.edges.get(name)) {
751
- graph.edges.set(name, /* @__PURE__ */ new Set());
752
- }
753
- }
754
- }
755
- });
756
- }
757
- }
758
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "methods") {
759
- const methodsNode = path1.node;
760
- if (methodsNode.value.type === "ObjectExpression") {
761
- methodsNode.value.properties.forEach((prop) => {
762
- if (prop.type === "ObjectProperty" || prop.type === "ObjectMethod") {
763
- if (prop.key.type === "Identifier") {
764
- const name = prop.key.name;
765
- graph.nodes.add(name);
766
- tNodes.set(name, prop.key);
767
- nodeCollection.addNode(name, prop, {
768
- isMethod: true,
769
- comment: getComment(prop)
770
- });
771
- if (!graph.edges.get(name)) {
772
- graph.edges.set(name, /* @__PURE__ */ new Set());
773
- }
774
- }
775
- }
776
- });
777
- }
778
- }
779
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "render" && (path1.node.value.type === "ArrowFunctionExpression" || path1.node.value.type === "FunctionExpression")) {
780
- traverse2(path1.node.value, {
781
- ReturnStatement(path2) {
782
- const templateNode = path2.node;
783
- traverse2(templateNode, {
784
- MemberExpression(path3) {
785
- if (path3.node.object && path3.node.object.type === "ThisExpression") {
786
- if (path3.node.property && path3.node.property.type === "Identifier") {
787
- nodesUsedInTemplate.add(path3.node.property.name);
788
- }
789
- }
790
- }
791
- }, path2.scope, path2);
792
- }
793
- }, path1.scope, path1);
794
- }
795
- }
796
- },
797
- ObjectMethod(path1) {
798
- if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
799
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
800
- const setupNode = path1.node;
801
- const spread = [];
802
- traverse2(setupNode, {
803
- ReturnStatement(path2) {
804
- if (path2.node.argument?.type === "ObjectExpression") {
805
- const returnNode = path2.node.argument;
806
- traverse2(returnNode, {
807
- SpreadElement(path3) {
808
- if (path3.node.argument.type === "CallExpression" && path3.node.argument.callee.type === "Identifier" && path3.node.argument.callee.name === "toRefs" && path3.node.argument.arguments[0].type === "Identifier") {
809
- spread.push(path3.node.argument.arguments[0].name);
810
- } else if (path3.node.argument.type === "Identifier") {
811
- spread.push(path3.node.argument.name);
812
- }
813
- }
814
- }, path2.scope, path2);
815
- }
816
- if (path2.node.argument?.type === "FunctionExpression" || path2.node.argument?.type === "ArrowFunctionExpression") {
817
- const templateNode = path2.node.argument.body;
818
- traverse2(templateNode, {
819
- Identifier(path3) {
820
- const binding = path3.scope.getBinding(path3.node.name);
821
- if (binding?.scope === path1.scope) {
822
- nodesUsedInTemplate.add(path3.node.name);
823
- }
824
- },
825
- JSXIdentifier(path3) {
826
- const binding = path3.scope.getBinding(path3.node.name);
827
- if (binding?.scope === path1.scope) {
828
- nodesUsedInTemplate.add(path3.node.name);
829
- }
830
- }
831
- }, path2.scope, path2);
832
- }
833
- }
834
- }, path1.scope, path1);
835
- const {
836
- graph: {
837
- nodes: tempNodes,
838
- edges: tempEdges,
839
- spread: tempSpread
840
- },
841
- nodeCollection: tempNodeCollection
842
- } = processSetup(setupNode, path1.scope, setupNode, spread, lineOffset);
843
- traverse2(setupNode, {
844
- ReturnStatement(path2) {
845
- if (path2.scope !== path1.scope) {
846
- return;
847
- }
848
- if (path2.node.argument?.type === "ObjectExpression") {
849
- const returnNode = path2.node.argument;
850
- traverse2(returnNode, {
851
- ObjectProperty(path3) {
852
- if (path3.parent === returnNode) {
853
- if (path3.node.key.type === "Identifier" && path3.node.value.type === "Identifier" && tempNodes.has(path3.node.value.name)) {
854
- const valName = path3.node.value.name;
855
- if (!graph.nodes.has(valName)) {
856
- graph.nodes.add(valName);
857
- tNodes.set(valName, path3.node.value);
858
- nodeCollection.addTypedNode(
859
- valName,
860
- tempNodeCollection.nodes.get(valName)
861
- );
862
- }
863
- if (!graph.edges.has(valName)) {
864
- graph.edges.set(valName, /* @__PURE__ */ new Set([...Array.from(
865
- tempEdges.get(valName) || /* @__PURE__ */ new Set()
866
- )]));
867
- }
868
- const name = path3.node.key.name;
869
- if (name !== valName) {
870
- graph.nodes.add(name);
871
- tNodes.set(name, path3.node.key);
872
- nodeCollection.addNode(name, path3.node.key, {
873
- comment: getComment(path3.node)
874
- });
875
- graph.edges.set(name, /* @__PURE__ */ new Set([{
876
- label: valName,
877
- type: getRelationType(path3)
878
- }]));
879
- }
880
- }
881
- }
882
- },
883
- SpreadElement(path3) {
884
- if (path3.node.argument.type === "CallExpression" && path3.node.argument.callee.type === "Identifier" && path3.node.argument.callee.name === "toRefs" && path3.node.argument.arguments[0].type === "Identifier" && tempSpread.get(path3.node.argument.arguments[0].name)) {
885
- tempSpread.get(path3.node.argument.arguments[0].name)?.forEach((name) => {
886
- graph.nodes.add(name);
887
- tNodes.set(name, path3.node.argument.arguments[0]);
888
- nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
889
- if (!graph.edges.get(name)) {
890
- graph.edges.set(name, /* @__PURE__ */ new Set());
891
- tempEdges.get(name)?.forEach((edge) => {
892
- graph.edges.get(name)?.add(edge);
893
- });
894
- }
895
- });
896
- } else if (path3.node.argument.type === "Identifier" && tempSpread.get(path3.node.argument.name)) {
897
- tempSpread.get(path3.node.argument.name)?.forEach((name) => {
898
- graph.nodes.add(name);
899
- tNodes.set(name, path3.node.argument);
900
- nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
901
- if (!graph.edges.get(name)) {
902
- graph.edges.set(name, /* @__PURE__ */ new Set());
903
- tempEdges.get(name)?.forEach((edge) => {
904
- graph.edges.get(name)?.add(edge);
905
- });
906
- }
907
- });
908
- }
909
- }
910
- }, path2.scope, path2);
911
- } else {
912
- graph.edges = tempEdges;
913
- graph.nodes = tempNodes;
914
- nodeCollection = tempNodeCollection;
915
- }
916
- }
917
- }, path1.scope, path1);
918
- }
919
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "data") {
920
- const dataNode = path1.node;
921
- traverse2(dataNode, {
922
- ReturnStatement(path2) {
923
- if (path2.parent === dataNode.body) {
924
- if (path2.node.argument?.type === "ObjectExpression") {
925
- path2.node.argument.properties.forEach((prop) => {
926
- if (prop.type === "ObjectProperty") {
927
- if (prop.key.type === "Identifier") {
928
- const name = prop.key.name;
929
- graph.nodes.add(name);
930
- tNodes.set(name, prop.key);
931
- nodeCollection.addNode(name, prop, {
932
- comment: getComment(prop)
933
- });
934
- if (!graph.edges.get(name)) {
935
- graph.edges.set(name, /* @__PURE__ */ new Set());
936
- }
937
- }
938
- }
939
- });
940
- }
941
- }
942
- }
943
- }, path1.scope, path1);
944
- }
945
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "render") {
946
- traverse2(path1.node, {
947
- ReturnStatement(path2) {
948
- const templateNode = path2.node;
949
- traverse2(templateNode, {
950
- MemberExpression(path3) {
951
- if (path3.node.object && path3.node.object.type === "ThisExpression") {
952
- if (path3.node.property && path3.node.property.type === "Identifier") {
953
- nodesUsedInTemplate.add(path3.node.property.name);
954
- }
955
- }
956
- }
957
- }, path2.scope, path2);
958
- }
959
- }, path1.scope, path1);
960
- }
961
- }
962
- }
963
- }, path.scope, path);
964
- traverse2(node, {
965
- ObjectMethod(path1) {
966
- if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
967
- if (path1.node.key.type === "Identifier" && vueLifeCycleHooks.includes(path1.node.key.name)) {
968
- const hookName = path1.node.key.name;
969
- traverse2(path1.node.body, {
970
- MemberExpression(path2) {
971
- if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
972
- const _node = nodeCollection.getNode(path2.node.property.name);
973
- if (_node?.info?.used) {
974
- _node?.info?.used?.add(hookName);
975
- } else if (_node) {
976
- _node.info = {
977
- ..._node?.info,
978
- used: /* @__PURE__ */ new Set([hookName])
979
- };
980
- }
981
- }
982
- }
983
- }, path1.scope, path1);
984
- }
985
- }
986
- },
987
- ObjectProperty(path1) {
988
- if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
989
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "computed") {
990
- const computedNode = path1.node;
991
- if (computedNode.value.type === "ObjectExpression") {
992
- computedNode.value.properties.forEach((prop) => {
993
- if (prop.type === "ObjectMethod" && prop.key.type === "Identifier") {
994
- const name = prop.key.name;
995
- traverse2(prop, {
996
- MemberExpression(path2) {
997
- if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
998
- graph.edges.get(name)?.add({
999
- label: path2.node.property.name,
1000
- type: getRelationType(path2)
1001
- });
1002
- }
1003
- }
1004
- }, path1.scope, path1);
1005
- }
1006
- if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.value.type === "ObjectExpression") {
1007
- const name = prop.key.name;
1008
- prop.value.properties.forEach((prop1) => {
1009
- if (prop1.type === "ObjectProperty" && prop1.key.type === "Identifier" && prop1.key.name === "get") {
1010
- traverse2(prop1, {
1011
- MemberExpression(path2) {
1012
- if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1013
- graph.edges.get(name)?.add({
1014
- label: path2.node.property.name,
1015
- type: getRelationType(path2)
1016
- });
1017
- }
1018
- }
1019
- }, path1.scope, path1);
1020
- }
1021
- });
1022
- }
1023
- });
1024
- }
1025
- }
1026
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "methods") {
1027
- const methodsNode = path1.node;
1028
- if (methodsNode.value.type === "ObjectExpression") {
1029
- methodsNode.value.properties.forEach((prop) => {
1030
- if ((prop.type === "ObjectMethod" || prop.type === "ObjectProperty") && prop.key.type === "Identifier") {
1031
- const name = prop.key.name;
1032
- traverse2(prop, {
1033
- MemberExpression(path2) {
1034
- if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1035
- graph.edges.get(name)?.add({
1036
- label: path2.node.property.name,
1037
- type: getRelationType(path2)
1038
- });
1039
- }
1040
- }
1041
- }, path1.scope, path1);
1042
- }
1043
- });
1044
- }
1045
- }
1046
- if (path1.node.key.type === "Identifier" && [...watchHooks, ...vueLifeCycleHooks].includes(path1.node.key.name)) {
1047
- const hookName = path1.node.key.name;
1048
- if (watchHooks.includes(hookName) && path1.node.value.type === "ObjectExpression") {
1049
- path1.node.value.properties.forEach((prop) => {
1050
- if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && (prop.key.type === "Identifier" || prop.key.type === "StringLiteral")) {
1051
- const keyName = prop.key.type === "Identifier" ? prop.key.name : prop.key.type === "StringLiteral" ? prop.key.value.split(".")[0] : "";
1052
- const watchArg = tNodes.get(keyName);
1053
- const _node = nodeCollection.getNode(keyName);
1054
- if (_node?.info?.used) {
1055
- _node?.info?.used?.add(hookName);
1056
- } else if (_node) {
1057
- _node.info = {
1058
- ..._node?.info,
1059
- used: /* @__PURE__ */ new Set([hookName])
1060
- };
1061
- }
1062
- traverse2(path1.node.value, {
1063
- MemberExpression(path2) {
1064
- if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1065
- if (watchArg && watchArg.name !== path2.node.property.name) {
1066
- graph.edges.get(watchArg.name)?.add({
1067
- label: path2.node.property.name,
1068
- type: getRelationType(path2)
1069
- });
1070
- }
1071
- }
1072
- }
1073
- }, path1.scope, path1);
1074
- }
1075
- });
1076
- } else {
1077
- traverse2(path1.node.value, {
1078
- MemberExpression(path2) {
1079
- if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1080
- const _node = nodeCollection.getNode(path2.node.property.name);
1081
- if (_node?.info?.used) {
1082
- _node?.info?.used?.add(hookName);
1083
- } else if (_node) {
1084
- _node.info = {
1085
- ..._node?.info,
1086
- used: /* @__PURE__ */ new Set([hookName])
1087
- };
1088
- }
1089
- }
1090
- }
1091
- }, path1.scope, path1);
1092
- }
1093
- }
1094
- }
1095
- }
1096
- }, path.scope, path);
1097
- }
1098
- traverse2(ast, {
1099
- ExportDefaultDeclaration(path) {
1100
- if (path.node.declaration.type === "ObjectExpression") {
1101
- process(path.node.declaration, path);
1102
- } else if (path.node.declaration.type === "CallExpression" && path.node.declaration.callee.type === "Identifier" && path.node.declaration.callee.name === "defineComponent" && path.node.declaration.arguments[0].type === "ObjectExpression") {
1103
- process(path.node.declaration.arguments[0], path);
1104
- }
1105
- }
1106
- });
1107
- return {
1108
- graph: nodeCollection.map(graph),
1109
- nodesUsedInTemplate
1110
- };
1111
- }
1112
-
1113
- // src/analyze/style.ts
1114
- function lexBinding(content, start) {
1115
- let state = 0 /* inParens */;
1116
- let parenDepth = 0;
1117
- for (let i = start; i < content.length; i++) {
1118
- const char = content.charAt(i);
1119
- switch (state) {
1120
- case 0 /* inParens */:
1121
- if (char === "'") {
1122
- state = 1 /* inSingleQuoteString */;
1123
- } else if (char === '"') {
1124
- state = 2 /* inDoubleQuoteString */;
1125
- } else if (char === "(") {
1126
- parenDepth++;
1127
- } else if (char === ")") {
1128
- if (parenDepth > 0) {
1129
- parenDepth--;
1130
- } else {
1131
- return i;
1132
- }
1133
- }
1134
- break;
1135
- case 1 /* inSingleQuoteString */:
1136
- if (char === "'") {
1137
- state = 0 /* inParens */;
1138
- }
1139
- break;
1140
- case 2 /* inDoubleQuoteString */:
1141
- if (char === '"') {
1142
- state = 0 /* inParens */;
1143
- }
1144
- break;
1145
- }
1146
- }
1147
- return null;
1148
- }
1149
- function normalizeExpression(exp) {
1150
- exp = exp.trim();
1151
- if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1152
- return exp.slice(1, -1);
1153
- }
1154
- return exp;
1155
- }
1156
- var vBindRE = /v-bind\s*\(/g;
1157
- function analyze3(styles) {
1158
- const nodes = /* @__PURE__ */ new Set();
1159
- styles.forEach((style) => {
1160
- let match;
1161
- const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1162
- while (match = vBindRE.exec(content)) {
1163
- const start = match.index + match[0].length;
1164
- const end = lexBinding(content, start);
1165
- if (end !== null) {
1166
- const variable = normalizeExpression(content.slice(start, end));
1167
- nodes.add(variable);
1168
- }
1169
- }
1170
- });
1171
- return nodes;
1172
- }
1173
-
1174
- // src/analyze/template.ts
1175
- import _traverse3 from "@babel/traverse";
1176
- import { babelParse as babelParse3, compileTemplate } from "@vue/compiler-sfc";
1177
- var traverse3 = _traverse3.default?.default || _traverse3.default || _traverse3;
1178
- function analyze4(content) {
1179
- const id = "template";
1180
- const { code } = compileTemplate({
1181
- id,
1182
- source: content,
1183
- filename: `${id}.js`
1184
- });
1185
- const ast = babelParse3(code, { sourceType: "module", plugins: [
1186
- "typescript"
1187
- ] });
1188
- const nodes = /* @__PURE__ */ new Set();
1189
- traverse3(ast, {
1190
- MemberExpression(path) {
1191
- if (path.type === "MemberExpression") {
1192
- if (path.node.object && path.node.object.type === "Identifier" && path.node.object.name === "_ctx") {
1193
- if (path.node.property && path.node.property.type === "Identifier") {
1194
- nodes.add(path.node.property.name);
1195
- }
1196
- }
1197
- }
1198
- },
1199
- ObjectProperty(path) {
1200
- if (path.node.key.type === "Identifier" && path.node.key.name === "ref") {
1201
- if (path.node.value.type === "StringLiteral") {
1202
- const name = path.node.value.value;
1203
- if (name) {
1204
- nodes.add(name);
1205
- }
1206
- }
1207
- }
1208
- },
1209
- // component
1210
- CallExpression(path) {
1211
- if (path.node.callee.type === "Identifier" && path.node.callee.name === "_resolveComponent") {
1212
- if (path.node.arguments[0].type === "StringLiteral") {
1213
- const name = path.node.arguments[0].value;
1214
- if (name) {
1215
- nodes.add(name);
1216
- }
1217
- }
1218
- }
1219
- }
1220
- });
1221
- return nodes;
1222
- }
1223
-
1224
- // src/analyze/tsx.ts
1225
- import { babelParse as babelParse4 } from "@vue/compiler-sfc";
1226
-
1227
- // src/utils/traverse.ts
1228
- import _traverse4 from "@babel/traverse";
1229
- var traverse4 = _traverse4.default?.default || _traverse4.default || _traverse4;
1230
- function rescureObjectPattern({ node, rootScope, res, parentScope, parentPath }) {
1231
- traverse4(node, {
1232
- ObjectProperty(path1) {
1233
- if (path1.node.type === "ObjectProperty" && path1.node.key.type === "Identifier" && path1.node.value.type === "Identifier") {
1234
- const name = path1.node.value.name;
1235
- const _scope = path1.scope.getBinding(name)?.scope;
1236
- if (_scope && _scope === rootScope) {
1237
- res.push(path1.node.value);
1238
- }
1239
- } else if (path1.node.type === "ObjectProperty" && path1.node.key.type === "Identifier" && path1.node.value.type === "ObjectPattern") {
1240
- rescureObjectPattern({
1241
- node: path1.node.value,
1242
- rootScope,
1243
- res,
1244
- parentScope: path1.scope,
1245
- parentPath: path1
1246
- });
1247
- }
1248
- },
1249
- RestElement(path1) {
1250
- if (path1.node.argument.type === "Identifier") {
1251
- const name = path1.node.argument.name;
1252
- const _scope = path1.scope.getBinding(name)?.scope;
1253
- if (_scope && _scope === rootScope) {
1254
- res.push(path1.node.argument);
1255
- }
1256
- }
1257
- }
1258
- }, parentScope, parentPath);
1259
- }
1260
- function rescureArrayPattern({ node, rootScope, res, parentScope, parentPath }) {
1261
- traverse4(node, {
1262
- Identifier(path1) {
1263
- if (path1.node.type === "Identifier") {
1264
- const name = path1.node.name;
1265
- const _scope = path1.scope.getBinding(name)?.scope;
1266
- if (_scope && _scope === rootScope) {
1267
- res.push(path1.node);
1268
- }
1269
- }
1270
- },
1271
- ArrayPattern(path1) {
1272
- if (path1.node.type === "ArrayPattern") {
1273
- rescureArrayPattern({
1274
- node: path1.node,
1275
- rootScope,
1276
- res,
1277
- parentScope: path1.scope,
1278
- parentPath: path1
1279
- });
1280
- }
1281
- }
1282
- }, parentScope, parentPath);
1283
- }
1284
- function parseNodeIdentifierPattern({
1285
- path,
1286
- rootScope,
1287
- cb
1288
- }) {
1289
- if (path.node.id.type !== "Identifier") {
1290
- return;
1291
- }
1292
- if (path.node.init?.type === "ArrowFunctionExpression" || path.node.init?.type === "FunctionExpression") {
1293
- cb?.({
1294
- name: path.node.id.name,
1295
- node: path.node,
1296
- path,
1297
- scope: rootScope
1298
- });
1299
- } else {
1300
- cb?.({
1301
- name: path.node.id.name,
1302
- node: path.node,
1303
- path,
1304
- scope: rootScope
1305
- });
1306
- }
1307
- }
1308
- function parseNodeObjectPattern({ path, rootScope, cb }) {
1309
- if (path.node.id.type !== "ObjectPattern") {
1310
- return;
1311
- }
1312
- path.node.id.properties.forEach((property) => {
1313
- if (property.type === "ObjectProperty" && property.key.type === "Identifier" && property.value.type === "Identifier") {
1314
- cb?.({
1315
- name: property.value.name,
1316
- node: property,
1317
- path,
1318
- scope: rootScope
1319
- });
1320
- } else if (property.type === "ObjectProperty" && property.key.type === "Identifier" && property.value.type === "AssignmentPattern") {
1321
- cb?.({
1322
- name: property.key.name,
1323
- node: property,
1324
- path,
1325
- scope: rootScope
1326
- });
1327
- } else if (property.type === "RestElement" && property.argument.type === "Identifier") {
1328
- cb?.({
1329
- name: property.argument.name,
1330
- node: property,
1331
- path,
1332
- scope: rootScope
1333
- });
1334
- } else if (property.type === "ObjectProperty" && property.key.type === "Identifier" && property.value.type === "ObjectPattern") {
1335
- const res = [];
1336
- rescureObjectPattern({
1337
- node: property.value,
1338
- rootScope,
1339
- res,
1340
- parentScope: path.scope,
1341
- parentPath: path
1342
- });
1343
- res.forEach((r) => cb?.({
1344
- name: r.name,
1345
- node: r,
1346
- path,
1347
- scope: rootScope
1348
- }));
1349
- }
1350
- });
1351
- }
1352
- function parseNodeArrayPattern({ path, rootScope, cb }) {
1353
- if (path.node.id.type !== "ArrayPattern") {
1354
- return;
1355
- }
1356
- path.node.id.elements.forEach((ele) => {
1357
- if (ele?.type === "Identifier") {
1358
- cb?.({
1359
- name: ele.name,
1360
- node: ele,
1361
- path,
1362
- scope: rootScope
1363
- });
1364
- } else if (ele?.type === "ArrayPattern") {
1365
- const res = [];
1366
- rescureArrayPattern({
1367
- node: ele,
1368
- rootScope,
1369
- res,
1370
- parentScope: path.scope,
1371
- parentPath: path
1372
- });
1373
- res.forEach((r) => cb?.({
1374
- name: r.name,
1375
- node: r,
1376
- path,
1377
- scope: rootScope
1378
- }));
1379
- } else if (ele?.type === "AssignmentPattern") {
1380
- if (ele.left.type === "Identifier") {
1381
- cb?.({
1382
- name: ele.left.name,
1383
- node: ele,
1384
- path,
1385
- scope: rootScope
1386
- });
1387
- }
1388
- } else if (ele?.type === "RestElement") {
1389
- if (ele.argument.type === "Identifier") {
1390
- cb?.({
1391
- name: ele.argument.name,
1392
- node: ele,
1393
- path,
1394
- scope: rootScope
1395
- });
1396
- }
1397
- }
1398
- });
1399
- }
1400
- function parseNodeFunctionPattern({ path, rootScope, cb }) {
1401
- if (path.node.type !== "FunctionDeclaration") {
1402
- return;
1403
- }
1404
- if (path.node.id?.type === "Identifier") {
1405
- cb?.({
1406
- name: path.node.id.name,
1407
- node: path.node,
1408
- path,
1409
- scope: rootScope
1410
- });
1411
- }
1412
- }
1413
- function parseEdgeLeftIdentifierPattern({ path, rootScope, cb, collectionNodes, spread }) {
1414
- if (!path.node.id || path.node.id.type !== "Identifier") {
1415
- return;
1416
- }
1417
- if (path.node.init?.type && [
1418
- "ArrowFunctionExpression",
1419
- "FunctionExpression",
1420
- "CallExpression",
1421
- "ObjectExpression",
1422
- "ArrayExpression"
1423
- ].includes(path.node.init.type)) {
1424
- if (collectionNodes.has(path.node.id.name) && path.scope.getBinding(path.node.id.name)?.scope === rootScope) {
1425
- const name = path.node.id.name;
1426
- traverse4(path.node.init, {
1427
- Identifier(path1) {
1428
- const binding = path1.scope.getBinding(path1.node.name);
1429
- if (binding?.scope === rootScope && collectionNodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node)) {
1430
- cb?.({
1431
- fromName: name,
1432
- toName: path1.node.name,
1433
- path: path1,
1434
- scope: rootScope,
1435
- collectionNodes
1436
- });
1437
- }
1438
- },
1439
- MemberExpression(path1) {
1440
- if (spread?.length && path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier") {
1441
- cb?.({
1442
- fromName: name,
1443
- toName: path1.node.property.name,
1444
- toScope: path1.scope.getBinding(path1.node.object.name)?.scope,
1445
- path: path1,
1446
- scope: rootScope,
1447
- collectionNodes
1448
- });
1449
- }
1450
- }
1451
- }, path.scope, path);
1452
- }
1453
- }
1454
- }
1455
- function parseEdgeLeftObjectPattern({ path, rootScope, cb, collectionNodes }) {
1456
- if (!path.node.id || path.node.id.type !== "ObjectPattern") {
1457
- return;
1458
- }
1459
- if (path.node.init?.type && [
1460
- "ArrowFunctionExpression",
1461
- "FunctionExpression",
1462
- "CallExpression",
1463
- "ObjectExpression",
1464
- "ArrayExpression"
1465
- ].includes(path.node.init.type)) {
1466
- const res = [];
1467
- rescureObjectPattern({
1468
- node: path.node.id,
1469
- rootScope,
1470
- res,
1471
- parentScope: path.scope,
1472
- parentPath: path
1473
- });
1474
- res.filter((r) => collectionNodes.has(r.name) && path.scope.getBinding(r.name)?.scope === rootScope);
1475
- traverse4(path.node.init, {
1476
- Identifier(path1) {
1477
- const binding = path1.scope.getBinding(path1.node.name);
1478
- if (binding?.scope === rootScope && collectionNodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node)) {
1479
- res.forEach((r) => {
1480
- cb?.({
1481
- fromName: r.name,
1482
- toName: path1.node.name,
1483
- path: path1,
1484
- scope: rootScope,
1485
- collectionNodes
1486
- });
1487
- });
1488
- }
1489
- }
1490
- }, path.scope, path);
1491
- }
1492
- }
1493
- function parseEdgeLeftArrayPattern({ path, rootScope, cb, collectionNodes }) {
1494
- if (!path.node.id || path.node.id.type !== "ArrayPattern") {
1495
- return;
1496
- }
1497
- if (path.node.init?.type && [
1498
- "ArrowFunctionExpression",
1499
- "FunctionExpression",
1500
- "CallExpression",
1501
- "ObjectExpression",
1502
- "ArrayExpression"
1503
- ].includes(path.node.init.type)) {
1504
- const res = [];
1505
- rescureArrayPattern({
1506
- node: path.node.id,
1507
- rootScope,
1508
- res,
1509
- parentScope: path.scope,
1510
- parentPath: path
1511
- });
1512
- res.filter((r) => collectionNodes.has(r.name) && path.scope.getBinding(r.name)?.scope === rootScope);
1513
- traverse4(path.node.init, {
1514
- Identifier(path1) {
1515
- const binding = path1.scope.getBinding(path1.node.name);
1516
- if (binding?.scope === rootScope && collectionNodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node)) {
1517
- res.forEach((r) => {
1518
- cb?.({
1519
- fromName: r.name,
1520
- toName: path1.node.name,
1521
- path: path1,
1522
- scope: rootScope,
1523
- collectionNodes
1524
- });
1525
- });
1526
- }
1527
- }
1528
- }, path.scope, path);
1529
- }
1530
- }
1531
- function parseEdgeFunctionPattern({ path, rootScope, cb, collectionNodes }) {
1532
- if (!path.node.id) {
1533
- return;
1534
- }
1535
- if (collectionNodes.has(path.node.id.name) && path.scope.getBinding(path.node.id.name)?.scope === rootScope) {
1536
- const name = path.node.id.name;
1537
- traverse4(path.node.body, {
1538
- Identifier(path1) {
1539
- const binding = path1.scope.getBinding(path1.node.name);
1540
- if (binding?.scope === rootScope && collectionNodes.has(path1.node.name)) {
1541
- cb?.({
1542
- fromName: name,
1543
- toName: path1.node.name,
1544
- path: path1,
1545
- scope: rootScope,
1546
- collectionNodes
1547
- });
1548
- }
1549
- }
1550
- }, path.scope, path);
1551
- }
1552
- }
1553
- function parseReturnJsxPattern({ path, parentPath, cb }) {
1554
- if (path.node.argument && // return () => (<div></div>)
1555
- // return function() (<div></div>)
1556
- ((path.node.argument.type === "ArrowFunctionExpression" || path.node.argument.type === "FunctionExpression") && (path.node.argument.body.type === "JSXElement" || path.node.argument.body.type === "JSXFragment") || (path.node.argument.type === "JSXElement" || path.node.argument.type === "JSXFragment"))) {
1557
- path.traverse({
1558
- Identifier(path1) {
1559
- cb?.({
1560
- name: path1.node.name,
1561
- path: path1,
1562
- parentPath
1563
- });
1564
- }
1565
- });
1566
- }
1567
- }
1568
- function traverseSetup({ node, parentScope, parentPath }) {
1569
- let path;
1570
- traverse4(node, {
1571
- ObjectMethod(path1) {
1572
- if (parentPath.node.declaration.type === "ObjectExpression" && path1.parent === parentPath.node.declaration || parentPath.node.declaration.type === "CallExpression" && path1.parent === parentPath.node.declaration.arguments[0]) {
1573
- if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
1574
- path = path1;
1575
- }
1576
- }
1577
- }
1578
- }, parentScope, parentPath);
1579
- return path;
1580
- }
1581
- function collectionSpread({ path: path1, spread }) {
1582
- path1.traverse({
1583
- ReturnStatement(path2) {
1584
- if (path2.node.argument?.type === "ObjectExpression") {
1585
- const returnNode = path2.node.argument;
1586
- traverse4(returnNode, {
1587
- SpreadElement(path3) {
1588
- if (path3.node.argument.type === "CallExpression" && path3.node.argument.callee.type === "Identifier" && path3.node.argument.callee.name === "toRefs" && path3.node.argument.arguments[0].type === "Identifier") {
1589
- spread.push(path3.node.argument.arguments[0].name);
1590
- } else if (path3.node.argument.type === "Identifier") {
1591
- spread.push(path3.node.argument.name);
1592
- }
1593
- }
1594
- }, path2.scope, path2);
1595
- }
1596
- }
1597
- });
1598
- }
1599
- function addIdentifiesToGraphByScanReturn({ path: path1, graph, nodeCollection, tempNodeCollection, tempEdges }) {
1600
- path1.traverse({
1601
- ReturnStatement(path2) {
1602
- if (path2.node.argument?.type === "ObjectExpression") {
1603
- const returnNode = path2.node.argument;
1604
- traverse4(returnNode, {
1605
- ObjectProperty(path3) {
1606
- if (path3.parent === returnNode) {
1607
- if (path3.node.key.type === "Identifier" && path3.node.value.type === "Identifier") {
1608
- const valName = path3.node.value.name;
1609
- if (!graph.nodes.has(valName)) {
1610
- graph.nodes.add(valName);
1611
- nodeCollection.addTypedNode(
1612
- valName,
1613
- tempNodeCollection.nodes.get(valName)
1614
- );
1615
- }
1616
- if (!graph.edges.has(valName)) {
1617
- graph.edges.set(valName, /* @__PURE__ */ new Set([...Array.from(
1618
- tempEdges.get(valName) || /* @__PURE__ */ new Set()
1619
- )]));
1620
- }
1621
- const name = path3.node.key.name;
1622
- if (name !== valName) {
1623
- graph.nodes.add(name);
1624
- nodeCollection.addNode(name, path3.node.key, {
1625
- comment: getComment(path3.node)
1626
- });
1627
- graph.edges.set(name, /* @__PURE__ */ new Set([{
1628
- label: valName,
1629
- type: getRelationType(path3)
1630
- }]));
1631
- }
1632
- }
1633
- }
1634
- }
1635
- }, path2.scope, path2);
1636
- }
1637
- }
1638
- });
1639
- }
1640
- function addSpreadToGraphByScanReturn({ path: path1, graph, nodeCollection, tempNodeCollection, tempEdges, tempSpread }) {
1641
- path1.traverse({
1642
- ReturnStatement(path2) {
1643
- if (path2.node.argument?.type === "ObjectExpression") {
1644
- const returnNode = path2.node.argument;
1645
- traverse4(returnNode, {
1646
- SpreadElement(path3) {
1647
- if (path3.node.argument.type === "CallExpression" && path3.node.argument.callee.type === "Identifier" && path3.node.argument.callee.name === "toRefs" && path3.node.argument.arguments[0].type === "Identifier" && tempSpread.get(path3.node.argument.arguments[0].name)) {
1648
- tempSpread.get(path3.node.argument.arguments[0].name)?.forEach((name) => {
1649
- graph.nodes.add(name);
1650
- nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
1651
- if (!graph.edges.get(name)) {
1652
- graph.edges.set(name, /* @__PURE__ */ new Set());
1653
- tempEdges.get(name)?.forEach((edge) => {
1654
- graph.edges.get(name)?.add(edge);
1655
- });
1656
- }
1657
- });
1658
- } else if (path3.node.argument.type === "Identifier" && tempSpread.get(path3.node.argument.name)) {
1659
- tempSpread.get(path3.node.argument.name)?.forEach((name) => {
1660
- graph.nodes.add(name);
1661
- nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
1662
- if (!graph.edges.get(name)) {
1663
- graph.edges.set(name, /* @__PURE__ */ new Set());
1664
- tempEdges.get(name)?.forEach((edge) => {
1665
- graph.edges.get(name)?.add(edge);
1666
- });
1667
- }
1668
- });
1669
- }
1670
- }
1671
- }, path2.scope, path2);
1672
- }
1673
- }
1674
- });
1675
- }
1676
- function addGraphBySpreadIdentifier({ path: path1, graph, nodeCollection, iname }) {
1677
- if (path1.node.init?.type === "ObjectExpression") {
1678
- path1.node.init?.properties.forEach((prop) => {
1679
- if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
1680
- const keyName = prop.key.name;
1681
- graph.nodes.add(keyName);
1682
- nodeCollection.addNode(keyName, prop, {
1683
- comment: getComment(prop)
1684
- });
1685
- if (!graph.edges.get(keyName)) {
1686
- graph.edges.set(keyName, /* @__PURE__ */ new Set());
1687
- }
1688
- if (graph.spread.has(iname)) {
1689
- graph.spread.get(iname)?.add(keyName);
1690
- } else {
1691
- graph.spread.set(iname, /* @__PURE__ */ new Set([keyName]));
1692
- }
1693
- } else if (prop.type === "SpreadElement") {
1694
- console.warn("not support spread in spread");
1695
- }
1696
- });
1697
- }
1698
- if (path1.node.init?.type === "CallExpression" && path1.node.init?.callee.type === "Identifier" && path1.node.init?.callee.name === "reactive") {
1699
- const arg = path1.node.init?.arguments[0];
1700
- if (arg.type === "ObjectExpression") {
1701
- arg.properties.forEach((prop) => {
1702
- if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
1703
- const keyName = prop.key.name;
1704
- graph.nodes.add(keyName);
1705
- nodeCollection.addNode(keyName, prop, {
1706
- comment: getComment(prop)
1707
- });
1708
- if (!graph.edges.get(keyName)) {
1709
- graph.edges.set(keyName, /* @__PURE__ */ new Set());
1710
- }
1711
- if (graph.spread.has(iname)) {
1712
- graph.spread.get(iname)?.add(keyName);
1713
- } else {
1714
- graph.spread.set(iname, /* @__PURE__ */ new Set([keyName]));
1715
- }
1716
- } else if (prop.type === "SpreadElement") {
1717
- console.warn("not support spread in spread");
1718
- }
1719
- });
1720
- }
1721
- }
1722
- }
1723
-
1724
- // src/analyze/tsx.ts
1725
- function processByReturnNotJSX(params) {
1726
- const { node, parentPath, lineOffset, addInfo } = params;
1727
- const spread = [];
1728
- const nodeCollection = new NodeCollection(lineOffset, addInfo);
1729
- const graph = {
1730
- nodes: /* @__PURE__ */ new Set(),
1731
- edges: /* @__PURE__ */ new Map()
1732
- };
1733
- const setupPath = traverseSetup({ node, parentScope: parentPath.scope, parentPath });
1734
- collectionSpread({ path: setupPath, spread });
1735
- const {
1736
- graph: {
1737
- nodes: tempNodes,
1738
- edges: tempEdges,
1739
- spread: tempSpread
1740
- },
1741
- nodeCollection: tempNodeCollection,
1742
- nodesUsedInTemplate
1743
- } = processByReturnJSX({ node, parentPath, spread, lineOffset, addInfo });
1744
- addIdentifiesToGraphByScanReturn({
1745
- path: setupPath,
1746
- graph,
1747
- nodeCollection,
1748
- tempNodeCollection,
1749
- tempEdges
1750
- });
1751
- addSpreadToGraphByScanReturn({
1752
- path: setupPath,
1753
- graph,
1754
- nodeCollection,
1755
- tempNodeCollection,
1756
- tempEdges,
1757
- tempSpread
1758
- });
1759
- return {
1760
- graph,
1761
- nodeCollection,
1762
- nodesUsedInTemplate
1763
- };
1764
- }
1765
- function processByReturnJSX(params) {
1766
- const { node, parentPath, spread = [], lineOffset, addInfo } = params;
1767
- const nodeCollection = new NodeCollection(lineOffset, addInfo);
1768
- const nodesUsedInTemplate = /* @__PURE__ */ new Set();
1769
- const graph = {
1770
- nodes: /* @__PURE__ */ new Set(),
1771
- edges: /* @__PURE__ */ new Map(),
1772
- spread: /* @__PURE__ */ new Map()
1773
- };
1774
- function addNode({ name, node: node2, path, scope }, commentParentNode) {
1775
- const binding = path.scope.getBinding(name);
1776
- if (scope === binding?.scope) {
1777
- graph.nodes.add(name);
1778
- nodeCollection.addNode(name, node2, {
1779
- comment: commentParentNode ? getComment(commentParentNode) : ""
1780
- });
1781
- if (!graph.edges.get(name)) {
1782
- graph.edges.set(name, /* @__PURE__ */ new Set());
1783
- }
1784
- }
1785
- }
1786
- function addEdge({ fromName, toName, path, scope, toScope, collectionNodes }) {
1787
- const bindingScope = toScope || path.scope.getBinding(toName)?.scope;
1788
- if (scope === bindingScope && collectionNodes.has(toName)) {
1789
- graph.edges.get(fromName)?.add({
1790
- label: toName,
1791
- type: getRelationType(path)
1792
- });
1793
- }
1794
- }
1795
- function addUsed({ name, path, parentPath: parentPath2 }) {
1796
- const binding = path.scope.getBinding(name);
1797
- if (binding?.scope === parentPath2.scope) {
1798
- nodesUsedInTemplate.add(name);
1799
- }
1800
- }
1801
- const setupPath = traverseSetup({ node, parentScope: parentPath.scope, parentPath });
1802
- const setupScope = setupPath.scope;
1803
- const setupNode = setupPath.node;
1804
- traverse4(setupNode, {
1805
- VariableDeclarator(path1) {
1806
- parseNodeIdentifierPattern({
1807
- path: path1,
1808
- rootScope: setupScope,
1809
- cb: (params2) => {
1810
- if (!spread.includes(params2.name)) {
1811
- addNode(params2, path1.node);
1812
- } else {
1813
- addGraphBySpreadIdentifier({
1814
- path: path1,
1815
- graph,
1816
- nodeCollection,
1817
- iname: params2.name
1818
- });
1819
- }
1820
- }
1821
- });
1822
- parseNodeObjectPattern({
1823
- path: path1,
1824
- rootScope: setupScope,
1825
- cb: addNode
1826
- });
1827
- parseNodeArrayPattern({
1828
- path: path1,
1829
- rootScope: setupScope,
1830
- cb: addNode
1831
- });
1832
- },
1833
- FunctionDeclaration(path1) {
1834
- parseNodeFunctionPattern({
1835
- path: path1,
1836
- rootScope: setupScope,
1837
- cb: addNode
1838
- });
1839
- }
1840
- }, setupScope, setupPath);
1841
- setupPath.traverse({
1842
- ReturnStatement(path2) {
1843
- parseReturnJsxPattern({
1844
- path: path2,
1845
- parentPath: setupPath,
1846
- cb: addUsed
1847
- });
1848
- }
1849
- });
1850
- traverse4(setupNode, {
1851
- VariableDeclarator(path1) {
1852
- parseEdgeLeftIdentifierPattern({
1853
- path: path1,
1854
- rootScope: setupScope,
1855
- collectionNodes: graph.nodes,
1856
- cb: addEdge,
1857
- spread
1858
- });
1859
- parseEdgeLeftObjectPattern({
1860
- path: path1,
1861
- rootScope: setupScope,
1862
- collectionNodes: graph.nodes,
1863
- cb: addEdge
1864
- });
1865
- parseEdgeLeftArrayPattern({
1866
- path: path1,
1867
- rootScope: setupScope,
1868
- collectionNodes: graph.nodes,
1869
- cb: addEdge
1870
- });
1871
- },
1872
- FunctionDeclaration(path1) {
1873
- parseEdgeFunctionPattern({
1874
- path: path1,
1875
- rootScope: setupScope,
1876
- collectionNodes: graph.nodes,
1877
- cb: addEdge
1878
- });
1879
- }
1880
- }, setupScope, setupPath);
1881
- return {
1882
- graph,
1883
- nodeCollection,
1884
- nodesUsedInTemplate
1885
- };
1886
- }
1887
- function processByReact(params) {
1888
- const { node, parentScope, parentPath, lineOffset, addInfo } = params;
1889
- const nodeCollection = new NodeCollection(lineOffset, addInfo);
1890
- const nodesUsedInTemplate = /* @__PURE__ */ new Set();
1891
- const graph = {
1892
- nodes: /* @__PURE__ */ new Set(),
1893
- edges: /* @__PURE__ */ new Map(),
1894
- spread: /* @__PURE__ */ new Map()
1895
- };
1896
- function addNode({ name, node: node2, path, scope }, commentParentNode) {
1897
- const binding = path.scope.getBinding(name);
1898
- if (scope === binding?.scope) {
1899
- graph.nodes.add(name);
1900
- nodeCollection.addNode(name, node2, {
1901
- comment: commentParentNode ? getComment(commentParentNode) : ""
1902
- });
1903
- if (!graph.edges.get(name)) {
1904
- graph.edges.set(name, /* @__PURE__ */ new Set());
1905
- }
1906
- }
1907
- }
1908
- function addEdge({ fromName, toName, path, scope, toScope, collectionNodes }) {
1909
- const bindingScope = toScope || path.scope.getBinding(toName)?.scope;
1910
- if (scope === bindingScope && collectionNodes.has(toName)) {
1911
- graph.edges.get(fromName)?.add({
1912
- label: toName,
1913
- type: getRelationType(path)
1914
- });
1915
- }
1916
- }
1917
- function addUsed({ name, path, parentPath: parentPath2 }) {
1918
- const binding = path.scope.getBinding(name);
1919
- if (binding?.scope === parentPath2.scope) {
1920
- nodesUsedInTemplate.add(name);
1921
- }
1922
- }
1923
- traverse4(node, {
1924
- VariableDeclarator(path1) {
1925
- parseNodeIdentifierPattern({
1926
- path: path1,
1927
- rootScope: parentScope,
1928
- cb: (params2) => {
1929
- addNode(params2, path1.node);
1930
- }
1931
- });
1932
- parseNodeObjectPattern({
1933
- path: path1,
1934
- rootScope: parentScope,
1935
- cb: addNode
1936
- });
1937
- parseNodeArrayPattern({
1938
- path: path1,
1939
- rootScope: parentScope,
1940
- cb: addNode
1941
- });
1942
- },
1943
- FunctionDeclaration(path1) {
1944
- parseNodeFunctionPattern({
1945
- path: path1,
1946
- rootScope: parentScope,
1947
- cb: addNode
1948
- });
1949
- }
1950
- }, parentScope, parentPath);
1951
- traverse4(node, {
1952
- ReturnStatement(path2) {
1953
- parseReturnJsxPattern({
1954
- path: path2,
1955
- parentPath,
1956
- cb: addUsed
1957
- });
1958
- }
1959
- }, parentScope, parentPath);
1960
- traverse4(node, {
1961
- VariableDeclarator(path1) {
1962
- parseEdgeLeftIdentifierPattern({
1963
- path: path1,
1964
- rootScope: parentScope,
1965
- collectionNodes: graph.nodes,
1966
- cb: addEdge
1967
- });
1968
- parseEdgeLeftObjectPattern({
1969
- path: path1,
1970
- rootScope: parentScope,
1971
- collectionNodes: graph.nodes,
1972
- cb: addEdge
1973
- });
1974
- parseEdgeLeftArrayPattern({
1975
- path: path1,
1976
- rootScope: parentScope,
1977
- collectionNodes: graph.nodes,
1978
- cb: addEdge
1979
- });
1980
- },
1981
- FunctionDeclaration(path1) {
1982
- parseEdgeFunctionPattern({
1983
- path: path1,
1984
- rootScope: parentScope,
1985
- collectionNodes: graph.nodes,
1986
- cb: addEdge
1987
- });
1988
- }
1989
- }, parentScope, parentPath);
1990
- return {
1991
- graph,
1992
- nodeCollection,
1993
- nodesUsedInTemplate
1994
- };
1995
- }
1996
- function processTsx(params) {
1997
- let result;
1998
- function process(params2) {
1999
- const { node, parentPath } = params2;
2000
- const setupPath = traverseSetup({ node, parentScope: parentPath.scope, parentPath });
2001
- setupPath.traverse({
2002
- ReturnStatement(path) {
2003
- if (path.node.argument && (path.node.argument.type === "ArrowFunctionExpression" || path.node.argument.type === "FunctionExpression") && (path.node.argument.body.type === "JSXElement" || path.node.argument.body.type === "JSXFragment")) {
2004
- result = processByReturnJSX(params2);
2005
- } else {
2006
- result = processByReturnNotJSX(params2);
2007
- }
2008
- }
2009
- });
2010
- }
2011
- traverse4(params.node, {
2012
- ExportDefaultDeclaration(path) {
2013
- if (params.type === "vue") {
2014
- if (path.node.declaration.type === "ObjectExpression") {
2015
- process({
2016
- ...params,
2017
- node: path.node.declaration,
2018
- parentNode: path.node,
2019
- parentPath: path
2020
- });
2021
- } else if (path.node.declaration.type === "CallExpression" && path.node.declaration.callee.type === "Identifier" && path.node.declaration.callee.name === "defineComponent" && path.node.declaration.arguments[0].type === "ObjectExpression") {
2022
- process({
2023
- ...params,
2024
- node: path.node.declaration.arguments[0],
2025
- parentNode: path.node,
2026
- parentPath: path
2027
- });
2028
- }
2029
- }
2030
- if (params.type === "react") {
2031
- if ((path.node.declaration.type === "FunctionDeclaration" || path.node.declaration.type === "ArrowFunctionExpression") && path.node.declaration.body.type === "BlockStatement") {
2032
- const functionPath = path.get("declaration");
2033
- result = processByReact({
2034
- ...params,
2035
- node: path.node.declaration.body,
2036
- parentNode: functionPath.node,
2037
- parentPath: functionPath,
2038
- parentScope: functionPath.scope
2039
- });
2040
- }
2041
- if (path.node.declaration.type === "ClassDeclaration") {
2042
- const renderFunction = path.node.declaration.body.body.find((node) => {
2043
- if (node.type === "ClassMethod" && node.key.type === "Identifier" && node.key.name === "render") {
2044
- return node;
2045
- }
2046
- return void 0;
2047
- });
2048
- if (!renderFunction) {
2049
- return;
2050
- }
2051
- const renderPath = path.get(`declaration.body.body.${path.node.declaration.body.body.indexOf(renderFunction)}`);
2052
- result = processByReact({
2053
- ...params,
2054
- node: renderFunction.body,
2055
- parentNode: renderFunction,
2056
- parentPath: renderPath,
2057
- parentScope: renderPath.scope
2058
- });
2059
- }
2060
- }
2061
- }
2062
- });
2063
- return result;
2064
- }
2065
- function analyze5(content, type = "vue", lineOffset = 0, addInfo = true) {
2066
- const ast = babelParse4(content, { sourceType: "module", plugins: [
2067
- "typescript",
2068
- "jsx"
2069
- ] });
2070
- const { graph, nodeCollection, nodesUsedInTemplate } = processTsx({
2071
- node: ast,
2072
- type,
2073
- lineOffset,
2074
- addInfo
2075
- });
2076
- return {
2077
- graph: nodeCollection.map(graph),
2078
- nodesUsedInTemplate
2079
- };
2080
- }
2081
-
2082
- // src/mermaid.ts
2083
- function getMermaidText(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set(), options = {}) {
2084
- const direction = options.direction || "TB";
2085
- const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2086
- let mermaidText = `flowchart ${direction}
2087
- %% Legend:
2088
- %% () = variable node
2089
- %% [] = function node
2090
- %% * suffix = unused in template/style
2091
- %% A --> B means A depends on B
2092
- `;
2093
- graph.nodes.forEach((node) => {
2094
- const shape = node.type === "var" ? "(" : "[";
2095
- const closeShape = node.type === "var" ? ")" : "]";
2096
- const unusedSuffix = !(usedNodes.has(node.label) || node.info?.used?.size) ? "*" : "";
2097
- mermaidText += ` ${node.label}${shape}${node.label}${unusedSuffix}${closeShape}
2098
- `;
2099
- });
2100
- graph.edges.forEach((edge, key) => {
2101
- edge.forEach((to) => {
2102
- if (!to || !to.node.label) {
2103
- return;
2104
- }
2105
- mermaidText += ` ${key.label} --> ${to.node.label}
2106
- `;
2107
- });
2108
- });
2109
- return mermaidText;
2110
- }
2111
-
2112
- // src/suggest/filter.ts
2113
- function noIndegreeFilter(graph) {
2114
- const nodes = Array.from(graph.keys());
2115
- const indegree = /* @__PURE__ */ new Map();
2116
- nodes.forEach((node) => {
2117
- indegree.set(node, 0);
2118
- });
2119
- graph.forEach((targets, node) => {
2120
- targets.forEach((target) => {
2121
- indegree.set(target.node, (indegree.get(target.node) || 0) + 1);
2122
- });
2123
- });
2124
- return nodes.filter((node) => indegree.get(node) === 0);
2125
- }
2126
- function findLinearPaths(graph) {
2127
- const linearPaths = [];
2128
- const visitedNodes = /* @__PURE__ */ new Set();
2129
- const nodeInDegrees = /* @__PURE__ */ new Map();
2130
- for (const [node, edges] of graph.entries()) {
2131
- if (!nodeInDegrees.has(node)) {
2132
- nodeInDegrees.set(node, 0);
2133
- }
2134
- for (const edge of edges) {
2135
- const inDegree = nodeInDegrees.get(edge.node) || 0;
2136
- nodeInDegrees.set(edge.node, inDegree + 1);
2137
- }
2138
- }
2139
- function dfs2(node, path) {
2140
- if (visitedNodes.has(node)) {
2141
- return;
2142
- }
2143
- path.push(node);
2144
- visitedNodes.add(node);
2145
- const edges = graph.get(node) || /* @__PURE__ */ new Set();
2146
- if (edges.size === 0 || edges.size > 1) {
2147
- if (path.length > 1) {
2148
- addOrUpdatePath([...path]);
2149
- }
2150
- } else {
2151
- const nextNode = Array.from(edges)[0].node;
2152
- const nextNodeInDegree = nodeInDegrees.get(nextNode) || 0;
2153
- if (nextNodeInDegree === 1) {
2154
- dfs2(nextNode, path);
2155
- }
2156
- }
2157
- path.pop();
2158
- visitedNodes.delete(node);
2159
- }
2160
- function addOrUpdatePath(newPath) {
2161
- let shouldAddNewPath = true;
2162
- for (let i = linearPaths.length - 1; i >= 0; i--) {
2163
- const existingPath = linearPaths[i];
2164
- if (isSubpath(existingPath, newPath)) {
2165
- linearPaths.splice(i, 1);
2166
- } else if (isSubpath(newPath, existingPath)) {
2167
- shouldAddNewPath = false;
2168
- break;
2169
- }
2170
- }
2171
- if (shouldAddNewPath && newPath.length > 2) {
2172
- linearPaths.push(newPath);
2173
- }
2174
- }
2175
- function isSubpath(shortPath, longPath) {
2176
- if (shortPath.length >= longPath.length) {
2177
- return false;
2178
- }
2179
- for (let i = 0; i <= longPath.length - shortPath.length; i++) {
2180
- let isSub = true;
2181
- for (let j = 0; j < shortPath.length; j++) {
2182
- if (shortPath[j] !== longPath[i + j]) {
2183
- isSub = false;
2184
- break;
2185
- }
2186
- }
2187
- if (isSub) {
2188
- return true;
2189
- }
2190
- }
2191
- return false;
2192
- }
2193
- for (const node of graph.keys()) {
2194
- dfs2(node, []);
2195
- }
2196
- return linearPaths;
2197
- }
2198
- function findArticulationPoints(graph) {
2199
- const noIndegreeNodes = noIndegreeFilter(graph);
2200
- let time = 0;
2201
- const low = /* @__PURE__ */ new Map();
2202
- const disc = /* @__PURE__ */ new Map();
2203
- const parent = /* @__PURE__ */ new Map();
2204
- const ap = /* @__PURE__ */ new Set();
2205
- const visited = /* @__PURE__ */ new Set();
2206
- function APUtil(graph2, node) {
2207
- let children = 0;
2208
- disc.set(node, time);
2209
- low.set(node, time);
2210
- time++;
2211
- visited.add(node);
2212
- for (const neighbor of graph2.get(node) || []) {
2213
- if (!visited.has(neighbor.node)) {
2214
- children++;
2215
- parent.set(neighbor.node, node);
2216
- APUtil(graph2, neighbor.node);
2217
- low.set(node, Math.min(low.get(node), low.get(neighbor.node)));
2218
- if (parent.get(node) === null && children > 1) {
2219
- ap.add(node);
2220
- }
2221
- if (parent.get(node) !== null && low.get(neighbor.node) >= disc.get(node)) {
2222
- ap.add(node);
2223
- }
2224
- } else if (neighbor.node !== parent.get(node)) {
2225
- low.set(node, Math.min(low.get(node), disc.get(neighbor.node)));
2226
- }
2227
- }
2228
- }
2229
- for (const node of graph.keys()) {
2230
- if (!visited.has(node) && !noIndegreeNodes.includes(node)) {
2231
- APUtil(graph, node);
2232
- }
2233
- }
2234
- return ap;
2235
- }
2236
-
2237
- // src/suggest/split.ts
2238
- function dfs(graph, node, targets, visited, component) {
2239
- component.add(node);
2240
- visited.add(node);
2241
- targets.forEach((target) => {
2242
- if (!visited.has(target.node)) {
2243
- dfs(graph, target.node, graph.get(target.node) || /* @__PURE__ */ new Set(), visited, component);
2244
- }
2245
- });
2246
- }
2247
- function haveIntersection(setA, setB) {
2248
- for (const item of setA) {
2249
- if (setB.has(item)) {
2250
- return true;
2251
- }
2252
- }
2253
- return false;
2254
- }
2255
- function mergeSets(arr) {
2256
- let result = [...arr];
2257
- for (let i = 0; i < result.length; i++) {
2258
- for (let j = i + 1; j < result.length; j++) {
2259
- if (haveIntersection(result[i], result[j])) {
2260
- const newSet = /* @__PURE__ */ new Set([...result[i], ...result[j]]);
2261
- result.splice(j, 1);
2262
- result.splice(i, 1);
2263
- result = [...result, newSet];
2264
- return mergeSets(result);
2265
- }
2266
- }
2267
- }
2268
- return result;
2269
- }
2270
- function splitGraph(graph) {
2271
- const components = [];
2272
- const sorted = Array.from(graph).sort((a, b) => b[1].size - a[1].size);
2273
- new Map(sorted).forEach((targets, node) => {
2274
- const visited = /* @__PURE__ */ new Set();
2275
- if (!visited.has(node)) {
2276
- const component = /* @__PURE__ */ new Set();
2277
- dfs(graph, node, targets, visited, component);
2278
- components.push(component);
2279
- }
2280
- });
2281
- return mergeSets(components).map((component) => {
2282
- const subGraph = /* @__PURE__ */ new Map();
2283
- component.forEach((node) => {
2284
- const targets = graph.get(node);
2285
- if (targets) {
2286
- subGraph.set(node, targets);
2287
- }
2288
- });
2289
- return subGraph;
2290
- });
2291
- }
2292
-
2293
- // src/suggest/utils.ts
2294
- function hasCycle(graph) {
2295
- const visited = /* @__PURE__ */ new Set();
2296
- const onStack = /* @__PURE__ */ new Set();
2297
- const stack = [];
2298
- let cycleNodes = [];
2299
- function dfs2(node) {
2300
- if (visited.has(node)) {
2301
- if (onStack.has(node)) {
2302
- const idx = stack.indexOf(node);
2303
- const cycle = stack.slice(idx);
2304
- const allNotGet = cycle.every((curr, i) => {
2305
- const next = cycle[(i + 1) % cycle.length];
2306
- return Array.from(graph.get(curr) || []).some(
2307
- (edge) => edge.node === next && edge.type !== "get"
2308
- );
2309
- });
2310
- if (allNotGet) {
2311
- cycleNodes = cycle;
2312
- return true;
2313
- }
2314
- return false;
2315
- }
2316
- return false;
2317
- }
2318
- visited.add(node);
2319
- onStack.add(node);
2320
- stack.push(node);
2321
- for (const neighbor of graph.get(node) || /* @__PURE__ */ new Set()) {
2322
- if (neighbor.node === node && neighbor.type !== "get") {
2323
- cycleNodes = [node];
2324
- return true;
2325
- }
2326
- if (dfs2(neighbor.node)) {
2327
- return true;
2328
- }
2329
- }
2330
- onStack.delete(node);
2331
- stack.pop();
2332
- return false;
2333
- }
2334
- for (const [node, targets] of graph) {
2335
- if (dfs2(node)) {
2336
- return { hasCycle: true, cycleNodes };
2337
- }
2338
- }
2339
- return { hasCycle: false, cycleNodes: [] };
2340
- }
2341
-
2342
- // src/suggest/index.ts
2343
- var SuggestionType = /* @__PURE__ */ ((SuggestionType2) => {
2344
- SuggestionType2["info"] = "info";
2345
- SuggestionType2["warning"] = "warning";
2346
- SuggestionType2["error"] = "error";
2347
- return SuggestionType2;
2348
- })(SuggestionType || {});
2349
- function gen(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2350
- const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2351
- const suggestions = [];
2352
- const splitedGraph = splitGraph(graph.edges);
2353
- splitedGraph.forEach((g) => {
2354
- const nodes = Array.from(g.keys());
2355
- if (splitedGraph.length > 1) {
2356
- if (nodes.length > 2 && nodes.some((node) => !usedNodes.has(node.label))) {
2357
- suggestions.push({
2358
- type: "info" /* info */,
2359
- message: `Nodes [${nodes.length > 10 ? `${nodes.slice(0, 10).map((node) => node.label).join(",")}...(${nodes.length})` : nodes.map((node) => node.label).join(",")}] are isolated, perhaps you can refactor them to an isolated file.`,
2360
- nodeInfo: nodes
2361
- });
2362
- }
2363
- }
2364
- if (nodes.length > 1 && nodes.every((node) => !usedNodes.has(node.label) && !node.info?.used?.size)) {
2365
- suggestions.push({
2366
- type: "info" /* info */,
2367
- message: `Nodes [${nodes.length > 10 ? `${nodes.slice(0, 10).map((node) => node.label).join(",")}...` : nodes.map((node) => node.label).join(",")}] are not used, perhaps you can remove them.`,
2368
- nodeInfo: nodes
2369
- });
2370
- }
2371
- const hasCycleResult = hasCycle(g);
2372
- if (hasCycleResult.hasCycle) {
2373
- suggestions.push({
2374
- type: "error" /* error */,
2375
- message: `There is a loop call in nodes [${hasCycleResult.cycleNodes.map((node) => node.label).join(",")}], perhaps you can refactor it.`,
2376
- nodeInfo: hasCycleResult.cycleNodes
2377
- });
2378
- }
2379
- const paths = findLinearPaths(g);
2380
- paths.forEach((path) => {
2381
- const firstUsedNodeIndex = path.findIndex((node) => usedNodes.has(node.label));
2382
- const reverseLastNotUsedNodeIndex = path.slice().reverse().findIndex((node) => !usedNodes.has(node.label));
2383
- const lastNotUsedNodeIndex = reverseLastNotUsedNodeIndex !== -1 ? path.length - 1 - reverseLastNotUsedNodeIndex : -1;
2384
- if (firstUsedNodeIndex > -1 && firstUsedNodeIndex < lastNotUsedNodeIndex) {
2385
- suggestions.push({
2386
- type: "warning" /* warning */,
2387
- message: `Nodes [${path.length > 10 ? `${path.slice(0, 10).map((node) => node.label).join(",")}...(${path.length})` : path.map((node) => node.label).join(",")}] are have function chain calls, perhaps you can refactor it.`,
2388
- nodeInfo: path
2389
- });
2390
- }
2391
- });
2392
- if (g.size > 5) {
2393
- const ap = findArticulationPoints(g);
2394
- ap.forEach((node) => {
2395
- if (node.type === "fun" /* fun */) {
2396
- suggestions.push({
2397
- type: "info" /* info */,
2398
- message: `Node [${node.label}] is an articulation point, perhaps you need to pay special attention to this node.`,
2399
- nodeInfo: node
2400
- });
2401
- }
2402
- });
2403
- }
2404
- });
2405
- const noIndegreeNodes = noIndegreeFilter(graph.edges);
2406
- noIndegreeNodes.forEach((node) => {
2407
- if (!usedNodes.has(node.label) && !node.info?.used?.size) {
2408
- suggestions.push({
2409
- type: "info" /* info */,
2410
- message: `Node [${node.label}] is not used, perhaps you can remove it.`,
2411
- nodeInfo: node
2412
- });
2413
- }
2414
- });
2415
- return suggestions;
2416
- }
2417
-
2418
- // src/vis.ts
2419
- function filterNodeUserd(used) {
2420
- const usedArray = Array.from(used || []);
2421
- return new Set(usedArray.filter((u) => ![
2422
- "Assignment Expression",
2423
- "Call Expression"
2424
- ].includes(u)));
2425
- }
2426
- function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2427
- const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2428
- const nodes = [];
2429
- const edges = [];
2430
- const inDegreeMap = {};
2431
- const outDegreeMap = {};
2432
- graph.edges.forEach((edge) => {
2433
- edge.forEach((to) => {
2434
- if (to) {
2435
- inDegreeMap[to.node.label] = (inDegreeMap[to.node.label] || 0) + 1;
2436
- }
2437
- });
2438
- });
2439
- graph.edges.forEach((edge, key) => {
2440
- outDegreeMap[key.label] = edge.size;
2441
- });
2442
- graph.nodes.forEach((node) => {
2443
- const inDegree = inDegreeMap[node.label] || 0;
2444
- const outDegree = outDegreeMap[node.label] || 0;
2445
- nodes.push({
2446
- id: node.label,
2447
- label: node.label,
2448
- shape: node.type === "var" ? "dot" : "diamond",
2449
- group: usedNodes.has(node.label) || node.info?.used?.size ? "used" : "normal",
2450
- size: 20 + 1.6 ** (inDegree * 0.75 + outDegree * 0.25),
2451
- title: `${filterNodeUserd(node.info?.used).size ? `used by ${Array.from(filterNodeUserd(node.info?.used))?.map((i) => `\`${i}\``).join(",")}
2452
-
2453
- ` : ""}${usedNodes.has(node.label) ? `used in ${[
2454
- nodesUsedInStyle.has(node.label) ? "style" : "",
2455
- nodesUsedInTemplate.has(node.label) ? "template" : ""
2456
- ].filter(Boolean).join(" and ")}
2457
-
2458
- ` : ""}${node.info?.comment || ""}`.trim() || void 0,
2459
- info: node.info
2460
- });
2461
- });
2462
- graph.edges.forEach((edge, key) => {
2463
- edge.forEach((to) => {
2464
- if (!to) {
2465
- return;
2466
- }
2467
- edges.push({
2468
- from: key.label,
2469
- to: to.node.label,
2470
- arrows: {
2471
- to: {
2472
- enabled: true,
2473
- scaleFactor: 0.8
2474
- }
2475
- }
2476
- });
2477
- });
2478
- });
2479
- return {
2480
- nodes,
2481
- edges
2482
- };
2483
- }
2484
-
2485
- // src/index.ts
2486
- import { parse } from "@vue/compiler-sfc";
2487
- export {
2488
- NodeType,
2489
- SuggestionType,
2490
- analyze2 as analyzeOptions,
2491
- analyze as analyzeSetupScript,
2492
- analyze3 as analyzeStyle,
2493
- analyze4 as analyzeTemplate,
2494
- analyze5 as analyzeTsx,
2495
- gen,
2496
- getMermaidText,
2497
- getVisData,
2498
- parse
2499
- };
2500
- //# sourceMappingURL=index.mjs.map