expose-kit 0.8.0 → 0.10.0

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/README.md CHANGED
@@ -321,9 +321,61 @@ Notes:
321
321
 
322
322
  ---
323
323
 
324
+ ### `expose fn-inliner`
325
+
326
+ Inline proxy function calls into expressions.
327
+ ```js
328
+ const add = (a, b) => a + b;
329
+ const sum = add(1, 2);
330
+ // after
331
+ const sum = 1 + 2;
332
+ ```
333
+ Example is [here](https://github.com/evex-dev/expose-kit/tree/main/commands/fn-inliner/mocks).
334
+
335
+ ```bash
336
+ expose fn-inliner path/to/file.js --output path/to/file.fn-inliner.js
337
+ ```
338
+
339
+ Args:
340
+ - `--o, --output <file>`
341
+ Output file path
342
+
343
+ Notes:
344
+ - Only inlines functions that return a single expression.
345
+ - Skips return expressions with assignments, functions, blocks, or sequences.
346
+
347
+ ---
348
+
349
+ ### `expose sequence-split`
350
+
351
+ Split sequence expressions into statements.
352
+ ```js
353
+ // before
354
+ a, b, c;
355
+ // after
356
+ a;
357
+ b;
358
+ c;
359
+ ```
360
+ Example is [here](https://github.com/evex-dev/expose-kit/tree/main/commands/sequence-split/mocks).
361
+
362
+ ```bash
363
+ expose sequence-split path/to/file.js --output path/to/file.sequence-split.js
364
+ ```
365
+
366
+ Args:
367
+ - `--o, --output <file>`
368
+ Output file path
369
+
370
+ Notes:
371
+ - Splits safe sequence expressions into standalone statements.
372
+ - Also normalizes single-statement control flow blocks.
373
+
374
+ ---
375
+
324
376
  ### `expose remove-unused`
325
-
326
- Remove unused variabless.
377
+
378
+ Remove unused variabless.
327
379
  ```js
328
380
  // before
329
381
  var a = 0, b = 1;
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // index.ts
4
4
  import { Command } from "commander";
5
- import { dirname as dirname9, join as join9 } from "path";
5
+ import { dirname as dirname11, join as join11 } from "path";
6
6
  import { fileURLToPath } from "url";
7
7
  import chalk4 from "chalk";
8
8
 
@@ -746,11 +746,7 @@ var expand_object_default = createCommand((program2) => {
746
746
  }
747
747
  const loader = loading4("Expanding object access...").start();
748
748
  try {
749
- const { code: output, replacedCount } = await expandObjectAccess(
750
- fileContent,
751
- filename,
752
- targetName
753
- );
749
+ const { code: output, replacedCount } = await expandObjectAccess(fileContent, filename, targetName);
754
750
  writeFileSync3(outputPath, output, "utf8");
755
751
  loader.succeed(
756
752
  `Saved expand-object file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${replacedCount} replacements)`
@@ -932,7 +928,11 @@ var object_packer_default = createCommand((program2) => {
932
928
  }
933
929
  const loader = loading5("Packing object properties...").start();
934
930
  try {
935
- const { code: output, packedCount, removedStatements } = packObjectProperties(fileContent, filename);
931
+ const {
932
+ code: output,
933
+ packedCount,
934
+ removedStatements
935
+ } = packObjectProperties(fileContent, filename);
936
936
  writeFileSync4(outputPath, output, "utf8");
937
937
  loader.succeed(
938
938
  `Saved object-packer file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${packedCount} properties packed, ${removedStatements} statements removed)`
@@ -1525,7 +1525,11 @@ var remove_reassign_default = createCommand((program2) => {
1525
1525
  }
1526
1526
  const loader = loading8("Removing reassign aliases...").start();
1527
1527
  try {
1528
- const { code: output, aliasReplacedCount, wrapperReplacedCount } = removeReassign(fileContent, filename);
1528
+ const {
1529
+ code: output,
1530
+ aliasReplacedCount,
1531
+ wrapperReplacedCount
1532
+ } = removeReassign(fileContent, filename);
1529
1533
  writeFileSync7(outputPath, output, "utf8");
1530
1534
  loader.succeed(
1531
1535
  `Saved remove-reassign file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${aliasReplacedCount} aliases, ${wrapperReplacedCount} calls inlined)`
@@ -1657,6 +1661,371 @@ var remove_unused_default = createCommand((program2) => {
1657
1661
  );
1658
1662
  });
1659
1663
 
1664
+ // commands/fn-inliner/index.ts
1665
+ import { readFileSync as readFileSync10, writeFileSync as writeFileSync9 } from "fs";
1666
+ import { basename as basename9, dirname as dirname9, extname as extname9, join as join9 } from "path";
1667
+ import { parse as parse10 } from "@babel/parser";
1668
+ import traverse9 from "@babel/traverse";
1669
+ import generate9 from "@babel/generator";
1670
+ import * as t8 from "@babel/types";
1671
+ import loading10 from "loading-cli";
1672
+ var walk = patchDefault(traverse9);
1673
+ var createDefaultOutputPath9 = (inputPath) => {
1674
+ const ext = extname9(inputPath);
1675
+ if (!ext) {
1676
+ return `${inputPath}.fn-inliner.js`;
1677
+ }
1678
+ const base = basename9(inputPath, ext);
1679
+ return join9(dirname9(inputPath), `${base}.fn-inliner${ext}`);
1680
+ };
1681
+ var isProxyFunctionExpression = (node) => {
1682
+ return t8.isFunction(node) && node.params.every((param) => t8.isIdentifier(param)) && (t8.isBlockStatement(node.body) && node.body.body.length === 1 && t8.isReturnStatement(node.body.body[0]) && (node.body.body[0].argument === void 0 || t8.isExpression(node.body.body[0].argument) && isProxyValue(node.body.body[0].argument)) || t8.isArrowFunctionExpression(node) && t8.isExpression(node.body) && isProxyValue(node.body));
1683
+ };
1684
+ var isProxyValue = (node) => {
1685
+ if (t8.isFunction(node) || t8.isBlockStatement(node) || t8.isSequenceExpression(node)) {
1686
+ return false;
1687
+ }
1688
+ let isValid = true;
1689
+ walk(node, {
1690
+ "SequenceExpression|BlockStatement|Function|AssignmentExpression"(path) {
1691
+ isValid = false;
1692
+ path.stop();
1693
+ },
1694
+ noScope: true
1695
+ });
1696
+ return isValid;
1697
+ };
1698
+ var copyExpression = (expression) => {
1699
+ return t8.cloneNode(expression, true);
1700
+ };
1701
+ var ProxyFunction = class {
1702
+ expression;
1703
+ constructor(expression) {
1704
+ this.expression = expression;
1705
+ }
1706
+ getReplacement(args) {
1707
+ const expression = t8.isExpression(this.expression.body) ? copyExpression(this.expression.body) : this.expression.body.body[0].argument ? copyExpression(this.expression.body.body[0].argument) : t8.identifier("undefined");
1708
+ this.replaceParameters(expression, args);
1709
+ return expression;
1710
+ }
1711
+ replaceParameters(expression, args) {
1712
+ const paramMap = new Map(
1713
+ this.expression.params.map((param, index) => [
1714
+ param.name,
1715
+ args[index] ?? t8.identifier("undefined")
1716
+ ])
1717
+ );
1718
+ const pathsToReplace = [];
1719
+ walk(expression, {
1720
+ enter(path) {
1721
+ if (t8.isIdentifier(path.node) && !(path.parentPath?.isMemberExpression() && path.key === "property") && paramMap.has(path.node.name)) {
1722
+ const replacement = paramMap.get(path.node.name);
1723
+ pathsToReplace.push([path, replacement]);
1724
+ }
1725
+ },
1726
+ noScope: true
1727
+ });
1728
+ for (const [path, replacement] of pathsToReplace) {
1729
+ path.replaceWith(t8.cloneNode(replacement, true));
1730
+ }
1731
+ }
1732
+ };
1733
+ var ProxyFunctionVariable = class extends ProxyFunction {
1734
+ binding;
1735
+ constructor(binding, expression) {
1736
+ super(expression);
1737
+ this.binding = binding;
1738
+ }
1739
+ getCalls() {
1740
+ return this.binding.referencePaths;
1741
+ }
1742
+ replaceCall(path) {
1743
+ if (!path.parentPath || !path.parentPath.isCallExpression() || path.key !== "callee") {
1744
+ return false;
1745
+ }
1746
+ const argumentNodes = path.parentPath.node.arguments;
1747
+ const args = [];
1748
+ for (const argument of argumentNodes) {
1749
+ if (!t8.isExpression(argument)) {
1750
+ return false;
1751
+ }
1752
+ args.push(t8.cloneNode(argument, true));
1753
+ }
1754
+ const expression = this.getReplacement(args);
1755
+ path.parentPath.replaceWith(expression);
1756
+ return true;
1757
+ }
1758
+ };
1759
+ var collectProxyFunctions = (ast) => {
1760
+ const proxies = [];
1761
+ walk(ast, {
1762
+ FunctionDeclaration(path) {
1763
+ if (!path.node.id) return;
1764
+ if (!isProxyFunctionExpression(path.node)) return;
1765
+ const binding = path.scope.getBinding(path.node.id.name);
1766
+ if (!binding || !binding.constant) return;
1767
+ proxies.push(new ProxyFunctionVariable(binding, path.node));
1768
+ },
1769
+ VariableDeclarator(path) {
1770
+ if (!t8.isIdentifier(path.node.id)) return;
1771
+ const init = path.node.init;
1772
+ if (!init || !isProxyFunctionExpression(init)) return;
1773
+ const binding = path.scope.getBinding(path.node.id.name);
1774
+ if (!binding || !binding.constant) return;
1775
+ proxies.push(new ProxyFunctionVariable(binding, init));
1776
+ }
1777
+ });
1778
+ return proxies;
1779
+ };
1780
+ var inlineProxyFunctions = (code, filename) => {
1781
+ const ast = parse10(code, createParseOptions(filename));
1782
+ const proxies = collectProxyFunctions(ast);
1783
+ let replacedCount = 0;
1784
+ for (const proxy of proxies) {
1785
+ for (const referencePath of proxy.getCalls()) {
1786
+ if (referencePath.removed) continue;
1787
+ if (proxy.replaceCall(referencePath)) {
1788
+ replacedCount += 1;
1789
+ }
1790
+ }
1791
+ }
1792
+ return {
1793
+ code: patchDefault(generate9)(ast).code,
1794
+ replacedCount
1795
+ };
1796
+ };
1797
+ var fn_inliner_default = createCommand((program2) => {
1798
+ program2.command("fn-inliner").description("Inline proxy function calls into expressions").argument("[file]", "The file to transform").option("--input, --file <file>", "The file to transform").option("--o, --output <file>", "Output file path").option("--unlimited", "Unlimited timeout").action(
1799
+ async (fileArgument, options) => {
1800
+ await timeout(
1801
+ async ({ finish }) => {
1802
+ const filename = fileArgument ?? options.file ?? await createPrompt("Enter the file path:");
1803
+ if (!filename) {
1804
+ showError("No file provided");
1805
+ return finish();
1806
+ }
1807
+ try {
1808
+ const fileContent = readFileSync10(filename, "utf8");
1809
+ const defaultOutputPath = createDefaultOutputPath9(filename);
1810
+ let outputPath = options.output;
1811
+ if (!outputPath) {
1812
+ const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
1813
+ outputPath = promptPath || defaultOutputPath;
1814
+ }
1815
+ const loader = loading10("Inlining proxy functions...").start();
1816
+ try {
1817
+ const { code: output, replacedCount } = inlineProxyFunctions(
1818
+ fileContent,
1819
+ filename
1820
+ );
1821
+ writeFileSync9(outputPath, output, "utf8");
1822
+ loader.succeed(
1823
+ `Saved fn-inliner file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${replacedCount} replacements)`
1824
+ );
1825
+ return finish();
1826
+ } catch (error) {
1827
+ loader.fail("Failed to apply fn-inliner transform");
1828
+ showError(
1829
+ `Error transforming file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
1830
+ );
1831
+ return finish();
1832
+ }
1833
+ } catch (error) {
1834
+ showError(
1835
+ `Error reading file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
1836
+ );
1837
+ return finish();
1838
+ }
1839
+ },
1840
+ options.unlimited ? null : 120 * 1e3
1841
+ );
1842
+ }
1843
+ );
1844
+ });
1845
+
1846
+ // commands/sequence-split/index.ts
1847
+ import { readFileSync as readFileSync11, writeFileSync as writeFileSync10 } from "fs";
1848
+ import { basename as basename10, dirname as dirname10, extname as extname10, join as join10 } from "path";
1849
+ import { parse as parse11 } from "@babel/parser";
1850
+ import traverse10 from "@babel/traverse";
1851
+ import generate10 from "@babel/generator";
1852
+ import * as t9 from "@babel/types";
1853
+ import loading11 from "loading-cli";
1854
+ var walk2 = patchDefault(traverse10);
1855
+ var createDefaultOutputPath10 = (inputPath) => {
1856
+ const ext = extname10(inputPath);
1857
+ if (!ext) {
1858
+ return `${inputPath}.sequence-split.js`;
1859
+ }
1860
+ const base = basename10(inputPath, ext);
1861
+ return join10(dirname10(inputPath), `${base}.sequence-split${ext}`);
1862
+ };
1863
+ var isExcluded = (node) => {
1864
+ return t9.isIdentifier(node) && node.name === "eval";
1865
+ };
1866
+ var sequenceSplit = (code, filename) => {
1867
+ const ast = parse11(code, createParseOptions(filename));
1868
+ let changedCount = 0;
1869
+ const markChanged = () => {
1870
+ changedCount += 1;
1871
+ };
1872
+ walk2(ast, {
1873
+ ConditionalExpression(path) {
1874
+ if (!path.parentPath || !path.parentPath.isExpressionStatement()) return;
1875
+ const replacement = t9.ifStatement(
1876
+ path.node.test,
1877
+ t9.expressionStatement(path.node.consequent),
1878
+ t9.expressionStatement(path.node.alternate)
1879
+ );
1880
+ if (path.parentPath.parentPath && path.parentPath.parentPath.key === "alternate" && path.parentPath.parentPath.isBlockStatement() && path.parentPath.parentPath.node.body.length === 1) {
1881
+ path.parentPath.parentPath.replaceWith(replacement);
1882
+ } else {
1883
+ path.parentPath.replaceWith(replacement);
1884
+ }
1885
+ path.skip();
1886
+ markChanged();
1887
+ },
1888
+ LogicalExpression(path) {
1889
+ if (!path.parentPath || !path.parentPath.isExpressionStatement()) return;
1890
+ if (path.node.operator !== "&&" && path.node.operator !== "||") return;
1891
+ const test = path.node.operator === "&&" ? path.node.left : t9.unaryExpression("!", path.node.left);
1892
+ const replacement = t9.ifStatement(
1893
+ test,
1894
+ t9.expressionStatement(path.node.right)
1895
+ );
1896
+ if (path.parentPath.parentPath && path.parentPath.parentPath.key === "alternate" && path.parentPath.parentPath.isBlockStatement() && path.parentPath.parentPath.node.body.length === 1) {
1897
+ path.parentPath.parentPath.replaceWith(replacement);
1898
+ } else {
1899
+ path.parentPath.replaceWith(replacement);
1900
+ }
1901
+ path.skip();
1902
+ markChanged();
1903
+ },
1904
+ "ForStatement|WhileStatement|DoWhileStatement"(path) {
1905
+ if (!path.isForStatement() && !path.isWhileStatement() && !path.isDoWhileStatement()) return;
1906
+ if (t9.isBlockStatement(path.node.body)) return;
1907
+ path.node.body = t9.blockStatement([path.node.body]);
1908
+ markChanged();
1909
+ },
1910
+ IfStatement(path) {
1911
+ if (!t9.isBlockStatement(path.node.consequent)) {
1912
+ path.node.consequent = t9.blockStatement([path.node.consequent]);
1913
+ markChanged();
1914
+ }
1915
+ if (path.node.alternate && !t9.isBlockStatement(path.node.alternate) && !t9.isIfStatement(path.node.alternate)) {
1916
+ path.node.alternate = t9.blockStatement([path.node.alternate]);
1917
+ markChanged();
1918
+ }
1919
+ },
1920
+ VariableDeclaration(path) {
1921
+ if (path.node.declarations.length <= 1) return;
1922
+ const replacements = path.node.declarations.map(
1923
+ (declaration) => t9.variableDeclaration(path.node.kind, [declaration])
1924
+ );
1925
+ if (path.parentPath?.isForStatement() && path.parentKey === "init") {
1926
+ const lastDeclaration = replacements.pop();
1927
+ if (lastDeclaration) {
1928
+ path.parentPath.insertBefore(replacements);
1929
+ path.parentPath.node.init = lastDeclaration;
1930
+ }
1931
+ } else {
1932
+ path.replaceWithMultiple(replacements);
1933
+ }
1934
+ markChanged();
1935
+ },
1936
+ SequenceExpression(path) {
1937
+ const expressions = path.node.expressions;
1938
+ if (expressions.length === 1 && expressions[0]) {
1939
+ path.replaceWith(expressions[0]);
1940
+ markChanged();
1941
+ return;
1942
+ }
1943
+ let outerPath = path;
1944
+ while (!t9.isStatement(outerPath.node)) {
1945
+ const parent = outerPath.parentPath;
1946
+ if (!parent) return;
1947
+ if (parent.isConditionalExpression() && (outerPath.key === "consequent" || outerPath.key === "alternate") || parent.isLogicalExpression() && outerPath.key === "right" || parent.isForStatement() && (outerPath.key === "test" || outerPath.key === "update") || parent.isDoWhileStatement() && outerPath.key === "test" || parent.isArrowFunctionExpression() && outerPath.key === "body") {
1948
+ return;
1949
+ }
1950
+ outerPath = parent;
1951
+ }
1952
+ const lastExpression = expressions[expressions.length - 1];
1953
+ if (lastExpression && isExcluded(lastExpression)) {
1954
+ const firstExpressions = expressions.splice(0, expressions.length - 2);
1955
+ if (firstExpressions.length > 0) {
1956
+ const expressionStatements = firstExpressions.map(
1957
+ (expression) => t9.expressionStatement(expression)
1958
+ );
1959
+ outerPath.insertBefore(expressionStatements);
1960
+ markChanged();
1961
+ }
1962
+ } else {
1963
+ const finalExpression = expressions.splice(expressions.length - 1, 1)[0];
1964
+ const expressionStatements = expressions.map(
1965
+ (expression) => t9.expressionStatement(expression)
1966
+ );
1967
+ outerPath.insertBefore(expressionStatements);
1968
+ if (finalExpression) {
1969
+ path.replaceWith(finalExpression);
1970
+ }
1971
+ markChanged();
1972
+ }
1973
+ }
1974
+ });
1975
+ return {
1976
+ code: patchDefault(generate10)(ast).code,
1977
+ changedCount
1978
+ };
1979
+ };
1980
+ var sequence_split_default = createCommand((program2) => {
1981
+ program2.command("sequence-split").description("Split sequence expressions into statements").argument("[file]", "The file to transform").option("--input, --file <file>", "The file to transform").option("--o, --output <file>", "Output file path").option("--unlimited", "Unlimited timeout").action(
1982
+ async (fileArgument, options) => {
1983
+ await timeout(
1984
+ async ({ finish }) => {
1985
+ const filename = fileArgument ?? options.file ?? await createPrompt("Enter the file path:");
1986
+ if (!filename) {
1987
+ showError("No file provided");
1988
+ return finish();
1989
+ }
1990
+ try {
1991
+ const fileContent = readFileSync11(filename, "utf8");
1992
+ const defaultOutputPath = createDefaultOutputPath10(filename);
1993
+ let outputPath = options.output;
1994
+ if (!outputPath) {
1995
+ const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
1996
+ outputPath = promptPath || defaultOutputPath;
1997
+ }
1998
+ const loader = loading11("Splitting sequences...").start();
1999
+ try {
2000
+ const { code: output, changedCount } = sequenceSplit(
2001
+ fileContent,
2002
+ filename
2003
+ );
2004
+ writeFileSync10(outputPath, output, "utf8");
2005
+ loader.succeed(
2006
+ `Saved sequence-split file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${changedCount} transforms)`
2007
+ );
2008
+ return finish();
2009
+ } catch (error) {
2010
+ loader.fail("Failed to apply sequence-split transform");
2011
+ showError(
2012
+ `Error transforming file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
2013
+ );
2014
+ return finish();
2015
+ }
2016
+ } catch (error) {
2017
+ showError(
2018
+ `Error reading file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
2019
+ );
2020
+ return finish();
2021
+ }
2022
+ },
2023
+ options.unlimited ? null : 120 * 1e3
2024
+ );
2025
+ }
2026
+ );
2027
+ });
2028
+
1660
2029
  // utils/cli/showCredit.ts
1661
2030
  import chalk3 from "chalk";
1662
2031
  var beautify = (strings, ...values) => {
@@ -1681,10 +2050,10 @@ var calmGradienrain = (text) => {
1681
2050
  const endHue = 300;
1682
2051
  const saturation = 0.45;
1683
2052
  const value = 0.8;
1684
- const ease = (t8) => t8 * t8 * (3 - 2 * t8);
2053
+ const ease = (t10) => t10 * t10 * (3 - 2 * t10);
1685
2054
  return text.split("").map((char, i) => {
1686
- const t8 = ease(i / Math.max(text.length - 1, 1));
1687
- const hue = startHue + (endHue - startHue) * t8;
2055
+ const t10 = ease(i / Math.max(text.length - 1, 1));
2056
+ const hue = startHue + (endHue - startHue) * t10;
1688
2057
  const c = value * saturation;
1689
2058
  const h = hue / 60;
1690
2059
  const x = c * (1 - Math.abs(h % 2 - 1));
@@ -1708,10 +2077,10 @@ ${calmGradienrain(`Expose Kit v${VERSION}`)}
1708
2077
  `;
1709
2078
 
1710
2079
  // index.ts
1711
- import { readFileSync as readFileSync10 } from "fs";
2080
+ import { readFileSync as readFileSync12 } from "fs";
1712
2081
  var __filename = fileURLToPath(import.meta.url);
1713
- var __dirname = dirname9(__filename);
1714
- var pkg = JSON.parse(readFileSync10(join9(__dirname, "package.json"), "utf8"));
2082
+ var __dirname = dirname11(__filename);
2083
+ var pkg = JSON.parse(readFileSync12(join11(__dirname, "package.json"), "utf8"));
1715
2084
  console.log(showCredit(pkg.version));
1716
2085
  console.log();
1717
2086
  var program = new Command();
@@ -1729,7 +2098,9 @@ var commands = [
1729
2098
  pre_evaluate_default,
1730
2099
  remove_updater_default,
1731
2100
  remove_reassign_default,
1732
- remove_unused_default
2101
+ fn_inliner_default,
2102
+ remove_unused_default,
2103
+ sequence_split_default
1733
2104
  ];
1734
2105
  for (const command of commands) {
1735
2106
  command(program);
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expose-kit",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "author": "EdamAmex <edame8080@gmail.com> (https://github.com/EdamAme-x)",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expose-kit",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "author": "EdamAmex <edame8080@gmail.com> (https://github.com/EdamAme-x)",