@@ -53,32 +53,41 @@ function find_builtins(reserved) {
53
53
"-Infinity" ,
54
54
"undefined" ,
55
55
] . forEach ( add ) ;
56
- [ Object , Array , Function , Number ,
57
- String , Boolean , Error , Math ,
58
- Date , RegExp
56
+ [
57
+ Array ,
58
+ Boolean ,
59
+ Date ,
60
+ Error ,
61
+ Function ,
62
+ Math ,
63
+ Number ,
64
+ Object ,
65
+ RegExp ,
66
+ String ,
59
67
] . forEach ( function ( ctor ) {
60
68
Object . getOwnPropertyNames ( ctor ) . map ( add ) ;
61
69
if ( ctor . prototype ) {
62
70
Object . getOwnPropertyNames ( ctor . prototype ) . map ( add ) ;
63
71
}
64
72
} ) ;
73
+
65
74
function add ( name ) {
66
75
push_uniq ( reserved , name ) ;
67
76
}
68
77
}
69
78
70
79
function reserve_quoted_keys ( ast , reserved ) {
71
- function add ( name ) {
72
- push_uniq ( reserved , name ) ;
73
- }
74
-
75
80
ast . walk ( new TreeWalker ( function ( node ) {
76
81
if ( node instanceof AST_ObjectKeyVal && node . quote ) {
77
82
add ( node . key ) ;
78
83
} else if ( node instanceof AST_Sub ) {
79
84
addStrings ( node . property , add ) ;
80
85
}
81
86
} ) ) ;
87
+
88
+ function add ( name ) {
89
+ push_uniq ( reserved , name ) ;
90
+ }
82
91
}
83
92
84
93
function addStrings ( node , add ) {
@@ -127,10 +136,8 @@ function mangle_properties(ast, options) {
127
136
// note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
128
137
// the same as passing an empty string.
129
138
var debug = options . debug !== false ;
130
- var debug_name_suffix ;
131
- if ( debug ) {
132
- debug_name_suffix = ( options . debug === true ? "" : options . debug ) ;
133
- }
139
+ var debug_suffix ;
140
+ if ( debug ) debug_suffix = options . debug === true ? "" : options . debug ;
134
141
135
142
var names_to_mangle = [ ] ;
136
143
var unmangleable = [ ] ;
@@ -139,18 +146,14 @@ function mangle_properties(ast, options) {
139
146
ast . walk ( new TreeWalker ( function ( node ) {
140
147
if ( node instanceof AST_ObjectKeyVal ) {
141
148
add ( node . key ) ;
142
- }
143
- else if ( node instanceof AST_ObjectProperty ) {
149
+ } else if ( node instanceof AST_ObjectProperty ) {
144
150
// setter or getter, since KeyVal is handled above
145
151
add ( node . key . name ) ;
146
- }
147
- else if ( node instanceof AST_Dot ) {
152
+ } else if ( node instanceof AST_Dot ) {
148
153
add ( node . property ) ;
149
- }
150
- else if ( node instanceof AST_Sub ) {
154
+ } else if ( node instanceof AST_Sub ) {
151
155
addStrings ( node . property , add ) ;
152
- }
153
- else if ( node instanceof AST_Call
156
+ } else if ( node instanceof AST_Call
154
157
&& node . expression . print_to_string ( ) == "Object.defineProperty" ) {
155
158
addStrings ( node . args [ 1 ] , add ) ;
156
159
}
@@ -160,18 +163,14 @@ function mangle_properties(ast, options) {
160
163
return ast . transform ( new TreeTransformer ( function ( node ) {
161
164
if ( node instanceof AST_ObjectKeyVal ) {
162
165
node . key = mangle ( node . key ) ;
163
- }
164
- else if ( node instanceof AST_ObjectProperty ) {
166
+ } else if ( node instanceof AST_ObjectProperty ) {
165
167
// setter or getter
166
168
node . key . name = mangle ( node . key . name ) ;
167
- }
168
- else if ( node instanceof AST_Dot ) {
169
+ } else if ( node instanceof AST_Dot ) {
169
170
node . property = mangle ( node . property ) ;
170
- }
171
- else if ( ! options . keep_quoted && node instanceof AST_Sub ) {
171
+ } else if ( ! options . keep_quoted && node instanceof AST_Sub ) {
172
172
node . property = mangleStrings ( node . property ) ;
173
- }
174
- else if ( node instanceof AST_Call
173
+ } else if ( node instanceof AST_Call
175
174
&& node . expression . print_to_string ( ) == "Object.defineProperty" ) {
176
175
node . args [ 1 ] = mangleStrings ( node . args [ 1 ] ) ;
177
176
}
@@ -182,52 +181,37 @@ function mangle_properties(ast, options) {
182
181
function can_mangle ( name ) {
183
182
if ( unmangleable . indexOf ( name ) >= 0 ) return false ;
184
183
if ( reserved . indexOf ( name ) >= 0 ) return false ;
185
- if ( options . only_cache ) {
186
- return cache . has ( name ) ;
187
- }
184
+ if ( options . only_cache ) return cache . has ( name ) ;
188
185
if ( / ^ - ? [ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ? ( e [ + - ] [ 0 - 9 ] + ) ? $ / . test ( name ) ) return false ;
189
186
return true ;
190
187
}
191
188
192
189
function should_mangle ( name ) {
193
190
if ( regex && ! regex . test ( name ) ) return false ;
194
191
if ( reserved . indexOf ( name ) >= 0 ) return false ;
195
- return cache . has ( name )
196
- || names_to_mangle . indexOf ( name ) >= 0 ;
192
+ return cache . has ( name ) || names_to_mangle . indexOf ( name ) >= 0 ;
197
193
}
198
194
199
195
function add ( name ) {
200
- if ( can_mangle ( name ) )
201
- push_uniq ( names_to_mangle , name ) ;
202
-
203
- if ( ! should_mangle ( name ) ) {
204
- push_uniq ( unmangleable , name ) ;
205
- }
196
+ if ( can_mangle ( name ) ) push_uniq ( names_to_mangle , name ) ;
197
+ if ( ! should_mangle ( name ) ) push_uniq ( unmangleable , name ) ;
206
198
}
207
199
208
200
function mangle ( name ) {
209
201
if ( ! should_mangle ( name ) ) {
210
202
return name ;
211
203
}
212
-
213
204
var mangled = cache . get ( name ) ;
214
205
if ( ! mangled ) {
215
206
if ( debug ) {
216
207
// debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_.
217
- var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_" ;
218
-
219
- if ( can_mangle ( debug_mangled ) ) {
220
- mangled = debug_mangled ;
221
- }
208
+ var debug_mangled = "_$" + name + "$" + debug_suffix + "_" ;
209
+ if ( can_mangle ( debug_mangled ) ) mangled = debug_mangled ;
222
210
}
223
-
224
211
// either debug mode is off, or it is on and we could not use the mangled name
225
- if ( ! mangled ) {
226
- do {
227
- mangled = base54 ( ++ cname ) ;
228
- } while ( ! can_mangle ( mangled ) ) ;
229
- }
230
-
212
+ if ( ! mangled ) do {
213
+ mangled = base54 ( ++ cname ) ;
214
+ } while ( ! can_mangle ( mangled ) ) ;
231
215
cache . set ( name , mangled ) ;
232
216
}
233
217
return mangled ;
@@ -238,11 +222,9 @@ function mangle_properties(ast, options) {
238
222
if ( node instanceof AST_Sequence ) {
239
223
var last = node . expressions . length - 1 ;
240
224
node . expressions [ last ] = mangleStrings ( node . expressions [ last ] ) ;
241
- }
242
- else if ( node instanceof AST_String ) {
225
+ } else if ( node instanceof AST_String ) {
243
226
node . value = mangle ( node . value ) ;
244
- }
245
- else if ( node instanceof AST_Conditional ) {
227
+ } else if ( node instanceof AST_Conditional ) {
246
228
node . consequent = mangleStrings ( node . consequent ) ;
247
229
node . alternative = mangleStrings ( node . alternative ) ;
248
230
}
0 commit comments