vue-hook-optimizer 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -86263,14 +86263,63 @@ function analyze(content) {
86263
86263
 
86264
86264
  // src/analyze/setupScript.ts
86265
86265
  var import_traverse2 = __toESM(require_lib13());
86266
+
86267
+ // src/analyze/utils.ts
86268
+ var NodeCollection = class {
86269
+ nodes = /* @__PURE__ */ new Map();
86270
+ addNode(label, node2, options = { isComputed: false }) {
86271
+ if (this.nodes.has(label)) {
86272
+ return;
86273
+ }
86274
+ if (!options.isComputed && (node2.type === "VariableDeclarator" && [
86275
+ "ArrowFunctionExpression",
86276
+ "FunctionDeclaration"
86277
+ ].includes(node2.init?.type || "") || node2.type === "FunctionDeclaration" || node2.type === "ObjectMethod")) {
86278
+ this.nodes.set(label, {
86279
+ label,
86280
+ type: "fun" /* fun */
86281
+ });
86282
+ } else {
86283
+ this.nodes.set(label, {
86284
+ label,
86285
+ type: "var" /* var */
86286
+ });
86287
+ }
86288
+ }
86289
+ addTypedNode(label, node2) {
86290
+ this.nodes.set(label, {
86291
+ label,
86292
+ type: node2.type
86293
+ });
86294
+ }
86295
+ map(graph) {
86296
+ const nodes = new Set(Array.from(graph.nodes).map((node2) => {
86297
+ return this.nodes.get(node2);
86298
+ }).filter((node2) => !!node2));
86299
+ const edges = new Map(Array.from(graph.edges).map(([from2, to]) => {
86300
+ return [this.nodes.get(from2), new Set(Array.from(to).map((node2) => {
86301
+ return this.nodes.get(node2);
86302
+ }).filter((node2) => !!node2))];
86303
+ }));
86304
+ return {
86305
+ nodes,
86306
+ edges
86307
+ };
86308
+ }
86309
+ };
86310
+
86311
+ // src/analyze/setupScript.ts
86266
86312
  var traverse2 = (
86267
86313
  //@ts-ignore
86268
86314
  import_traverse2.default.default?.default || import_traverse2.default.default || import_traverse2.default
86269
86315
  );
86270
- function processSetup(ast, parentScope, parentPath) {
86316
+ function processSetup(ast, parentScope, parentPath, _spread) {
86317
+ const spread = _spread || [];
86318
+ const nodeCollection = new NodeCollection();
86271
86319
  const graph = {
86272
86320
  nodes: /* @__PURE__ */ new Set(),
86273
- edges: /* @__PURE__ */ new Map()
86321
+ edges: /* @__PURE__ */ new Map(),
86322
+ spread: /* @__PURE__ */ new Map()
86274
86323
  };
86275
86324
  traverse2(ast, {
86276
86325
  VariableDeclaration(path2) {
@@ -86282,6 +86331,7 @@ function processSetup(ast, parentScope, parentPath) {
86282
86331
  const binding2 = path2.scope.getBinding(name);
86283
86332
  if (binding2 && (path2.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path2.parent) && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86284
86333
  graph.nodes.add(name);
86334
+ nodeCollection.addNode(name, declaration2);
86285
86335
  if (!graph.edges.get(name)) {
86286
86336
  graph.edges.set(name, /* @__PURE__ */ new Set());
86287
86337
  }
@@ -86296,6 +86346,7 @@ function processSetup(ast, parentScope, parentPath) {
86296
86346
  const binding2 = path2.scope.getBinding(name);
86297
86347
  if (binding2 && (path2.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path2.parent) && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86298
86348
  graph.nodes.add(name);
86349
+ nodeCollection.addNode(name, declaration2);
86299
86350
  if (!graph.edges.get(name)) {
86300
86351
  graph.edges.set(name, /* @__PURE__ */ new Set());
86301
86352
  }
@@ -86308,9 +86359,53 @@ function processSetup(ast, parentScope, parentPath) {
86308
86359
  const binding2 = path2.scope.getBinding(name);
86309
86360
  if (binding2 && (path2.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path2.parent) && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86310
86361
  graph.nodes.add(name);
86362
+ nodeCollection.addNode(name, declaration2);
86311
86363
  if (!graph.edges.get(name)) {
86312
86364
  graph.edges.set(name, /* @__PURE__ */ new Set());
86313
86365
  }
86366
+ if (spread.includes(name)) {
86367
+ if (declaration2.init?.type === "ObjectExpression") {
86368
+ declaration2.init?.properties.forEach((prop) => {
86369
+ if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
86370
+ const keyName = prop.key.name;
86371
+ graph.nodes.add(keyName);
86372
+ nodeCollection.addNode(keyName, prop);
86373
+ if (!graph.edges.get(keyName)) {
86374
+ graph.edges.set(keyName, /* @__PURE__ */ new Set());
86375
+ }
86376
+ if (graph.spread.has(name)) {
86377
+ graph.spread.get(name)?.add(keyName);
86378
+ } else {
86379
+ graph.spread.set(name, /* @__PURE__ */ new Set([keyName]));
86380
+ }
86381
+ } else if (prop.type === "SpreadElement") {
86382
+ console.warn("not support spread in spread");
86383
+ }
86384
+ });
86385
+ }
86386
+ if (declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && declaration2.init?.callee.name === "reactive") {
86387
+ const arg = declaration2.init?.arguments[0];
86388
+ if (arg.type === "ObjectExpression") {
86389
+ arg.properties.forEach((prop) => {
86390
+ if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
86391
+ const keyName = prop.key.name;
86392
+ graph.nodes.add(keyName);
86393
+ nodeCollection.addNode(keyName, prop);
86394
+ if (!graph.edges.get(keyName)) {
86395
+ graph.edges.set(keyName, /* @__PURE__ */ new Set());
86396
+ }
86397
+ if (graph.spread.has(name)) {
86398
+ graph.spread.get(name)?.add(keyName);
86399
+ } else {
86400
+ graph.spread.set(name, /* @__PURE__ */ new Set([keyName]));
86401
+ }
86402
+ } else if (prop.type === "SpreadElement") {
86403
+ console.warn("not support spread in spread");
86404
+ }
86405
+ });
86406
+ }
86407
+ }
86408
+ }
86314
86409
  }
86315
86410
  }
86316
86411
  });
@@ -86321,6 +86416,7 @@ function processSetup(ast, parentScope, parentPath) {
86321
86416
  const binding2 = path2.scope.getBinding(name);
86322
86417
  if (binding2 && (path2.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path2.parent)) {
86323
86418
  graph.nodes.add(name);
86419
+ nodeCollection.addNode(name, path2.node);
86324
86420
  if (!graph.edges.get(name)) {
86325
86421
  graph.edges.set(name, /* @__PURE__ */ new Set());
86326
86422
  }
@@ -86338,6 +86434,14 @@ function processSetup(ast, parentScope, parentPath) {
86338
86434
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86339
86435
  graph.edges.get(name)?.add(path1.node.name);
86340
86436
  }
86437
+ },
86438
+ MemberExpression(path1) {
86439
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86440
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86441
+ if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && path1.node.property.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86442
+ graph.edges.get(name)?.add(path1.node.property.name);
86443
+ }
86444
+ }
86341
86445
  }
86342
86446
  });
86343
86447
  }
@@ -86355,6 +86459,14 @@ function processSetup(ast, parentScope, parentPath) {
86355
86459
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86356
86460
  graph.edges.get(name)?.add(path1.node.name);
86357
86461
  }
86462
+ },
86463
+ MemberExpression(path1) {
86464
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86465
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86466
+ if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && path1.node.property.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86467
+ graph.edges.get(name)?.add(path1.node.property.name);
86468
+ }
86469
+ }
86358
86470
  }
