Esformatter plugin to rename variables, parameters, and labels
This was built to make comprehending deobfuscated scripts easier (e.g. rename a
to jQuery
). It is based off its sister project esformatter-phonetic
which renames variables to pronouncable equivalents (also good for deobfuscating scripts).
Features:
- Supports ES6 arrow functions (e.g.
(a) => a + 1
) - Supports
let
(e.g.let a = 1;
) - Supports destructured variables (e.g.
var {a, b} = obj;
)
Install the module with: npm install esformatter-rename
Then, register it as a plugin and format your JS:
// Load and register our plugin
var esformatter = require('esformatter');
var esformatterRename = require('esformatter-rename');
esformatter.register(esformatterRename);
// Format our code
esformatter.format([
'function myFn() {',
'var a = \'hello\';',
'console.log(a);',
'}'
].join('\n'), {
rename: {
variables: {
a: 'hello'
}
}
});
/*
function myFn() {
var hello = 'hello';
console.log(hello);
}
*/
Alternatively, load it via format
or .esformatter
:
{
plugins: [
'esformatter-rename'
]
}
esformatter-rename
exposes exports.transform
for consumption by esformatter
.
We provide the following options to configure esformatter-rename
during transformation. These should be stored under rename
in your esformatter
options.
- variables
Object
- Key value pairing of original variable name to new name- For example
variables: {hello: 'world'}
will rename allhello
variables toworld
variablesfunction hello() { console.log('hai'); } -> function world() { console.log('hai'); }
- If you are potentially doing something dangerous (e.g. renaming an undeclared variable), we will warn you/skip it since it can have global reprecussions)
- These warnings can be ignored and actions can be taken via the related option
- For example
- labels
Object
- Key value pairing of origin label name to new name- For example
labels: {loop1: 'myLoop'}
will rename allloop1
labels tomyLoop
loop1: while (true) { break loop1; } -> myLoop: while (true) { break myLoop; }
- For example
- renameTopLevel
Boolean
- Allow for renaming of top level variables (i.e. anything declared with avar
in the global scope)- If
true
, renaming is allowed. Iffalse
, it is not and a warning is logged. - By default, this is
false
.
- If
- renameUndeclared
Boolean
- Allow for renaming of undeclared variables (e.g. variable referenced without avar
)- If
true
, renaming is allowed. Iffalse
, it is not and a warning is logged. - By default, this is
false
.
- If
- ignoreWith
Boolean
- Allow for renaming of variables that were referenced at least once in awith
- For example
var obj = {}; var hello; with (obj) { hello(); }
will not allow renaming ofhello
- If
true
, renaming is allowed. Iffalse
, it is not and a warning is logged. - By default, this is
false
.
- For example
In this example, we will rest jQuery
from its normal convention of $
to the full name jQuery
.
Script before renaming:
console.log($('#main').text());
Formatter options:
{
rename: {
variables: {
'$': 'jQuery'
},
renameUndeclared: true
}
}
Script after renaming:
console.log(jQuery('#main').text());
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
Support this project and others by twolfson via gratipay.
As of Nov 13 2014, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.