Codemod

From wikinotes

codemod is a fantastic, and flexible tool for refactoring your code and previewing changes as you go. Regex, and multiline-regex are both supported.

Documentation

github https://github.com/facebook/codemod

Install

NOTE:
only supports python2 - 2020/04

macos

sudo port install python27
sudo port install py27-pip
python2.7 -m pip install codemod

export PATH="${PATH}:/System/Volumes/Data/Users/${USER}/Library/Python/2.7/bin"

Usage

Commandline

codemod  'search' \
         'replace'                         # search/replace

codemod  -m 'abc\ndef'  \
            'abcdef()'                     # multiline search/replace

codemod --extensions rb,yml 'foo' 'bar'    # only replace in extensions

codemod  'def T[a-zA-Z_]+(' \
         'def Name'                        # regex replace

codemod  'def T([a-z]+)(' \
         'def Z\1'                         # regex replace, referencing match (python-regex)

Python

import codemod

codemod.Query(
    suggestor= lambda y: x.replace('A', 'B') for x in y,   # function operates on list of matches
    root_directory='.',
    path_filter=helpers.path_filter( extensions=['php','py'] ),
)

Take a look at: