vue-hook-optimizer 0.0.70 → 0.0.72-beta.1

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
@@ -77,9 +77,19 @@ var NodeCollection = class {
77
77
  return this.nodes.get(node);
78
78
  }).filter((node) => !!node));
79
79
  const edges = new Map(Array.from(graph.edges).map(([from, to]) => {
80
- return [this.nodes.get(from), new Set(Array.from(to).map((node) => {
81
- return this.nodes.get(node);
82
- }).filter((node) => !!node))];
80
+ const labelMap = /* @__PURE__ */ new Map();
81
+ for (const item of to) {
82
+ const node = this.nodes.get(item.label);
83
+ if (!node) {
84
+ continue;
85
+ }
86
+ const existing = labelMap.get(item.label);
87
+ if (!existing || existing.type === "get" && item.type === "set") {
88
+ labelMap.set(item.label, { node, type: item.type });
89
+ }
90
+ }
91
+ const items = Array.from(labelMap.values());
92
+ return [this.nodes.get(from), new Set(items)];
83
93
  }));
84
94
  return {
85
95
  nodes,
@@ -112,6 +122,23 @@ function getComment(node) {
112
122
  });
113
123
  return comment.trim();
114
124
  }
125
+ function isWritingNode(path) {
126
+ const assignParent = path.findParent((p) => p.isAssignmentExpression());
127
+ if (assignParent) {
128
+ const leftNode = assignParent.node.left;
129
+ if (leftNode.start != null && path.node.start >= leftNode.start && path.node.end <= leftNode.end) {
130
+ return true;
131
+ }
132
+ }
133
+ const updateParent = path.findParent((p) => p.isUpdateExpression());
134
+ if (updateParent) {
135
+ const argNode = updateParent.node.argument;
136
+ if (argNode.start != null && path.node.start >= argNode.start && path.node.end <= argNode.end) {
137
+ return true;
138
+ }
139
+ }
140
+ return false;
141
+ }
115
142
 
116
143
  // src/analyze/setupScript.ts
117
144
  var traverse = _traverse.default?.default || _traverse.default || _traverse;
@@ -363,7 +390,10 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
363
390
  const watchArgsNames = Array.from(watchArgs).map((arg) => arg.name);
364
391
  watchArgs.forEach((watchArg) => {
365
392
  if (!watchArgsNames.includes(path1.node.name)) {
366
- graph.edges.get(watchArg.name)?.add(path1.node.name);
393
+ graph.edges.get(watchArg.name)?.add({
394
+ label: path1.node.name,
395
+ type: isWritingNode(path1) ? "set" : "get"
396
+ });
367
397
  }
368
398
  });
369
399
  }
@@ -391,14 +421,20 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
391
421
  Identifier(path1) {
392
422
  const binding = path1.scope.getBinding(path1.node.name);
393
423
  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)) {
394
- graph.edges.get(name)?.add(path1.node.name);
424
+ graph.edges.get(name)?.add({
425
+ label: path1.node.name,
426
+ type: isWritingNode(path1) ? "set" : "get"
427
+ });
395
428
  }
396
429
  },
397
430
  MemberExpression(path1) {
398
431
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
399
432
  const binding = path1.scope.getBinding(path1.node.object.name);
400
433
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
401
- graph.edges.get(name)?.add(path1.node.property.name);
434
+ graph.edges.get(name)?.add({
435
+ label: path1.node.property.name,
436
+ type: isWritingNode(path1) ? "set" : "get"
437
+ });
402
438
  }
403
439
  }
404
440
  }
@@ -416,14 +452,20 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
416
452
  Identifier(path1) {
417
453
  const binding = path1.scope.getBinding(path1.node.name);
418
454
  if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
419
- graph.edges.get(name)?.add(path1.node.name);
455
+ graph.edges.get(name)?.add({
456
+ label: path1.node.name,
457
+ type: isWritingNode(path1) ? "set" : "get"
458
+ });
420
459
  }
421
460
  },
422
461
  MemberExpression(path1) {
423
462
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
424
463
  const binding = path1.scope.getBinding(path1.node.object.name);
425
464
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
426
- graph.edges.get(name)?.add(path1.node.property.name);
465
+ graph.edges.get(name)?.add({
466
+ label: path1.node.property.name,
467
+ type: isWritingNode(path1) ? "set" : "get"
468
+ });
427
469
  }
428
470
  }
429
471
  }
@@ -440,14 +482,20 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
440
482
  Identifier(path1) {
441
483
  const binding = path1.scope.getBinding(path1.node.name);
442
484
  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)) {
443
- graph.edges.get(name)?.add(path1.node.name);
485
+ graph.edges.get(name)?.add({
486
+ label: path1.node.name,
487
+ type: isWritingNode(path1) ? "set" : "get"
488
+ });
444
489
  }
