Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

tsmod

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsmod

Refactor TypScript code programmatically using codemods

latest
npmnpm
Version
1.0.9
Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 30 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts