vue-hook-optimizer 0.0.72-beta.1 → 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.mjs CHANGED
@@ -139,6 +139,22 @@ function isWritingNode(path) {
139
139
  }
140
140
  return false;
141
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
+ }
142
158
 
143
159
  // src/analyze/setupScript.ts
144
160
  var traverse = _traverse.default?.default || _traverse.default || _traverse;
@@ -392,7 +408,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
392
408
  if (!watchArgsNames.includes(path1.node.name)) {
393
409
  graph.edges.get(watchArg.name)?.add({
394
410
  label: path1.node.name,
395
- type: isWritingNode(path1) ? "set" : "get"
411
+ type: getRelationType(path1)
396
412
  });
397
413
  }
398
414
  });
@@ -423,7 +439,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
423
439
  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)) {
424
440
  graph.edges.get(name)?.add({
425
441
  label: path1.node.name,
426
- type: isWritingNode(path1) ? "set" : "get"
442
+ type: getRelationType(path1)
427
443
  });
428
444
  }
429
445
  },
@@ -433,7 +449,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
433
449
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
434
450
  graph.edges.get(name)?.add({
435
451
  label: path1.node.property.name,
436
- type: isWritingNode(path1) ? "set" : "get"
452
+ type: getRelationType(path1)
437
453
  });
438
454
  }
439
455
  }
@@ -454,7 +470,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
454
470
  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
471
  graph.edges.get(name)?.add({
456
472
  label: path1.node.name,
457
- type: isWritingNode(path1) ? "set" : "get"
473
+ type: getRelationType(path1)
458
474
  });
459
475
  }
460
476
  },
@@ -464,7 +480,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
464
480
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
465
481
  graph.edges.get(name)?.add({
466
482
  label: path1.node.property.name,
467
- type: isWritingNode(path1) ? "set" : "get"
483
+ type: getRelationType(path1)
468
484
  });
469
485
  }
470
486
  }
@@ -484,7 +500,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
484
500
  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
501
  graph.edges.get(name)?.add({
486
502
  label: path1.node.name,
487
- type: isWritingNode(path1) ? "set" : "get"
503
+ type: getRelationType(path1)
488
504
  });
489
505
  }
490
506
  },
@@ -494,7 +510,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
494
510
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
495
511
  graph.edges.get(name)?.add({
496
512
  label: path1.node.property.name,
497
- type: isWritingNode(path1) ? "set" : "get"
513
+ type: getRelationType(path1)
498
514
  });
499
515
  }
500
516
  }
@@ -519,7 +535,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
519
535
  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)) {
520
536
  graph.edges.get(name)?.add({
521
537
  label: path1.node.name,
522
- type: isWritingNode(path1) ? "set" : "get"
538
+ type: getRelationType(path1)
523
539
  });
524
540
  }
525
541
  },
@@ -529,7 +545,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
529
545
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
530
546
  graph.edges.get(name)?.add({
531
547
  label: path1.node.property.name,
532
- type: isWritingNode(path1) ? "set" : "get"
548
+ type: getRelationType(path1)
533
549
  });
534
550
  }
535
551
  }
@@ -543,7 +559,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
543
559
  if (graph.nodes.has(path.node.init.name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
544
560
  graph.edges.get(name)?.add({
545
561
  label: path.node.init.name,
546
- type: isWritingNode(path) ? "set" : "get"
562
+ type: getRelationType(path)
547
563
  });
548
564
  }
549
565
  } else {
@@ -553,7 +569,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
553
569
  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
570
  graph.edges.get(name)?.add({
555
571
  label: path1.node.name,
556
- type: isWritingNode(path1) ? "set" : "get"
572
+ type: getRelationType(path1)
557
573
  });
558
574
  }
559
575
  }
@@ -571,7 +587,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
571
587
  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)) {
572
588
  graph.edges.get(name)?.add({
573
589
  label: path1.node.name,
574
- type: isWritingNode(path1) ? "set" : "get"
590
+ type: getRelationType(path1)
575
591
  });
576
592
  }
577
593
  },
@@ -581,7 +597,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
581
597
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
582
598
  graph.edges.get(name)?.add({
583
599
  label: path1.node.property.name,
584
- type: isWritingNode(path1) ? "set" : "get"
600
+ type: getRelationType(path1)
585
601
  });
586
602
  }
587
603
  }
@@ -599,7 +615,7 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
599
615
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
600
616
  graph.edges.get(name)?.add({
601
617
  label: path1.node.property.name,
602
- type: isWritingNode(path1) ? "set" : "get"
618
+ type: getRelationType(path1)
603
619
  });
604
620
  }
605
621
  }
@@ -843,7 +859,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
843
859
  });
844
860
  graph.edges.set(name, /* @__PURE__ */ new Set([{
845
861
  label: valName,
846
- type: isWritingNode(path3) ? "set" : "get"
862
+ type: getRelationType(path3)
847
863
  }]));
848
864
  }
