JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 100
  • Score
    100M100P100Q67889F
  • License ISC

Code-Modifier for Typescript based projects

Package Exports

  • ts-codemod

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-codemod) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-codemod Build Status

Code-Modifier for Typescript based projects

Index

Installation

npm i -g ts-codemod

Command Line Usage

A typical command looks like -

ts-codemod --write --transformation=[transformation name]  --param.[param name]=[param value] [glob pattern]

Say I want to —

// convert something like —
import * as components from '../../../component'

// to a module path like —
import * as components from 'component'

I can use the [normalize-import-path] transformation to achieve this —

ts-codemod --write --transformation normalize-import-path  --params.moduleName=component src/**/*.ts

CLI Arguments

Argument Purpose Value
--write -w (optional) Writes back to the file false
--transformation -t (required) Name of the transformation or file path
--param -p ( optional) Additional transformation specific args

Custom transformation

Writing a custom transformation isn't very easy and one needs to understand how typescript internally converts plain string to an AST.

A good starter could be to checkout the [transformations] directory. Those transformations are written for a varied level of complexity.

A custom transformation (my-custom-transformation.ts) can be implemented via extending the Transformation class.

import * as ts from 'typescript'
import {Transformation} from 'ts-codemod'

// my-custom-transformation.ts
export default class MyCustomTransformation extends Transformation {
  apply(node: ts.Node): ts.VisitResult<ts.Node> {
    // write your implementation here

    return node // will apply no-change
  }
}

It can then be executed as —

ts-codemod -t ./my-custom-transformation.ts src/**.ts