vue-hook-optimizer 0.0.65 → 0.0.67

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)) {
@@ -404,7 +355,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
404
355
  }
405
356
  }
406
357
  } else {
407
- traverse2(argNode, {
358
+ traverse(argNode, {
408
359
  Identifier(path1) {
409
360
  const binding = path1.scope.getBinding(path1.node.name);
410
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)) {
@@ -432,11 +383,11 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
432
383
  });
433
384
  }
434
385
  }
435
- traverse2(ast, {
386
+ traverse(ast, {
436
387
  FunctionDeclaration(path) {
437
388
  const name = path.node.id?.name;
438
389
  if (name && graph.nodes.has(name)) {
439
- traverse2(path.node.body, {
390
+ traverse(path.node.body, {
440
391
  Identifier(path1) {
441
392
  const binding = path1.scope.getBinding(path1.node.name);
442
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)) {
@@ -461,7 +412,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
461
412
  if (element?.type === "Identifier") {
462
413
  const name = element.name;
463
414
  if (name && graph.nodes.has(name) && path.node.init?.type === "CallExpression") {
464
- traverse2(path.node.init, {
415
+ traverse(path.node.init, {
465
416
  Identifier(path1) {
466
417
  const binding = path1.scope.getBinding(path1.node.name);
467
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)) {
@@ -485,7 +436,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
485
436
  if (property.type === "ObjectProperty" && property.value.type === "Identifier") {
486
437
  const name = property.value.name;
487
438
  if (name && graph.nodes.has(name) && path.node.init) {
488
- traverse2(path.node.init, {
439
+ traverse(path.node.init, {
489
440
  Identifier(path1) {
490
441
  const binding = path1.scope.getBinding(path1.node.name);
491
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)) {
@@ -514,7 +465,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
514
465
  }
515
466
  const name = path.node.id?.name;
516
467
  if (name && graph.nodes.has(name)) {
517
- traverse2(path.node.init, {
468
+ traverse(path.node.init, {
518
469
  Identifier(path1) {
519
470
  const binding = path1.scope.getBinding(path1.node.name);
520
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)) {
@@ -539,7 +490,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
539
490
  graph.edges.get(name)?.add(path.node.init.name);
540
491
  }
541
492
  } else {
542
- traverse2(path.node.init, {
493
+ traverse(path.node.init, {
543
494
  Identifier(path1) {
544
495
  const binding = path1.scope.getBinding(path1.node.name);
545
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)) {
@@ -554,7 +505,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
554
505
  ObjectMethod(path) {
555
506
  if (path.node.key.type === "Identifier" && graph.nodes.has(path.node.key.name)) {
556
507
  const name = path.node.key.name;
557
- traverse2(path.node.body, {
508
+ traverse(path.node.body, {
558
509
  Identifier(path1) {
559
510
  const binding = path1.scope.getBinding(path1.node.name);
560
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)) {
@@ -575,7 +526,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
575
526
  ObjectProperty(path) {
576
527
  if (path.node.key.type === "Identifier" && graph.nodes.has(path.node.key.name)) {
577
528
  const name = path.node.key.name;
578
- traverse2(path.node.value, {
529
+ traverse(path.node.value, {
579
530
  MemberExpression(path1) {
580
531
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
581
532
  const binding = path1.scope.getBinding(path1.node.object.name);
@@ -614,8 +565,8 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
614
565
  nodeCollection
615
566
  };
616
567
  }
617
- function analyze2(content, lineOffset = 0, jsx = false) {
618
- const ast = babelParse2(content, { sourceType: "module", plugins: [
568
+ function analyze(content, lineOffset = 0, jsx = false) {
569
+ const ast = babelParse(content, { sourceType: "module", plugins: [
619
570
  "typescript",
620
571
  ...jsx ? ["jsx"] : []
621
572
  ] });
@@ -624,9 +575,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
624
575
  }
625
576
 
626
577
  // src/analyze/options.ts
627
- import { babelParse as babelParse3 } from "@vue/compiler-sfc";
628
- import _traverse3 from "@babel/traverse";
629
- var traverse3 = _traverse3.default?.default || _traverse3.default || _traverse3;
578
+ var traverse2 = _traverse2.default?.default || _traverse2.default || _traverse2;
630
579
  var vueLifeCycleHooks = [
631
580
  "beforeCreate",
632
581
  "created",
@@ -643,8 +592,8 @@ var vueLifeCycleHooks = [
643
592
  "renderTriggered",
644
593
  "provide"
645
594
  ];
646
- function analyze3(content, lineOffset = 0, jsx = false) {
647
- const ast = babelParse3(content, { sourceType: "module", plugins: [
595
+ function analyze2(content, lineOffset = 0, jsx = false) {
596
+ const ast = babelParse2(content, { sourceType: "module", plugins: [
648
597
  "typescript",
649
598
  ...jsx ? ["jsx"] : []
650
599
  ] });
@@ -656,12 +605,12 @@ function analyze3(content, lineOffset = 0, jsx = false) {
656
605
  };
657
606
  const nodesUsedInTemplate = /* @__PURE__ */ new Set();
658
607
  function process(node, path) {
659
- traverse3(node, {
608
+ traverse2(node, {
660
609
  ObjectProperty(path1) {
661
610
  if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
662
611
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "data" && (path1.node.value.type === "ArrowFunctionExpression" || path1.node.value.type === "FunctionExpression")) {
663
612
  const dataNode = path1.node.value;
664
- traverse3(dataNode, {
613
+ traverse2(dataNode, {
665
614
  ReturnStatement(path2) {
666
615
  if (path2.parent === dataNode.body) {
667
616
  if (path2.node.argument?.type === "ObjectExpression") {
@@ -728,10 +677,10 @@ function analyze3(content, lineOffset = 0, jsx = false) {
728
677
  }
729
678
  }
730
679
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "render" && (path1.node.value.type === "ArrowFunctionExpression" || path1.node.value.type === "FunctionExpression")) {
731
- traverse3(path1.node.value, {
680
+ traverse2(path1.node.value, {
732
681
  ReturnStatement(path2) {
733
682
  const templateNode = path2.node;
734
- traverse3(templateNode, {
683
+ traverse2(templateNode, {
735
684
  MemberExpression(path3) {
736
685
  if (path3.node.object && path3.node.object.type === "ThisExpression") {
737
686
  if (path3.node.property && path3.node.property.type === "Identifier") {
@@ -750,11 +699,11 @@ function analyze3(content, lineOffset = 0, jsx = false) {
750
699
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
751
700
  const setupNode = path1.node;
752
701
  const spread = [];
753
- traverse3(setupNode, {
702
+ traverse2(setupNode, {
754
703
  ReturnStatement(path2) {
755
704
  if (path2.node.argument?.type === "ObjectExpression") {
756
705
  const returnNode = path2.node.argument;
757
- traverse3(returnNode, {
706
+ traverse2(returnNode, {
758
707
  SpreadElement(path3) {
759
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") {
760
709
  spread.push(path3.node.argument.arguments[0].name);
@@ -766,7 +715,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
766
715
  }
767
716
  if (path2.node.argument?.type === "FunctionExpression" || path2.node.argument?.type === "ArrowFunctionExpression") {
768
717
  const templateNode = path2.node.argument.body;
769
- traverse3(templateNode, {
718
+ traverse2(templateNode, {
770
719
  Identifier(path3) {
771
720
  const binding = path3.scope.getBinding(path3.node.name);
772
721
  if (binding?.scope === path1.scope) {
@@ -791,14 +740,14 @@ function analyze3(content, lineOffset = 0, jsx = false) {
791
740
  },
792
741
  nodeCollection: tempNodeCollection
793
742
  } = processSetup(setupNode, path1.scope, setupNode, spread, lineOffset);
794
- traverse3(setupNode, {
743
+ traverse2(setupNode, {
795
744
  ReturnStatement(path2) {
796
745
  if (path2.scope !== path1.scope) {
797
746
  return;
798
747
  }
799
748
  if (path2.node.argument?.type === "ObjectExpression") {
800
749
  const returnNode = path2.node.argument;
801
- traverse3(returnNode, {
750
+ traverse2(returnNode, {
802
751
  ObjectProperty(path3) {
803
752
  if (path3.parent === returnNode) {
804
753
  if (path3.node.key.type === "Identifier" && path3.node.value.type === "Identifier" && tempNodes.has(path3.node.value.name)) {
@@ -866,7 +815,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
866
815
  }
867
816
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "data") {
868
817
  const dataNode = path1.node;
869
- traverse3(dataNode, {
818
+ traverse2(dataNode, {
870
819
  ReturnStatement(path2) {
871
820
  if (path2.parent === dataNode.body) {
872
821
  if (path2.node.argument?.type === "ObjectExpression") {
@@ -891,10 +840,10 @@ function analyze3(content, lineOffset = 0, jsx = false) {
891
840
  }, path1.scope, path1);
892
841
  }
893
842
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "render") {
894
- traverse3(path1.node, {
843
+ traverse2(path1.node, {
895
844
  ReturnStatement(path2) {
896
845
  const templateNode = path2.node;
897
- traverse3(templateNode, {
846
+ traverse2(templateNode, {
898
847
  MemberExpression(path3) {
899
848
  if (path3.node.object && path3.node.object.type === "ThisExpression") {
900
849
  if (path3.node.property && path3.node.property.type === "Identifier") {
@@ -909,12 +858,12 @@ function analyze3(content, lineOffset = 0, jsx = false) {
909
858
  }
910
859
  }
911
860
  }, path.scope, path);
912
- traverse3(node, {
861
+ traverse2(node, {
913
862
  ObjectMethod(path1) {
914
863
  if (path.node.declaration.type === "ObjectExpression" && path1.parent === path.node.declaration || path.node.declaration.type === "CallExpression" && path1.parent === path.node.declaration.arguments[0]) {
915
864
  if (path1.node.key.type === "Identifier" && vueLifeCycleHooks.includes(path1.node.key.name)) {
916
865
  const hookName = path1.node.key.name;
917
- traverse3(path1.node.body, {
866
+ traverse2(path1.node.body, {
918
867
  MemberExpression(path2) {
919
868
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
920
869
  const _node = nodeCollection.getNode(path2.node.property.name);
@@ -940,7 +889,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
940
889
  computedNode.value.properties.forEach((prop) => {
941
890
  if (prop.type === "ObjectMethod" && prop.key.type === "Identifier") {
942
891
  const name = prop.key.name;
943
- traverse3(prop, {
892
+ traverse2(prop, {
944
893
  MemberExpression(path2) {
945
894
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
946
895
  graph.edges.get(name)?.add(path2.node.property.name);
@@ -952,7 +901,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
952
901
  const name = prop.key.name;
953
902
  prop.value.properties.forEach((prop1) => {
954
903
  if (prop1.type === "ObjectProperty" && prop1.key.type === "Identifier" && prop1.key.name === "get") {
955
- traverse3(prop1, {
904
+ traverse2(prop1, {
956
905
  MemberExpression(path2) {
957
906
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
958
907
  graph.edges.get(name)?.add(path2.node.property.name);
@@ -971,7 +920,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
971
920
  methodsNode.value.properties.forEach((prop) => {
972
921
  if ((prop.type === "ObjectMethod" || prop.type === "ObjectProperty") && prop.key.type === "Identifier") {
973
922
  const name = prop.key.name;
974
- traverse3(prop, {
923
+ traverse2(prop, {
975
924
  MemberExpression(path2) {
976
925
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
977
926
  graph.edges.get(name)?.add(path2.node.property.name);
@@ -998,7 +947,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
998
947
  used: /* @__PURE__ */ new Set([hookName])
999
948
  };
1000
949
  }
1001
- traverse3(path1.node.value, {
950
+ traverse2(path1.node.value, {
1002
951
  MemberExpression(path2) {
1003
952
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1004
953
  if (watchArg && watchArg.name !== path2.node.property.name) {
@@ -1010,7 +959,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
1010
959
  }
1011
960
  });
1012
961
  } else {
1013
- traverse3(path1.node.value, {
962
+ traverse2(path1.node.value, {
1014
963
  MemberExpression(path2) {
1015
964
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1016
965
  const _node = nodeCollection.getNode(path2.node.property.name);
@@ -1031,7 +980,7 @@ function analyze3(content, lineOffset = 0, jsx = false) {
1031
980
  }
1032
981
  }, path.scope, path);
1033
982
  }
1034
- traverse3(ast, {
983
+ traverse2(ast, {
1035
984
  ExportDefaultDeclaration(path) {
1036
985
  if (path.node.declaration.type === "ObjectExpression") {
1037
986
  process(path.node.declaration, path);
@@ -1046,6 +995,117 @@ function analyze3(content, lineOffset = 0, jsx = false) {
1046
995
  };
1047
996
  }
1048
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
+
1049
1109
  // src/analyze/tsx.ts
1050
1110
  import { babelParse as babelParse4 } from "@vue/compiler-sfc";
1051
1111
 
@@ -1878,7 +1938,7 @@ function processTsx(params) {
1878
1938
  });
1879
1939
  return result;
1880
1940
  }
1881
- function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1941
+ function analyze5(content, type = "vue", lineOffset = 0, addInfo = true) {
1882
1942
  const ast = babelParse4(content, { sourceType: "module", plugins: [
1883
1943
  "typescript",
1884
1944
  "jsx"
@@ -1895,121 +1955,32 @@ function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1895
1955
  };
1896
1956
  }
1897
1957
 
1898
- // src/analyze/style.ts
1899
- function lexBinding(content, start) {
1900
- let state = 0 /* inParens */;
1901
- let parenDepth = 0;
1902
- for (let i = start; i < content.length; i++) {
1903
- const char = content.charAt(i);
1904
- switch (state) {
1905
- case 0 /* inParens */:
1906
- if (char === "'") {
1907
- state = 1 /* inSingleQuoteString */;
1908
- } else if (char === '"') {
1909
- state = 2 /* inDoubleQuoteString */;
1910
- } else if (char === "(") {
1911
- parenDepth++;
1912
- } else if (char === ")") {
1913
- if (parenDepth > 0) {
1914
- parenDepth--;
1915
- } else {
1916
- return i;
1917
- }
1918
- }
1919
- break;
1920
- case 1 /* inSingleQuoteString */:
1921
- if (char === "'") {
1922
- state = 0 /* inParens */;
1923
- }
1924
- break;
1925
- case 2 /* inDoubleQuoteString */:
1926
- if (char === '"') {
1927
- state = 0 /* inParens */;
1928
- }
1929
- break;
1930
- }
1931
- }
1932
- return null;
1933
- }
1934
- function normalizeExpression(exp) {
1935
- exp = exp.trim();
1936
- if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1937
- return exp.slice(1, -1);
1938
- }
1939
- return exp;
1940
- }
1941
- var vBindRE = /v-bind\s*\(/g;
1942
- function analyze5(styles) {
1943
- const nodes = /* @__PURE__ */ new Set();
1944
- styles.forEach((style) => {
1945
- let match;
1946
- const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1947
- while (match = vBindRE.exec(content)) {
1948
- const start = match.index + match[0].length;
1949
- const end = lexBinding(content, start);
1950
- if (end !== null) {
1951
- const variable = normalizeExpression(content.slice(start, end));
1952
- nodes.add(variable);
1953
- }
1954
- }
1955
- });
1956
- return nodes;
1957
- }
1958
-
1959
- // src/suggest/split.ts
1960
- function dfs(graph, node, targets, visited, component) {
1961
- component.add(node);
1962
- visited.add(node);
1963
- targets.forEach((target) => {
1964
- if (!visited.has(target)) {
1965
- dfs(graph, target, graph.get(target) || /* @__PURE__ */ new Set(), visited, component);
1966
- }
1967
- });
1968
- }
1969
- function haveIntersection(setA, setB) {
1970
- for (const item of setA) {
1971
- if (setB.has(item)) {
1972
- return true;
1973
- }
1974
- }
1975
- return false;
1976
- }
1977
- function mergeSets(arr) {
1978
- let result = [...arr];
1979
- for (let i = 0; i < result.length; i++) {
1980
- for (let j = i + 1; j < result.length; j++) {
1981
- if (haveIntersection(result[i], result[j])) {
1982
- const newSet = /* @__PURE__ */ new Set([...result[i], ...result[j]]);
1983
- result.splice(j, 1);
1984
- result.splice(i, 1);
1985
- result = [...result, newSet];
1986
- return mergeSets(result);
1987
- }
1988
- }
1989
- }
1990
- return result;
1991
- }
1992
- function splitGraph(graph) {
1993
- const components = [];
1994
- const sorted = Array.from(graph).sort((a, b) => b[1].size - a[1].size);
1995
- new Map(sorted).forEach((targets, node) => {
1996
- const visited = /* @__PURE__ */ new Set();
1997
- if (!visited.has(node)) {
1998
- const component = /* @__PURE__ */ new Set();
1999
- dfs(graph, node, targets, visited, component);
2000
- 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
+ `;
2001
1972
  }
2002
1973
  });
2003
- return mergeSets(components).map((component) => {
2004
- const subGraph = /* @__PURE__ */ new Map();
2005
- component.forEach((node) => {
2006
- const targets = graph.get(node);
2007
- if (targets) {
2008
- subGraph.set(node, targets);
1974
+ graph.edges.forEach((edge, key) => {
1975
+ edge.forEach((to) => {
1976
+ if (!to) {
1977
+ return;
2009
1978
  }
1979
+ mermaidText += ` ${key.label} --> ${to.label}
1980
+ `;
2010
1981
  });
2011
- return subGraph;
2012
1982
  });
1983
+ return mermaidText;
2013
1984
  }
2014
1985
 
2015
1986
  // src/suggest/filter.ts
@@ -2137,6 +2108,62 @@ function findArticulationPoints(graph) {
2137
2108
  return ap;
2138
2109
  }
2139
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
+
2140
2167
  // src/suggest/utils.ts
2141
2168
  function hasCycle(graph) {
2142
2169
  const visited = /* @__PURE__ */ new Set();
@@ -2314,15 +2341,19 @@ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__
2314
2341
  edges
2315
2342
  };
2316
2343
  }
2344
+
2345
+ // src/index.ts
2346
+ import { parse } from "@vue/compiler-sfc";
2317
2347
  export {
2318
2348
  NodeType,
2319
2349
  SuggestionType,
2320
- analyze3 as analyzeOptions,
2321
- analyze2 as analyzeSetupScript,
2322
- analyze5 as analyzeStyle,
2323
- analyze as analyzeTemplate,
2324
- analyze4 as analyzeTsx,
2350
+ analyze2 as analyzeOptions,
2351
+ analyze as analyzeSetupScript,
2352
+ analyze3 as analyzeStyle,
2353
+ analyze4 as analyzeTemplate,
2354
+ analyze5 as analyzeTsx,
2325
2355
  gen,
2356
+ getMermaidText,
2326
2357
  getVisData,
2327
2358
  parse
2328
2359
  };