@@ -79,6 +79,7 @@ function Compressor(options, false_by_default) {
79
79
screw_ie8 : true ,
80
80
drop_console : false ,
81
81
angular : false ,
82
+ expression : false ,
82
83
warnings : true ,
83
84
global_defs : { } ,
84
85
passes : 1 ,
@@ -115,12 +116,18 @@ Compressor.prototype = new TreeTransformer;
115
116
merge ( Compressor . prototype , {
116
117
option : function ( key ) { return this . options [ key ] } ,
117
118
compress : function ( node ) {
119
+ if ( this . option ( "expression" ) ) {
120
+ node = node . process_expression ( true ) ;
121
+ }
118
122
var passes = + this . options . passes || 1 ;
119
123
for ( var pass = 0 ; pass < passes && pass < 3 ; ++ pass ) {
120
124
if ( pass > 0 || this . option ( "reduce_vars" ) )
121
125
node . reset_opt_flags ( this , true ) ;
122
126
node = node . transform ( this ) ;
123
127
}
128
+ if ( this . option ( "expression" ) ) {
129
+ node = node . process_expression ( false ) ;
130
+ }
124
131
return node ;
125
132
} ,
126
133
warn : function ( text , props ) {
@@ -177,6 +184,42 @@ merge(Compressor.prototype, {
177
184
return this . print_to_string ( ) == node . print_to_string ( ) ;
178
185
} ) ;
179
186
187
+ AST_Node . DEFMETHOD ( "process_expression" , function ( insert ) {
188
+ var self = this ;
189
+ var tt = new TreeTransformer ( function ( node ) {
190
+ if ( insert && node instanceof AST_SimpleStatement ) {
191
+ return make_node ( AST_Return , node , {
192
+ value : node . body
193
+ } ) ;
194
+ }
195
+ if ( ! insert && node instanceof AST_Return ) {
196
+ return make_node ( AST_SimpleStatement , node , {
197
+ body : node . value || make_node ( AST_Undefined , node )
198
+ } ) ;
199
+ }
200
+ if ( node instanceof AST_Lambda && node !== self ) {
201
+ return node ;
202
+ }
203
+ if ( node instanceof AST_Block ) {
204
+ var index = node . body . length - 1 ;
205
+ if ( index >= 0 ) {
206
+ node . body [ index ] = node . body [ index ] . transform ( tt ) ;
207
+ }
208
+ }
209
+ if ( node instanceof AST_If ) {
210
+ node . body = node . body . transform ( tt ) ;
211
+ if ( node . alternative ) {
212
+ node . alternative = node . alternative . transform ( tt ) ;
213
+ }
214
+ }
215
+ if ( node instanceof AST_With ) {
216
+ node . body = node . body . transform ( tt ) ;
217
+ }
218
+ return node ;
219
+ } ) ;
220
+ return self . transform ( tt ) ;
221
+ } ) ;
222
+
180
223
AST_Node . DEFMETHOD ( "reset_opt_flags" , function ( compressor , rescan ) {
181
224
var reduce_vars = rescan && compressor . option ( "reduce_vars" ) ;
182
225
var safe_ids = [ ] ;
@@ -1989,7 +2032,14 @@ merge(Compressor.prototype, {
1989
2032
def ( AST_Constant , return_null ) ;
1990
2033
def ( AST_This , return_null ) ;
1991
2034
def ( AST_Call , function ( compressor , first_in_statement ) {
1992
- if ( ! this . has_pure_annotation ( compressor ) && compressor . pure_funcs ( this ) ) return this ;
2035
+ if ( ! this . has_pure_annotation ( compressor ) && compressor . pure_funcs ( this ) ) {
2036
+ if ( this . expression instanceof AST_Function ) {
2037
+ var node = this . clone ( ) ;
2038
+ node . expression = node . expression . process_expression ( false ) ;
2039
+ return node ;
2040
+ }
2041
+ return this ;
2042
+ }
1993
2043
if ( this . pure ) {
1994
2044
compressor . warn ( "Dropping __PURE__ call [{file}:{line},{col}]" , this . start ) ;
1995
2045
this . pure . value = this . pure . value . replace ( / [ @ # ] _ _ P U R E _ _ / g, ' ' ) ;
@@ -2672,9 +2722,9 @@ merge(Compressor.prototype, {
2672
2722
}
2673
2723
if ( compressor . option ( "side_effects" ) ) {
2674
2724
if ( self . expression instanceof AST_Function
2675
- && self . args . length == 0
2676
2725
&& ! AST_Block . prototype . has_side_effects . call ( self . expression , compressor ) ) {
2677
- return make_node ( AST_Undefined , self ) . transform ( compressor ) ;
2726
+ var args = self . args . concat ( make_node ( AST_Undefined , self ) ) ;
2727
+ return AST_Seq . from_array ( args ) . transform ( compressor ) ;
2678
2728
}
2679
2729
}
2680
2730
if ( compressor . option ( "drop_console" ) ) {
0 commit comments