86359
86471
  }, path2.scope, path2);
86360
86472
  }
@@ -86372,6 +86484,14 @@ function processSetup(ast, parentScope, parentPath) {
86372
86484
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86373
86485
  graph.edges.get(name)?.add(path1.node.name);
86374
86486
  }
86487
+ },
86488
+ MemberExpression(path1) {
86489
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86490
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86491
+ if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && path1.node.property.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86492
+ graph.edges.get(name)?.add(path1.node.property.name);
86493
+ }
86494
+ }
86375
86495
  }
86376
86496
  }, path2.scope, path2);
86377
86497
  }
@@ -86391,14 +86511,44 @@ function processSetup(ast, parentScope, parentPath) {
86391
86511
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86392
86512
  graph.edges.get(name)?.add(path1.node.name);
86393
86513
  }
86514
+ },
86515
+ MemberExpression(path1) {
86516
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86517
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86518
+ if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && path1.node.property.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86519
+ graph.edges.get(name)?.add(path1.node.property.name);
86520
+ }
86521
+ }
86394
86522
  }
86395
86523
  }, path2.scope, path2);
86396
86524
  }
86397
86525
  }
86398
86526
  }
86527
+ },
86528
+ ObjectMethod(path2) {
86529
+ if (path2.node.key.type === "Identifier" && graph.nodes.has(path2.node.key.name)) {
86530
+ const name = path2.node.key.name;
86531
+ path2.traverse({
86532
+ Identifier(path1) {
86533
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name) {
86534
+ graph.edges.get(name)?.add(path1.node.name);
86535
+ }
86536
+ },
86537
+ MemberExpression(path1) {
86538
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86539
+ if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && path1.node.property.name !== name) {
86540
+ graph.edges.get(name)?.add(path1.node.property.name);
86541
+ }
86542
+ }
86543
+ }
86544
+ });
86545
+ }
86399
86546
  }
