Codemod: Difference between revisions
From wikinotes
(→macos) |
(→Usage) |
||
(One intermediate revision by the same user not shown) | |||
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 29: | Line 35: | ||
codemod -m 'abc\ndef' \ | codemod -m 'abc\ndef' \ | ||
'abcdef()' # multiline search/replace | 'abcdef()' # multiline search/replace | ||
codemod --extensions rb,yml 'foo' 'bar' # only replace in extensions | |||
codemod 'def T[a-zA-Z_]+(' \ | codemod 'def T[a-zA-Z_]+(' \ | ||
Line 41: | Line 49: | ||
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> |
Latest revision as of 20:06, 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/04macos
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: