Skip to content

Latest commit

 

History

History
186 lines (135 loc) · 2.48 KB

CODEMOD.md

File metadata and controls

186 lines (135 loc) · 2.48 KB

Index

Normalize Import Path

Converts relative path of a module reference to a default node_module based path.

Input:

import {a, b, c} from '../../../abc'

Output:

import {a, b, c} from 'abc'

.tscodemodrc

{
  transformation: 'normalize-import-path',
  params: {
    // name of the module
    module: 'abc'
  }
}

Shift Imports

Migrates imports from one module to another.

Input:

import {a, b, x, xx} from 'ab'

Output:

import {a, b} from 'ab'
import {x, xx} from 'x'

.tscodemodrc

{
  transformation: 'shift-imports',
  params: {
    // name of the source module
    from: 'ab',

    // name of the target module
    to: 'x',

    // all the import specifiers that should be migrated
    imports: ['x', 'xx']
  }
}

Array to Rest Params

Converts a function call that takes an argument of type Array into a function call where each element of the array is passed as a separate argument.

Input:

myCustomFunction([1, 2, 3])

Output:

myCustomFunction(1, 2, 3)

.tscodemodrc

{
  transformation: 'array-to-rest-params',
  params: {
    // name of the function to transform
    functiontransformation: 'myCustomFunction'
  }
}

Convert to Call

Converts a value to a function call.

Input:

const result = abc

Output:

const result = abc()

.tscodemodrc

{
  transformation: 'convert-to-call',
  params: {
    // name of the identifier
    name: 'abc'
  }
}

Migrate Modules

Migrates module specifier

Input:

import abc from 'abc'

Output:

import abc from 'abc-abc' // renamed the module specifier

.tscodemodrc

{
  transformation: 'migrate-modules',
  params: {
    modules: {
      // module map
      abc: 'abc-abc'
    }
  }
}

Replace Node

Replaces a ts.Node with another one. It is kind of like search and replace but much more powerful. It ignores whitespaces and works directly on the AST.

Input:

const abc = a(b(c))

Output:

const abc0 = K(a, b)(c)

.tscodemodrc

{
  transformation: 'replace-node',
  params: {
    matchWith: 'a(b(c))',
    replaceWith: 'K(a, b)(c)'
  }
}