vue-hook-optimizer 0.0.9 → 0.0.11

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
@@ -49,8 +49,8 @@ var require_virtual_types = __commonJS({
49
49
  exports.Statement = Statement;
50
50
  var Expression = ["Expression"];
51
51
  exports.Expression = Expression;
52
- var Scope2 = ["Scopable", "Pattern"];
53
- exports.Scope = Scope2;
52
+ var Scope3 = ["Scopable", "Pattern"];
53
+ exports.Scope = Scope3;
54
54
  var Referenced = null;
55
55
  exports.Referenced = Referenced;
56
56
  var BlockScoped = null;
@@ -18063,7 +18063,7 @@ var require_scope = __commonJS({
18063
18063
  }
18064
18064
  };
18065
18065
  var uid = 0;
18066
- var Scope2 = class _Scope {
18066
+ var Scope3 = class _Scope {
18067
18067
  constructor(path2) {
18068
18068
  this.uid = void 0;
18069
18069
  this.path = void 0;
@@ -18698,9 +18698,9 @@ var require_scope = __commonJS({
18698
18698
  } while (scope = scope.parent);
18699
18699
  }
18700
18700
  };
18701
- exports.default = Scope2;
18702
- Scope2.globals = Object.keys(_globals.builtin);
18703
- Scope2.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
18701
+ exports.default = Scope3;
18702
+ Scope3.globals = Object.keys(_globals.builtin);
18703
+ Scope3.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
18704
18704
  }
18705
18705
  });
18706
18706
 
@@ -29012,7 +29012,7 @@ var require_lib9 = __commonJS({
29012
29012
  var CLASS_ELEMENT_INSTANCE_GETTER2 = CLASS_ELEMENT_KIND_GETTER2;
29013
29013
  var CLASS_ELEMENT_INSTANCE_SETTER2 = CLASS_ELEMENT_KIND_SETTER2;
29014
29014
  var CLASS_ELEMENT_OTHER2 = 0;
29015
- var Scope2 = class {
29015
+ var Scope3 = class {
29016
29016
  constructor(flags) {
29017
29017
  this.var = /* @__PURE__ */ new Set();
29018
29018
  this.lexical = /* @__PURE__ */ new Set();
@@ -29068,7 +29068,7 @@ var require_lib9 = __commonJS({
29068
29068
  return this.treatFunctionsAsVarInScope(this.currentScope());
29069
29069
  }
29070
29070
  createScope(flags) {
29071
- return new Scope2(flags);
29071
+ return new Scope3(flags);
29072
29072
  }
29073
29073
  enter(flags) {
29074
29074
  this.scopeStack.push(this.createScope(flags));
@@ -29163,7 +29163,7 @@ var require_lib9 = __commonJS({
29163
29163
  }
29164
29164
  }
29165
29165
  };
29166
- var FlowScope2 = class extends Scope2 {
29166
+ var FlowScope2 = class extends Scope3 {
29167
29167
  constructor(...args) {
29168
29168
  super(...args);
29169
29169
  this.declareFunctions = /* @__PURE__ */ new Set();
@@ -34669,7 +34669,7 @@ var require_lib9 = __commonJS({
34669
34669
  }
34670
34670
  }
34671
34671
  };
34672
- var TypeScriptScope2 = class extends Scope2 {
34672
+ var TypeScriptScope2 = class extends Scope3 {
34673
34673
  constructor(...args) {
34674
34674
  super(...args);
34675
34675
  this.types = /* @__PURE__ */ new Set();
@@ -86263,17 +86263,58 @@ 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, isComputed = false) {
86271
+ if (this.nodes.has(label)) {
86272
+ return;
86273
+ }
86274
+ if (!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, type) {
86290
+ this.nodes.set(label, {
86291
+ label,
86292
+ type
86293
+ });
86294
+ }
86295
+ map(graph) {
86296
+ const nodes = new Set(Array.from(graph.nodes).map((node2) => {
86297
+ return this.nodes.get(node2);
86298
+ }));
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
+ }))];
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 analyze2(content) {
86271
- const ast = parse_1$1(content, {
86272
- sourceType: "module",
86273
- plugins: [
86274
- "typescript"
86275
- ]
86276
- });
86316
+ function processSetup(ast, parentScope, parentPath) {
86317
+ const nodeCollection = new NodeCollection();
86277
86318
  const graph = {
86278
86319
  nodes: /* @__PURE__ */ new Set(),
86279
86320
  edges: /* @__PURE__ */ new Map()
@@ -86286,8 +86327,9 @@ function analyze2(content) {
86286
86327
  if (element?.type === "Identifier") {
86287
86328
  const name = element.name;
86288
86329
  const binding2 = path2.scope.getBinding(name);
86289
- if (binding2 && path2.parent.type === "Program" && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86330
+ 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))) {
86290
86331
  graph.nodes.add(name);
86332
+ nodeCollection.addNode(name, declaration2);
86291
86333
  if (!graph.edges.get(name)) {
86292
86334
  graph.edges.set(name, /* @__PURE__ */ new Set());
86293
86335
  }
@@ -86300,8 +86342,9 @@ function analyze2(content) {
86300
86342
  if (property.type === "ObjectProperty" && property.value.type === "Identifier") {
86301
86343
  const name = property.value.name;
86302
86344
  const binding2 = path2.scope.getBinding(name);
86303
- if (binding2 && path2.parent.type === "Program" && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86345
+ 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))) {
86304
86346
  graph.nodes.add(name);
86347
+ nodeCollection.addNode(name, declaration2);
86305
86348
  if (!graph.edges.get(name)) {
86306
86349
  graph.edges.set(name, /* @__PURE__ */ new Set());
86307
86350
  }
@@ -86312,8 +86355,9 @@ function analyze2(content) {
86312
86355
  if (declaration2.id?.type === "Identifier") {
86313
86356
  const name = declaration2.id.name;
86314
86357
  const binding2 = path2.scope.getBinding(name);
86315
- if (binding2 && path2.parent.type === "Program" && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86358
+ 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))) {
86316
86359
  graph.nodes.add(name);
86360
+ nodeCollection.addNode(name, declaration2);
86317
86361
  if (!graph.edges.get(name)) {
86318
86362
  graph.edges.set(name, /* @__PURE__ */ new Set());
86319
86363
  }
@@ -86325,15 +86369,16 @@ function analyze2(content) {
86325
86369
  const name = path2.node.id?.name;
86326
86370
  if (name) {
86327
86371
  const binding2 = path2.scope.getBinding(name);
86328
- if (binding2 && path2.parent.type === "Program") {
86372
+ if (binding2 && (path2.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path2.parent)) {
86329
86373
  graph.nodes.add(name);
86374
+ nodeCollection.addNode(name, path2.node);
86330
86375
  if (!graph.edges.get(name)) {
86331
86376
  graph.edges.set(name, /* @__PURE__ */ new Set());
86332
86377
  }
86333
86378
  }
86334
86379
  }
86335
86380
  }
86336
- });
86381
+ }, parentScope, parentPath);
86337
86382
  traverse2(ast, {
86338
86383
  FunctionDeclaration(path2) {
86339
86384
  const name = path2.node.id?.name;
@@ -86341,7 +86386,7 @@ function analyze2(content) {
86341
86386
  path2.traverse({
86342
86387
  Identifier(path1) {
86343
86388
  const binding2 = path1.scope.getBinding(path1.node.name);
86344
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86389
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86345
86390
  graph.edges.get(name)?.add(path1.node.name);
86346
86391
  }
86347
86392
  }
@@ -86358,7 +86403,7 @@ function analyze2(content) {
86358
86403
  traverse2(path2.node.init, {
86359
86404
  Identifier(path1) {
86360
86405
  const binding2 = path1.scope.getBinding(path1.node.name);
86361
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86406
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86362
86407
  graph.edges.get(name)?.add(path1.node.name);
86363
86408
  }
86364
86409
  }
@@ -86375,7 +86420,7 @@ function analyze2(content) {
86375
86420
  traverse2(path2.node.init, {
86376
86421
  Identifier(path1) {
86377
86422
  const binding2 = path1.scope.getBinding(path1.node.name);
86378
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86423
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86379
86424
  graph.edges.get(name)?.add(path1.node.name);
86380
86425
  }
86381
86426
  }
@@ -86394,7 +86439,7 @@ function analyze2(content) {
86394
86439
  traverse2(path2.node.init, {
86395
86440
  Identifier(path1) {
86396
86441
  const binding2 = path1.scope.getBinding(path1.node.name);
86397
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86442
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86398
86443
  graph.edges.get(name)?.add(path1.node.name);
86399
86444
  }
86400
86445
  }
@@ -86403,8 +86448,21 @@ function analyze2(content) {
86403
86448
  }
86404
86449
  }
86405
86450
  }
86451
+ }, parentScope, parentPath);
86452
+ return {
86453
+ graph,
86454
+ nodeCollection
86455
+ };
86456
+ }
86457
+ function analyze2(content) {
86458
+ const ast = parse_1$1(content, {
86459
+ sourceType: "module",
86460
+ plugins: [
86461
+ "typescript"
86462
+ ]
86406
86463
  });
86407
- return graph;
86464
+ const { graph, nodeCollection } = processSetup(ast);
86465
+ return nodeCollection.map(graph);
86408
86466
  }
86409
86467
 
86410
86468
  // src/analyze/options.ts
@@ -86420,6 +86478,7 @@ function analyze3(content) {
86420
86478
  "typescript"
86421
86479
  ]
86422
86480
  });
86481
+ const nodeCollection = new NodeCollection();
86423
86482
  const graph = {
86424
86483
  nodes: /* @__PURE__ */ new Set(),
86425
86484
  edges: /* @__PURE__ */ new Map()
@@ -86436,6 +86495,7 @@ function analyze3(content) {
86436
86495
  if (prop.key.type === "Identifier") {
86437
86496
  const name = prop.key.name;
86438
86497
  graph.nodes.add(name);
86498
+ nodeCollection.addNode(name, prop, true);
86439
86499
  if (!graph.edges.get(name)) {
86440
86500
  graph.edges.set(name, /* @__PURE__ */ new Set());
86441
86501
  }
@@ -86452,6 +86512,7 @@ function analyze3(content) {
86452
86512
  if (prop.key.type === "Identifier") {
86453
86513
  const name = prop.key.name;
86454
86514
  graph.nodes.add(name);
86515
+ nodeCollection.addNode(name, prop);
86455
86516
  if (!graph.edges.get(name)) {
86456
86517
  graph.edges.set(name, /* @__PURE__ */ new Set());
86457
86518
  }
@@ -86466,68 +86527,13 @@ function analyze3(content) {
86466
86527
  if (path2.node.declaration.type === "ObjectExpression" && path1.parent === path2.node.declaration || path2.node.declaration.type === "CallExpression" && path1.parent === path2.node.declaration.arguments[0]) {
86467
86528
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
86468
86529
  const setupNode = path1.node;
86469
- const tempNodes = /* @__PURE__ */ new Set();
86470
- const tempEdges = /* @__PURE__ */ new Map();
86471
- traverse3(setupNode, {
86472
- VariableDeclaration(path22) {
86473
- path22.node.declarations.forEach((declaration2) => {
86474
- if (declaration2.id?.type === "Identifier") {
86475
- const name = declaration2.id.name;
86476
- if (path22.parent == setupNode.body) {
86477
- tempNodes.add(name);
86478
- if (!tempEdges.get(name)) {
86479
- tempEdges.set(name, /* @__PURE__ */ new Set());
86480
- }
86481
- }
86482
- }
86483
- });
86484
- },
86485
- FunctionDeclaration(path22) {
86486
- const name = path22.node.id?.name;
86487
- if (name) {
86488
- if (path22.parent == setupNode.body) {
86489
- tempNodes.add(name);
86490
- if (!tempEdges.get(name)) {
86491
- tempEdges.set(name, /* @__PURE__ */ new Set());
86492
- }
86493
- }
86494
- }
86495
- }
86496
- }, path1.scope, path1);
86497
- traverse3(setupNode, {
86498
- FunctionDeclaration(path3) {
86499
- const name = path3.node.id?.name;
86500
- if (name && tempNodes.has(name)) {
86501
- path3.traverse({
86502
- Identifier(path4) {
86503
- if (tempNodes.has(path4.node.name) && path4.node.name !== name) {
86504
- tempEdges.get(name)?.add(path4.node.name);
86505
- }
86506
- }
86507
- });
86508
- }
86530
+ const {
86531
+ graph: {
86532
+ nodes: tempNodes,
86533
+ edges: tempEdges
86509
86534
  },
86510
- VariableDeclarator(path3) {
86511
- if (path3.node.init) {
86512
- if ([
86513
- "CallExpression",
86514
- "ArrowFunctionExpression",
86515
- "FunctionDeclaration"
86516
- ].includes(path3.node.init.type) && path3.node.id.type === "Identifier") {
86517
- const name = path3.node.id?.name;
86518
- if (name && tempNodes.has(name)) {
86519
- path3.traverse({
86520
- Identifier(path4) {
86521
- if (tempNodes.has(path4.node.name) && path4.node.name !== name) {
86522
- tempEdges.get(name)?.add(path4.node.name);
86523
- }
86524
- }
86525
- });
86526
- }
86527
- }
86528
- }
86529
- }
86530
- }, path1.scope, path1);
86535
+ nodeCollection: tempNodeCollection
86536
+ } = processSetup(setupNode, path1.scope, setupNode);
86531
86537
  traverse3(setupNode, {
86532
86538
  ReturnStatement(path22) {
86533
86539
  if (path22.node.argument?.type === "ObjectExpression") {
@@ -86540,6 +86546,7 @@ function analyze3(content) {
86540
86546
  const valName = path3.node.value.type === "Identifier" ? path3.node.value.name : "";
86541
86547
  if (valName && tempNodes.has(valName)) {
86542
86548
  graph.nodes.add(name);
86549
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName).type);
86543
86550
  if (!graph.edges.get(name)) {
86544
86551
  graph.edges.set(name, /* @__PURE__ */ new Set());
86545
86552
  tempEdges.get(valName)?.forEach((edge) => {
@@ -86568,6 +86575,7 @@ function analyze3(content) {
86568
86575
  if (prop.key.type === "Identifier") {
86569
86576
  const name = prop.key.name;
86570
86577
  graph.nodes.add(name);
86578
+ nodeCollection.addNode(name, prop);
86571
86579
  if (!graph.edges.get(name)) {
86572
86580
  graph.edges.set(name, /* @__PURE__ */ new Set());
86573
86581
  }
@@ -86646,7 +86654,7 @@ function analyze3(content) {
86646
86654
  }
86647
86655
  }
86648
86656
  });
86649
- return graph;
86657
+ return nodeCollection.map(graph);
86650
86658
  }
86651
86659
 
86652
86660
  // src/vis.ts
@@ -86655,16 +86663,19 @@ function getVisData(graph, usedNodes) {
86655
86663
  const edges = [];
86656
86664
  graph.nodes.forEach((node2) => {
86657
86665
  nodes.push({
86658
- id: node2,
86659
- label: node2,
86660
- group: usedNodes.has(node2) ? "used" : "normal"
86666
+ id: node2.label,
86667
+ label: node2.label,
86668
+ shape: node2.type === "var" ? "dot" : "diamond",
86669
+ group: usedNodes.has(node2.label) ? "used" : "normal"
86661
86670
  });
86662
86671
  });
86663
86672
  graph.edges.forEach((edge, key) => {
86664
86673
  edge.forEach((to) => {
86674
+ if (!to)
86675
+ return;
86665
86676
  edges.push({
86666
- from: key,
86667
- to,
86677
+ from: key.label,
86678
+ to: to.label,
86668
86679
  arrows: {
86669
86680
  to: {
86670
86681
  enabled: true,