vue-hook-optimizer 0.0.72 → 0.0.73

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.d.mts CHANGED
@@ -16,13 +16,14 @@ declare enum NodeType {
16
16
  var = "var",
17
17
  fun = "fun"
18
18
  }
19
+ type RelationType = 'get' | 'set' | 'call';
19
20
 
20
21
  declare function analyze$4(content: string, lineOffset?: number, jsx?: boolean): {
21
22
  graph: {
22
23
  nodes: Set<TypedNode>;
23
24
  edges: Map<TypedNode, Set<{
24
25
  node: TypedNode;
25
- type: "get" | "set";
26
+ type: RelationType;
26
27
  }>>;
27
28
  };
28
29
  nodesUsedInTemplate: Set<string>;
@@ -32,7 +33,7 @@ declare function analyze$3(content: string, lineOffset?: number, jsx?: boolean):
32
33
  nodes: Set<TypedNode>;
33
34
  edges: Map<TypedNode, Set<{
34
35
  node: TypedNode;
35
- type: "get" | "set";
36
+ type: RelationType;
36
37
  }>>;
37
38
  };
38
39
 
@@ -45,7 +46,7 @@ declare function analyze(content: string, type?: "vue" | "react", lineOffset?: n
45
46
  nodes: Set<TypedNode>;
46
47
  edges: Map<TypedNode, Set<{
47
48
  node: TypedNode;
48
- type: "get" | "set";
49
+ type: RelationType;
49
50
  }>>;
50
51
  };
51
52
  nodesUsedInTemplate: Set<string>;
@@ -73,7 +74,7 @@ declare function gen(graph: {
73
74
  nodes: Set<TypedNode>;
74
75
  edges: Map<TypedNode, Set<{
75
76
  node: TypedNode;
76
- type: 'get' | 'set';
77
+ type: RelationType;
77
78
  }>>;
78
79
  }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): Suggestion[];
79
80
 
@@ -84,11 +85,11 @@ declare function getVisData(graph: {
84
85
  nodes: Set<TypedNode>;
85
86
  edges: Map<TypedNode, Set<{
86
87
  node: TypedNode;
87
- type: 'get' | 'set';
88
+ type: RelationType;
88
89
  }>>;
89
90
  }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): {
90
91
  nodes: CustomNode[];
91
92
  edges: Edge[];
92
93
  };
93
94
 
94
- export { NodeType, type Suggestion, SuggestionType, type TypedNode, analyze$4 as analyzeOptions, analyze$3 as analyzeSetupScript, analyze$2 as analyzeStyle, analyze$1 as analyzeTemplate, analyze as analyzeTsx, gen, getMermaidText, getVisData };
95
+ export { NodeType, type RelationType, type Suggestion, SuggestionType, type TypedNode, analyze$4 as analyzeOptions, analyze$3 as analyzeSetupScript, analyze$2 as analyzeStyle, analyze$1 as analyzeTemplate, analyze as analyzeTsx, gen, getMermaidText, getVisData };
package/dist/index.d.ts CHANGED
@@ -16,13 +16,14 @@ declare enum NodeType {
16
16
  var = "var",
17
17
  fun = "fun"
18
18
  }
19
+ type RelationType = 'get' | 'set' | 'call';
19
20
 
20
21
  declare function analyze$4(content: string, lineOffset?: number, jsx?: boolean): {
21
22
  graph: {
22
23
  nodes: Set<TypedNode>;
23
24
  edges: Map<TypedNode, Set<{
24
25
  node: TypedNode;
25
- type: "get" | "set";
26
+ type: RelationType;
26
27
  }>>;
27
28
  };
28
29
  nodesUsedInTemplate: Set<string>;
@@ -32,7 +33,7 @@ declare function analyze$3(content: string, lineOffset?: number, jsx?: boolean):
32
33
  nodes: Set<TypedNode>;
33
34
  edges: Map<TypedNode, Set<{
34
35
  node: TypedNode;
35
- type: "get" | "set";
36
+ type: RelationType;
36
37
  }>>;
37
38
  };
38
39
 
