vue-hook-optimizer 0.0.64 → 0.0.66

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 CHANGED
@@ -1,59 +1,10 @@
1
- // src/index.ts
2
- import { parse } from "@vue/compiler-sfc";
3
-
4
- // src/analyze/template.ts
5
- import { babelParse, compileTemplate } from "@vue/compiler-sfc";
6
- import _traverse from "@babel/traverse";
7
- var traverse = _traverse.default?.default || _traverse.default || _traverse;
8
- function analyze(content) {
9
- const id = "template";
10
- const { code } = compileTemplate({
11
- id,
12
- source: content,
13
- filename: `${id}.js`
14
- });
15
- const ast = babelParse(code, { sourceType: "module", plugins: [
16
- "typescript"
17
- ] });
18
- const nodes = /* @__PURE__ */ new Set();
19
- traverse(ast, {
20
- MemberExpression(path) {
21
- if (path.type === "MemberExpression") {
22
- if (path.node.object && path.node.object.type === "Identifier" && path.node.object.name === "_ctx") {
23
- if (path.node.property && path.node.property.type === "Identifier") {
24
- nodes.add(path.node.property.name);
25
- }
26
- }
27
- }
28
- },
29
- ObjectProperty(path) {
30
- if (path.node.key.type === "Identifier" && path.node.key.name === "ref") {
31
- if (path.node.value.type === "StringLiteral") {
32
- const name = path.node.value.value;
33
- if (name) {
34
- nodes.add(name);
35
- }
36
- }
37
- }
38
- },
39
- // component
40
- CallExpression(path) {
41
- if (path.node.callee.type === "Identifier" && path.node.callee.name === "_resolveComponent") {
42
- if (path.node.arguments[0].type === "StringLiteral") {
43
- const name = path.node.arguments[0].value;
44
- if (name) {
45
- nodes.add(name);
46
- }
47
- }
48
- }
49
- }
50
- });
51
- return nodes;
52
- }
1
+ // src/analyze/options.ts
2
+ import _traverse2 from "@babel/traverse";
3
+ import { babelParse as babelParse2 } from "@vue/compiler-sfc";
53
4
 
54
5
  // src/analyze/setupScript.ts
55
- import { babelParse as babelParse2 } from "@vue/compiler-sfc";
56
- import _traverse2 from "@babel/traverse";
6
+ import _traverse from "@babel/traverse";
7
+ import { babelParse } from "@vue/compiler-sfc";
57
8
 
58
9
  // src/analyze/utils.ts