445
490
  },
446
491
  MemberExpression(path1) {
447
492
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
448
493
  const binding = path1.scope.getBinding(path1.node.object.name);
449
494
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
450
- graph.edges.get(name)?.add(path1.node.property.name);
495
+ graph.edges.get(name)?.add({
496
+ label: path1.node.property.name,
497
+ type: isWritingNode(path1) ? "set" : "get"
498
+ });
451
499
  }
452
500
  }
453
501
  }
@@ -469,14 +517,20 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
469
517
  Identifier(path1) {
470
518
  const binding = path1.scope.getBinding(path1.node.name);
471
519
  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)) {
472
- graph.edges.get(name)?.add(path1.node.name);
520
+ graph.edges.get(name)?.add({
521
+ label: path1.node.name,
522
+ type: isWritingNode(path1) ? "set" : "get"
523
+ });
473
524
  }
474
525
  },
475
526
  MemberExpression(path1) {
476
527
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
477
528
  const binding = path1.scope.getBinding(path1.node.object.name);
478
529
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
479
- graph.edges.get(name)?.add(path1.node.property.name);
530
+ graph.edges.get(name)?.add({
531
+ label: path1.node.property.name,
532
+ type: isWritingNode(path1) ? "set" : "get"
533
+ });
480
534
  }
481
535
  }
482
536
  }
@@ -487,14 +541,20 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
487
541
  if (path.node.init.type === "Identifier") {
488
542
  const binding = path.scope.getBinding(path.node.init.name);
489
543
  if (graph.nodes.has(path.node.init.name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
490
- graph.edges.get(name)?.add(path.node.init.name);
544
+ graph.edges.get(name)?.add({
545
+ label: path.node.init.name,
546
+ type: isWritingNode(path) ? "set" : "get"
547
+ });
491
548
  }
492
549
  } else {
493
550
  traverse(path.node.init, {
494
551
  Identifier(path1) {
495
552
  const binding = path1.scope.getBinding(path1.node.name);
496
553
  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)) {
497
- graph.edges.get(name)?.add(path1.node.name);
554
+ graph.edges.get(name)?.add({
555
+ label: path1.node.name,
556
+ type: isWritingNode(path1) ? "set" : "get"
557
+ });
498
558
  }
499
559
  }
500
560
  }, path.scope, path);
@@ -509,14 +569,20 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
509
569
  Identifier(path1) {
510
570
  const binding = path1.scope.getBinding(path1.node.name);
511
571
  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)) {
512
- graph.edges.get(name)?.add(path1.node.name);
572
+ graph.edges.get(name)?.add({
573
+ label: path1.node.name,
574
+ type: isWritingNode(path1) ? "set" : "get"
575
+ });
513
576
  }
514
577
  },
515
578
  MemberExpression(path1) {
516
579
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
517
580
  const binding = path1.scope.getBinding(path1.node.object.name);
518
581
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
519
- graph.edges.get(name)?.add(path1.node.property.name);
582
+ graph.edges.get(name)?.add({
583
+ label: path1.node.property.name,
584
+ type: isWritingNode(path1) ? "set" : "get"
585
+ });
520
586
  }
521
587
  }
522
588
  }
@@ -531,7 +597,10 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
531
597
  if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
532
598
  const binding = path1.scope.getBinding(path1.node.object.name);
533
599
  if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
534
- graph.edges.get(name)?.add(path1.node.property.name);
600
+ graph.edges.get(name)?.add({
601
+ label: path1.node.property.name,
602
+ type: isWritingNode(path1) ? "set" : "get"
603
+ });
535
604
  }
536
605
  }
537
606
  }
@@ -772,7 +841,10 @@ function analyze2(content, lineOffset = 0, jsx = false) {
772
841
  nodeCollection.addNode(name, path3.node.key, {
773
842
  comment: getComment(path3.node)
774
843
  });
775
- graph.edges.set(name, /* @__PURE__ */ new Set([valName]));
844
+ graph.edges.set(name, /* @__PURE__ */ new Set([{
845
+ label: valName,
846
+ type: isWritingNode(path3) ? "set" : "get"
847
+ }]));
776
848
  }
777
849
  }
778
850
  }
@@ -892,7 +964,10 @@ function analyze2(content, lineOffset = 0, jsx = false) {
892
964
  traverse2(prop, {
893
965
  MemberExpression(path2) {
894
966
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
895
- graph.edges.get(name)?.add(path2.node.property.name);
967
+ graph.edges.get(name)?.add({
968
+ label: path2.node.property.name,
969
+ type: isWritingNode(path2) ? "set" : "get"
970
+ });
896
971
  }
897
972
  }
898
973
  }, path1.scope, path1);
