Reputation: 1799
I'm working with some deminified javascript that I want to reverse engineer. It would be helpful if I could start giving things variable names. Unfortunately the code has a habit of reusing function and variable names globally and in function scopes, how can I safely replace variable names in these cases in an ast-aware way?
Upvotes: 0
Views: 593
Reputation: 827
The simplest way would be to use 🐊Putout code transformer, I'm working on. Here is the code:
export const fix = ({path, names}) => {
const {name} = path.node;
const to = names[name];
path.scope.rename(name, to);
}
export const traverse = ({push}) => {
const names = {
a: 'username',
b: 'password',
};
const namesKeys = Object.keys(names);
return {
Identifier(path) {
const {name} = path.node;
const bindings = path.scope.getAllBindings();
if (namesKeys.includes(name) && bindings[name])
push({
path,
names,
});
}
};
};
From:
const a = 'John';
const b = 'Wick';
login();
function login() {
if (a)
log(a);
if (b)
log(b);
}
To:
const username = 'John';
const password = 'Wick';
login();
function login() {
if (username)
log(username);
if (password)
log(password);
}
You can get realtime results in 🐊Putout Editor, just modify names
map with a values you need.
Upvotes: 2