849
865
  }
@@ -966,7 +982,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
966
982
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
967
983
  graph.edges.get(name)?.add({
968
984
  label: path2.node.property.name,
969
- type: isWritingNode(path2) ? "set" : "get"
985
+ type: getRelationType(path2)
970
986
  });
971
987
  }
972
988
  }
@@ -981,7 +997,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
981
997
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
982
998
  graph.edges.get(name)?.add({
983
999
  label: path2.node.property.name,
984
- type: isWritingNode(path2) ? "set" : "get"
1000
+ type: getRelationType(path2)
985
1001
  });
986
1002
  }
987
1003
  }
@@ -1003,7 +1019,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
1003
1019
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
1004
1020
  graph.edges.get(name)?.add({
1005
1021
  label: path2.node.property.name,
1006
- type: isWritingNode(path2) ? "set" : "get"
1022
+ type: getRelationType(path2)
1007
1023
  });
1008
1024
  }
1009
1025
  }
@@ -1034,7 +1050,7 @@ function analyze2(content, lineOffset = 0, jsx = false) {
1034
1050
  if (watchArg && watchArg.name !== path2.node.property.name) {
1035
1051
  graph.edges.get(watchArg.name)?.add({
1036
1052
  label: path2.node.property.name,
1037
- type: isWritingNode(path2) ? "set" : "get"
1053
+ type: getRelationType(path2)
1038
1054
  });
1039
1055
  }
1040
1056
  }
@@ -1595,7 +1611,7 @@ function addIdentifiesToGraphByScanReturn({ path: path1, graph, nodeCollection,
1595
1611
  });
1596
1612
  graph.edges.set(name, /* @__PURE__ */ new Set([{
1597
1613
  label: valName,
1598
- type: isWritingNode(path3) ? "set" : "get"
1614
+ type: getRelationType(path3)
1599
1615
  }]));
1600
1616
  }
1601
1617
  }
@@ -1757,7 +1773,7 @@ function processByReturnJSX(params) {
1757
1773
  if (scope === bindingScope && collectionNodes.has(toName)) {
1758
1774
  graph.edges.get(fromName)?.add({
1759
1775
  label: toName,
1760
- type: isWritingNode(path) ? "set" : "get"
1776
+ type: getRelationType(path)
1761
1777
  });
1762
1778
  }
1763
1779
  }
@@ -1879,7 +1895,7 @@ function processByReact(params) {
1879
1895
  if (scope === bindingScope && collectionNodes.has(toName)) {
1880
1896
  graph.edges.get(fromName)?.add({
1881
1897
  label: toName,
1882
- type: isWritingNode(path) ? "set" : "get"
1898
+ type: getRelationType(path)
1883
1899
  });
1884
1900
  }
1885
1901
  }
@@ -2264,18 +2280,23 @@ function hasCycle(graph) {
2264
2280
  const visited = /* @__PURE__ */ new Set();
2265
2281
  const onStack = /* @__PURE__ */ new Set();
2266
2282
  const stack = [];
2283
+ let cycleNodes = [];
2267
2284
  function dfs2(node) {
2268
2285
  if (visited.has(node)) {
2269
2286
  if (onStack.has(node)) {
2270
2287
  const idx = stack.indexOf(node);
2271
2288
  const cycle = stack.slice(idx);
2272
- const allSet = cycle.every((curr, i) => {
2289
+ const allNotGet = cycle.every((curr, i) => {
2273
2290
  const next = cycle[(i + 1) % cycle.length];
2274
2291
  return Array.from(graph.get(curr) || []).some(
2275
- (edge) => edge.node === next && edge.type === "set"
2292
+ (edge) => edge.node === next && edge.type !== "get"
2276
2293
  );
2277
2294
  });
2278
- return allSet;
2295
+ if (allNotGet) {
2296
+ cycleNodes = cycle;
2297
+ return true;
2298
+ }
2299
+ return false;
2279
2300
  }
2280
2301
  return false;
2281
2302
  }
@@ -2283,6 +2304,10 @@ function hasCycle(graph) {
2283
2304
  onStack.add(node);
2284
2305
  stack.push(node);
2285
2306
  for (const neighbor of graph.get(node) || /* @__PURE__ */ new Set()) {
2307
+ if (neighbor.node === node && neighbor.type !== "get") {
2308
+ cycleNodes = [node];
2309
+ return true;
2310
+ }
2286
2311
  if (dfs2(neighbor.node)) {
2287
2312
  return true;
2288
2313
  }
@@ -2293,7 +2318,7 @@ function hasCycle(graph) {
2293
2318
  }
2294
2319
  for (const [node, targets] of graph) {
2295
2320
  if (dfs2(node)) {
2296
- return { hasCycle: true, cycleNodes: [...stack] };
2321
+ return { hasCycle: true, cycleNodes };
2297
2322
  }
2298
2323
  }
2299
2324
  return { hasCycle: false, cycleNodes: [] };