vue-hook-optimizer 0.0.9 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -21,11 +21,11 @@ Open the browser and visit `http://localhost:3000/`.
21
21
 
22
22
  1. paste your `vue` code into the editor
23
23
 
24
- Up to now, it only supports the code with `<script setup>` syntax block.If your code use `options api`, it's not working at the moment.
24
+ ~~Up to now, it only supports the code with `<script setup>` syntax block.If your code use `options api`, it's not working at the moment.~~
25
25
 
26
26
  2. click `Analyze` button
27
27
 
28
- The tool will analyze the `setup block` and `template block`, and show the relations between the variables and the methods. This is a simple demo.
28
+ The tool will analyze the code, and show the relations between the variables and the methods. This is a simple demo.
29
29
 
30
30
  ![demo](./images/demo1.png)
31
31
 
@@ -37,7 +37,7 @@ So I want to build a tool to help us analyze the code, and find the relations be
37
37
 
38
38
  ## Development Plan
39
39
 
40
- - [ ] add node type and more info
40
+ - [ ] add ~~node type~~ (has added `var` and `fun` types) and more info
41
41
  - [ ] provide some suggestions for optimization
42
42
  - [x] support `options api`
43
43
  - [x] [vscode extension](./packages/vscode)
package/dist/index.d.mts CHANGED
@@ -3,19 +3,51 @@ import * as vis_network from 'vis-network';
3
3
 
4
4
  declare function analyze$2(content: string): Set<string>;
5
5
 
6
+ declare enum NodeType {
7
+ var = "var",
8
+ fun = "fun"
9
+ }
10
+
6
11
  declare function analyze$1(content: string): {
7
- nodes: Set<string>;
8
- edges: Map<string, Set<string>>;
12
+ nodes: Set<{
13
+ label: string;
14
+ type: NodeType;
15
+ }>;
16
+ edges: Map<{
17
+ label: string;
18
+ type: NodeType;
19
+ }, Set<{
20
+ label: string;
21
+ type: NodeType;
22
+ }>>;
9
23
  };
10
24
 
11
25
  declare function analyze(content: string): {
12
- nodes: Set<string>;
13
- edges: Map<string, Set<string>>;
26
+ nodes: Set<{
27
+ label: string;
28
+ type: NodeType;
29
+ }>;
30
+ edges: Map<{
31
+ label: string;
32
+ type: NodeType;
33
+ }, Set<{
34
+ label: string;
35
+ type: NodeType;
36
+ }>>;
14
37
  };
15
38
 