@@ -904,7 +979,10 @@ function analyze2(content, lineOffset = 0, jsx = false) {
904
979
  traverse2(prop1, {
905
980
  MemberExpression(path2) {
906
981
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
907
- graph.edges.get(name)?.add(path2.node.property.name);
982
+ graph.edges.get(name)?.add({
983
+ label: path2.node.property.name,
984
+ type: isWritingNode(path2) ? "set" : "get"
985
+ });
908
986
  }
909
987
  }
910
988
  }, path1.scope, path1);
@@ -923,7 +1001,10 @@ function analyze2(content, lineOffset = 0, jsx = false) {
923
1001
  traverse2(prop, {
924
1002
  MemberExpression(path2) {
925
1003
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
926
- graph.edges.get(name)?.add(path2.node.property.name);
1004
+ graph.edges.get(name)?.add({
1005
+ label: path2.node.property.name,
1006
+ type: isWritingNode(path2) ? "set" : "get"
1007
+ });
927
1008
  }
928
1009
  }
929
1010
  }, path1.scope, path1);
@@ -951,7 +1032,10 @@ function analyze2(content, lineOffset = 0, jsx = false) {
951
1032
  MemberExpression(path2) {
952
1033
  if (path2.node.object.type === "ThisExpression" && path2.node.property.type === "Identifier") {
953
1034
  if (watchArg && watchArg.name !== path2.node.property.name) {
954
- graph.edges.get(watchArg.name)?.add(path2.node.property.name);
1035
+ graph.edges.get(watchArg.name)?.add({
1036
+ label: path2.node.property.name,
1037
+ type: isWritingNode(path2) ? "set" : "get"
1038
+ });
955
1039
  }
956
1040
  }
957
1041
  }
@@ -1509,7 +1593,10 @@ function addIdentifiesToGraphByScanReturn({ path: path1, graph, nodeCollection,
1509
1593
  nodeCollection.addNode(name, path3.node.key, {
1510
1594
  comment: getComment(path3.node)
1511
1595
  });
1512
- graph.edges.set(name, /* @__PURE__ */ new Set([valName]));
1596
+ graph.edges.set(name, /* @__PURE__ */ new Set([{
1597
+ label: valName,
1598
+ type: isWritingNode(path3) ? "set" : "get"
1599
+ }]));
1513
1600
  }
1514
1601
  }
1515
1602
  }
