Codemod: Difference between revisions

From wikinotes
 
No edit summary
Line 2: Line 2:
and previewing changes as you go. Regex, and multiline-regex are both supported.
and previewing changes as you go. Regex, and multiline-regex are both supported.


https://github.com/facebook/codemod
= Documentation =
 
<blockquote>
{| class="wikitable"
|-
| github || https://github.com/facebook/codemod
|-
|}
</blockquote><!-- Documentation -->


= Install =
= Install =
Line 41: Line 47:
import codemod
import codemod


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

Revision as of 20:05, 4 November 2021

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  '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: