Skip to content

Commit

Permalink
chore: add arrow function remove code
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronm-2112 committed Jan 31, 2025
1 parent 0c89644 commit d265ec5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 24 deletions.
23 changes: 16 additions & 7 deletions src/renderer/src/scripts/cleanupScripts/linesToRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@ const fs = require("fs");
// Read the ESLint output file
const eslintOutput = JSON.parse(fs.readFileSync("../formatted-eslint-output.json", "utf8"));

const messages = ["'event' is not defined.", "'path' is not defined.", "'event' is defined but never used.", "'path' is defined but never used.", "Empty block statement."];
const messages = [
"'event' is not defined.",
"'path' is not defined.",
"'event' is defined but never used.",
"'path' is defined but never used.",
"Empty block statement.",
];

// Filter messages with the specific message
const unusedEventMessages = eslintOutput.filter(result =>
result.messages.some(message =>
message.message === "Empty block statement."
)
const unusedEventMessages = eslintOutput.filter((result) =>
// result.messages.some(message =>
// message.message === "Empty block statement."
// )
result.messages.some((message) => message.ruleId === "no-unused-vars")
);

// Extract the lines and columns to remove
const targetLine = unusedEventMessages.flatMap((result) =>
result.messages
.filter(message => message.message === "Empty block statement.")
.map(message => ({
.filter((message) => message.ruleId === "no-unused-vars")
.map((message) => ({
filePath: result.filePath,
line: message.line,
column: message.column,
source: result.source,
nodeType: message.nodeType,
endLine: message.endLine,
}))
);

Expand Down
107 changes: 90 additions & 17 deletions src/renderer/src/scripts/cleanupScripts/removeUnused.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require("fs");
const { truncate } = require("fs-extra");
const linesToRemove = JSON.parse(fs.readFileSync("lines-to-remove.json", "utf8"));

// Sort linesToRemove in reverse order based on line and column
Expand All @@ -14,19 +15,43 @@ linesToRemove.sort((a, b) => {

let hits = 0;


module.exports = function(fileInfo, api) {
module.exports = function (fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);

// Debugging: Log the file being processed
console.log(`Processing file: ${fileInfo.path}`);

// Find and remove the unused 'event' variables in reverse order
linesToRemove.forEach(({ filePath, line, column }) => {
linesToRemove.forEach(({ filePath, line, column, endLine }) => {
// remove unused function declarations of type function name() {}
// root
// .find(j.FunctionDeclaration)
// .filter((funcPath) => {
// const funcStart = funcPath?.node?.loc.start.line;
// if (funcStart) {
// return funcStart.line === line;
// }
// })
// .remove();

// remove unused function expressions of type const name = () => {}
root
.find(j.VariableDeclaration)
.filter((varPath) => {
const varStart = varPath?.node?.loc.start.line;
// get column
// const varColumn = varPath?.node?.loc.start.column;
if (varStart && varStart === line && varPath.node.kind === "const" && column === 7) {
return true;
} else {
return false;
}
})
.remove();

// Debugging: Log the file path, line, and column being checked
// console.log(`Checking: ${filePath} at line ${line}, column ${column}`);

// remove identifiers matching the line and column from the esLint list of items to remove
// root.find(j.Identifier, { name: 'event' })
// .filter(path => {
Expand All @@ -36,7 +61,6 @@ module.exports = function(fileInfo, api) {
// return start.line === line && start.column === column - 1;
// })
// .remove();

// transform identifiers matching the line and column from the esLint list of items to transform
// root.find(j.Identifier, { name: 'path' })
// .forEach(path => {
Expand All @@ -49,20 +73,69 @@ module.exports = function(fileInfo, api) {
// }
// }
// });


// Remove empty block statements
root.find(j.BlockStatement)
.filter( path => {
const start = path.node.loc.start;
if (start.line === line) {
hits = hits + 1;
return path;
}
}).remove()

// root.find(j.BlockStatement)
// .filter( path => {
// const start = path.node.loc.start;
// if (start.line === line) {
// hits = hits + 1;
// return path;
// }
// }).remove()
// remove all unused vars (should be identifiers)
// let rs = root.find(j.Identifier);
// for (const r of rs) {
// console.log(r);
// }
// .filter((path) => {
// if (!path || !path.node) {
// return false;
// }
// const start = path.node.loc?.start;
// if (start) {
// return linesToRemove.some(
// (loc) => start.line === loc.line && start.column === loc.column - 1
// );
// } else {
// return false;
// }
// })
// .forEach((path) => {
// hits = hits + 1;
// j(path).remove();
// });
// root.find(j.Identifier).forEach((path) => {
// const start = path?.node?.loc?.start;
// if (start && start.line === 17747) {
// console.log("in like flynn");
// if (start.line === line) {
// // console.log(start.line, start.column);
// hits = hits + 1;
// const parent = path.parent.node;
// if (
// // j.FunctionDeclaration.check(parent) ||
// // j.FunctionExpression.check(parent) ||
// parent.init.type === "ArrowFunctionExpression"
// ) {
// console.log("Inside the thing");
// // remove the whole function
// let functionStart = parent.init.loc.start;
// // let functionEnd = parent.init.loc.end;
// } else {
// // j(path).closest(path.parent.node).remove();
// // j(path).remove();
// }
// }
// }
// });
});

// console.log(JSON.stringify(root, null, 2));

console.log(`Total hits for path: ${hits}`);
return root.toSource();
try {
return root.toSource();
} catch (e) {
console.log(e);
}
};

0 comments on commit d265ec5

Please sign in to comment.