tsmod

1.0.9 • Public • Published

Tsmod

Refactor TypScript code programmatically using codemods.

Installation

npm install -g tsmod

Usage

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.ts

Please Note: A tsconfig.json file 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

Readme

Keywords

none

Package Sidebar

Install

npm i tsmod

Weekly Downloads

1,385

Version

1.0.9

License

MIT

Unpacked Size

29.4 kB

Total Files

16

Last publish

Collaborators

  • remojansen