expose-kit 0.6.0 → 0.8.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 +63 -0
- package/dist/index.js +356 -20
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -258,6 +258,69 @@ Notes:
|
|
|
258
258
|
|
|
259
259
|
---
|
|
260
260
|
|
|
261
|
+
### `expose remove-updater`
|
|
262
|
+
|
|
263
|
+
Replace safe update expressions with += or -=.
|
|
264
|
+
```js
|
|
265
|
+
// before
|
|
266
|
+
a++;
|
|
267
|
+
--b;
|
|
268
|
+
// after
|
|
269
|
+
a += 1;
|
|
270
|
+
b -= 1;
|
|
271
|
+
```
|
|
272
|
+
Example is [here](https://github.com/evex-dev/expose-kit/tree/main/commands/remove-updater/mocks).
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
expose remove-updater path/to/file.js --output path/to/file.remove-updater.js
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Args:
|
|
279
|
+
- `--o, --output <file>`
|
|
280
|
+
Output file path
|
|
281
|
+
|
|
282
|
+
Notes:
|
|
283
|
+
- Only replaces update expressions whose value is not used.
|
|
284
|
+
- Safe for expression statements and for-loop update clauses.
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
### `expose remove-reassign`
|
|
289
|
+
|
|
290
|
+
Inline safe alias assignments and wrapper calls.
|
|
291
|
+
```js
|
|
292
|
+
const a = 0;
|
|
293
|
+
const b = a;
|
|
294
|
+
const c = b;
|
|
295
|
+
console.log(c); // => console.log(a);
|
|
296
|
+
```
|
|
297
|
+
```js
|
|
298
|
+
function a(arg) {
|
|
299
|
+
return b(arg);
|
|
300
|
+
}
|
|
301
|
+
function c(arg) {
|
|
302
|
+
return d[arg];
|
|
303
|
+
}
|
|
304
|
+
a(0); // => b(0)
|
|
305
|
+
c(0); // => d[0]
|
|
306
|
+
```
|
|
307
|
+
Example is [here](https://github.com/evex-dev/expose-kit/tree/main/commands/remove-reassign/mocks).
|
|
308
|
+
|
|
309
|
+
```bash
|
|
310
|
+
expose remove-reassign path/to/file.js --output path/to/file.remove-reassign.js
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Args:
|
|
314
|
+
- `--o, --output <file>`
|
|
315
|
+
Output file path
|
|
316
|
+
|
|
317
|
+
Notes:
|
|
318
|
+
- Only inlines const/immutable alias chains.
|
|
319
|
+
- Skips object shorthand replacements and shadowed bindings.
|
|
320
|
+
- Wrapper inlining is limited to single-return passthroughs.
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
261
324
|
### `expose remove-unused`
|
|
262
325
|
|
|
263
326
|
Remove unused variabless.
|
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
|
|
5
|
+
import { dirname as dirname9, join as join9 } from "path";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
7
7
|
import chalk4 from "chalk";
|
|
8
8
|
|
|
@@ -1217,7 +1217,7 @@ var pre_evaluate_default = createCommand((program2) => {
|
|
|
1217
1217
|
);
|
|
1218
1218
|
});
|
|
1219
1219
|
|
|
1220
|
-
// commands/remove-
|
|
1220
|
+
// commands/remove-updater/index.ts
|
|
1221
1221
|
import { readFileSync as readFileSync7, writeFileSync as writeFileSync6 } from "fs";
|
|
1222
1222
|
import { basename as basename6, dirname as dirname6, extname as extname6, join as join6 } from "path";
|
|
1223
1223
|
import { parse as parse7 } from "@babel/parser";
|
|
@@ -1228,19 +1228,353 @@ import loading7 from "loading-cli";
|
|
|
1228
1228
|
var createDefaultOutputPath6 = (inputPath) => {
|
|
1229
1229
|
const ext = extname6(inputPath);
|
|
1230
1230
|
if (!ext) {
|
|
1231
|
-
return `${inputPath}.remove-
|
|
1231
|
+
return `${inputPath}.remove-updater.js`;
|
|
1232
1232
|
}
|
|
1233
1233
|
const base = basename6(inputPath, ext);
|
|
1234
|
-
return join6(dirname6(inputPath), `${base}.remove-
|
|
1234
|
+
return join6(dirname6(inputPath), `${base}.remove-updater${ext}`);
|
|
1235
1235
|
};
|
|
1236
|
-
var
|
|
1236
|
+
var isSafeStandaloneUpdate = (path) => {
|
|
1237
|
+
const parent = path.parentPath;
|
|
1238
|
+
if (!parent) return false;
|
|
1239
|
+
if (parent.isExpressionStatement()) return true;
|
|
1240
|
+
if (parent.isForStatement() && path.key === "update") return true;
|
|
1241
|
+
return false;
|
|
1242
|
+
};
|
|
1243
|
+
var removeUpdaters = (code, filename) => {
|
|
1237
1244
|
const ast = parse7(code, createParseOptions(filename));
|
|
1238
|
-
let
|
|
1245
|
+
let replacedCount = 0;
|
|
1239
1246
|
patchDefault(traverse6)(ast, {
|
|
1247
|
+
UpdateExpression(path) {
|
|
1248
|
+
if (!isSafeStandaloneUpdate(path)) return;
|
|
1249
|
+
const operator = path.node.operator === "++" ? "+=" : "-=";
|
|
1250
|
+
const left = t5.cloneNode(path.node.argument, true);
|
|
1251
|
+
const replacement = t5.assignmentExpression(
|
|
1252
|
+
operator,
|
|
1253
|
+
left,
|
|
1254
|
+
t5.numericLiteral(1)
|
|
1255
|
+
);
|
|
1256
|
+
path.replaceWith(replacement);
|
|
1257
|
+
replacedCount += 1;
|
|
1258
|
+
}
|
|
1259
|
+
});
|
|
1260
|
+
return {
|
|
1261
|
+
code: patchDefault(generate6)(ast).code,
|
|
1262
|
+
replacedCount
|
|
1263
|
+
};
|
|
1264
|
+
};
|
|
1265
|
+
var remove_updater_default = createCommand((program2) => {
|
|
1266
|
+
program2.command("remove-updater").description("Replace safe update expressions with += or -=").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(
|
|
1267
|
+
async (fileArgument, options) => {
|
|
1268
|
+
await timeout(
|
|
1269
|
+
async ({ finish }) => {
|
|
1270
|
+
const filename = fileArgument ?? options.file ?? await createPrompt("Enter the file path:");
|
|
1271
|
+
if (!filename) {
|
|
1272
|
+
showError("No file provided");
|
|
1273
|
+
return finish();
|
|
1274
|
+
}
|
|
1275
|
+
try {
|
|
1276
|
+
const fileContent = readFileSync7(filename, "utf8");
|
|
1277
|
+
const defaultOutputPath = createDefaultOutputPath6(filename);
|
|
1278
|
+
let outputPath = options.output;
|
|
1279
|
+
if (!outputPath) {
|
|
1280
|
+
const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
|
|
1281
|
+
outputPath = promptPath || defaultOutputPath;
|
|
1282
|
+
}
|
|
1283
|
+
const loader = loading7("Removing update expressions...").start();
|
|
1284
|
+
try {
|
|
1285
|
+
const { code: output, replacedCount } = removeUpdaters(
|
|
1286
|
+
fileContent,
|
|
1287
|
+
filename
|
|
1288
|
+
);
|
|
1289
|
+
writeFileSync6(outputPath, output, "utf8");
|
|
1290
|
+
loader.succeed(
|
|
1291
|
+
`Saved remove-updater file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${replacedCount} updates replaced)`
|
|
1292
|
+
);
|
|
1293
|
+
return finish();
|
|
1294
|
+
} catch (error) {
|
|
1295
|
+
loader.fail("Failed to apply remove-updater transform");
|
|
1296
|
+
showError(
|
|
1297
|
+
`Error transforming file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1298
|
+
);
|
|
1299
|
+
return finish();
|
|
1300
|
+
}
|
|
1301
|
+
} catch (error) {
|
|
1302
|
+
showError(
|
|
1303
|
+
`Error reading file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1304
|
+
);
|
|
1305
|
+
return finish();
|
|
1306
|
+
}
|
|
1307
|
+
},
|
|
1308
|
+
options.unlimited ? null : 120 * 1e3
|
|
1309
|
+
);
|
|
1310
|
+
}
|
|
1311
|
+
);
|
|
1312
|
+
});
|
|
1313
|
+
|
|
1314
|
+
// commands/remove-reassign/index.ts
|
|
1315
|
+
import { readFileSync as readFileSync8, writeFileSync as writeFileSync7 } from "fs";
|
|
1316
|
+
import { basename as basename7, dirname as dirname7, extname as extname7, join as join7 } from "path";
|
|
1317
|
+
import { parse as parse8 } from "@babel/parser";
|
|
1318
|
+
import traverse7 from "@babel/traverse";
|
|
1319
|
+
import generate7 from "@babel/generator";
|
|
1320
|
+
import * as t6 from "@babel/types";
|
|
1321
|
+
import loading8 from "loading-cli";
|
|
1322
|
+
var createDefaultOutputPath7 = (inputPath) => {
|
|
1323
|
+
const ext = extname7(inputPath);
|
|
1324
|
+
if (!ext) {
|
|
1325
|
+
return `${inputPath}.remove-reassign.js`;
|
|
1326
|
+
}
|
|
1327
|
+
const base = basename7(inputPath, ext);
|
|
1328
|
+
return join7(dirname7(inputPath), `${base}.remove-reassign${ext}`);
|
|
1329
|
+
};
|
|
1330
|
+
var isShorthandObjectKey = (path) => {
|
|
1331
|
+
const parent = path.parentPath;
|
|
1332
|
+
if (!parent || !parent.isObjectProperty()) return false;
|
|
1333
|
+
return parent.node.shorthand && parent.get("key") === path;
|
|
1334
|
+
};
|
|
1335
|
+
var resolveFinalAlias = (binding, getBinding) => {
|
|
1336
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1337
|
+
let currentBinding = binding;
|
|
1338
|
+
let currentName = null;
|
|
1339
|
+
while (currentBinding && !visited.has(currentBinding)) {
|
|
1340
|
+
visited.add(currentBinding);
|
|
1341
|
+
if (!t6.isVariableDeclarator(currentBinding.path.node)) break;
|
|
1342
|
+
const init = currentBinding.path.node.init;
|
|
1343
|
+
if (!init || !t6.isIdentifier(init)) break;
|
|
1344
|
+
const nextBinding = getBinding(init.name);
|
|
1345
|
+
if (!nextBinding || !nextBinding.constant) break;
|
|
1346
|
+
currentBinding = nextBinding;
|
|
1347
|
+
currentName = init.name;
|
|
1348
|
+
}
|
|
1349
|
+
if (!currentBinding || !currentName) {
|
|
1350
|
+
return null;
|
|
1351
|
+
}
|
|
1352
|
+
return { targetBinding: currentBinding, targetName: currentName };
|
|
1353
|
+
};
|
|
1354
|
+
var getReturnExpression = (functionPath) => {
|
|
1355
|
+
const body = functionPath.node.body;
|
|
1356
|
+
if (!t6.isBlockStatement(body)) {
|
|
1357
|
+
return body;
|
|
1358
|
+
}
|
|
1359
|
+
if (body.body.length !== 1) return null;
|
|
1360
|
+
const statement = body.body[0];
|
|
1361
|
+
if (!t6.isReturnStatement(statement) || !statement.argument) return null;
|
|
1362
|
+
return statement.argument;
|
|
1363
|
+
};
|
|
1364
|
+
var getWrapperInfo = (functionPath, wrapperBinding) => {
|
|
1365
|
+
const params = functionPath.node.params;
|
|
1366
|
+
if (params.length === 0) return null;
|
|
1367
|
+
const paramNames = [];
|
|
1368
|
+
for (const param of params) {
|
|
1369
|
+
if (!t6.isIdentifier(param)) return null;
|
|
1370
|
+
paramNames.push(param.name);
|
|
1371
|
+
}
|
|
1372
|
+
const expression = getReturnExpression(functionPath);
|
|
1373
|
+
if (!expression) return null;
|
|
1374
|
+
if (t6.isCallExpression(expression)) {
|
|
1375
|
+
if (!t6.isIdentifier(expression.callee)) return null;
|
|
1376
|
+
if (expression.arguments.length !== paramNames.length) return null;
|
|
1377
|
+
for (let i = 0; i < expression.arguments.length; i++) {
|
|
1378
|
+
const arg = expression.arguments[i];
|
|
1379
|
+
if (!t6.isIdentifier(arg)) return null;
|
|
1380
|
+
if (arg.name !== paramNames[i]) return null;
|
|
1381
|
+
}
|
|
1382
|
+
const targetBinding = functionPath.scope.getBinding(expression.callee.name);
|
|
1383
|
+
if (!targetBinding) return null;
|
|
1384
|
+
return {
|
|
1385
|
+
kind: "call",
|
|
1386
|
+
wrapperBinding,
|
|
1387
|
+
targetName: expression.callee.name,
|
|
1388
|
+
targetBinding,
|
|
1389
|
+
paramNames
|
|
1390
|
+
};
|
|
1391
|
+
}
|
|
1392
|
+
if (t6.isMemberExpression(expression) && expression.computed && t6.isIdentifier(expression.object) && t6.isIdentifier(expression.property) && paramNames.length === 1 && expression.property.name === paramNames[0]) {
|
|
1393
|
+
const targetBinding = functionPath.scope.getBinding(expression.object.name);
|
|
1394
|
+
if (!targetBinding) return null;
|
|
1395
|
+
return {
|
|
1396
|
+
kind: "member",
|
|
1397
|
+
wrapperBinding,
|
|
1398
|
+
targetName: expression.object.name,
|
|
1399
|
+
targetBinding
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
return null;
|
|
1403
|
+
};
|
|
1404
|
+
var removeReassign = (code, filename) => {
|
|
1405
|
+
const ast = parse8(code, createParseOptions(filename));
|
|
1406
|
+
const aliases = [];
|
|
1407
|
+
const wrappers = [];
|
|
1408
|
+
patchDefault(traverse7)(ast, {
|
|
1409
|
+
VariableDeclarator(path) {
|
|
1410
|
+
if (!t6.isIdentifier(path.node.id)) return;
|
|
1411
|
+
if (!path.node.init || !t6.isIdentifier(path.node.init)) return;
|
|
1412
|
+
const binding = path.scope.getBinding(path.node.id.name);
|
|
1413
|
+
if (!binding || !binding.constant) return;
|
|
1414
|
+
const targetBinding = path.scope.getBinding(path.node.init.name);
|
|
1415
|
+
if (!targetBinding || !targetBinding.constant) return;
|
|
1416
|
+
const resolved = resolveFinalAlias(
|
|
1417
|
+
binding,
|
|
1418
|
+
(name) => path.scope.getBinding(name)
|
|
1419
|
+
);
|
|
1420
|
+
if (!resolved) return;
|
|
1421
|
+
aliases.push({
|
|
1422
|
+
aliasBinding: binding,
|
|
1423
|
+
targetBinding: resolved.targetBinding,
|
|
1424
|
+
targetName: resolved.targetName
|
|
1425
|
+
});
|
|
1426
|
+
},
|
|
1427
|
+
FunctionDeclaration(path) {
|
|
1428
|
+
if (!path.node.id) return;
|
|
1429
|
+
const binding = path.scope.getBinding(path.node.id.name);
|
|
1430
|
+
if (!binding) return;
|
|
1431
|
+
const wrapper = getWrapperInfo(path, binding);
|
|
1432
|
+
if (wrapper) wrappers.push(wrapper);
|
|
1433
|
+
}
|
|
1434
|
+
});
|
|
1435
|
+
patchDefault(traverse7)(ast, {
|
|
1436
|
+
VariableDeclarator(path) {
|
|
1437
|
+
if (!t6.isIdentifier(path.node.id)) return;
|
|
1438
|
+
const initPath = path.get("init");
|
|
1439
|
+
if (!initPath || !initPath.isFunctionExpression() && !initPath.isArrowFunctionExpression()) {
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
const binding = path.scope.getBinding(path.node.id.name);
|
|
1443
|
+
if (!binding || !binding.constant) return;
|
|
1444
|
+
const wrapper = getWrapperInfo(
|
|
1445
|
+
initPath,
|
|
1446
|
+
binding
|
|
1447
|
+
);
|
|
1448
|
+
if (wrapper) wrappers.push(wrapper);
|
|
1449
|
+
}
|
|
1450
|
+
});
|
|
1451
|
+
let aliasReplacedCount = 0;
|
|
1452
|
+
let wrapperReplacedCount = 0;
|
|
1453
|
+
for (const alias of aliases) {
|
|
1454
|
+
for (const referencePath of alias.aliasBinding.referencePaths) {
|
|
1455
|
+
if (isShorthandObjectKey(referencePath)) continue;
|
|
1456
|
+
const targetBinding = referencePath.scope.getBinding(alias.targetName);
|
|
1457
|
+
if (targetBinding !== alias.targetBinding) continue;
|
|
1458
|
+
referencePath.replaceWith(t6.identifier(alias.targetName));
|
|
1459
|
+
aliasReplacedCount += 1;
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
const wrapperMap = /* @__PURE__ */ new Map();
|
|
1463
|
+
for (const wrapper of wrappers) {
|
|
1464
|
+
wrapperMap.set(wrapper.wrapperBinding, wrapper);
|
|
1465
|
+
}
|
|
1466
|
+
patchDefault(traverse7)(ast, {
|
|
1467
|
+
CallExpression(path) {
|
|
1468
|
+
if (!t6.isIdentifier(path.node.callee)) return;
|
|
1469
|
+
const calleeBinding = path.scope.getBinding(path.node.callee.name);
|
|
1470
|
+
if (!calleeBinding) return;
|
|
1471
|
+
const wrapper = wrapperMap.get(calleeBinding);
|
|
1472
|
+
if (!wrapper) return;
|
|
1473
|
+
const targetBinding = path.scope.getBinding(wrapper.targetName);
|
|
1474
|
+
if (targetBinding !== wrapper.targetBinding) return;
|
|
1475
|
+
if (wrapper.kind === "call") {
|
|
1476
|
+
if (path.node.arguments.length !== wrapper.paramNames.length) return;
|
|
1477
|
+
for (const arg2 of path.node.arguments) {
|
|
1478
|
+
if (t6.isSpreadElement(arg2)) return;
|
|
1479
|
+
}
|
|
1480
|
+
const nextArgs = path.node.arguments.map(
|
|
1481
|
+
(arg2) => t6.cloneNode(arg2, true)
|
|
1482
|
+
);
|
|
1483
|
+
path.replaceWith(
|
|
1484
|
+
t6.callExpression(t6.identifier(wrapper.targetName), nextArgs)
|
|
1485
|
+
);
|
|
1486
|
+
wrapperReplacedCount += 1;
|
|
1487
|
+
return;
|
|
1488
|
+
}
|
|
1489
|
+
if (path.node.arguments.length !== 1) return;
|
|
1490
|
+
const arg = path.node.arguments[0];
|
|
1491
|
+
if (!t6.isExpression(arg) || t6.isSpreadElement(arg)) return;
|
|
1492
|
+
path.replaceWith(
|
|
1493
|
+
t6.memberExpression(
|
|
1494
|
+
t6.identifier(wrapper.targetName),
|
|
1495
|
+
t6.cloneNode(arg, true),
|
|
1496
|
+
true
|
|
1497
|
+
)
|
|
1498
|
+
);
|
|
1499
|
+
wrapperReplacedCount += 1;
|
|
1500
|
+
}
|
|
1501
|
+
});
|
|
1502
|
+
return {
|
|
1503
|
+
code: patchDefault(generate7)(ast).code,
|
|
1504
|
+
aliasReplacedCount,
|
|
1505
|
+
wrapperReplacedCount
|
|
1506
|
+
};
|
|
1507
|
+
};
|
|
1508
|
+
var remove_reassign_default = createCommand((program2) => {
|
|
1509
|
+
program2.command("remove-reassign").description("Inline safe alias assignments and wrapper calls").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(
|
|
1510
|
+
async (fileArgument, options) => {
|
|
1511
|
+
await timeout(
|
|
1512
|
+
async ({ finish }) => {
|
|
1513
|
+
const filename = fileArgument ?? options.file ?? await createPrompt("Enter the file path:");
|
|
1514
|
+
if (!filename) {
|
|
1515
|
+
showError("No file provided");
|
|
1516
|
+
return finish();
|
|
1517
|
+
}
|
|
1518
|
+
try {
|
|
1519
|
+
const fileContent = readFileSync8(filename, "utf8");
|
|
1520
|
+
const defaultOutputPath = createDefaultOutputPath7(filename);
|
|
1521
|
+
let outputPath = options.output;
|
|
1522
|
+
if (!outputPath) {
|
|
1523
|
+
const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
|
|
1524
|
+
outputPath = promptPath || defaultOutputPath;
|
|
1525
|
+
}
|
|
1526
|
+
const loader = loading8("Removing reassign aliases...").start();
|
|
1527
|
+
try {
|
|
1528
|
+
const { code: output, aliasReplacedCount, wrapperReplacedCount } = removeReassign(fileContent, filename);
|
|
1529
|
+
writeFileSync7(outputPath, output, "utf8");
|
|
1530
|
+
loader.succeed(
|
|
1531
|
+
`Saved remove-reassign file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${aliasReplacedCount} aliases, ${wrapperReplacedCount} calls inlined)`
|
|
1532
|
+
);
|
|
1533
|
+
return finish();
|
|
1534
|
+
} catch (error) {
|
|
1535
|
+
loader.fail("Failed to apply remove-reassign transform");
|
|
1536
|
+
showError(
|
|
1537
|
+
`Error transforming file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1538
|
+
);
|
|
1539
|
+
return finish();
|
|
1540
|
+
}
|
|
1541
|
+
} catch (error) {
|
|
1542
|
+
showError(
|
|
1543
|
+
`Error reading file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1544
|
+
);
|
|
1545
|
+
return finish();
|
|
1546
|
+
}
|
|
1547
|
+
},
|
|
1548
|
+
options.unlimited ? null : 120 * 1e3
|
|
1549
|
+
);
|
|
1550
|
+
}
|
|
1551
|
+
);
|
|
1552
|
+
});
|
|
1553
|
+
|
|
1554
|
+
// commands/remove-unused/index.ts
|
|
1555
|
+
import { readFileSync as readFileSync9, writeFileSync as writeFileSync8 } from "fs";
|
|
1556
|
+
import { basename as basename8, dirname as dirname8, extname as extname8, join as join8 } from "path";
|
|
1557
|
+
import { parse as parse9 } from "@babel/parser";
|
|
1558
|
+
import traverse8 from "@babel/traverse";
|
|
1559
|
+
import generate8 from "@babel/generator";
|
|
1560
|
+
import * as t7 from "@babel/types";
|
|
1561
|
+
import loading9 from "loading-cli";
|
|
1562
|
+
var createDefaultOutputPath8 = (inputPath) => {
|
|
1563
|
+
const ext = extname8(inputPath);
|
|
1564
|
+
if (!ext) {
|
|
1565
|
+
return `${inputPath}.remove-unused.js`;
|
|
1566
|
+
}
|
|
1567
|
+
const base = basename8(inputPath, ext);
|
|
1568
|
+
return join8(dirname8(inputPath), `${base}.remove-unused${ext}`);
|
|
1569
|
+
};
|
|
1570
|
+
var removeUnusedVariables = (code, filename) => {
|
|
1571
|
+
const ast = parse9(code, createParseOptions(filename));
|
|
1572
|
+
let changed = false;
|
|
1573
|
+
patchDefault(traverse8)(ast, {
|
|
1240
1574
|
Scope(path) {
|
|
1241
1575
|
for (const binding of Object.values(path.scope.bindings)) {
|
|
1242
1576
|
if (!binding.referenced && binding.constantViolations.length === 0 && binding.path.key !== "handler" && !binding.path.isFunctionExpression()) {
|
|
1243
|
-
if (
|
|
1577
|
+
if (t7.isProgram(binding.scope.block) && (binding.kind === "var" || binding.kind === "hoisted")) {
|
|
1244
1578
|
continue;
|
|
1245
1579
|
}
|
|
1246
1580
|
const targets = binding.path.parentKey === "params" ? [...binding.referencePaths, ...binding.constantViolations] : [
|
|
@@ -1249,11 +1583,11 @@ var removeUnusedVariables = (code, filename) => {
|
|
|
1249
1583
|
...binding.constantViolations
|
|
1250
1584
|
];
|
|
1251
1585
|
for (const targetPath of targets) {
|
|
1252
|
-
if (targetPath.isVariableDeclarator() && (
|
|
1586
|
+
if (targetPath.isVariableDeclarator() && (t7.isArrayPattern(targetPath.node.id) && targetPath.node.id.elements.length > 1 || t7.isObjectPattern(targetPath.node.id) && targetPath.node.id.properties.length > 1)) {
|
|
1253
1587
|
continue;
|
|
1254
1588
|
}
|
|
1255
1589
|
if (targetPath.key === "consequent" || targetPath.key === "alternate" || targetPath.key === "body") {
|
|
1256
|
-
targetPath.replaceWith(
|
|
1590
|
+
targetPath.replaceWith(t7.blockStatement([]));
|
|
1257
1591
|
} else {
|
|
1258
1592
|
const parentPath = targetPath.parentPath;
|
|
1259
1593
|
if (parentPath?.isVariableDeclaration() && parentPath.node.declarations.length === 1) {
|
|
@@ -1269,7 +1603,7 @@ var removeUnusedVariables = (code, filename) => {
|
|
|
1269
1603
|
}
|
|
1270
1604
|
});
|
|
1271
1605
|
return {
|
|
1272
|
-
code: patchDefault(
|
|
1606
|
+
code: patchDefault(generate8)(ast).code,
|
|
1273
1607
|
changed
|
|
1274
1608
|
};
|
|
1275
1609
|
};
|
|
@@ -1284,20 +1618,20 @@ var remove_unused_default = createCommand((program2) => {
|
|
|
1284
1618
|
return finish();
|
|
1285
1619
|
}
|
|
1286
1620
|
try {
|
|
1287
|
-
const fileContent =
|
|
1288
|
-
const defaultOutputPath =
|
|
1621
|
+
const fileContent = readFileSync9(filename, "utf8");
|
|
1622
|
+
const defaultOutputPath = createDefaultOutputPath8(filename);
|
|
1289
1623
|
let outputPath = options.output;
|
|
1290
1624
|
if (!outputPath) {
|
|
1291
1625
|
const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
|
|
1292
1626
|
outputPath = promptPath || defaultOutputPath;
|
|
1293
1627
|
}
|
|
1294
|
-
const loader =
|
|
1628
|
+
const loader = loading9("Removing unused variables...").start();
|
|
1295
1629
|
try {
|
|
1296
1630
|
const { code: output, changed } = removeUnusedVariables(
|
|
1297
1631
|
fileContent,
|
|
1298
1632
|
filename
|
|
1299
1633
|
);
|
|
1300
|
-
|
|
1634
|
+
writeFileSync8(outputPath, output, "utf8");
|
|
1301
1635
|
const diffLines = diff(fileContent, output).length;
|
|
1302
1636
|
loader.succeed(
|
|
1303
1637
|
`Saved remove-unused file to: ${outputPath} (${diffLines} lines changed${changed ? ", removed unused declarations" : ", no changes"})`
|
|
@@ -1347,10 +1681,10 @@ var calmGradienrain = (text) => {
|
|
|
1347
1681
|
const endHue = 300;
|
|
1348
1682
|
const saturation = 0.45;
|
|
1349
1683
|
const value = 0.8;
|
|
1350
|
-
const ease = (
|
|
1684
|
+
const ease = (t8) => t8 * t8 * (3 - 2 * t8);
|
|
1351
1685
|
return text.split("").map((char, i) => {
|
|
1352
|
-
const
|
|
1353
|
-
const hue = startHue + (endHue - startHue) *
|
|
1686
|
+
const t8 = ease(i / Math.max(text.length - 1, 1));
|
|
1687
|
+
const hue = startHue + (endHue - startHue) * t8;
|
|
1354
1688
|
const c = value * saturation;
|
|
1355
1689
|
const h = hue / 60;
|
|
1356
1690
|
const x = c * (1 - Math.abs(h % 2 - 1));
|
|
@@ -1374,10 +1708,10 @@ ${calmGradienrain(`Expose Kit v${VERSION}`)}
|
|
|
1374
1708
|
`;
|
|
1375
1709
|
|
|
1376
1710
|
// index.ts
|
|
1377
|
-
import { readFileSync as
|
|
1711
|
+
import { readFileSync as readFileSync10 } from "fs";
|
|
1378
1712
|
var __filename = fileURLToPath(import.meta.url);
|
|
1379
|
-
var __dirname =
|
|
1380
|
-
var pkg = JSON.parse(
|
|
1713
|
+
var __dirname = dirname9(__filename);
|
|
1714
|
+
var pkg = JSON.parse(readFileSync10(join9(__dirname, "package.json"), "utf8"));
|
|
1381
1715
|
console.log(showCredit(pkg.version));
|
|
1382
1716
|
console.log();
|
|
1383
1717
|
var program = new Command();
|
|
@@ -1393,6 +1727,8 @@ var commands = [
|
|
|
1393
1727
|
expand_object_default,
|
|
1394
1728
|
object_packer_default,
|
|
1395
1729
|
pre_evaluate_default,
|
|
1730
|
+
remove_updater_default,
|
|
1731
|
+
remove_reassign_default,
|
|
1396
1732
|
remove_unused_default
|
|
1397
1733
|
];
|
|
1398
1734
|
for (const command of commands) {
|
package/dist/package.json
CHANGED