@@ -1668,7 +1755,10 @@ function processByReturnJSX(params) {
1668
1755
  function addEdge({ fromName, toName, path, scope, toScope, collectionNodes }) {
1669
1756
  const bindingScope = toScope || path.scope.getBinding(toName)?.scope;
1670
1757
  if (scope === bindingScope && collectionNodes.has(toName)) {
1671
- graph.edges.get(fromName)?.add(toName);
1758
+ graph.edges.get(fromName)?.add({
1759
+ label: toName,
1760
+ type: isWritingNode(path) ? "set" : "get"
1761
+ });
1672
1762
  }
1673
1763
  }
1674
1764
  function addUsed({ name, path, parentPath: parentPath2 }) {
@@ -1787,7 +1877,10 @@ function processByReact(params) {
1787
1877
  function addEdge({ fromName, toName, path, scope, toScope, collectionNodes }) {
1788
1878
  const bindingScope = toScope || path.scope.getBinding(toName)?.scope;
1789
1879
  if (scope === bindingScope && collectionNodes.has(toName)) {
1790
- graph.edges.get(fromName)?.add(toName);
1880
+ graph.edges.get(fromName)?.add({
1881
+ label: toName,
1882
+ type: isWritingNode(path) ? "set" : "get"
1883
+ });
1791
1884
  }
1792
1885
  }
1793
1886
  function addUsed({ name, path, parentPath: parentPath2 }) {
@@ -1994,7 +2087,7 @@ function noIndegreeFilter(graph) {
1994
2087
  });
1995
2088
  graph.forEach((targets, node) => {
1996
2089
  targets.forEach((target) => {
1997
- indegree.set(target, (indegree.get(target) || 0) + 1);
2090
+ indegree.set(target.node, (indegree.get(target.node) || 0) + 1);
1998
2091
  });
1999
2092
  });
2000
2093
  return nodes.filter((node) => indegree.get(node) === 0);
@@ -2008,8 +2101,8 @@ function findLinearPaths(graph) {
2008
2101
  nodeInDegrees.set(node, 0);
2009
2102
  }
2010
2103
  for (const edge of edges) {
2011
- const inDegree = nodeInDegrees.get(edge) || 0;
2012
- nodeInDegrees.set(edge, inDegree + 1);
2104
+ const inDegree = nodeInDegrees.get(edge.node) || 0;
2105
+ nodeInDegrees.set(edge.node, inDegree + 1);
2013
2106
  }
2014
2107
  }
2015
2108
  function dfs2(node, path) {
@@ -2024,7 +2117,7 @@ function findLinearPaths(graph) {
2024
2117
  addOrUpdatePath([...path]);
2025
2118
  }
2026
2119
  } else {
2027
- const nextNode = Array.from(edges)[0];
2120
+ const nextNode = Array.from(edges)[0].node;
2028
2121
  const nextNodeInDegree = nodeInDegrees.get(nextNode) || 0;
2029
2122
  if (nextNodeInDegree === 1) {
2030
2123
  dfs2(nextNode, path);
@@ -2086,19 +2179,19 @@ function findArticulationPoints(graph) {
2086
2179
  time++;
2087
2180
  visited.add(node);
2088
2181
  for (const neighbor of graph2.get(node) || []) {
2089
- if (!visited.has(neighbor)) {
2182
+ if (!visited.has(neighbor.node)) {
2090
2183
  children++;
2091
- parent.set(neighbor, node);
2092
- APUtil(graph2, neighbor);
2093
- low.set(node, Math.min(low.get(node), low.get(neighbor)));
2184
+ parent.set(neighbor.node, node);
2185
+ APUtil(graph2, neighbor.node);
2186
+ low.set(node, Math.min(low.get(node), low.get(neighbor.node)));
2094
2187
  if (parent.get(node) === null && children > 1) {
2095
2188
  ap.add(node);
2096
2189
  }
2097
- if (parent.get(node) !== null && low.get(neighbor) >= disc.get(node)) {
2190
+ if (parent.get(node) !== null && low.get(neighbor.node) >= disc.get(node)) {
2098
2191
  ap.add(node);
2099
2192
  }
2100
- } else if (neighbor !== parent.get(node)) {
2101
- low.set(node, Math.min(low.get(node), disc.get(neighbor)));
2193
+ } else if (neighbor.node !== parent.get(node)) {
2194
+ low.set(node, Math.min(low.get(node), disc.get(neighbor.node)));
2102
2195
  }
2103
2196
  }
2104
2197
  }
@@ -2115,8 +2208,8 @@ function dfs(graph, node, targets, visited, component) {
2115
2208
  component.add(node);
2116
2209
  visited.add(node);
2117
2210
  targets.forEach((target) => {
2118
- if (!visited.has(target)) {
2119
- dfs(graph, target, graph.get(target) || /* @__PURE__ */ new Set(), visited, component);
2211
+ if (!visited.has(target.node)) {
2212
+ dfs(graph, target.node, graph.get(target.node) || /* @__PURE__ */ new Set(), visited, component);
2120
2213
  }
2121
2214
  });
2122
2215
  }
@@ -2174,10 +2267,15 @@ function hasCycle(graph) {
2174
2267
  function dfs2(node) {
2175
2268
  if (visited.has(node)) {
2176
2269
  if (onStack.has(node)) {
2177
- while (stack[0] !== node) {
2178
- onStack.delete(stack.shift());
2179
- }
2180
- return true;
2270
+ const idx = stack.indexOf(node);
2271
+ const cycle = stack.slice(idx);
2272
+ const allSet = cycle.every((curr, i) => {
2273
+ const next = cycle[(i + 1) % cycle.length];
2274
+ return Array.from(graph.get(curr) || []).some(
2275
+ (edge) => edge.node === next && edge.type === "set"
2276
+ );
2277
+ });
2278
+ return allSet;
2181
2279
  }
2182
2280
  return false;
2183
2281
  }
@@ -2185,7 +2283,7 @@ function hasCycle(graph) {
2185
2283
  onStack.add(node);
2186
2284
  stack.push(node);
2187
2285
  for (const neighbor of graph.get(node) || /* @__PURE__ */ new Set()) {
2188
- if (dfs2(neighbor)) {
2286
+ if (dfs2(neighbor.node)) {
2189
2287
  return true;
2190
2288
  }
2191
2289
  }
@@ -2294,7 +2392,7 @@ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__
2294
2392
  graph.edges.forEach((edge) => {
2295
2393
  edge.forEach((to) => {
2296
2394
  if (to) {
2297
- inDegreeMap[to.label] = (inDegreeMap[to.label] || 0) + 1;
2395
+ inDegreeMap[to.node.label] = (inDegreeMap[to.node.label] || 0) + 1;
2298
2396
  }
2299
2397
  });
2300
2398
  });
@@ -2328,7 +2426,7 @@ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__
2328
2426
  }
2329
2427
  edges.push({
2330
2428
  from: key.label,
2331
- to: to.label,
2429
+ to: to.node.label,
2332
2430
  arrows: {
2333
2431
  to: {
2334
2432
  enabled: true,