@@ -45,7 +46,7 @@ declare function analyze(content: string, type?: "vue" | "react", lineOffset?: n
45
46
  nodes: Set<TypedNode>;
46
47
  edges: Map<TypedNode, Set<{
47
48
  node: TypedNode;
48
- type: "get" | "set";
49
+ type: RelationType;
49
50
  }>>;
50
51
  };
51
52
  nodesUsedInTemplate: Set<string>;
@@ -73,7 +74,7 @@ declare function gen(graph: {
73
74
  nodes: Set<TypedNode>;
74
75
  edges: Map<TypedNode, Set<{
75
76
  node: TypedNode;
76
- type: 'get' | 'set';
77
+ type: RelationType;
77
78
  }>>;
78
79
  }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): Suggestion[];
79
80
 
@@ -84,11 +85,11 @@ declare function getVisData(graph: {
84
85
  nodes: Set<TypedNode>;
85
86
  edges: Map<TypedNode, Set<{
86
87
  node: TypedNode;
87
- type: 'get' | 'set';
88
+ type: RelationType;
88
89
  }>>;
89
90
  }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): {
90
91
  nodes: CustomNode[];
91
92
  edges: Edge[];
92
93
  };
93
94
 
94
- export { NodeType, type Suggestion, SuggestionType, type TypedNode, analyze$4 as analyzeOptions, analyze$3 as analyzeSetupScript, analyze$2 as analyzeStyle, analyze$1 as analyzeTemplate, analyze as analyzeTsx, gen, getMermaidText, getVisData };
95
+ export { NodeType, type RelationType, type Suggestion, SuggestionType, type TypedNode, analyze$4 as analyzeOptions, analyze$3 as analyzeSetupScript, analyze$2 as analyzeStyle, analyze$1 as analyzeTemplate, analyze as analyzeTsx, gen, getMermaidText, getVisData };
package/dist/index.js CHANGED
@@ -185,6 +185,22 @@ function isWritingNode(path) {
185
185
  }
186
186
  return false;
187
187
  }
188
+ function isCallingNode(path) {
189
+ const parent = path.parentPath;
190
+ if (parent && parent.isCallExpression()) {
191
+ return parent.node.callee === path.node;
192
+ }
193
+ return false;
194
+ }
195
+ function getRelationType(path) {
196
+ if (path.node.type === "Identifier" && isCallingNode(path)) {
197
+ return "call";
198
+ }
199
+ if (isWritingNode(path)) {
200
+ return "set";
201
+ }
202
+ return "get";
203
+ }
188
204
 
189
205
  // src/analyze/setupScript.ts
190
206
  var traverse = import_traverse.default.default?.default || import_traverse.default.default || import_traverse.default;
@@ -438,7 +454,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
438
454
  if (!watchArgsNames.includes(path1.node.name)) {
439
455
  graph.edges.get(watchArg.name)?.add({
440
456
  label: path1.node.name,
441
- type: isWritingNode(path1) ? "set" : "get"
457
+ type: getRelationType(path1)
442
458
  });
443
459
  }
444
460
  });
@@ -469,7 +485,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
469
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)) {
470
486
  graph.edges.get(name)?.add({
471
487
  label: path1.node.name,
472
- type: isWritingNode(path1) ? "set" : "get"
488
+ type: getRelationType(path1)
473
489
  });
474
490
  }
475
491
  },
@@ -479,7 +495,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
479
495
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
480
496
  graph.edges.get(name)?.add({
481
497
  label: path1.node.property.name,
482
- type: isWritingNode(path1) ? "set" : "get"
498
+ type: getRelationType(path1)
483
499
  });
484
500
  }
485
501
  }
@@ -500,7 +516,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
500
516
  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)) {
501
517
  graph.edges.get(name)?.add({
502
518
  label: path1.node.name,
503
- type: isWritingNode(path1) ? "set" : "get"
519
+ type: getRelationType(path1)
504
520
  });
505
521
  }
506
522
  },