59
10
  var NodeType = /* @__PURE__ */ ((NodeType3) => {
@@ -163,7 +114,7 @@ function getComment(node) {
163
114
  }
164
115
 
165
116
  // src/analyze/setupScript.ts
166
- var traverse2 = _traverse2.default?.default || _traverse2.default || _traverse2;
117
+ var traverse = _traverse.default?.default || _traverse.default || _traverse;
167
118
  var ignoreFunctionsName = ["defineProps", "defineEmits", "withDefaults"];
168
119
  function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
169
120
  const spread = _spread || [];
@@ -173,7 +124,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
173
124
  edges: /* @__PURE__ */ new Map(),
174
125
  spread: /* @__PURE__ */ new Map()
175
126
  };
176
- traverse2(ast, {
127
+ traverse(ast, {
177
128
  VariableDeclaration(path) {
178
129
  path.node.declarations.forEach((declaration) => {
179
130
  if (declaration.id.type === "ArrayPattern") {
@@ -335,7 +286,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
335
286
  const expression = node.type === "ExpressionStatement" ? node.expression : node;
336
287
  const watchArgs = /* @__PURE__ */ new Set();
337
288
  if (hookName === "provide") {
338
- traverse2(expression, {
289
+ traverse(expression, {
339
290
  Identifier(path1) {
340
291
  const binding = path1.scope.getBinding(path1.node.name);
341
292
  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)) {
@@ -358,7 +309,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
358
309
  watchArgs.add(expression.arguments[0]);
359
310
  }
360
311
  } else {
361
- traverse2(expression.arguments[0], {
312
+ traverse(expression.arguments[0], {
362
313
  Identifier(path1) {
363
314
  const binding = path1.scope.getBinding(path1.node.name);
364
315
  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)) {
@@ -368,7 +319,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
368
319
  }, patentScope, node);
369
320
  }
370
321
  } else if (hookName === "useEffect" && expression.arguments[1].type === "ArrayExpression") {
371
- traverse2(expression.arguments[1], {
322
+ traverse(expression.arguments[1], {
372
323
  Identifier(path1) {
373
324
  const binding = path1.scope.getBinding(path1.node.name);
374
325
  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)) {
@@ -390,38 +341,53 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
390
341
  }
391
342
  return;
392
343
  }
393
- traverse2(argNode, {
394
- Identifier(path1) {
395
- const binding = path1.scope.getBinding(path1.node.name);
396
- 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)) {
397
- if (["watch", "useEffect"].includes(hookName) && watchArgs.size > 0) {
398
- const watchArgsNames = Array.from(watchArgs).map((arg) => arg.name);
399
- watchArgs.forEach((watchArg) => {
400
- if (!watchArgsNames.includes(path1.node.name)) {
401
- graph.edges.get(watchArg.name)?.add(path1.node.name);
402
- }
403
- });
404
- }
405
- const _node = nodeCollection.getNode(path1.node.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
- }
344
+ if (argNode.type === "Identifier") {
345
+ const binding = patentScope.getBinding(argNode.name);
346
+ if (graph.nodes.has(argNode.name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
347
+ const _node = nodeCollection.getNode(argNode.name);
348
+ if (_node?.info?.used) {
349
+ _node?.info?.used?.add(hookName);
350
+ } else if (_node) {
351
+ _node.info = {
352
+ ..._node?.info,
353
+ used: /* @__PURE__ */ new Set([hookName])
354
+ };
414
355
  }
415
356
  }
416
- }, patentScope, node);
357
+ } else {
358
+ traverse(argNode, {
359
+ Identifier(path1) {
360
+ const binding = path1.scope.getBinding(path1.node.name);
361
+ 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)) {
362
+ if (["watch", "useEffect"].includes(hookName) && watchArgs.size > 0) {
363
+ const watchArgsNames = Array.from(watchArgs).map((arg) => arg.name);
364
+ watchArgs.forEach((watchArg) => {
365
+ if (!watchArgsNames.includes(path1.node.name)) {
366
+ graph.edges.get(watchArg.name)?.add(path1.node.name);
367
+ }
368
+ });
369
+ }
370
+ const _node = nodeCollection.getNode(path1.node.name);
371
+ if (_node?.info?.used) {
372
+ _node?.info?.used?.add(hookName);
373
+ } else if (_node) {
374
+ _node.info = {
375
+ ..._node?.info,
376
+ used: /* @__PURE__ */ new Set([hookName])
377
+ };
378
+ }
379
+ }
380
+ }
381
+ }, patentScope, node);
382
+ }
417
383
  });
418
384
  }
419
385
  }
420
- traverse2(ast, {
386
+ traverse(ast, {
421
387
  FunctionDeclaration(path) {
422
388
  const name = path.node.id?.name;
423
389
  if (name && graph.nodes.has(name)) {
424
- traverse2(path.node.body, {
390
+ traverse(path.node.body, {
425
391
  Identifier(path1) {
426
392
  const binding = path1.scope.getBinding(path1.node.name);
427
393
  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)) {
@@ -446,7 +412,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
446
412
  if (element?.type === "Identifier") {
447
413
  const name = element.name;
448
414
  if (name && graph.nodes.has(name) && path.node.init?.type === "CallExpression") {
449
- traverse2(path.node.init, {
415
+ traverse(path.node.init, {
450
416
  Identifier(path1) {
451
417
  const binding = path1.scope.getBinding(path1.node.name);
452
418
  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)) {
@@ -470,7 +436,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
470
436
  if (property.type === "ObjectProperty" && property.value.type === "Identifier") {
471
437
  const name = property.value.name;
472
438
  if (name && graph.nodes.has(name) && path.node.init) {
473
- traverse2(path.node.init, {
439
+ traverse(path.node.init, {
474
440
  Identifier(path1) {
475
441
  const binding = path1.scope.getBinding(path1.node.name);
476
442
  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)) {
@@ -499,7 +465,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
499
465
  }
500
466
  const name = path.node.id?.name;
501
467
  if (name && graph.nodes.has(name)) {
502
- traverse2(path.node.init, {
468
+ traverse(path.node.init, {
503
469
  Identifier(path1) {
504
470
  const binding = path1.scope.getBinding(path1.node.name);
505
471
  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)) {
@@ -524,7 +490,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
524
490
  graph.edges.get(name)?.add(path.node.init.name);
525
491
  }
526
492
  } else {
527
- traverse2(path.node.init, {
493
+ traverse(path.node.init, {
528
494
  Identifier(path1) {
529
495
  const binding = path1.scope.getBinding(path1.node.name);
530
496
  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)) {
@@ -539,7 +505,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
539
505
  ObjectMethod(path) {
540
506
  if (path.node.key.type === "Identifier" && graph.nodes.has(path.node.key.name)) {
541
507
  const name = path.node.key.name;
542
- traverse2(path.node.body, {
508
+ traverse(path.node.body, {
543
509
  Identifier(path1) {
544
510
  const binding = path1.scope.getBinding(path1.node.name);
545
511
  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)) {
@@ -560,7 +526,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
560
526
  ObjectProperty(path) {
561
527
  if (path.node.key.type === "Identifier" && graph.nodes.has(path.node.key.name)) {
562
528
  const name = path.node.key.name;
563
- traverse2(path.node.value, {
529
+ traverse(path.node.value, {
564
530
  MemberExpression(path1) {
565
531
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
566
532
  const binding = path1.scope.getBinding(path1.node.object.name);
@@ -599,8 +565,8 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
599
565
  nodeCollection
600
566
  };
601
567
  }
602
- function analyze2(content, lineOffset = 0, jsx = false) {
603
- const ast = babelParse2(content, { sourceType: "module", plugins: [
568
+ function analyze(content, lineOffset = 0, jsx = false) {
569
+ const ast = babelParse(content, { sourceType: "module", plugins: [
604
570
  "typescript",
605
571
  ...jsx ? ["jsx"] : []
606
572
  ] });
@@ -609,9 +575,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
609
575
  }
610
576
 
611
577
  // src/analyze/options.ts
612
- import { babelParse as babelParse3 } from "@vue/compiler-sfc";
613
- import _traverse3 from "@babel/traverse";
614
- var traverse3 = _traverse3.default?.default || _traverse3.default || _traverse3;
578
+ var traverse2 = _traverse2.default?.default || _traverse2.default || _traverse2;
615
579
  var vueLifeCycleHooks = [
616
580
  "beforeCreate",
617
581
  "created",
@@ -628,8 +592,8 @@ var vueLifeCycleHooks = [
628
592
  "renderTriggered",
629
593
  "provide"
630
594
  ];
631
- function analyze3(content, lineOffset = 0, jsx = false) {
632
- const ast = babelParse3(content, { sourceType: "module", plugins: [
595
+ function analyze2(content, lineOffset = 0, jsx = false) {
596
+ const ast = babelParse2(content, { sourceType: "module", plugins: [
633
597
  "typescript",
634
598
  ...jsx ? ["jsx"] : []
635
599
  ] });
@@ -641,12 +605,12 @@ function analyze3(content, lineOffset = 0, jsx = false) {
641
605
  };
642
606
  const nodesUsedInTemplate = /* @__PURE__ */ new Set();
643
607
  function process(node, path) {
644
- traverse3(node, {
608
+ traverse2(node, {
645
609
  ObjectProperty(path1) {
646
610
  if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
647
611
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "data" && (path1.node.value.type === "ArrowFunctionExpression" || path1.node.value.type === "FunctionExpression")) {
648
612
  const dataNode = path1.node.value;
649
- traverse3(dataNode, {
613
+ traverse2(dataNode, {
650
614
  ReturnStatement(path2) {
651
615
  if (path2.parent === dataNode.body) {
652
616
  if (path2.node.argument?.type === "ObjectExpression") {
@@ -713,10 +677,10 @@ function analyze3(content, lineOffset = 0, jsx = false) {
713
677
  }
714
678
  }
715
679
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "render" && (path1.node.value.type === "ArrowFunctionExpression" || path1.node.value.type === "FunctionExpression")) {
716
- traverse3(path1.node.value, {
680
+ traverse2(path1.node.value, {
717
681
  ReturnStatement(path2) {
718
682
  const templateNode = path2.node;
719
- traverse3(templateNode, {
683
+ traverse2(templateNode, {
720
684
  MemberExpression(path3) {
721
685
  if (path3.node.object && path3.node.object.type === "ThisExpression") {
722
686
  if (path3.node.property && path3.node.property.type === "Identifier") {
@@ -735,11 +699,11 @@ function analyze3(content, lineOffset = 0, jsx = false) {
735
699
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
736
700
  const setupNode = path1.node;
737
701
  const spread = [];
738
- traverse3(setupNode, {
702
+ traverse2(setupNode, {
739
703
  ReturnStatement(path2) {
740
704
  if (path2.node.argument?.type === "ObjectExpression") {
741
705
  const returnNode = path2.node.argument;
742
- traverse3(returnNode, {
706
+ traverse2(returnNode, {
743
707
  SpreadElement(path3) {
744
708
  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") {
745
709
  spread.push(path3.node.argument.arguments[0].name);
@@ -751,7 +715,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
751
715
  }
752
716
  if (path2.node.argument?.type === "FunctionExpression" || path2.node.argument?.type === "ArrowFunctionExpression") {
753
717
  const templateNode = path2.node.argument.body;
754
- traverse3(templateNode, {
718
+ traverse2(templateNode, {
755
719
  Identifier(path3) {
756
720
  const binding = path3.scope.getBinding(path3.node.name);
757
721
  if (binding?.scope === path1.scope) {
@@ -776,14 +740,14 @@ function analyze3(content, lineOffset = 0, jsx = false) {
776
740
  },
777
741
  nodeCollection: tempNodeCollection
778
742
  } = processSetup(setupNode, path1.scope, setupNode, spread, lineOffset);
779
- traverse3(setupNode, {
743
+ traverse2(setupNode, {
780
744
  ReturnStatement(path2) {
781
745
  if (path2.scope !== path1.scope) {
782
746
  return;
783
747
  }
784
748
  if (path2.node.argument?.type === "ObjectExpression") {
785
749
  const returnNode = path2.node.argument;
786
- traverse3(returnNode, {
750
+ traverse2(returnNode, {
787
751
  ObjectProperty(path3) {
788
752
  if (path3.parent === returnNode) {
789
753
  if (path3.node.key.type === "Identifier" && path3.node.value.type === "Identifier" && tempNodes.has(path3.node.value.name)) {
@@ -851,7 +815,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
851
815
  }
852
816
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "data") {
853
817
  const dataNode = path1.node;
854
- traverse3(dataNode, {
818
+ traverse2(dataNode, {
855
819
  ReturnStatement(path2) {
856
820
  if (path2.parent === dataNode.body) {
857
821
  if (path2.node.argument?.type === "ObjectExpression") {
@@ -876,10 +840,10 @@ function analyze3(content, lineOffset = 0, jsx = false) {
876
840
  }, path1.scope, path1);
877
841
  }
878
842
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "render") {
879
- traverse3(path1.node, {
843
+ traverse2(path1.node, {
880
844
  ReturnStatement(path2) {
881
845
  const templateNode = path2.node;
882
- traverse3(templateNode, {
846
+ traverse2(templateNode, {
883
847
  MemberExpression(path3) {
884
848
  if (path3.node.object && path3.node.object.type === "ThisExpression") {
885
849
  if (path3.node.property && path3.node.property.type === "Identifier") {
@@ -894,12 +858,12 @@ function analyze3(content, lineOffset = 0, jsx = false) {
894
858
  }
895
859
  }
896
860
  }, path.scope, path);
897
- traverse3(node, {
861
+ traverse2(node, {
898
862
  ObjectMethod(path1) {
899
863
  if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
900
864
  if (path1.node.key.type === "Identifier" && vueLifeCycleHooks.includes(path1.node.key.name)) {
901
865
  const hookName = path1.node.key.name;
902
- traverse3(path1.node.body, {
866
+ traverse2(path1.node.body, {
903
867
  MemberExpression(path2) {
904
868
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
905
869
  const _node = nodeCollection.getNode(path2.node.property.name);
@@ -925,7 +889,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
925
889
  computedNode.value.properties.forEach((prop) => {
926
890
  if (prop.type === "ObjectMethod" && prop.key.type === "Identifier") {
927
891
  const name = prop.key.name;
928
- traverse3(prop, {
892
+ traverse2(prop, {
929
893
  MemberExpression(path2) {
930
894
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
931
895
  graph.edges.get(name)?.add(path2.node.property.name);
@@ -937,7 +901,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
937
901
  const name = prop.key.name;
938
902
  prop.value.properties.forEach((prop1) => {
939
903
  if (prop1.type === "ObjectProperty" && prop1.key.type === "Identifier" && prop1.key.name === "get") {
940
- traverse3(prop1, {
904
+ traverse2(prop1, {
941
905
  MemberExpression(path2) {
942
906
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
943
907
  graph.edges.get(name)?.add(path2.node.property.name);
@@ -956,7 +920,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
956
920
  methodsNode.value.properties.forEach((prop) => {
957
921
  if ((prop.type === "ObjectMethod" || prop.type === "ObjectProperty") && prop.key.type === "Identifier") {
958
922
  const name = prop.key.name;
959
- traverse3(prop, {
923
+ traverse2(prop, {
960
924
  MemberExpression(path2) {
961
925
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
962
926
  graph.edges.get(name)?.add(path2.node.property.name);
@@ -983,7 +947,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
983
947
  used: /* @__PURE__ */ new Set([hookName])
984
948
  };
985
949
  }
986
- traverse3(path1.node.value, {
950
+ traverse2(path1.node.value, {
987
951
  MemberExpression(path2) {
988
952
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
989
953
  if (watchArg && watchArg.name !== path2.node.property.name) {
@@ -995,7 +959,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
995
959
  }
996
960
  });
997
961
  } else {
998
- traverse3(path1.node.value, {
962
+ traverse2(path1.node.value, {
999
963
  MemberExpression(path2) {
1000
964
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1001
965
  const _node = nodeCollection.getNode(path2.node.property.name);
@@ -1016,7 +980,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
1016
980
  }
1017
981
  }, path.scope, path);
1018
982
  }
1019
- traverse3(ast, {
983
+ traverse2(ast, {
1020
984
  ExportDefaultDeclaration(path) {
1021
985
  if (path.node.declaration.type === "ObjectExpression") {
1022
986
  process(path.node.declaration, path);
@@ -1031,6 +995,117 @@ function analyze3(content, lineOffset = 0, jsx = false) {
1031
995
  };
1032
996
  }
1033
997
 
998
+ // src/analyze/style.ts
999
+ function lexBinding(content, start) {
1000
+ let state = 0 /* inParens */;
1001
+ let parenDepth = 0;
1002
+ for (let i = start; i < content.length; i++) {
1003
+ const char = content.charAt(i);
1004
+ switch (state) {
1005
+ case 0 /* inParens */:
1006
+ if (char === "'") {
1007
+ state = 1 /* inSingleQuoteString */;
1008
+ } else if (char === '"') {
1009
+ state = 2 /* inDoubleQuoteString */;
1010
+ } else if (char === "(") {
1011
+ parenDepth++;
1012
+ } else if (char === ")") {
1013
+ if (parenDepth > 0) {
1014
+ parenDepth--;
1015
+ } else {
1016
+ return i;
1017
+ }
1018
+ }
1019
+ break;
1020
+ case 1 /* inSingleQuoteString */:
1021
+ if (char === "'") {
1022
+ state = 0 /* inParens */;
1023
+ }
1024
+ break;
1025
+ case 2 /* inDoubleQuoteString */:
1026
+ if (char === '"') {
1027
+ state = 0 /* inParens */;
1028
+ }
1029
+ break;
1030
+ }
1031
+ }
1032
+ return null;
1033
+ }
1034
+ function normalizeExpression(exp) {
1035
+ exp = exp.trim();
1036
+ if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1037
+ return exp.slice(1, -1);
1038
+ }
1039
+ return exp;
1040
+ }
1041
+ var vBindRE = /v-bind\s*\(/g;
1042
+ function analyze3(styles) {
1043
+ const nodes = /* @__PURE__ */ new Set();
1044
+ styles.forEach((style) => {
1045
+ let match;
1046
+ const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1047
+ while (match = vBindRE.exec(content)) {
1048
+ const start = match.index + match[0].length;
1049
+ const end = lexBinding(content, start);
1050
+ if (end !== null) {
1051
+ const variable = normalizeExpression(content.slice(start, end));
1052
+ nodes.add(variable);
1053
+ }
1054
+ }
1055
+ });
1056
+ return nodes;
1057
+ }
1058
+
1059
+ // src/analyze/template.ts
1060
+ import _traverse3 from "@babel/traverse";
1061
+ import { babelParse as babelParse3, compileTemplate } from "@vue/compiler-sfc";
1062
+ var traverse3 = _traverse3.default?.default || _traverse3.default || _traverse3;
1063
+ function analyze4(content) {
1064
+ const id = "template";
1065
+ const { code } = compileTemplate({
1066
+ id,
1067
+ source: content,
1068
+ filename: `${id}.js`
1069
+ });
1070
+ const ast = babelParse3(code, { sourceType: "module", plugins: [
1071
+ "typescript"
1072
+ ] });
1073
+ const nodes = /* @__PURE__ */ new Set();
1074
+ traverse3(ast, {
1075
+ MemberExpression(path) {
1076
+ if (path.type === "MemberExpression") {
1077
+ if (path.node.object && path.node.object.type === "Identifier" && path.node.object.name === "_ctx") {
1078
+ if (path.node.property && path.node.property.type === "Identifier") {
1079
+ nodes.add(path.node.property.name);
1080
+ }
1081
+ }
1082
+ }
1083
+ },
1084
+ ObjectProperty(path) {
1085
+ if (path.node.key.type === "Identifier" && path.node.key.name === "ref") {
1086
+ if (path.node.value.type === "StringLiteral") {
1087
+ const name = path.node.value.value;
1088
+ if (name) {
1089
+ nodes.add(name);
1090
+ }
1091
+ }
1092
+ }
1093
+ },
1094
+ // component
1095
+ CallExpression(path) {
1096
+ if (path.node.callee.type === "Identifier" && path.node.callee.name === "_resolveComponent") {
1097
+ if (path.node.arguments[0].type === "StringLiteral") {
1098
+ const name = path.node.arguments[0].value;
1099
+ if (name) {
1100
+ nodes.add(name);
1101
+ }
1102
+ }
1103
+ }
1104
+ }
1105
+ });
1106
+ return nodes;
1107
+ }
1108
+
1034
1109
  // src/analyze/tsx.ts
1035
1110
  import { babelParse as babelParse4 } from "@vue/compiler-sfc";
1036
1111
 
@@ -1863,7 +1938,7 @@ function processTsx(params) {
1863
1938
  });
1864
1939
  return result;
1865
1940
  }
1866
- function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1941
+ function analyze5(content, type = "vue", lineOffset = 0, addInfo = true) {
1867
1942
  const ast = babelParse4(content, { sourceType: "module", plugins: [
1868
1943
  "typescript",
1869
1944
  "jsx"
@@ -1880,121 +1955,32 @@ function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1880
1955
  };
1881
1956
  }
1882
1957
 
1883
- // src/analyze/style.ts
1884
- function lexBinding(content, start) {
1885
- let state = 0 /* inParens */;
1886
- let parenDepth = 0;
1887
- for (let i = start; i < content.length; i++) {
1888
- const char = content.charAt(i);
1889
- switch (state) {
1890
- case 0 /* inParens */:
1891
- if (char === "'") {
1892
- state = 1 /* inSingleQuoteString */;
1893
- } else if (char === '"') {
1894
- state = 2 /* inDoubleQuoteString */;
1895
- } else if (char === "(") {
1896
- parenDepth++;
1897
- } else if (char === ")") {
1898
- if (parenDepth > 0) {
1899
- parenDepth--;
1900
- } else {
1901
- return i;
1902
- }
1903
- }
1904
- break;
1905
- case 1 /* inSingleQuoteString */:
1906
- if (char === "'") {
1907
- state = 0 /* inParens */;
1908
- }
1909
- break;
1910
- case 2 /* inDoubleQuoteString */:
1911
- if (char === '"') {
1912
- state = 0 /* inParens */;
1913
- }
1914
- break;
1915
- }
1916
- }
1917
- return null;
1918
- }
1919
- function normalizeExpression(exp) {
1920
- exp = exp.trim();
1921
- if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1922
- return exp.slice(1, -1);
1923
- }
1924
- return exp;
1925
- }
1926
- var vBindRE = /v-bind\s*\(/g;
1927
- function analyze5(styles) {
1928
- const nodes = /* @__PURE__ */ new Set();
1929
- styles.forEach((style) => {
1930
- let match;
1931
- const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1932
- while (match = vBindRE.exec(content)) {
1933
- const start = match.index + match[0].length;
1934
- const end = lexBinding(content, start);
1935
- if (end !== null) {
1936
- const variable = normalizeExpression(content.slice(start, end));
1937
- nodes.add(variable);
1938
- }
1939
- }
1940
- });
1941
- return nodes;
1942
- }
1943
-
1944
- // src/suggest/split.ts
1945
- function dfs(graph, node, targets, visited, component) {
1946
- component.add(node);
1947
- visited.add(node);
1948
- targets.forEach((target) => {
1949
- if (!visited.has(target)) {
1950
- dfs(graph, target, graph.get(target) || /* @__PURE__ */ new Set(), visited, component);
1951
- }
1952
- });
1953
- }
1954
- function haveIntersection(setA, setB) {
1955
- for (const item of setA) {
1956
- if (setB.has(item)) {
1957
- return true;
1958
- }
1959
- }
1960
- return false;
1961
- }
1962
- function mergeSets(arr) {
1963
- let result = [...arr];
1964
- for (let i = 0; i < result.length; i++) {
1965
- for (let j = i + 1; j < result.length; j++) {
1966
- if (haveIntersection(result[i], result[j])) {
1967
- const newSet = /* @__PURE__ */ new Set([...result[i], ...result[j]]);
1968
- result.splice(j, 1);
1969
- result.splice(i, 1);
1970
- result = [...result, newSet];
1971
- return mergeSets(result);
1972
- }
1973
- }
1974
- }
1975
- return result;
1976
- }
1977
- function splitGraph(graph) {
1978
- const components = [];
1979
- const sorted = Array.from(graph).sort((a, b) => b[1].size - a[1].size);
1980
- new Map(sorted).forEach((targets, node) => {
1981
- const visited = /* @__PURE__ */ new Set();
1982
- if (!visited.has(node)) {
1983
- const component = /* @__PURE__ */ new Set();
1984
- dfs(graph, node, targets, visited, component);
1985
- components.push(component);
1958
+ // src/mermaid.ts
1959
+ function getMermaidText(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set(), options = {}) {
1960
+ const direction = options.direction || "TB";
1961
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
1962
+ let mermaidText = `flowchart ${direction}
1963
+ `;
1964
+ graph.nodes.forEach((node) => {
1965
+ const shape = node.type === "var" ? `((${node.label}))` : `{${node.label}}`;
1966
+ const style = usedNodes.has(node.label) || node.info?.used?.size ? `style ${node.label} fill:#e1f5fe` : "";
1967
+ mermaidText += ` ${node.label}${shape}
1968
+ `;
1969
+ if (style) {
1970
+ mermaidText += ` ${style}
1971
+ `;
1986
1972
  }
1987
1973
  });
1988
- return mergeSets(components).map((component) => {
1989
- const subGraph = /* @__PURE__ */ new Map();
1990
- component.forEach((node) => {
1991
- const targets = graph.get(node);
1992
- if (targets) {
1993
- subGraph.set(node, targets);
1974
+ graph.edges.forEach((edge, key) => {
1975
+ edge.forEach((to) => {
1976
+ if (!to) {
1977
+ return;
1994
1978
  }
1979
+ mermaidText += ` ${key.label} --> ${to.label}
1980
+ `;
1995
1981
  });
1996
- return subGraph;
1997
1982
  });
1983
+ return mermaidText;
1998
1984
  }
1999
1985
 
2000
1986
  // src/suggest/filter.ts
@@ -2122,6 +2108,62 @@ function findArticulationPoints(graph) {
2122
2108
  return ap;
2123
2109
  }
2124
2110
 
2111
+ // src/suggest/split.ts
2112
+ function dfs(graph, node, targets, visited, component) {
2113
+ component.add(node);
2114
+ visited.add(node);
2115
+ targets.forEach((target) => {
2116
+ if (!visited.has(target)) {
2117
+ dfs(graph, target, graph.get(target) || /* @__PURE__ */ new Set(), visited, component);
2118
+ }
2119
+ });
2120
+ }
2121
+ function haveIntersection(setA, setB) {
2122
+ for (const item of setA) {
2123
+ if (setB.has(item)) {
2124
+ return true;
2125
+ }
2126
+ }
2127
+ return false;
2128
+ }
2129
+ function mergeSets(arr) {
2130
+ let result = [...arr];
2131
+ for (let i = 0; i < result.length; i++) {
2132
+ for (let j = i + 1; j < result.length; j++) {
2133
+ if (haveIntersection(result[i], result[j])) {
2134
+ const newSet = /* @__PURE__ */ new Set([...result[i], ...result[j]]);
2135
+ result.splice(j, 1);
2136
+ result.splice(i, 1);
2137
+ result = [...result, newSet];
2138
+ return mergeSets(result);
2139
+ }
2140
+ }
2141
+ }
2142
+ return result;
2143
+ }
2144
+ function splitGraph(graph) {
2145
+ const components = [];
2146
+ const sorted = Array.from(graph).sort((a, b) => b[1].size - a[1].size);
2147
+ new Map(sorted).forEach((targets, node) => {
2148
+ const visited = /* @__PURE__ */ new Set();
2149
+ if (!visited.has(node)) {
2150
+ const component = /* @__PURE__ */ new Set();
2151
+ dfs(graph, node, targets, visited, component);
2152
+ components.push(component);
2153
+ }
2154
+ });
2155
+ return mergeSets(components).map((component) => {
2156
+ const subGraph = /* @__PURE__ */ new Map();
2157
+ component.forEach((node) => {
2158
+ const targets = graph.get(node);
2159
+ if (targets) {
2160
+ subGraph.set(node, targets);
2161
+ }
2162
+ });
2163
+ return subGraph;
2164
+ });
2165
+ }
2166
+
2125
2167
  // src/suggest/utils.ts
2126
2168
  function hasCycle(graph) {
2127
2169
  const visited = /* @__PURE__ */ new Set();
@@ -2299,15 +2341,19 @@ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__
2299
2341
  edges
2300
2342
  };
2301
2343
  }
2344
+
2345
+ // src/index.ts
2346
+ import { parse } from "@vue/compiler-sfc";
2302
2347
  export {
2303
2348
  NodeType,
2304
2349
  SuggestionType,
2305
- analyze3 as analyzeOptions,
2306
- analyze2 as analyzeSetupScript,
2307
- analyze5 as analyzeStyle,
2308
- analyze as analyzeTemplate,
2309
- analyze4 as analyzeTsx,
2350
+ analyze2 as analyzeOptions,
2351
+ analyze as analyzeSetupScript,
2352
+ analyze3 as analyzeStyle,
2353
+ analyze4 as analyzeTemplate,
2354
+ analyze5 as analyzeTsx,
2310
2355
  gen,
2356
+ getMermaidText,
2311
2357
  getVisData,
2312
2358
  parse
2313
2359
  };