Skip to content

Commit

Permalink
fix: recursive ast for if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
marabesi committed Jun 27, 2024
1 parent 5339134 commit b337790
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion vscode/src/cli/find-smells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function execute() {
try {
const fileContents = await fs.readFile(fileName, { encoding: 'utf8' });
const smellDetector = new SmellDetector(fileContents);
console.log(smellDetector.findAll());
console.info(smellDetector.findAll());
} catch (err) {
console.log(`[SMELLY] error: ${err}`);
}
Expand Down
20 changes: 11 additions & 9 deletions vscode/src/modules/smells-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export class SmellDetector {

findAll(): Smell[] {
const smells: Smell[] = [];
const ifs: any[] = [];

const ifs = containsIfStatement(this.ast).filter((item: any) => item.type === Syntax.IfStatement);
findIfStatements(this.ast, ifs).filter((item: any) => item.type === Syntax.IfStatement);

for (const statement of ifs) {
smells.push({
Expand All @@ -30,16 +31,17 @@ export class SmellDetector {
}
}

function containsIfStatement(node: any) {
if (node.type === Syntax.IfStatement) {
return node;
function findIfStatements(node: any, ifStatements: any[] = []) {
if (node.type === 'IfStatement') {
ifStatements.push(node);
}
for (const key in node) {

// Recursively search in all child nodes
for (let key in node) {
if (node[key] && typeof node[key] === 'object') {
if (containsIfStatement(node[key])) {
return node[key];
}
findIfStatements(node[key], ifStatements);
}
}
return false;

return ifStatements;
}

0 comments on commit b337790

Please sign in to comment.