@@ -510,7 +526,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
510
526
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
511
527
  graph.edges.get(name)?.add({
512
528
  label: path1.node.property.name,
513
- type: isWritingNode(path1) ? "set" : "get"
529
+ type: getRelationType(path1)
514
530
  });
515
531
  }
516
532
  }
@@ -530,7 +546,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
530
546
  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)) {
531
547
  graph.edges.get(name)?.add({
532
548
  label: path1.node.name,
533
- type: isWritingNode(path1) ? "set" : "get"
549
+ type: getRelationType(path1)
534
550
  });
535
551
  }
536
552
  },
@@ -540,7 +556,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
540
556
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
541
557
  graph.edges.get(name)?.add({
542
558
  label: path1.node.property.name,
543
- type: isWritingNode(path1) ? "set" : "get"
559
+ type: getRelationType(path1)
544
560
  });
545
561
  }
546
562
  }
@@ -565,7 +581,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
565
581
  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)) {
566
582
  graph.edges.get(name)?.add({
567
583
  label: path1.node.name,
568
- type: isWritingNode(path1) ? "set" : "get"
584
+ type: getRelationType(path1)
569
585
  });
570
586
  }
571
587
  },
@@ -575,7 +591,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
575
591
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
576
592
  graph.edges.get(name)?.add({
577
593
  label: path1.node.property.name,
578
- type: isWritingNode(path1) ? "set" : "get"
594
+ type: getRelationType(path1)
579
595
  });
580
596
  }
581
597
  }
@@ -589,7 +605,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
589
605
  if (graph.nodes.has(path.node.init.name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
590
606
  graph.edges.get(name)?.add({
591
607
  label: path.node.init.name,
592
- type: isWritingNode(path) ? "set" : "get"
608
+ type: getRelationType(path)
593
609
  });
594
610
  }
595
611
  } else {
@@ -599,7 +615,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
599
615
  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)) {
600
616
  graph.edges.get(name)?.add({
601
617
  label: path1.node.name,
602
- type: isWritingNode(path1) ? "set" : "get"
618
+ type: getRelationType(path1)
603
619
  });
604
620
  }
605
621
  }
@@ -617,7 +633,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
617
633
  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)) {
618
634
  graph.edges.get(name)?.add({
619
635
  label: path1.node.name,
620
- type: isWritingNode(path1) ? "set" : "get"
636
+ type: getRelationType(path1)
621
637
  });
622
638
  }
623
639
  },
@@ -627,7 +643,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
627
643
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
628
644
  graph.edges.get(name)?.add({
629
645
  label: path1.node.property.name,
630
- type: isWritingNode(path1) ? "set" : "get"
646
+ type: getRelationType(path1)
631
647
  });
632
648
  }
633
649
  }
@@ -645,7 +661,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
645
661
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
646
662
  graph.edges.get(name)?.add({
647
663
  label: path1.node.property.name,
648
- type: isWritingNode(path1) ? "set" : "get"
664
+ type: getRelationType(path1)
649
665
  });
650
666
  }
651
667
  }
@@ -889,7 +905,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
889
905
  });
890
906
  graph.edges.set(name, /* @__PURE__ */ new Set([{
891
907
  label: valName,
892
- type: isWritingNode(path3) ? "set" : "get"
908
+ type: getRelationType(path3)
893
909
  }]));
894
910
  }
895
911
  }
@@ -1012,7 +1028,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
1012
1028
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1013
1029
  graph.edges.get(name)?.add({
1014
1030
  label: path2.node.property.name,
1015
- type: isWritingNode(path2) ? "set" : "get"
1031
+ type: getRelationType(path2)
1016
1032
  });
1017
1033
  }
1018
1034
  }
@@ -1027,7 +1043,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
1027
1043
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1028
1044
  graph.edges.get(name)?.add({
1029
1045
  label: path2.node.property.name,
1030
- type: isWritingNode(path2) ? "set" : "get"
1046
+ type: getRelationType(path2)
1031
1047
  });
1032
1048
  }
1033
1049
  }
@@ -1049,7 +1065,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
1049
1065
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1050
1066
  graph.edges.get(name)?.add({
1051
1067
  label: path2.node.property.name,
1052
- type: isWritingNode(path2) ? "set" : "get"
1068
+ type: getRelationType(path2)
1053
1069
  });
