1.0.9 • Published 6 years ago
tsmod v1.0.9
Tsmod
Refactor TypScript code programmatically using codemods.
Installation
npm install -g tsmodUsage
The following example applies the transform ./var_to_const_tramsform.ts to the files ./fileA.ts and ./fileB.ts:
tsmod -t ./var_to_const_tramsform.ts ./fileA.ts ./fileB.tsPlease Note: A
tsconfig.jsonfile is expected in the current directory when you run the previous command.
Transform example
The transfroms are powered by ts-morph you can learn more about the API at https://ts-morph.com.
The following example changes all var variable declarations into const variable declarations:
import { SourceFile, SyntaxKind, VariableDeclarationKind } from "ts-morph";
export const varToConstTransform = (file: SourceFile, transformArgs: {}) => {
// Find all variable declarations in source file
const variableStatements = file.getDescendantsOfKind(
SyntaxKind.VariableStatement
);
// Change var for const for each statement
variableStatements.forEach(variableStatement => {
const declarationKind = variableStatement.getDeclarationKind();
if (declarationKind === VariableDeclarationKind.Var) {
variableStatement.setDeclarationKind(VariableDeclarationKind.Const);
}
});
// Return source code
const updatedSourceCode = file.getText();
return updatedSourceCode;
};Options
For additional help use the following:
tsmod -h