86400
86547
  }, parentScope, parentPath);
86401
- return graph;
86548
+ return {
86549
+ graph,
86550
+ nodeCollection
86551
+ };
86402
86552
  }
86403
86553
  function analyze2(content) {
86404
86554
  const ast = parse_1$1(content, {
@@ -86407,7 +86557,8 @@ function analyze2(content) {
86407
86557
  "typescript"
86408
86558
  ]
86409
86559
  });
86410
- return processSetup(ast);
86560
+ const { graph, nodeCollection } = processSetup(ast);
86561
+ return nodeCollection.map(graph);
86411
86562
  }
86412
86563
 
86413
86564
  // src/analyze/options.ts
@@ -86423,6 +86574,7 @@ function analyze3(content) {
86423
86574
  "typescript"
86424
86575
  ]
86425
86576
  });
86577
+ const nodeCollection = new NodeCollection();
86426
86578
  const graph = {
86427
86579
  nodes: /* @__PURE__ */ new Set(),
86428
86580
  edges: /* @__PURE__ */ new Map()
@@ -86439,6 +86591,9 @@ function analyze3(content) {
86439
86591
  if (prop.key.type === "Identifier") {
86440
86592
  const name = prop.key.name;
86441
86593
  graph.nodes.add(name);
86594
+ nodeCollection.addNode(name, prop, {
86595
+ isComputed: true
86596
+ });
86442
86597
  if (!graph.edges.get(name)) {
86443
86598
  graph.edges.set(name, /* @__PURE__ */ new Set());
86444
86599
  }
@@ -86455,6 +86610,7 @@ function analyze3(content) {
86455
86610
  if (prop.key.type === "Identifier") {
86456
86611
  const name = prop.key.name;
86457
86612
  graph.nodes.add(name);
86613
+ nodeCollection.addNode(name, prop);
86458
86614
  if (!graph.edges.get(name)) {
86459
86615
  graph.edges.set(name, /* @__PURE__ */ new Set());
86460
86616
  }
@@ -86469,10 +86625,31 @@ function analyze3(content) {
86469
86625
  if (path2.node.declaration.type === "ObjectExpression" && path1.parent === path2.node.declaration || path2.node.declaration.type === "CallExpression" && path1.parent === path2.node.declaration.arguments[0]) {
86470
86626
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
86471
86627
  const setupNode = path1.node;
86628
+ const spread = [];
86629
+ traverse3(setupNode, {
86630
+ ReturnStatement(path22) {
86631
+ if (path22.node.argument?.type === "ObjectExpression") {
86632
+ const returnNode = path22.node.argument;
86633
+ traverse3(returnNode, {
86634
+ SpreadElement(path3) {
86635
+ 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") {
86636
+ spread.push(path3.node.argument.arguments[0].name);
86637
+ } else if (path3.node.argument.type === "Identifier") {
86638
+ spread.push(path3.node.argument.name);
86639
+ }
86640
+ }
86641
+ }, path22.scope, path22);
86642
+ }
86643
+ }
86644
+ }, path1.scope, path1);
86472
86645
  const {
86473
- nodes: tempNodes,
86474
- edges: tempEdges
86475
- } = processSetup(setupNode, path1.scope, setupNode);
86646
+ graph: {
86647
+ nodes: tempNodes,
86648
+ edges: tempEdges,
86649
+ spread: tempSpread
86650
+ },
86651
+ nodeCollection: tempNodeCollection
86652
+ } = processSetup(setupNode, path1.scope, setupNode, spread);
86476
86653
  traverse3(setupNode, {
86477
86654
  ReturnStatement(path22) {
86478
86655
  if (path22.node.argument?.type === "ObjectExpression") {
@@ -86485,6 +86662,7 @@ function analyze3(content) {
86485
86662
  const valName = path3.node.value.type === "Identifier" ? path3.node.value.name : "";
86486
86663
  if (valName && tempNodes.has(valName)) {
86487
86664
  graph.nodes.add(name);
86665
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName));
86488
86666
  if (!graph.edges.get(name)) {
86489
86667
  graph.edges.set(name, /* @__PURE__ */ new Set());
86490
86668
  tempEdges.get(valName)?.forEach((edge) => {
@@ -86494,6 +86672,31 @@ function analyze3(content) {
86494
86672
  }
86495
86673
  }
86496
86674
  }
86675
+ },
86676
+ SpreadElement(path3) {
86677
+ 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" && tempSpread.get(path3.node.argument.arguments[0].name)) {
86678
+ tempSpread.get(path3.node.argument.arguments[0].name)?.forEach((name) => {
86679
+ graph.nodes.add(name);
86680
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
86681
+ if (!graph.edges.get(name)) {
86682
+ graph.edges.set(name, /* @__PURE__ */ new Set());
86683
+ tempEdges.get(name)?.forEach((edge) => {
86684
+ graph.edges.get(name)?.add(edge);
86685
+ });
86686
+ }
86687
+ });
86688
+ } else if (path3.node.argument.type === "Identifier" && tempSpread.get(path3.node.argument.name)) {
86689
+ tempSpread.get(path3.node.argument.name)?.forEach((name) => {
86690
+ graph.nodes.add(name);
86691
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
86692
+ if (!graph.edges.get(name)) {
86693
+ graph.edges.set(name, /* @__PURE__ */ new Set());
86694
+ tempEdges.get(name)?.forEach((edge) => {
86695
+ graph.edges.get(name)?.add(edge);
86696
+ });
86697
+ }
86698
+ });
86699
+ }
86497
86700
  }
86498
86701
  }, path22.scope, path22);
86499
86702
  } else {
@@ -86513,6 +86716,7 @@ function analyze3(content) {
86513
86716
  if (prop.key.type === "Identifier") {
86514
86717
  const name = prop.key.name;
86515
86718
  graph.nodes.add(name);
86719
+ nodeCollection.addNode(name, prop);
86516
86720
  if (!graph.edges.get(name)) {
86517
86721
  graph.edges.set(name, /* @__PURE__ */ new Set());
86518
86722
  }
@@ -86591,7 +86795,7 @@ function analyze3(content) {
86591
86795
  }
86592
86796
  }
86593
86797
  });
86594
- return graph;
86798
+ return nodeCollection.map(graph);
86595
86799
  }
86596
86800
 
86597
86801
  // src/vis.ts
@@ -86600,16 +86804,19 @@ function getVisData(graph, usedNodes) {
86600
86804
  const edges = [];
86601
86805
  graph.nodes.forEach((node2) => {
86602
86806
  nodes.push({
86603
- id: node2,
86604
- label: node2,
86605
- group: usedNodes.has(node2) ? "used" : "normal"
86807
+ id: node2.label,
86808
+ label: node2.label,
86809
+ shape: node2.type === "var" ? "dot" : "diamond",
86810
+ group: usedNodes.has(node2.label) ? "used" : "normal"
86606
86811
  });
86607
86812
  });
86608
86813
  graph.edges.forEach((edge, key) => {
86609
86814
  edge.forEach((to) => {
86815
+ if (!to)
86816
+ return;
86610
86817
  edges.push({
86611
- from: key,
86612
- to,
86818
+ from: key.label,
86819
+ to: to.label,
86613
86820
  arrows: {
86614
86821
  to: {
86615
86822
  enabled: true,