pruny 1.44.2 → 1.44.4

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.
Files changed (2) hide show
  1. package/dist/index.js +58 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -14964,7 +14964,6 @@ async function scanUnusedExports(config, routes = [], options = {}) {
14964
14964
  console.log(`[DEBUG USE] ${exp.name} used internally in ${file} at line ${i + 1}: ${line.trim()}`);
14965
14965
  }
14966
14966
  usedInternally = true;
14967
- isUsed = true;
14968
14967
  break;
14969
14968
  }
14970
14969
  }
@@ -14985,6 +14984,10 @@ async function scanUnusedExports(config, routes = [], options = {}) {
14985
14984
  const absoluteOtherFileFixed = isAbsolute2(otherFile) ? otherFile : join4(scanCwd, otherFile);
14986
14985
  const hasIgnoreRanges = ignoreRanges.has(absoluteOtherFileFixed);
14987
14986
  const isGeneric = GENERIC_METHOD_NAMES.has(exp.name);
14987
+ const hasSelfImport = new RegExp(`import.*\\b${escapeRegExp(exp.name)}\\b.*from`).test(content);
14988
+ const hasSelfDecl = new RegExp(`(?:export\\s+)?(?:abstract\\s+)?(?:interface|class|enum)\\s+${escapeRegExp(exp.name)}\\b|` + `(?:export\\s+)?(?:async\\s+)?(?:function)\\s+${escapeRegExp(exp.name)}\\b|` + `(?:export\\s+)?(?:const|let|var|type)\\s+${escapeRegExp(exp.name)}\\s*[=<]`).test(content);
14989
+ if (hasSelfDecl && !hasSelfImport)
14990
+ continue;
14988
14991
  if (!hasIgnoreRanges && !isGeneric) {
14989
14992
  const jsxPattern = new RegExp(`<${exp.name}[\\s/>]`);
14990
14993
  if (jsxPattern.test(content)) {
@@ -15325,11 +15328,18 @@ function removeExportFromLine(rootDir, exp) {
15325
15328
  const content = readFileSync7(fullPath, "utf-8");
15326
15329
  const lines = content.split(`
15327
15330
  `);
15328
- const lineIndex = findDeclarationIndex(lines, exp.name, exp.line - 1);
15331
+ let lineIndex = findDeclarationIndex(lines, exp.name, exp.line - 1);
15332
+ if (lineIndex === -1) {
15333
+ lineIndex = findDeclarationIndex(lines, exp.name, 0);
15334
+ }
15329
15335
  if (!exp.usedInternally) {
15336
+ if (lineIndex === -1) {
15337
+ return removeFromBlockExport(lines, fullPath, exp.name, exp.line);
15338
+ }
15330
15339
  const trueStartLine = findDeclarationStart(lines, lineIndex);
15331
15340
  const deletedLines = deleteDeclaration(lines, trueStartLine, exp.name);
15332
15341
  if (deletedLines > 0) {
15342
+ removeFromBlockExport(lines, fullPath, exp.name, exp.line);
15333
15343
  const newContent = lines.join(`
15334
15344
  `);
15335
15345
  if (isFileEmpty(newContent)) {
@@ -15341,7 +15351,12 @@ function removeExportFromLine(rootDir, exp) {
15341
15351
  }
15342
15352
  return false;
15343
15353
  }
15354
+ if (lineIndex === -1) {
15355
+ return removeFromBlockExport(lines, fullPath, exp.name, exp.line);
15356
+ }
15344
15357
  const originalLine = lines[lineIndex];
15358
+ if (!originalLine)
15359
+ return false;
15345
15360
  if (originalLine.trim().startsWith("export ")) {
15346
15361
  const newLine = originalLine.replace(/(\s*)export\s+/, "$1");
15347
15362
  lines[lineIndex] = newLine;
@@ -15349,12 +15364,31 @@ function removeExportFromLine(rootDir, exp) {
15349
15364
  `), "utf-8");
15350
15365
  return true;
15351
15366
  }
15352
- return false;
15367
+ return removeFromBlockExport(lines, fullPath, exp.name, exp.line);
15353
15368
  } catch (err) {
15354
15369
  console.error(`Error fixing export in ${exp.file}:`, err);
15355
15370
  return false;
15356
15371
  }
15357
15372
  }
15373
+ function removeFromBlockExport(lines, fullPath, name, exportLine) {
15374
+ const lineIdx = exportLine - 1;
15375
+ const line = lines[lineIdx];
15376
+ if (!line)
15377
+ return false;
15378
+ if (!/^export\s*\{/.test(line.trim()))
15379
+ return false;
15380
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
15381
+ let newLine = line.replace(new RegExp(`\\b${escaped}\\b\\s*,\\s*`), "").replace(new RegExp(`,\\s*\\b${escaped}\\b`), "").replace(new RegExp(`\\b${escaped}\\b`), "");
15382
+ newLine = newLine.replace(/\{\s*\}/, "{}").replace(/\{\s+/, "{ ").replace(/\s+\}/, " }");
15383
+ if (newLine.trim() === "export {}" || newLine.trim() === "export { }") {
15384
+ lines.splice(lineIdx, 1);
15385
+ } else {
15386
+ lines[lineIdx] = newLine;
15387
+ }
15388
+ writeFileSync(fullPath, lines.join(`
15389
+ `), "utf-8");
15390
+ return true;
15391
+ }
15358
15392
  function findDeclarationIndex(lines, name, startLine = 0) {
15359
15393
  const methodRegex = new RegExp(`(?:public|private|protected|static|async|readonly)?\\s*(?:async)?\\s*${name}\\s*[(<]`);
15360
15394
  const declRegex = new RegExp(`(?:export\\s+)?(?:default\\s+)?(?:(?:abstract|async|static|readonly)\\s+)*(?:class|interface|type|enum|function|const|let|var)\\s+${name}\\b`);
@@ -15446,6 +15480,16 @@ function deleteDeclaration(lines, startLine, name) {
15446
15480
  if (inTemplateLiteral) {
15447
15481
  if (backtickCount % 2 !== 0) {
15448
15482
  inTemplateLiteral = false;
15483
+ const lastTick = line.lastIndexOf("`");
15484
+ const afterTick = sanitizeLine(line.slice(lastTick + 1));
15485
+ const extraOpens = (afterTick.match(/{/g) || []).length;
15486
+ const extraCloses = (afterTick.match(/}/g) || []).length;
15487
+ braceCount += extraOpens - extraCloses;
15488
+ if (foundBodyOpening && braceCount <= 0) {
15489
+ endLine = i;
15490
+ foundClosing = true;
15491
+ break;
15492
+ }
15449
15493
  }
15450
15494
  continue;
15451
15495
  }
@@ -18050,6 +18094,7 @@ Analyzing cascading impact...`));
18050
18094
  }
18051
18095
  const selectedList = options.cleanup ? options.cleanup.split(",").map((s) => s.trim()) : [selected];
18052
18096
  let fixedSomething = false;
18097
+ let fixedCode = false;
18053
18098
  if (selectedList.includes("broken-links")) {
18054
18099
  if (result.brokenLinks && result.brokenLinks.total > 0) {
18055
18100
  console.log(source_default.yellow.bold(`
@@ -18189,6 +18234,7 @@ Analyzing cascading impact...`));
18189
18234
  actuallyDeleted.add(path2);
18190
18235
  console.log(source_default.red(` Deleted: ${relative5(config.dir, path2)}`));
18191
18236
  fixedSomething = true;
18237
+ fixedCode = true;
18192
18238
  } else {
18193
18239
  console.log(source_default.yellow(` ⚠ Path not found, skipping: ${relative5(config.dir, path2)}`));
18194
18240
  }
@@ -18205,6 +18251,7 @@ Analyzing cascading impact...`));
18205
18251
  actuallyFixed.add(absPath);
18206
18252
  console.log(source_default.green(` Fixed: Removed ${rem.name} from ${relative5(config.dir, absPath)} (${rem.label})`));
18207
18253
  fixedSomething = true;
18254
+ fixedCode = true;
18208
18255
  }
18209
18256
  }
18210
18257
  }
@@ -18233,6 +18280,7 @@ Analyzing cascading impact...`));
18233
18280
  rmSync(fullPath, { force: true });
18234
18281
  console.log(source_default.red(` Deleted: ${file.path}`));
18235
18282
  fixedSomething = true;
18283
+ fixedCode = true;
18236
18284
  } catch (_err) {
18237
18285
  console.log(source_default.yellow(` Failed to delete: ${file.path}`));
18238
18286
  }
@@ -18246,7 +18294,9 @@ Analyzing cascading impact...`));
18246
18294
  }
