简体   繁体   English

在JavaScript中重命名变量

[英]Renaming variables in JavaScript

I've been stuck with the unpleasant task of "unminifying" a minified JavaScript code file. 我一直困扰着“消除”缩小的JavaScript代码文件的令人不快的任务。 Using JSBeautifier, the resulting file is about 6000 lines long. 使用JSBeautifier,生成的文件大约6000行。

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. 通常,变量和参数名称将永久丢失,但在这种情况下,我有一个过时版本的原始文件,从中生成缩小的JavaScript代码文件。 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. 我想知道是否有一些方法可以在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 ? 是否有一些工具,我可以告诉,在这个文件中,函数foo的参数a应该是clientName并让它在语义上将该参数的所有实例重命名为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). 不幸的是,我在一个拥有经过批准的软件列表的大型组织工作,并且我在可预见的未来中坚持使用Visual Studio 2010(没有VS 2012)。

Update: @Kos, we don't use Git, but we do use source control. 更新: @Kos,我们不使用Git,但我们确实使用源代码控制。 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. 问题是,不再为我的组织工作的开发人员一旦对文件进行了更改,将其缩小,并且仅在缩小版本中检查了源代码控制,因此他对原始文件的更改已丢失。

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 . 我对这个答案迟了一年,但我遇到了类似的问题,所以我建立了这个: https//github.com/zertosh/beautify-with-words It unminifies code using UglifyJS2 but uses a phonetic word generator to rename variables. 它使用UglifyJS2取消代码,但使用语音字生成器重命名变量。 You get "long-ish" variable names so it's a breeze to do a find-and-replace. 你会得到“long-ish”变量名,所以进行查找和替换是一件轻而易举的事。 Hope this helps someone else! 希望这有助于其他人!

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; Diff不会做这种比较; 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); 我们的SmartDifferencer工具将执行此操作(通过使用特定于语言的完整解析器生成AST,然后比较AST); in effect, it compares the programs in spite of whitepspacing. 实际上,它比较了程序,尽管有whitepspacing。 SmartDifferencer also handles renaming; SmartDifferencer还处理重命名; if two file are identical modulo a single renaming, that's what SmartDifferencer tell you. 如果两个文件相同,则模拟一次重命名,这就是SmartDifferencer告诉你的。

I don't know how well this work work out; 我不知道这项工作有多顺利; we haven't tried SmartDifferencer with 6000 lines of "consistently renamed" variables. 我们还没有尝试过具有6000行“一致重命名”变量的SmartDifferencer。

我发现我们在这里获得许可的Visual Studio扩展称为“Telerik JustCode”,它具有我想要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM