vue-hook-optimizer 0.0.54 → 0.0.56

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.d.mts CHANGED
@@ -1,7 +1,8 @@
1
+ import { SFCStyleBlock } from '@vue/compiler-sfc';
1
2
  export { parse } from '@vue/compiler-sfc';
2
3
  import { Edge, Node } from 'vis-network';
3
4
 
4
- declare function analyze$3(content: string): Set<string>;
5
+ declare function analyze$4(content: string): Set<string>;
5
6
 
6
7
  interface TypedNode {
7
8
  label: string;
@@ -18,12 +19,12 @@ declare enum NodeType {
18
19
  fun = "fun"
19
20
  }
20
21
 
21
- declare function analyze$2(content: string, lineOffset?: number, jsx?: boolean): {
22
+ declare function analyze$3(content: string, lineOffset?: number, jsx?: boolean): {
22
23
  nodes: Set<TypedNode>;
23
24
  edges: Map<TypedNode, Set<TypedNode>>;
24
25
  };
25
26
 
26
- declare function analyze$1(content: string, lineOffset?: number, jsx?: boolean): {
27
+ declare function analyze$2(content: string, lineOffset?: number, jsx?: boolean): {
27
28
  graph: {
28
29
  nodes: Set<TypedNode>;
29
30
  edges: Map<TypedNode, Set<TypedNode>>;
@@ -31,7 +32,7 @@ declare function analyze$1(content: string, lineOffset?: number, jsx?: boolean):
31
32
  nodesUsedInTemplate: Set<string>;
32
33
  };
33
34
 
34
- declare function analyze(content: string, type?: "vue" | "react", lineOffset?: number, addInfo?: boolean): {
35
+ declare function analyze$1(content: string, type?: "vue" | "react", lineOffset?: number, addInfo?: boolean): {
35
36
  graph: {
36
37
  nodes: Set<TypedNode>;
37
38
  edges: Map<TypedNode, Set<TypedNode>>;
@@ -39,6 +40,8 @@ declare function analyze(content: string, type?: "vue" | "react", lineOffset?: n
39
40
  nodesUsedInTemplate: Set<string>;
40
41
  };
41
42
 
43
+ declare function analyze(styles: SFCStyleBlock[]): Set<string>;
44
+
42
45
  declare enum SuggestionType {
43
46
  info = "info",
44
47
  warning = "warning",
@@ -52,7 +55,7 @@ interface Suggestion {
52
55
  declare function gen(graph: {
53
56
  nodes: Set<TypedNode>;
54
57
  edges: Map<TypedNode, Set<TypedNode>>;
55
- }, usedNodes: Set<string>): Suggestion[];
58
+ }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): Suggestion[];
56
59
 
57
60
  type CustomNode = Node & {
58
61
  info: TypedNode['info'];
@@ -60,9 +63,9 @@ type CustomNode = Node & {
60
63
  declare function getVisData(graph: {
61
64
  nodes: Set<TypedNode>;
62
65
  edges: Map<TypedNode, Set<TypedNode>>;
63
- }, usedNodes: Set<string>): {
66
+ }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): {
64
67
  nodes: CustomNode[];
65
68
  edges: Edge[];
66
69
  };
67
70
 
68
- export { NodeType, type Suggestion, SuggestionType, type TypedNode, analyze$1 as analyzeOptions, analyze$2 as analyzeSetupScript, analyze$3 as analyzeTemplate, analyze as analyzeTsx, gen, getVisData };
71
+ export { NodeType, type Suggestion, SuggestionType, type TypedNode, analyze$2 as analyzeOptions, analyze$3 as analyzeSetupScript, analyze as analyzeStyle, analyze$4 as analyzeTemplate, analyze$1 as analyzeTsx, gen, getVisData };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import { SFCStyleBlock } from '@vue/compiler-sfc';
1
2
  export { parse } from '@vue/compiler-sfc';
2
3
  import { Edge, Node } from 'vis-network';
3
4
 
4
- declare function analyze$3(content: string): Set<string>;
5
+ declare function analyze$4(content: string): Set<string>;
5
6
 
6
7
  interface TypedNode {
7
8
  label: string;
@@ -18,12 +19,12 @@ declare enum NodeType {
18
19
  fun = "fun"
19
20
  }
20
21
 
21
- declare function analyze$2(content: string, lineOffset?: number, jsx?: boolean): {
22
+ declare function analyze$3(content: string, lineOffset?: number, jsx?: boolean): {
22
23
  nodes: Set<TypedNode>;
23
24
  edges: Map<TypedNode, Set<TypedNode>>;
24
25
  };
25
26
 
26
- declare function analyze$1(content: string, lineOffset?: number, jsx?: boolean): {
27
+ declare function analyze$2(content: string, lineOffset?: number, jsx?: boolean): {
27
28
  graph: {
28
29
  nodes: Set<TypedNode>;
29
30
  edges: Map<TypedNode, Set<TypedNode>>;
@@ -31,7 +32,7 @@ declare function analyze$1(content: string, lineOffset?: number, jsx?: boolean):
31
32
  nodesUsedInTemplate: Set<string>;
32
33
  };
33
34
 
34
- declare function analyze(content: string, type?: "vue" | "react", lineOffset?: number, addInfo?: boolean): {
35
+ declare function analyze$1(content: string, type?: "vue" | "react", lineOffset?: number, addInfo?: boolean): {
35
36
  graph: {
36
37
  nodes: Set<TypedNode>;
37
38
  edges: Map<TypedNode, Set<TypedNode>>;
@@ -39,6 +40,8 @@ declare function analyze(content: string, type?: "vue" | "react", lineOffset?: n
39
40
  nodesUsedInTemplate: Set<string>;
40
41
  };
41
42
 
43
+ declare function analyze(styles: SFCStyleBlock[]): Set<string>;
44
+
42
45
  declare enum SuggestionType {
43
46
  info = "info",
44
47
  warning = "warning",
@@ -52,7 +55,7 @@ interface Suggestion {
52
55
  declare function gen(graph: {
53
56
  nodes: Set<TypedNode>;
54
57
  edges: Map<TypedNode, Set<TypedNode>>;
55
- }, usedNodes: Set<string>): Suggestion[];
58
+ }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): Suggestion[];
56
59
 
57
60
  type CustomNode = Node & {
58
61
  info: TypedNode['info'];
@@ -60,9 +63,9 @@ type CustomNode = Node & {
60
63
  declare function getVisData(graph: {
61
64
  nodes: Set<TypedNode>;
62
65
  edges: Map<TypedNode, Set<TypedNode>>;
63
- }, usedNodes: Set<string>): {
66
+ }, nodesUsedInTemplate: Set<string>, nodesUsedInStyle?: Set<string>): {
64
67
  nodes: CustomNode[];
65
68
  edges: Edge[];
66
69
  };
67
70
 
68
- export { NodeType, type Suggestion, SuggestionType, type TypedNode, analyze$1 as analyzeOptions, analyze$2 as analyzeSetupScript, analyze$3 as analyzeTemplate, analyze as analyzeTsx, gen, getVisData };
71
+ export { NodeType, type Suggestion, SuggestionType, type TypedNode, analyze$2 as analyzeOptions, analyze$3 as analyzeSetupScript, analyze as analyzeStyle, analyze$4 as analyzeTemplate, analyze$1 as analyzeTsx, gen, getVisData };
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ __export(src_exports, {
34
34
  SuggestionType: () => SuggestionType,
35
35
  analyzeOptions: () => analyze3,
36
36
  analyzeSetupScript: () => analyze2,
37
+ analyzeStyle: () => analyze5,
37
38
  analyzeTemplate: () => analyze,
38
39
  analyzeTsx: () => analyze4,
39
40
  gen: () => gen,
@@ -513,7 +514,24 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
513
514
  return;
514
515
  }
515
516
  const watchArgs = /* @__PURE__ */ new Set();
516
- if (hookName === "watch") {
517
+ if (hookName === "provide") {
518
+ traverse2(path.node.expression, {
519
+ Identifier(path1) {
520
+ const binding = path1.scope.getBinding(path1.node.name);
521
+ if (graph.nodes.has(path1.node.name) && (path1.parent.type !== "MemberExpression" && path1.parent.type !== "OptionalMemberExpression" || path1.parent.object === path1.node) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
522
+ const _node = nodeCollection.getNode(path1.node.name);
523
+ if (_node?.info?.used) {
524
+ _node?.info?.used?.add(hookName);
525
+ } else if (_node) {
526
+ _node.info = {
527
+ ..._node?.info,
528
+ used: /* @__PURE__ */ new Set([hookName])
529
+ };
530
+ }
531
+ }
532
+ }
533
+ }, path.scope, path);
534
+ } else if (hookName === "watch") {
517
535
  if (path.node.expression.arguments[0].type === "Identifier") {
518
536
  const binding = path.scope.getBinding(path.node.expression.arguments[0].name);
519
537
  if (graph.nodes.has(path.node.expression.arguments[0].name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
@@ -611,7 +629,8 @@ var vueLifeCycleHooks = [
611
629
  "deactivated",
612
630
  "errorCaptured",
613
631
  "renderTracked",
614
- "renderTriggered"
632
+ "renderTriggered",
633
+ "provide"
615
634
  ];
616
635
  function analyze3(content, lineOffset = 0, jsx = false) {
617
636
  const ast = (0, import_compiler_sfc3.babelParse)(content, { sourceType: "module", plugins: [
@@ -1865,6 +1884,67 @@ function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1865
1884
  };
1866
1885
  }
1867
1886
 
1887
+ // src/analyze/style.ts
1888
+ function lexBinding(content, start) {
1889
+ let state = 0 /* inParens */;
1890
+ let parenDepth = 0;
1891
+ for (let i = start; i < content.length; i++) {
1892
+ const char = content.charAt(i);
1893
+ switch (state) {
1894
+ case 0 /* inParens */:
1895
+ if (char === "'") {
1896
+ state = 1 /* inSingleQuoteString */;
1897
+ } else if (char === '"') {
1898
+ state = 2 /* inDoubleQuoteString */;
1899
+ } else if (char === "(") {
1900
+ parenDepth++;
1901
+ } else if (char === ")") {
1902
+ if (parenDepth > 0) {
1903
+ parenDepth--;
1904
+ } else {
1905
+ return i;
1906
+ }
1907
+ }
1908
+ break;
1909
+ case 1 /* inSingleQuoteString */:
1910
+ if (char === "'") {
1911
+ state = 0 /* inParens */;
1912
+ }
1913
+ break;
1914
+ case 2 /* inDoubleQuoteString */:
1915
+ if (char === '"') {
1916
+ state = 0 /* inParens */;
1917
+ }
1918
+ break;
1919
+ }
1920
+ }
1921
+ return null;
1922
+ }
1923
+ function normalizeExpression(exp) {
1924
+ exp = exp.trim();
1925
+ if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1926
+ return exp.slice(1, -1);
1927
+ }
1928
+ return exp;
1929
+ }
1930
+ var vBindRE = /v-bind\s*\(/g;
1931
+ function analyze5(styles) {
1932
+ const nodes = /* @__PURE__ */ new Set();
1933
+ styles.forEach((style) => {
1934
+ let match;
1935
+ const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1936
+ while (match = vBindRE.exec(content)) {
1937
+ const start = match.index + match[0].length;
1938
+ const end = lexBinding(content, start);
1939
+ if (end !== null) {
1940
+ const variable = normalizeExpression(content.slice(start, end));
1941
+ nodes.add(variable);
1942
+ }
1943
+ }
1944
+ });
1945
+ return nodes;
1946
+ }
1947
+
1868
1948
  // src/suggest/split.ts
1869
1949
  function dfs(graph, node, targets, visited, component) {
1870
1950
  component.add(node);
@@ -2039,7 +2119,8 @@ var SuggestionType = /* @__PURE__ */ ((SuggestionType2) => {
2039
2119
  SuggestionType2["error"] = "error";
2040
2120
  return SuggestionType2;
2041
2121
  })(SuggestionType || {});
2042
- function gen(graph, usedNodes) {
2122
+ function gen(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2123
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2043
2124
  const suggestions = [];
2044
2125
  const splitedGraph = splitGraph(graph.edges);
2045
2126
  splitedGraph.forEach((g) => {
@@ -2103,7 +2184,8 @@ function gen(graph, usedNodes) {
2103
2184
  }
2104
2185
 
2105
2186
  // src/vis.ts
2106
- function getVisData(graph, usedNodes) {
2187
+ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2188
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2107
2189
  const nodes = [];
2108
2190
  const edges = [];
2109
2191
  graph.nodes.forEach((node) => {
@@ -2114,7 +2196,12 @@ function getVisData(graph, usedNodes) {
2114
2196
  group: usedNodes.has(node.label) || node.info?.used?.size ? "used" : "normal",
2115
2197
  title: `${node.info?.used?.size ? `used by ${Array.from(node.info?.used || [])?.map((i) => `\`${i}\``).join(",")}
2116
2198
 
2117
- ` : ""}${usedNodes.has(node.label) ? "used in template\n\n" : ""}${node.info?.comment || ""}`.trim() || void 0,
2199
+ ` : ""}${usedNodes.has(node.label) ? `used in ${[
2200
+ nodesUsedInStyle.has(node.label) ? "style" : "",
2201
+ nodesUsedInTemplate.has(node.label) ? "template" : ""
2202
+ ].filter(Boolean).join(" and ")}
2203
+
2204
+ ` : ""}${node.info?.comment || ""}`.trim() || void 0,
2118
2205
  info: node.info
2119
2206
  });
2120
2207
  });
@@ -2146,6 +2233,7 @@ function getVisData(graph, usedNodes) {
2146
2233
  SuggestionType,
2147
2234
  analyzeOptions,
2148
2235
  analyzeSetupScript,
2236
+ analyzeStyle,
2149
2237
  analyzeTemplate,
2150
2238
  analyzeTsx,
2151
2239
  gen,