Skip to content

Commit

Permalink
refactor: avoid forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
Namchee committed Nov 17, 2024
1 parent f1e27b3 commit fe853a3
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions codemods/strip-ansi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@ export default function (options) {

let importName = 'stripAnsi';

importDeclarations.forEach((declaration) => {
const name = declaration.value.specifiers?.[0].local?.name;
for (const declaration of importDeclarations.nodes()) {
const name = declaration.specifiers?.[0].local?.name;
if (name) {
importName = name;
}
declaration.value.source.value = 'node:util';
declaration.value.specifiers = [j.importSpecifier(j.identifier('stripVTControlCharacters'))];
declaration.source.value = 'node:util';
declaration.specifiers = [j.importSpecifier(j.identifier('stripVTControlCharacters'))];
dirtyFlag = true;
});
}

root.find(j.CallExpression, {
const callExpression = root.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: importName,
}
}).forEach((expression) => {
expression.value.callee.name = 'stripVTControlCharacters';
dirtyFlag = true;
},
});

for (const expression of callExpression.nodes()) {
if (expression.callee.type === 'Identifier') {
expression.callee.name = 'stripVTControlCharacters';
dirtyFlag = true;
}
}

return dirtyFlag ? root.toSource() : file.source;
},
};
Expand Down

0 comments on commit fe853a3

Please sign in to comment.