Renaming variables in JavaScript
Asked Answered
E

3

7

I've been stuck with the unpleasant task of "unminifying" a minified JavaScript code file. Using JSBeautifier, the resulting file is about 6000 lines long.

Ordinarily, the variable and parameter names would be permanently lost, but in this case, I have an obsolete version of the original file that the minified JavaScript code file was generated from. This obsolete version of the original file contains most of the code comments and variable names, but absolutely cannot be used in place of the current version.

I would like to know if there is some way of renaming all instances of a particular parameter or variable in JavaScript. Since minification reduces the names to a single character, find-and-replace is impossible.

Is there some tool out there, which I can tell, in this file, the parameter a to function foo should be clientName and have it semantically rename all instances of that parameter to clientName?

Unfortunately, I work for a large organization with an approved list of software and I am stuck with Visual Studio 2010 for the forseeable future (no VS 2012).

Update: @Kos, we don't use Git, but we do use source control. The problem is that a developer who doesn't work for my organization anymore once made changes to the file, minified it, and only checked in the minified version to source control, so his changes to the original have been lost.

Ere answered 22/2, 2013 at 16:51 Comment(7)
I hope that Git is on your organisation's list, because it would save you right now :-). Hmm... You could try and use JSBeautifier on both versions (to unify the formatting), then make a patch and apply it selectively. This would half-automate your work if the other changes aren't huge.Subclavian
You can checkout TypeScript. typescriptlang.org All javascript is by default TypeScript, so I am assuming, there will be some kind of assistance in variable renaming - though it might not do that across files. And the good news is Visual Studio is the only available IDE for TypeScript AFAIK.Screwball
I wouldn't write off a find-and-replace solution so quickly. JS variables are only valid in expressions when used certain ways. I think you could probably do a few find/replaces for each case and make it workSpheroidal
It might be faster and better to rewrite it from scratch. Perhaps using jQuery.Kopple
@anoopelias: All javascript is by default TypeScript? This isn't even remotely the case. I don't think that technology applies in this case.Spheroidal
@AndrewWhitaker I haven't used it personally, I could be wrong, but this is in reference to the intro video in typescriptlang.org at time 3:25Screwball
@anoopelias: I misunderstood the comment--I see what you mean now. JavaScript can be seen as a subset of TypeScript. Sorry about that.Spheroidal
E
1

I found that a Visual Studio extension we've licensed here called "Telerik JustCode" has functionality to do what I want.

Ere answered 27/2, 2013 at 20:9 Comment(0)
R
7

I'm a year late for this answer, but I had a similar problem to yours so I built this: https://github.com/zertosh/beautify-with-words. It unminifies code using UglifyJS2 but uses a phonetic word generator to rename variables. You get "long-ish" variable names so it's a breeze to do a find-and-replace. Hope this helps someone else!

Request answered 26/1, 2014 at 3:13 Comment(0)
L
2

You might have another way out.

Check out the last unminified version of the code. Compare to the minified version. Arguably most of it should be the same modulo consistent variable renaming. The differences you'll have to rename and remerge.

Diff won't do this kind of compare; you need tools that compare the programs as code, not text. Our SmartDifferencer tool will do this (by using language-specific full parsers to generate ASTs, and then comparing the ASTs); in effect, it compares the programs in spite of whitepspacing. SmartDifferencer also handles renaming; if two file are identical modulo a single renaming, that's what SmartDifferencer tell you.

I don't know how well this work work out; we haven't tried SmartDifferencer with 6000 lines of "consistently renamed" variables.

Lonna answered 22/2, 2013 at 23:7 Comment(0)
E
1

I found that a Visual Studio extension we've licensed here called "Telerik JustCode" has functionality to do what I want.

Ere answered 27/2, 2013 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.