vue-hook-optimizer 0.0.54 → 0.0.56

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -471,7 +471,24 @@ function processSetup(ast, parentScope, parentPath, _spread, _lineOffset = 0) {
471
471
  return;
472
472
  }
473
473
  const watchArgs = /* @__PURE__ */ new Set();
474
- if (hookName === "watch") {
474
+ if (hookName === "provide") {
475
+ traverse2(path.node.expression, {
476
+ Identifier(path1) {
477
+ const binding = path1.scope.getBinding(path1.node.name);
478
+ 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)) {
479
+ const _node = nodeCollection.getNode(path1.node.name);
480
+ if (_node?.info?.used) {
481
+ _node?.info?.used?.add(hookName);
482
+ } else if (_node) {
483
+ _node.info = {
484
+ ..._node?.info,
485
+ used: /* @__PURE__ */ new Set([hookName])
486
+ };
487
+ }
488
+ }
489
+ }
490
+ }, path.scope, path);
491
+ } else if (hookName === "watch") {
475
492
  if (path.node.expression.arguments[0].type === "Identifier") {
476
493
  const binding = path.scope.getBinding(path.node.expression.arguments[0].name);
477
494
  if (graph.nodes.has(path.node.expression.arguments[0].name) && (binding?.scope.block.type === "Program" || parentScope === binding?.scope)) {
@@ -569,7 +586,8 @@ var vueLifeCycleHooks = [
569
586
  "deactivated",
570
587
  "errorCaptured",
571
588
  "renderTracked",
572
- "renderTriggered"
589
+ "renderTriggered",
590
+ "provide"
573
591
  ];
574
592
  function analyze3(content, lineOffset = 0, jsx = false) {
575
593
  const ast = babelParse3(content, { sourceType: "module", plugins: [
@@ -1823,6 +1841,67 @@ function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1823
1841
  };
1824
1842
  }
1825
1843
 
1844
+ // src/analyze/style.ts
1845
+ function lexBinding(content, start) {
1846
+ let state = 0 /* inParens */;
1847
+ let parenDepth = 0;
1848
+ for (let i = start; i < content.length; i++) {
1849
+ const char = content.charAt(i);
1850
+ switch (state) {
1851
+ case 0 /* inParens */:
1852
+ if (char === "'") {
1853
+ state = 1 /* inSingleQuoteString */;
1854
+ } else if (char === '"') {
1855
+ state = 2 /* inDoubleQuoteString */;
1856
+ } else if (char === "(") {
1857
+ parenDepth++;
1858
+ } else if (char === ")") {
1859
+ if (parenDepth > 0) {
1860
+ parenDepth--;
1861
+ } else {
1862
+ return i;
1863
+ }
1864
+ }
1865
+ break;
1866
+ case 1 /* inSingleQuoteString */:
1867
+ if (char === "'") {
1868
+ state = 0 /* inParens */;
1869
+ }
1870
+ break;
1871
+ case 2 /* inDoubleQuoteString */:
1872
+ if (char === '"') {
1873
+ state = 0 /* inParens */;
1874
+ }
1875
+ break;
1876
+ }
1877
+ }
1878
+ return null;
1879
+ }
1880
+ function normalizeExpression(exp) {
1881
+ exp = exp.trim();
1882
+ if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1883
+ return exp.slice(1, -1);
1884
+ }
1885
+ return exp;
1886
+ }
1887
+ var vBindRE = /v-bind\s*\(/g;
1888
+ function analyze5(styles) {
1889
+ const nodes = /* @__PURE__ */ new Set();
1890
+ styles.forEach((style) => {
1891
+ let match;
1892
+ const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1893
+ while (match = vBindRE.exec(content)) {
1894
+ const start = match.index + match[0].length;
1895
+ const end = lexBinding(content, start);
1896
+ if (end !== null) {
1897
+ const variable = normalizeExpression(content.slice(start, end));
1898
+ nodes.add(variable);
1899
+ }
1900
+ }
1901
+ });
1902
+ return nodes;
1903
+ }
1904
+
1826
1905
  // src/suggest/split.ts
1827
1906
  function dfs(graph, node, targets, visited, component) {
1828
1907
  component.add(node);
@@ -1997,7 +2076,8 @@ var SuggestionType = /* @__PURE__ */ ((SuggestionType2) => {
1997
2076
  SuggestionType2["error"] = "error";
1998
2077
  return SuggestionType2;
1999
2078
  })(SuggestionType || {});
2000
- function gen(graph, usedNodes) {
2079
+ function gen(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2080
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2001
2081
  const suggestions = [];
2002
2082
  const splitedGraph = splitGraph(graph.edges);
2003
2083
  splitedGraph.forEach((g) => {
@@ -2061,7 +2141,8 @@ function gen(graph, usedNodes) {
2061
2141
  }
2062
2142
 
2063
2143
  // src/vis.ts
2064
- function getVisData(graph, usedNodes) {
2144
+ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2145
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2065
2146
  const nodes = [];
2066
2147
  const edges = [];
2067
2148
  graph.nodes.forEach((node) => {
@@ -2072,7 +2153,12 @@ function getVisData(graph, usedNodes) {
2072
2153
  group: usedNodes.has(node.label) || node.info?.used?.size ? "used" : "normal",
2073
2154
  title: `${node.info?.used?.size ? `used by ${Array.from(node.info?.used || [])?.map((i) => `\`${i}\``).join(",")}
2074
2155
 
2075
- ` : ""}${usedNodes.has(node.label) ? "used in template\n\n" : ""}${node.info?.comment || ""}`.trim() || void 0,
2156
+ ` : ""}${usedNodes.has(node.label) ? `used in ${[
2157
+ nodesUsedInStyle.has(node.label) ? "style" : "",
2158
+ nodesUsedInTemplate.has(node.label) ? "template" : ""
2159
+ ].filter(Boolean).join(" and ")}
2160
+
2161
+ ` : ""}${node.info?.comment || ""}`.trim() || void 0,
2076
2162
  info: node.info
2077
2163
  });
2078
2164
  });
@@ -2103,6 +2189,7 @@ export {
2103
2189
  SuggestionType,
2104
2190
  analyze3 as analyzeOptions,
2105
2191
  analyze2 as analyzeSetupScript,
2192
+ analyze5 as analyzeStyle,
2106
2193
  analyze as analyzeTemplate,
2107
2194
  analyze4 as analyzeTsx,
2108
2195
  gen,