16
39
  declare function getVisData(graph: {
17
- nodes: Set<string>;
18
- edges: Map<string, Set<string>>;
40
+ nodes: Set<{
41
+ label: string;
42
+ type: string;
43
+ }>;
44
+ edges: Map<{
45
+ label: string;
46
+ type: string;
47
+ }, Set<{
48
+ label: string;
49
+ type: string;
50
+ }>>;
19
51
  }, usedNodes: Set<string>): {
20
52
  nodes: vis_network.Node[];
21
53
  edges: vis_network.Edge[];
package/dist/index.d.ts CHANGED
@@ -3,19 +3,51 @@ import * as vis_network from 'vis-network';
3
3
 
4
4
  declare function analyze$2(content: string): Set<string>;
5
5
 
6
+ declare enum NodeType {
7
+ var = "var",
8
+ fun = "fun"
9
+ }
10
+
6
11
  declare function analyze$1(content: string): {
7
- nodes: Set<string>;
8
- edges: Map<string, Set<string>>;
12
+ nodes: Set<{
13
+ label: string;
14
+ type: NodeType;
15
+ }>;
16
+ edges: Map<{
17
+ label: string;
18
+ type: NodeType;
19
+ }, Set<{
20
+ label: string;
21
+ type: NodeType;
22
+ }>>;
9
23
  };
10
24
 
11
25
  declare function analyze(content: string): {
12
- nodes: Set<string>;
13
- edges: Map<string, Set<string>>;
26
+ nodes: Set<{
27
+ label: string;
28
+ type: NodeType;
29
+ }>;
30
+ edges: Map<{
31
+ label: string;
32
+ type: NodeType;
33
+ }, Set<{
34
+ label: string;
35
+ type: NodeType;
36
+ }>>;
14
37
  };
15
38
 
16
39
  declare function getVisData(graph: {
17
- nodes: Set<string>;
18
- edges: Map<string, Set<string>>;
40
+ nodes: Set<{
41
+ label: string;
42
+ type: string;
43
+ }>;
44
+ edges: Map<{
45
+ label: string;
46
+ type: string;
47
+ }, Set<{
48
+ label: string;
49
+ type: string;
50
+ }>>;
19
51
  }, usedNodes: Set<string>): {
20
52
  nodes: vis_network.Node[];
21
53
  edges: vis_network.Edge[];
package/dist/index.js CHANGED
@@ -48,8 +48,8 @@ var require_virtual_types = __commonJS({
48
48
  exports.Statement = Statement;
49
49
  var Expression = ["Expression"];
50
50
  exports.Expression = Expression;
51
- var Scope2 = ["Scopable", "Pattern"];
52
- exports.Scope = Scope2;
51
+ var Scope3 = ["Scopable", "Pattern"];
52
+ exports.Scope = Scope3;
53
53
  var Referenced = null;
54
54
  exports.Referenced = Referenced;
55
55
  var BlockScoped = null;
@@ -18062,7 +18062,7 @@ var require_scope = __commonJS({
18062
18062
  }
18063
18063
  };
18064
18064
  var uid = 0;
18065
- var Scope2 = class _Scope {
18065
+ var Scope3 = class _Scope {
18066
18066
  constructor(path2) {
18067
18067
  this.uid = void 0;
18068
18068
  this.path = void 0;
@@ -18697,9 +18697,9 @@ var require_scope = __commonJS({
18697
18697
  } while (scope = scope.parent);
18698
18698
  }
18699
18699
  };
18700
- exports.default = Scope2;
18701
- Scope2.globals = Object.keys(_globals.builtin);
18702
- Scope2.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
18700
+ exports.default = Scope3;
18701
+ Scope3.globals = Object.keys(_globals.builtin);
18702
+ Scope3.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
18703
18703
  }
18704
18704
  });
18705
18705
 
@@ -29011,7 +29011,7 @@ var require_lib9 = __commonJS({
29011
29011
  var CLASS_ELEMENT_INSTANCE_GETTER2 = CLASS_ELEMENT_KIND_GETTER2;
29012
29012
  var CLASS_ELEMENT_INSTANCE_SETTER2 = CLASS_ELEMENT_KIND_SETTER2;
29013
29013
  var CLASS_ELEMENT_OTHER2 = 0;
29014
- var Scope2 = class {
29014
+ var Scope3 = class {
29015
29015
  constructor(flags) {
29016
29016
  this.var = /* @__PURE__ */ new Set();
29017
29017
  this.lexical = /* @__PURE__ */ new Set();
@@ -29067,7 +29067,7 @@ var require_lib9 = __commonJS({
29067
29067
  return this.treatFunctionsAsVarInScope(this.currentScope());
29068
29068
  }
29069
29069
  createScope(flags) {
29070
- return new Scope2(flags);
29070
+ return new Scope3(flags);
29071
29071
  }
29072
29072
  enter(flags) {
29073
29073
  this.scopeStack.push(this.createScope(flags));
@@ -29162,7 +29162,7 @@ var require_lib9 = __commonJS({
29162
29162
  }
29163
29163
  }
29164
29164
  };
29165
- var FlowScope2 = class extends Scope2 {
29165
+ var FlowScope2 = class extends Scope3 {
29166
29166
  constructor(...args) {
29167
29167
  super(...args);
29168
29168
  this.declareFunctions = /* @__PURE__ */ new Set();
@@ -34668,7 +34668,7 @@ var require_lib9 = __commonJS({
34668
34668
  }
34669
34669
  }
34670
34670
  };
34671
- var TypeScriptScope2 = class extends Scope2 {
34671
+ var TypeScriptScope2 = class extends Scope3 {
34672
34672
  constructor(...args) {
34673
34673
  super(...args);
34674
34674
  this.types = /* @__PURE__ */ new Set();
@@ -86273,17 +86273,58 @@ function analyze(content) {
86273
86273
 
86274
86274
  // src/analyze/setupScript.ts
86275
86275
  var import_traverse2 = __toESM(require_lib13());
86276
+
86277
+ // src/analyze/utils.ts
86278
+ var NodeCollection = class {
86279
+ nodes = /* @__PURE__ */ new Map();
86280
+ addNode(label, node2, isComputed = false) {
86281
+ if (this.nodes.has(label)) {
86282
+ return;
86283
+ }
86284
+ if (!isComputed && (node2.type === "VariableDeclarator" && [
86285
+ "ArrowFunctionExpression",
86286
+ "FunctionDeclaration"
86287
+ ].includes(node2.init?.type || "") || node2.type === "FunctionDeclaration" || node2.type === "ObjectMethod")) {
86288
+ this.nodes.set(label, {
86289
+ label,
86290
+ type: "fun" /* fun */
86291
+ });
86292
+ } else {
86293
+ this.nodes.set(label, {
86294
+ label,
86295
+ type: "var" /* var */
86296
+ });
86297
+ }
86298
+ }
86299
+ addTypedNode(label, type) {
86300
+ this.nodes.set(label, {
86301
+ label,
86302
+ type
86303
+ });
86304
+ }
86305
+ map(graph) {
86306
+ const nodes = new Set(Array.from(graph.nodes).map((node2) => {
86307
+ return this.nodes.get(node2);
86308
+ }));
86309
+ const edges = new Map(Array.from(graph.edges).map(([from2, to]) => {
86310
+ return [this.nodes.get(from2), new Set(Array.from(to).map((node2) => {
86311
+ return this.nodes.get(node2);
86312
+ }))];
86313
+ }));
86314
+ return {
86315
+ nodes,
86316
+ edges
86317
+ };
86318
+ }
86319
+ };
86320
+
86321
+ // src/analyze/setupScript.ts
86276
86322
  var traverse2 = (
86277
86323
  //@ts-ignore
86278
86324
  import_traverse2.default.default?.default || import_traverse2.default.default || import_traverse2.default
86279
86325
  );
86280
- function analyze2(content) {
86281
- const ast = parse_1$1(content, {
86282
- sourceType: "module",
86283
- plugins: [
86284
- "typescript"
86285
- ]
86286
- });
86326
+ function processSetup(ast, parentScope, parentPath) {
86327
+ const nodeCollection = new NodeCollection();
86287
86328
  const graph = {
86288
86329
  nodes: /* @__PURE__ */ new Set(),
86289
86330
  edges: /* @__PURE__ */ new Map()
@@ -86296,8 +86337,9 @@ function analyze2(content) {
86296
86337
  if (element?.type === "Identifier") {
86297
86338
  const name = element.name;
86298
86339
  const binding2 = path2.scope.getBinding(name);
86299
- if (binding2 && path2.parent.type === "Program" && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86340
+ 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))) {
86300
86341
  graph.nodes.add(name);
86342
+ nodeCollection.addNode(name, declaration2);
86301
86343
  if (!graph.edges.get(name)) {
86302
86344
  graph.edges.set(name, /* @__PURE__ */ new Set());
86303
86345
  }
@@ -86310,8 +86352,9 @@ function analyze2(content) {
86310
86352
  if (property.type === "ObjectProperty" && property.value.type === "Identifier") {
86311
86353
  const name = property.value.name;
86312
86354
  const binding2 = path2.scope.getBinding(name);
86313
- if (binding2 && path2.parent.type === "Program" && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86355
+ 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))) {
86314
86356
  graph.nodes.add(name);
86357
+ nodeCollection.addNode(name, declaration2);
86315
86358
  if (!graph.edges.get(name)) {
86316
86359
  graph.edges.set(name, /* @__PURE__ */ new Set());
86317
86360
  }
@@ -86322,8 +86365,9 @@ function analyze2(content) {
86322
86365
  if (declaration2.id?.type === "Identifier") {
86323
86366
  const name = declaration2.id.name;
86324
86367
  const binding2 = path2.scope.getBinding(name);
86325
- if (binding2 && path2.parent.type === "Program" && !(declaration2.init?.type === "CallExpression" && declaration2.init?.callee.type === "Identifier" && ["defineProps", "defineEmits"].includes(declaration2.init?.callee.name))) {
86368
+ 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))) {
86326
86369
  graph.nodes.add(name);
86370
+ nodeCollection.addNode(name, declaration2);
86327
86371
  if (!graph.edges.get(name)) {
86328
86372
  graph.edges.set(name, /* @__PURE__ */ new Set());
86329
86373
  }
@@ -86335,15 +86379,16 @@ function analyze2(content) {
86335
86379
  const name = path2.node.id?.name;
86336
86380
  if (name) {
86337
86381
  const binding2 = path2.scope.getBinding(name);
86338
- if (binding2 && path2.parent.type === "Program") {
86382
+ if (binding2 && (path2.parent.type === "Program" || parentPath?.type === "ObjectMethod" && parentPath.body === path2.parent)) {
86339
86383
  graph.nodes.add(name);
86384
+ nodeCollection.addNode(name, path2.node);
86340
86385
  if (!graph.edges.get(name)) {
86341
86386
  graph.edges.set(name, /* @__PURE__ */ new Set());
86342
86387
  }
86343
86388
  }
86344
86389
  }
86345
86390
  }
86346
- });
86391
+ }, parentScope, parentPath);
86347
86392
  traverse2(ast, {
86348
86393
  FunctionDeclaration(path2) {
86349
86394
  const name = path2.node.id?.name;
@@ -86351,7 +86396,7 @@ function analyze2(content) {
86351
86396
  path2.traverse({
86352
86397
  Identifier(path1) {
86353
86398
  const binding2 = path1.scope.getBinding(path1.node.name);
86354
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86399
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86355
86400
  graph.edges.get(name)?.add(path1.node.name);
86356
86401
  }
86357
86402
  }
@@ -86368,7 +86413,7 @@ function analyze2(content) {
86368
86413
  traverse2(path2.node.init, {
86369
86414
  Identifier(path1) {
86370
86415
  const binding2 = path1.scope.getBinding(path1.node.name);
86371
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86416
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86372
86417
  graph.edges.get(name)?.add(path1.node.name);
86373
86418
  }
86374
86419
  }
@@ -86385,7 +86430,7 @@ function analyze2(content) {
86385
86430
  traverse2(path2.node.init, {
86386
86431
  Identifier(path1) {
86387
86432
  const binding2 = path1.scope.getBinding(path1.node.name);
86388
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86433
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86389
86434
  graph.edges.get(name)?.add(path1.node.name);
86390
86435
  }
86391
86436
  }
@@ -86404,7 +86449,7 @@ function analyze2(content) {
86404
86449
  traverse2(path2.node.init, {
86405
86450
  Identifier(path1) {
86406
86451
  const binding2 = path1.scope.getBinding(path1.node.name);
86407
- if (graph.nodes.has(path1.node.name) && path1.node.name !== name && binding2?.scope.block.type === "Program") {
86452
+ if (graph.nodes.has(path1.node.name) && path1.node.name !== name && (binding2?.scope.block.type === "Program" || parentScope === binding2?.scope)) {
86408
86453
  graph.edges.get(name)?.add(path1.node.name);
86409
86454
  }
86410
86455
  }
@@ -86413,8 +86458,21 @@ function analyze2(content) {
86413
86458
  }
86414
86459
  }
86415
86460
  }
86461
+ }, parentScope, parentPath);
86462
+ return {
86463
+ graph,
86464
+ nodeCollection
86465
+ };
86466
+ }
86467
+ function analyze2(content) {
86468
+ const ast = parse_1$1(content, {
86469
+ sourceType: "module",
86470
+ plugins: [
86471
+ "typescript"
86472
+ ]
86416
86473
  });
86417
- return graph;
86474
+ const { graph, nodeCollection } = processSetup(ast);
86475
+ return nodeCollection.map(graph);
86418
86476
  }
86419
86477
 
86420
86478
  // src/analyze/options.ts
@@ -86430,6 +86488,7 @@ function analyze3(content) {
86430
86488
  "typescript"
86431
86489
  ]
86432
86490
  });
86491
+ const nodeCollection = new NodeCollection();
86433
86492
  const graph = {
86434
86493
  nodes: /* @__PURE__ */ new Set(),
86435
86494
  edges: /* @__PURE__ */ new Map()
@@ -86446,6 +86505,7 @@ function analyze3(content) {
86446
86505
  if (prop.key.type === "Identifier") {
86447
86506
  const name = prop.key.name;
86448
86507
  graph.nodes.add(name);
86508
+ nodeCollection.addNode(name, prop, true);
86449
86509
  if (!graph.edges.get(name)) {
86450
86510
  graph.edges.set(name, /* @__PURE__ */ new Set());
86451
86511
  }
@@ -86462,6 +86522,7 @@ function analyze3(content) {
86462
86522
  if (prop.key.type === "Identifier") {
86463
86523
  const name = prop.key.name;
86464
86524
  graph.nodes.add(name);
86525
+ nodeCollection.addNode(name, prop);
86465
86526
  if (!graph.edges.get(name)) {
86466
86527
  graph.edges.set(name, /* @__PURE__ */ new Set());
86467
86528
  }
@@ -86476,68 +86537,13 @@ function analyze3(content) {
86476
86537
  if (path2.node.declaration.type === "ObjectExpression" && path1.parent === path2.node.declaration || path2.node.declaration.type === "CallExpression" && path1.parent === path2.node.declaration.arguments[0]) {
86477
86538
  if (path1.node.key.type === "Identifier" && path1.node.key.name === "setup") {
86478
86539
  const setupNode = path1.node;
86479
- const tempNodes = /* @__PURE__ */ new Set();
86480
- const tempEdges = /* @__PURE__ */ new Map();
86481
- traverse3(setupNode, {
86482
- VariableDeclaration(path22) {
86483
- path22.node.declarations.forEach((declaration2) => {
86484
- if (declaration2.id?.type === "Identifier") {
86485
- const name = declaration2.id.name;
86486
- if (path22.parent == setupNode.body) {
86487
- tempNodes.add(name);
86488
- if (!tempEdges.get(name)) {
86489
- tempEdges.set(name, /* @__PURE__ */ new Set());
86490
- }
86491
- }
86492
- }
86493
- });
86494
- },
86495
- FunctionDeclaration(path22) {
86496
- const name = path22.node.id?.name;
86497
- if (name) {
86498
- if (path22.parent == setupNode.body) {
86499
- tempNodes.add(name);
86500
- if (!tempEdges.get(name)) {
86501
- tempEdges.set(name, /* @__PURE__ */ new Set());
86502
- }
86503
- }
86504
- }
86505
- }
86506
- }, path1.scope, path1);
86507
- traverse3(setupNode, {
86508
- FunctionDeclaration(path3) {
86509
- const name = path3.node.id?.name;
86510
- if (name && tempNodes.has(name)) {
86511
- path3.traverse({
86512
- Identifier(path4) {
86513
- if (tempNodes.has(path4.node.name) && path4.node.name !== name) {
86514
- tempEdges.get(name)?.add(path4.node.name);
86515
- }
86516
- }
86517
- });
86518
- }
86540
+ const {
86541
+ graph: {
86542
+ nodes: tempNodes,
86543
+ edges: tempEdges
86519
86544
  },
86520
- VariableDeclarator(path3) {
86521
- if (path3.node.init) {
86522
- if ([
86523
- "CallExpression",
86524
- "ArrowFunctionExpression",
86525
- "FunctionDeclaration"
86526
- ].includes(path3.node.init.type) && path3.node.id.type === "Identifier") {
86527
- const name = path3.node.id?.name;
86528
- if (name && tempNodes.has(name)) {
86529
- path3.traverse({
86530
- Identifier(path4) {
86531
- if (tempNodes.has(path4.node.name) && path4.node.name !== name) {
86532
- tempEdges.get(name)?.add(path4.node.name);
86533
- }
86534
- }
86535
- });
86536
- }
86537
- }
86538
- }
86539
- }
86540
- }, path1.scope, path1);
86545
+ nodeCollection: tempNodeCollection
86546
+ } = processSetup(setupNode, path1.scope, setupNode);
86541
86547
  traverse3(setupNode, {
86542
86548
  ReturnStatement(path22) {
86543
86549
  if (path22.node.argument?.type === "ObjectExpression") {
@@ -86550,6 +86556,7 @@ function analyze3(content) {
86550
86556
  const valName = path3.node.value.type === "Identifier" ? path3.node.value.name : "";
86551
86557
  if (valName && tempNodes.has(valName)) {
86552
86558
  graph.nodes.add(name);
86559
+ nodeCollection.addTypedNode(name, tempNodeCollection.nodes.get(valName).type);
86553
86560
  if (!graph.edges.get(name)) {
86554
86561
  graph.edges.set(name, /* @__PURE__ */ new Set());
86555
86562
  tempEdges.get(valName)?.forEach((edge) => {
@@ -86578,6 +86585,7 @@ function analyze3(content) {
86578
86585
  if (prop.key.type === "Identifier") {
86579
86586
  const name = prop.key.name;
86580
86587
  graph.nodes.add(name);
86588
+ nodeCollection.addNode(name, prop);
86581
86589
  if (!graph.edges.get(name)) {
86582
86590
  graph.edges.set(name, /* @__PURE__ */ new Set());
86583
86591
  }
@@ -86656,7 +86664,7 @@ function analyze3(content) {
86656
86664
  }
86657
86665
  }
86658
86666
  });
86659
- return graph;
86667
+ return nodeCollection.map(graph);
86660
86668
  }
86661
86669
 
86662
86670
  // src/vis.ts
@@ -86665,16 +86673,19 @@ function getVisData(graph, usedNodes) {
86665
86673
  const edges = [];
86666
86674
  graph.nodes.forEach((node2) => {
86667
86675
  nodes.push({
86668
- id: node2,
86669
- label: node2,
86670
- group: usedNodes.has(node2) ? "used" : "normal"
86676
+ id: node2.label,
86677
+ label: node2.label,
86678
+ shape: node2.type === "var" ? "dot" : "diamond",
86679
+ group: usedNodes.has(node2.label) ? "used" : "normal"
86671
86680
  });
86672
86681
  });
86673
86682
  graph.edges.forEach((edge, key) => {
86674
86683
  edge.forEach((to) => {
86684
+ if (!to)
86685
+ return;
86675
86686
  edges.push({
86676
- from: key,
86677
- to,
86687
+ from: key.label,
86688
+ to: to.label,
86678
86689
  arrows: {
86679
86690
  to: {
86680
86691
  enabled: true,