1054
1070
  }
1055
1071
  }
@@ -1080,7 +1096,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
1080
1096
  if (watchArg && watchArg.name !== path2.node.property.name) {
1081
1097
  graph.edges.get(watchArg.name)?.add({
1082
1098
  label: path2.node.property.name,
1083
- type: isWritingNode(path2) ? "set" : "get"
1099
+ type: getRelationType(path2)
1084
1100
  });
1085
1101
  }
1086
1102
  }
@@ -1641,7 +1657,7 @@ function addIdentifiesToGraphByScanReturn({ path: path1, graph, nodeCollection,
1641
1657
  });
1642
1658
  graph.edges.set(name, /* @__PURE__ */ new Set([{
1643
1659
  label: valName,
1644
- type: isWritingNode(path3) ? "set" : "get"
1660
+ type: getRelationType(path3)
1645
1661
  }]));
1646
1662
  }
1647
1663
  }
@@ -1803,7 +1819,7 @@ function processByReturnJSX(params) {
1803
1819
  if (scope === bindingScope && collectionNodes.has(toName)) {
1804
1820
  graph.edges.get(fromName)?.add({
1805
1821
  label: toName,
1806
- type: isWritingNode(path) ? "set" : "get"
1822
+ type: getRelationType(path)
1807
1823
  });
1808
1824
  }
1809
1825
  }
@@ -1925,7 +1941,7 @@ function processByReact(params) {
1925
1941
  if (scope === bindingScope && collectionNodes.has(toName)) {
1926
1942
  graph.edges.get(fromName)?.add({
1927
1943
  label: toName,
1928
- type: isWritingNode(path) ? "set" : "get"
1944
+ type: getRelationType(path)
1929
1945
  });
1930
1946
  }
1931
1947
  }
@@ -2310,18 +2326,23 @@ function hasCycle(graph) {
2310
2326
  const visited = /* @__PURE__ */ new Set();
2311
2327
  const onStack = /* @__PURE__ */ new Set();
2312
2328
  const stack = [];
2329
+ let cycleNodes = [];
2313
2330
  function dfs2(node) {
2314
2331
  if (visited.has(node)) {
2315
2332
  if (onStack.has(node)) {
2316
2333
  const idx = stack.indexOf(node);
2317
2334
  const cycle = stack.slice(idx);
2318
- const allSet = cycle.every((curr, i) => {
2335
+ const allNotGet = cycle.every((curr, i) => {
2319
2336
  const next = cycle[(i + 1) % cycle.length];
2320
2337
  return Array.from(graph.get(curr) || []).some(
2321
- (edge) => edge.node === next && edge.type === "set"
2338
+ (edge) => edge.node === next && edge.type !== "get"
2322
2339
  );
2323
2340
  });
2324
- return allSet;
2341
+ if (allNotGet) {
2342
+ cycleNodes = cycle;
2343
+ return true;
2344
+ }
2345
+ return false;
2325
2346
  }
2326
2347
  return false;
2327
2348
  }
@@ -2329,6 +2350,10 @@ function hasCycle(graph) {
2329
2350
  onStack.add(node);
2330
2351
  stack.push(node);
2331
2352
  for (const neighbor of graph.get(node) || /* @__PURE__ */ new Set()) {
2353
+ if (neighbor.node === node && neighbor.type !== "get") {
2354
+ cycleNodes = [node];
2355
+ return true;
2356
+ }
2332
2357
  if (dfs2(neighbor.node)) {
2333
2358
  return true;
2334
2359
  }
@@ -2339,7 +2364,7 @@ function hasCycle(graph) {
2339
2364
  }
2340
2365
  for (const [node, targets] of graph) {
2341
2366
  if (dfs2(node)) {
2342
- return { hasCycle: true, cycleNodes: [...stack] };
2367
+ return { hasCycle: true, cycleNodes };
2343
2368
  }
2344
2369
  }
2345
2370
  return { hasCycle: false, cycleNodes: [] };