Reverse engineering code
Hello :)
Today, i was trying to understand how a open source project was working and i met some difficult to understand the behaviour.
For debugging my projects, i have the habbit to log every function like that :
myClass {
myFunction () {
console.log('myClass.myFunction');
// Do something
}
}
Insteed of using console.log i use my logger library with its level. So, while i’m developing i can trace every executing function and i win a lot of time to understand what it is doing. In bonus, the cost of this method is close to zero and it is very useful !
While i was trying to understand the codebase, i was asking myself : “It would be so funny if all function could be logged, like that i could see which code is performed when i do such action”.
So, i had the idea to use a regex to add with one click the line to log name’s function everywhere.
I created this
(^\s+(?!for|if|switch|while|data)([a-zA-Z]*) (\(.*\))(: [a-zA-Z]*)? [{])

Note that you can add all other line that you do not want by adding the first word in the regex at this place
(?!for|if|switch|while|data|myCustomWord)
Then, using VSCode i can using this regex to make a “search and replace” with :
$1
console.log('$2');
The first group is the detected line and the second is the name of the function, so i replaced the line by itself with adding an end of line and the log of the name of the function.
TADA !

Edit (April, 26th 2021)
The same regex for arrow function
(const ([a-zA-Z]*) = (async)? \(?[a-z, ]*\)? => \{)