vue-hook-optimizer 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  [![NPM version](https://img.shields.io/npm/v/vue-hook-optimizer?color=a1b858&label=)](https://www.npmjs.com/package/vue-hook-optimizer)
2
+ <a href="https://marketplace.visualstudio.com/items?itemName=zcf0508.vue-hook-optimizer-ext" target="__blank"><img src="https://img.shields.io/visual-studio-marketplace/v/zcf0508.vue-hook-optimizer-ext.svg?color=eee&amp;label=VS%20Code%20Marketplace&logo=visual-studio-code" alt="Visual Studio Marketplace Version" /></a>
2
3
 
3
4
  [中文文档](./README_cn.md)
4
5
 
5
- This is a tool to analyze your `vue` code.
6
+ This is a tool to analyze your `vue` code. Viste [hook.huali.cafe](https://hook.huali.cafe) or try the vscode extension [vue-hook-optimizer-ext](https://marketplace.visualstudio.com/items?itemName=zcf0508.vue-hook-optimizer-ext).
6
7
 
7
8
  ## Install And Run Playground
8
9
 
package/dist/index.js CHANGED
@@ -86277,11 +86277,11 @@ var import_traverse2 = __toESM(require_lib13());
86277
86277
  // src/analyze/utils.ts
86278
86278
  var NodeCollection = class {
86279
86279
  nodes = /* @__PURE__ */ new Map();
86280
- addNode(label, node2, isComputed = false) {
86280
+ addNode(label, node2, options = { isComputed: false }) {
86281
86281
  if (this.nodes.has(label)) {
86282
86282
  return;
86283
86283
  }
86284
- if (!isComputed && (node2.type === "VariableDeclarator" && [
86284
+ if (!options.isComputed && (node2.type === "VariableDeclarator" && [
86285
86285
  "ArrowFunctionExpression",
86286
86286
  "FunctionDeclaration"
86287
86287
  ].includes(node2.init?.type || "") || node2.type === "FunctionDeclaration" || node2.type === "ObjectMethod")) {
@@ -86296,20 +86296,20 @@ var NodeCollection = class {
86296
86296
  });
86297
86297
  }
86298
86298
  }
86299
- addTypedNode(label, type) {
86299
+ addTypedNode(label, node2) {
86300
86300
  this.nodes.set(label, {
86301
86301
  label,
86302
- type
86302
+ type: node2.type
86303
86303
  });
86304
86304
  }
86305
86305
  map(graph) {
86306
86306
  const nodes = new Set(Array.from(graph.nodes).map((node2) => {
86307
86307
  return this.nodes.get(node2);
86308
- }));
86308
+ }).filter((node2) => !!node2));
86309
86309
  const edges = new Map(Array.from(graph.edges).map(([from2, to]) => {
86310
86310
  return [this.nodes.get(from2), new Set(Array.from(to).map((node2) => {
86311
86311
  return this.nodes.get(node2);
86312
- }))];
86312
+ }).filter((node2) => !!node2))];
86313
86313
  }));
86314
86314
  return {
86315
86315
  nodes,
@@ -86323,11 +86323,13 @@ var traverse2 = (
86323
86323
  //@ts-ignore
86324
86324
  import_traverse2.default.default?.default || import_traverse2.default.default || import_traverse2.default
86325
86325
  );
86326
- function processSetup(ast, parentScope, parentPath) {
86326
+ function processSetup(ast, parentScope, parentPath, _spread) {
86327
+ const spread = _spread || [];
86327
86328
  const nodeCollection = new NodeCollection();
86328
86329
  const graph = {
86329
86330
  nodes: /* @__PURE__ */ new Set(),
86330
- edges: /* @__PURE__ */ new Map()
86331
+ edges: /* @__PURE__ */ new Map(),
86332
+ spread: /* @__PURE__ */ new Map()
86331
86333
  };
86332
86334
  traverse2(ast, {
86333
86335
  VariableDeclaration(path2) {
@@ -86371,6 +86373,49 @@ function processSetup(ast, parentScope, parentPath) {
86371
86373
  if (!graph.edges.get(name)) {
86372
86374
  graph.edges.set(name, /* @__PURE__ */ new Set());
86373
86375
  }
86376
+ if (spread.includes(name)) {
86377
+ if (declaration2.init?.type === "ObjectExpression") {
86378
+ declaration2.init?.properties.forEach((prop) => {
86379
+ if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
86380
+ const keyName = prop.key.name;
86381
+ graph.nodes.add(keyName);
86382
+ nodeCollection.addNode(keyName, prop);
86383
+ if (!graph.edges.get(keyName)) {
86384
+ graph.edges.set(keyName, /* @__PURE__ */ new Set());
86385
+ }
86386
+ if (graph.spread.has(name)) {
86387
+ graph.spread.get(name)?.add(keyName);
86388
+ } else {
86389
+ graph.spread.set(name, /* @__PURE__ */ new Set([keyName]));
86390
+ }
86391
+ } else if (prop.type === "SpreadElement") {
86392
+ console.warn("not support spread in spread");
86393
+ }
86394
+ });
86395
+ }
86396
+ if (declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && declaration2.init?.callee.name === "reactive") {
86397
+ const arg = declaration2.init?.arguments[0];
86398
+ if (arg.type === "ObjectExpression") {
86399
+ arg.properties.forEach((prop) => {
86400
+ if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
86401
+ const keyName = prop.key.name;
86402
+ graph.nodes.add(keyName);
86403
+ nodeCollection.addNode(keyName, prop);
86404
+ if (!graph.edges.get(keyName)) {
86405
+ graph.edges.set(keyName, /* @__PURE__ */ new Set());
86406
+ }
86407
+ if (graph.spread.has(name)) {
86408
+ graph.spread.get(name)?.add(keyName);
86409
+ } else {
86410
+ graph.spread.set(name, /* @__PURE__ */ new Set([keyName]));
86411
+ }
86412
+ } else if (prop.type === "SpreadElement") {
86413
+ console.warn("not support spread in spread");
86414
+ }
86415
+ });
86416
+ }
86417
+ }
86418
+ }
86374
86419
  }
86375
86420
  }
86376
86421
  });
@@ -86399,6 +86444,14 @@ function processSetup(ast, parentScope, parentPath) {
86399
86444
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86400
86445
  graph.edges.get(name)?.add(path1.node.name);
86401
86446
  }
86447
+ },
86448
+ MemberExpression(path1) {
86449
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86450
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86451
+ 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)) {
86452
+ graph.edges.get(name)?.add(path1.node.property.name);
86453
+ }
86454
+ }
86402
86455
  }
86403
86456
  });
86404
86457
  }
@@ -86416,6 +86469,14 @@ function processSetup(ast, parentScope, parentPath) {
86416
86469
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86417
86470
  graph.edges.get(name)?.add(path1.node.name);
86418
86471
  }
86472
+ },
86473
+ MemberExpression(path1) {
86474
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86475
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86476
+ 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)) {
86477
+ graph.edges.get(name)?.add(path1.node.property.name);
86478
+ }
86479
+ }
86419
86480
  }
86420
86481
  }, path2.scope, path2);
86421
86482
  }
@@ -86433,6 +86494,14 @@ function processSetup(ast, parentScope, parentPath) {
86433
86494
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86434
86495
  graph.edges.get(name)?.add(path1.node.name);
86435
86496
  }
86497
+ },
86498
+ MemberExpression(path1) {
86499
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86500
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86501
+ 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)) {
86502
+ graph.edges.get(name)?.add(path1.node.property.name);
86503
+ }
86504
+ }
86436
86505
  }
86437
86506
  }, path2.scope, path2);
86438
86507
  }
@@ -86452,11 +86521,38 @@ function processSetup(ast, parentScope, parentPath) {
86452
86521
  if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86453
86522
  graph.edges.get(name)?.add(path1.node.name);
86454
86523
  }
86524
+ },
86525
+ MemberExpression(path1) {
86526
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86527
+ const binding2 = path1.scope.getBinding(path1.node.object.name);
86528
+ 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)) {
86529
+ graph.edges.get(name)?.add(path1.node.property.name);
86530
+ }
86531
+ }
86455
86532
  }
86456
86533
  }, path2.scope, path2);
86457
86534
  }
86458
86535
  }
86459
86536
  }
86537
+ },
86538
+ ObjectMethod(path2) {
86539
+ if (path2.node.key.type === "Identifier" && graph.nodes.has(path2.node.key.name)) {
86540
+ const name = path2.node.key.name;
86541
+ path2.traverse({
86542
+ Identifier(path1) {
86543
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name) {
86544
+ graph.edges.get(name)?.add(path1.node.name);
86545
+ }
86546
+ },
86547
+ MemberExpression(path1) {
86548
+ if (path1.node.object.type === "Identifier" && spread.includes(path1.node.object.name)) {
86549
+ if (spread.includes(path1.node.object.name) && path1.node.property.type === "Identifier" && path1.node.property.name !== name) {
86550
+ graph.edges.get(name)?.add(path1.node.property.name);
86551
+ }
86552
+ }
86553
+ }
86554
+ });
86555
+ }
86460
86556
  }
86461
86557
  }, parentScope, parentPath);
86462
86558
  return {
@@ -86505,7 +86601,9 @@ function analyze3(content) {
86505
86601
  if (prop.key.type === "Identifier") {
86506
86602
  const name = prop.key.name;
86507
86603
  graph.nodes.add(name);
86508
- nodeCollection.addNode(name, prop, true);
86604
+ nodeCollection.addNode(name, prop, {
86605
+ isComputed: true
86606
+ });
86509
86607
  if (!graph.edges.get(name)) {
86510
86608
  graph.edges.set(name, /* @__PURE__ */ new Set());
86511
86609
  }
@@ -86537,13 +86635,31 @@ function analyze3(content) {
86537
86635
  if (path2.node.declaration.type === "ObjectExpression" && path1.parent === path2.node.declaration || path2.node.declaration.type === "CallExpression" && path1.parent === path2.node.declaration.arguments[0]) {
86538
86636
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
86539
86637
  const setupNode = path1.node;
86638
+ const spread = [];
86639
+ traverse3(setupNode, {
86640
+ ReturnStatement(path22) {
86641
+ if (path22.node.argument?.type === "ObjectExpression") {
86642
+ const returnNode = path22.node.argument;
86643
+ traverse3(returnNode, {
86644
+ SpreadElement(path3) {
86645
+ 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") {
86646
+ spread.push(path3.node.argument.arguments[0].name);
86647
+ } else if (path3.node.argument.type === "Identifier") {
86648
+ spread.push(path3.node.argument.name);
86649
+ }
86650
+ }
86651
+ }, path22.scope, path22);
86652
+ }
86653
+ }
86654
+ }, path1.scope, path1);
86540
86655
  const {
86541
86656
  graph: {
86542
86657
  nodes: tempNodes,
86543
- edges: tempEdges
86658
+ edges: tempEdges,
86659
+ spread: tempSpread
86544
86660
  },
86545
86661
  nodeCollection: tempNodeCollection
86546
- } = processSetup(setupNode, path1.scope, setupNode);
86662
+ } = processSetup(setupNode, path1.scope, setupNode, spread);
86547
86663
  traverse3(setupNode, {
86548
86664
  ReturnStatement(path22) {
86549
86665
  if (path22.node.argument?.type === "ObjectExpression") {
@@ -86556,7 +86672,7 @@ function analyze3(content) {
86556
86672
  const valName = path3.node.value.type === "Identifier" ? path3.node.value.name : "";
86557
86673
  if (valName && tempNodes.has(valName)) {
86558
86674
  graph.nodes.add(name);
86559
- nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName).type);
86675
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName));
86560
86676
  if (!graph.edges.get(name)) {
86561
86677
  graph.edges.set(name, /* @__PURE__ */ new Set());
86562
86678
  tempEdges.get(valName)?.forEach((edge) => {
@@ -86566,6 +86682,31 @@ function analyze3(content) {
86566
86682
  }
86567
86683
  }
86568
86684
  }
86685
+ },
86686
+ SpreadElement(path3) {
86687
+ 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)) {
86688
+ tempSpread.get(path3.node.argument.arguments[0].name)?.forEach((name) => {
86689
+ graph.nodes.add(name);
86690
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
86691
+ if (!graph.edges.get(name)) {
86692
+ graph.edges.set(name, /* @__PURE__ */ new Set());
86693
+ tempEdges.get(name)?.forEach((edge) => {
86694
+ graph.edges.get(name)?.add(edge);
86695
+ });
86696
+ }
86697
+ });
86698
+ } else if (path3.node.argument.type === "Identifier" && tempSpread.get(path3.node.argument.name)) {
86699
+ tempSpread.get(path3.node.argument.name)?.forEach((name) => {
86700
+ graph.nodes.add(name);
86701
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(name));
86702
+ if (!graph.edges.get(name)) {
86703
+ graph.edges.set(name, /* @__PURE__ */ new Set());
86704
+ tempEdges.get(name)?.forEach((edge) => {
86705
+ graph.edges.get(name)?.add(edge);
86706
+ });
86707
+ }
86708
+ });
86709
+ }
86569
86710
  }
86570
86711
  }, path22.scope, path22);
86571
86712
  } else {