Skip to content

Commit 7d6faef

Browse files
committed
fix typescript bugs
2 parents 08ee8ed + cb003d5 commit 7d6faef

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/index.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const parser = require("@babel/parser");
66
const traverse = require("@babel/traverse").default;
77
const generator = require("@babel/generator").default;
88

9+
10+
911
const consoleMethods = [
1012
"assert",
1113
"clear",
@@ -29,30 +31,62 @@ const consoleMethods = [
2931
"warn",
3032
];
3133

34+
// Retrieve command-line arguments (excluding first two default arguments)
35+
const rawArgs = process.argv.slice(3);
36+
// Normalize arguments by removing leading dashes, e.g., "--dry-run" becomes "dry-run"
37+
const args = rawArgs.map(arg => arg.replace(/^--?/, ""));
38+
// Check if dry-run mode is enabled
39+
const dryRun = args.includes("dry-run");
40+
// Check if backup mode is enabled
41+
const backup = args.includes("backup");
42+
3243
function removeConsoleLogsFromFile(filePath) {
3344
let code = fs.readFileSync(filePath, "utf-8");
34-
const ast = parser.parse(code, { sourceType: "module", plugins: ["jsx"] });
45+
const ast = parser.parse(code, {
46+
sourceType: "module",
47+
plugins: ["jsx", "typescript"] // Add "typescript" here
48+
});
3549
const args = process.argv.slice(3);
3650
const allowedMethods = args.includes("all")
3751
? consoleMethods
3852
: args[0]?.split(",") || ["log"];
3953

54+
// Counter for the number of removed console calls in this file
55+
let removedCount = 0;
56+
4057
traverse(ast, {
4158
CallExpression(path) {
4259
const { callee } = path.node;
60+
// Check if the call is a console method and if it is allowed to be removed
4361
if (
4462
callee.object?.name === "console" &&
4563
allowedMethods.includes(callee.property?.name)
4664
) {
65+
removedCount++;
4766
path.remove();
4867
}
4968
},
5069
});
5170

52-
const { code: newCode } = generator(ast, { retainLines: true });
53-
fs.writeFileSync(filePath, newCode, "utf-8");
71+
// If any console calls were removed, generate new code
72+
if (removedCount > 0) {
73+
const { code: newCode } = generator(ast, { retainLines: true });
74+
if (dryRun) {
75+
// In dry-run mode, just log the changes without modifying the file
76+
console.log(`[Dry Run] ${filePath} would have removed ${removedCount} console call(s).`);
77+
} else {
78+
// If backup flag is enabled, create a backup file before modifying the original file
79+
if (backup) {
80+
fs.copyFileSync(filePath, filePath + ".bak");
81+
console.log(`Backup created for: ${filePath}`);
82+
}
83+
fs.writeFileSync(filePath, newCode, "utf-8");
84+
console.log(`Processed: ${filePath} (removed ${removedCount} console call(s))`);
85+
}
86+
}
5487
}
5588

89+
// Recursive function to process all files in the given directory
5690
function processDirectory(dirPath) {
5791
fs.readdirSync(dirPath).forEach((file) => {
5892
const fullPath = path.join(dirPath, file);
@@ -62,11 +96,17 @@ function processDirectory(dirPath) {
6296
[".js", ".ts", ".jsx", ".tsx"].some((ext) => file.endsWith(ext))
6397
) {
6498
removeConsoleLogsFromFile(fullPath);
65-
console.log(`Processed: ${fullPath}`);
6699
}
67100
});
68101
}
69102

103+
// Get the target directory from command-line arguments (default to current directory)
70104
const targetDir = process.argv[2] || ".";
71105
processDirectory(targetDir);
72-
console.log("✅ All console.log statements removed!");
106+
107+
// Final log message based on mode used
108+
if (dryRun) {
109+
console.log("✅ Dry run complete! No files were modified.");
110+
} else {
111+
console.log("✅ All specified console.log statements removed!");
112+
}

0 commit comments

Comments
 (0)