18247
18295
  if (selectedList.includes("exports")) {
18248
18296
  if (result.unusedExports && result.unusedExports.exports.length > 0) {
18249
- fixedSomething = await fixUnusedExports(result, config) || fixedSomething;
18297
+ const exportsFixed = await fixUnusedExports(result, config);
18298
+ fixedSomething = exportsFixed || fixedSomething;
18299
+ fixedCode = exportsFixed || fixedCode;
18250
18300
  } else {
18251
18301
  console.log(source_default.green(`
18252
18302
  ✅ No unused exports found!`));
@@ -18278,6 +18328,7 @@ Analyzing cascading impact...`));
18278
18328
  console.log(source_default.green(` Fixed: ${method.name} in ${file}`));
18279
18329
  fixedCount++;
18280
18330
  fixedSomething = true;
18331
+ fixedCode = true;
18281
18332
  }
18282
18333
  }
18283
18334
  }
@@ -18293,7 +18344,7 @@ Analyzing cascading impact...`));
18293
18344
  ✅ No unused services found!`));
18294
18345
  }
18295
18346
  }
18296
- if (fixedSomething) {
18347
+ if (fixedCode) {
18297
18348
  console.log(source_default.cyan.bold(`
18298
18349
  \uD83D\uDD04 Checking for cascading dead code (newly unused implementation)...`));
18299
18350
  const secondPass = await scanUnusedExports(config);
@@ -18365,6 +18416,8 @@ Analyzing cascading impact...`));
18365
18416
  result.used = result.routes.filter((r) => r.used).length;
18366
18417
  result.total = result.routes.length;
18367
18418
  }
18419
+ if (options.cleanup)
18420
+ return "exit";
18368
18421
  console.log("");
18369
18422
  continue;
18370
18423
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pruny",
3
- "version": "1.44.2",
3
+ "version": "1.44.4",
4
4
  "description": "Find and remove unused Next.js API routes & Nest.js Controllers",
5
5
  "type": "module",
6
6
  "files": [