vue-hook-optimizer 0.0.11 → 0.0.13

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
@@ -86267,11 +86267,11 @@ var import_traverse2 = __toESM(require_lib13());
86267
86267
  // src/analyze/utils.ts
86268
86268
  var NodeCollection = class {
86269
86269
  nodes = /* @__PURE__ */ new Map();
86270
- addNode(label, node2, isComputed = false) {
86270
+ addNode(label, node2, options = { isComputed: false }) {
86271
86271
  if (this.nodes.has(label)) {
86272
86272
  return;
86273
86273
  }
86274
- if (!isComputed && (node2.type === "VariableDeclarator" && [
86274
+ if (!options.isComputed && (node2.type === "VariableDeclarator" && [
86275
86275
  "ArrowFunctionExpression",
86276
86276
  "FunctionDeclaration"
86277
86277
  ].includes(node2.init?.type || "") || node2.type === "FunctionDeclaration" || node2.type === "ObjectMethod")) {
@@ -86286,20 +86286,20 @@ var NodeCollection = class {
86286
86286
  });
86287
86287
  }
86288
86288
  }
86289
- addTypedNode(label, type) {
86289
+ addTypedNode(label, node2) {
86290
86290
  this.nodes.set(label, {
86291
86291
  label,
86292
- type
86292
+ type: node2.type
86293
86293
  });
86294
86294
  }
86295
86295
  map(graph) {
86296
86296
  const nodes = new Set(Array.from(graph.nodes).map((node2) => {
86297
86297
  return this.nodes.get(node2);
86298
- }));
86298
+ }).filter((node2) => !!node2));
86299
86299
  const edges = new Map(Array.from(graph.edges).map(([from2, to]) => {
86300
86300
  return [this.nodes.get(from2), new Set(Array.from(to).map((node2) => {
86301
86301
  return this.nodes.get(node2);
86302
- }))];
86302
+ }).filter((node2) => !!node2))];
86303
86303
  }));
86304
86304
  return {
86305
86305
  nodes,
@@ -86313,11 +86313,13 @@ var traverse2 = (
86313
86313
  //@ts-ignore
86314
86314
  import_traverse2.default.default?.default || import_traverse2.default.default || import_traverse2.default
86315
86315
  );
86316
- function processSetup(ast, parentScope, parentPath) {
86316
+ function processSetup(ast, parentScope, parentPath, _spread) {
86317
+ const spread = _spread || [];
86317
86318
  const nodeCollection = new NodeCollection();
86318
86319
  const graph = {
86319
86320
  nodes: /* @__PURE__ */ new Set(),
86320
- edges: /* @__PURE__ */ new Map()
86321
+ edges: /* @__PURE__ */ new Map(),
86322
+ spread: /* @__PURE__ */ new Map()
86321
86323
  };
86322
86324
  traverse2(ast, {
86323
86325
  VariableDeclaration(path2) {
@@ -86361,6 +86363,49 @@ function processSetup(ast, parentScope, parentPath) {
86361
86363
  if (!graph.edges.get(name)) {
86362
86364
  graph.edges.set(name, /* @__PURE__ */ new Set());
86363
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
+ }
86364
86409
  }
86365
86410
  }
86366
86411
  });
@@ -86389,6 +86434,14 @@ function processSetup(ast, parentScope, parentPath) {
86389
86434
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86390
86435
  graph.edges.get(name)?.add(path1.node.name);
86391
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
+ }
86392
86445
  }
86393
86446
  });
86394
86447
  }
@@ -86406,6 +86459,14 @@ function processSetup(ast, parentScope, parentPath) {
86406
86459
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86407
86460
  graph.edges.get(name)?.add(path1.node.name);
86408
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
+ }
86409
86470
  }
86410
86471
  }, path2.scope, path2);
86411
86472
  }
@@ -86423,6 +86484,14 @@ function processSetup(ast, parentScope, parentPath) {
86423
86484
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86424
86485
  graph.edges.get(name)?.add(path1.node.name);
86425
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
+ }
86426
86495
  }
86427
86496
  }, path2.scope, path2);
86428
86497
  }
@@ -86442,11 +86511,38 @@ function processSetup(ast, parentScope, parentPath) {
86442
86511
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86443
86512
  graph.edges.get(name)?.add(path1.node.name);
86444
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
+ }
86445
86522
  }
86446
86523
  }, path2.scope, path2);
86447
86524
  }
86448
86525
  }
86449
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
+ }
86450
86546
  }
86451
86547
  }, parentScope, parentPath);
86452
86548
  return {
@@ -86495,7 +86591,9 @@ function analyze3(content) {
86495
86591
  if (prop.key.type === "Identifier") {
86496
86592
  const name = prop.key.name;
86497
86593
  graph.nodes.add(name);
86498
- nodeCollection.addNode(name, prop, true);
86594
+ nodeCollection.addNode(name, prop, {
86595
+ isComputed: true
86596
+ });
86499
86597
  if (!graph.edges.get(name)) {
86500
86598
  graph.edges.set(name, /* @__PURE__ */ new Set());
86501
86599
  }
@@ -86527,13 +86625,31 @@ function analyze3(content) {
86527
86625
  if (path2.node.declaration.type === "ObjectExpression" && path1.parent === path2.node.declaration || path2.node.declaration.type === "CallExpression" && path1.parent === path2.node.declaration.arguments[0]) {
86528
86626
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
86529
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);
86530
86645
  const {
86531
86646
  graph: {
86532
86647
  nodes: tempNodes,
86533
- edges: tempEdges
86648
+ edges: tempEdges,
86649
+ spread: tempSpread
86534
86650
  },
86535
86651
  nodeCollection: tempNodeCollection
86536
- } = processSetup(setupNode, path1.scope, setupNode);
86652
+ } = processSetup(setupNode, path1.scope, setupNode, spread);
86537
86653
  traverse3(setupNode, {
86538
86654
  ReturnStatement(path22) {
86539
86655
  if (path22.node.argument?.type === "ObjectExpression") {
@@ -86546,7 +86662,7 @@ function analyze3(content) {
86546
86662
  const valName = path3.node.value.type === "Identifier" ? path3.node.value.name : "";
86547
86663
  if (valName && tempNodes.has(valName)) {
86548
86664
  graph.nodes.add(name);
86549
- nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName).type);
86665
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName));
86550
86666
  if (!graph.edges.get(name)) {
86551
86667
  graph.edges.set(name, /* @__PURE__ */ new Set());
86552
86668
  tempEdges.get(valName)?.forEach((edge) => {
@@ -86556,6 +86672,31 @@ function analyze3(content) {
86556
86672
  }
86557
86673
  }
86558
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
+ }
86559
86700
  }
86560
86701
  }, path22.scope, path22);
86561
86702
  } else {