@@ -136,24 +136,23 @@ var reduceableBlockStatements = {
136136 WhileStatement : true
137137} ;
138138
139+ var shortVars = {
140+ "r" : "reader" ,
141+ "w" : "writer" ,
142+ "m" : "message" ,
143+ "t" : "tag" ,
144+ "l" : "length" ,
145+ "c" : "end" , "c2" : "end2" ,
146+ "k" : "key" ,
147+ "ks" : "keys" , "ks2" : "keys2" ,
148+ "e" : "error" ,
149+ "f" : "impl" ,
150+ "o" : "options" ,
151+ "d" : "object" ,
152+ "n" : "long"
153+ } ;
154+
139155function beautifyCode ( code ) {
140- // Rename short vars
141- code = code
142- . replace ( / \b (? ! \\ ) r \b / g, "reader" )
143- . replace ( / \b (? ! \\ ) w \b / g, "writer" )
144- . replace ( / \b (? ! \\ ) m \b / g, "message" )
145- . replace ( / \b (? ! \\ ) t \b / g, "tag" )
146- . replace ( / \b (? ! \\ ) l \b / g, "length" )
147- . replace ( / \b (? ! \\ ) c \b / g, "end" )
148- . replace ( / \b (? ! \\ ) c 2 \b / g, "end2" )
149- . replace ( / \b (? ! \\ ) k \b / g, "key" )
150- . replace ( / \b (? ! \\ ) k s \b / g, "keys" )
151- . replace ( / \b (? ! \\ ) k s 2 \b / g, "keys2" )
152- . replace ( / \b (? ! \\ ) e \b / g, "error" )
153- . replace ( / \b (? ! \\ ) f \b / g, "impl" )
154- . replace ( / \b (? ! \\ ) o \b / g, "options" )
155- . replace ( / \b (? ! \\ ) d \b / g, "object" )
156- . replace ( / \b (? ! \\ ) n \b / g, "long" ) ;
157156 // Add semicolons
158157 code = UglifyJS . minify ( code , {
159158 fromString : true ,
@@ -167,6 +166,12 @@ function beautifyCode(code) {
167166 var ast = esprima . parse ( code ) ;
168167 estraverse . replace ( ast , {
169168 enter : function ( node , parent ) {
169+ // rename short vars
170+ if ( node . type === "Identifier" && parent . property !== node && shortVars [ node . name ] )
171+ return {
172+ "type" : "Identifier" ,
173+ "name" : shortVars [ node . name ]
174+ } ;
170175 // remove braces around block statements with a single child
171176 if ( node . type === "BlockStatement" && reduceableBlockStatements [ parent . type ] && node . body . length === 1 )
172177 return node . body [ 0 ] ;
0 commit comments