1
1
define ( [ "require" , "exports" , "module" , "./parse-js" , "./squeeze-more" ] , function ( require , exports , module ) {
2
+
2
3
/***********************************************************************
3
4
4
5
A JavaScript tokenizer / parser / beautifier / compressor.
@@ -62,6 +63,7 @@ define(["require", "exports", "module", "./parse-js", "./squeeze-more"], functio
62
63
var jsp = require ( "./parse-js" ) ,
63
64
slice = jsp . slice ,
64
65
member = jsp . member ,
66
+ is_identifier_char = jsp . is_identifier_char ,
65
67
PRECEDENCE = jsp . PRECEDENCE ,
66
68
OPERATORS = jsp . OPERATORS ;
67
69
@@ -510,7 +512,13 @@ function ast_add_scope(ast) {
510
512
511
513
function ast_mangle ( ast , options ) {
512
514
var w = ast_walker ( ) , walk = w . walk , scope ;
513
- options = options || { } ;
515
+ options = defaults ( options , {
516
+ mangle : true ,
517
+ toplevel : false ,
518
+ defines : null ,
519
+ except : null ,
520
+ no_functions : false
521
+ } ) ;
514
522
515
523
function get_mangled ( name , newMangle ) {
516
524
if ( ! options . mangle ) return name ;
@@ -537,7 +545,7 @@ function ast_mangle(ast, options) {
537
545
} ;
538
546
539
547
function _lambda ( name , args , body ) {
540
- if ( ! options . no_functions ) {
548
+ if ( ! options . no_functions && options . mangle ) {
541
549
var is_defun = this [ 0 ] == "defun" , extra ;
542
550
if ( name ) {
543
551
if ( is_defun ) name = get_mangled ( name ) ;
@@ -1237,6 +1245,9 @@ function ast_squeeze(ast, options) {
1237
1245
t = walk ( t ) ;
1238
1246
e = walk ( e ) ;
1239
1247
1248
+ if ( empty ( e ) && empty ( t ) )
1249
+ return [ "stat" , c ] ;
1250
+
1240
1251
if ( empty ( t ) ) {
1241
1252
c = negate ( c ) ;
1242
1253
t = e ;
@@ -1257,8 +1268,6 @@ function ast_squeeze(ast, options) {
1257
1268
}
1258
1269
} ) ( ) ;
1259
1270
}
1260
- if ( empty ( e ) && empty ( t ) )
1261
- return [ "stat" , c ] ;
1262
1271
var ret = [ "if" , c , t , e ] ;
1263
1272
if ( t [ 0 ] == "if" && empty ( t [ 3 ] ) && empty ( e ) ) {
1264
1273
ret = best_of ( ret , walk ( [ "if" , [ "binary" , "&&" , c , t [ 1 ] ] , t [ 2 ] ] ) ) ;
@@ -1402,6 +1411,15 @@ function ast_squeeze(ast, options) {
1402
1411
return expr [ 1 ] ;
1403
1412
}
1404
1413
return [ this [ 0 ] , expr , MAP ( args , walk ) ] ;
1414
+ } ,
1415
+ "num" : function ( num ) {
1416
+ if ( ! isFinite ( num ) )
1417
+ return [ "binary" , "/" , num === 1 / 0
1418
+ ? [ "num" , 1 ] : num === - 1 / 0
1419
+ ? [ "unary-prefix" , "-" , [ "num" , 1 ] ]
1420
+ : [ "num" , 0 ] , [ "num" , 0 ] ] ;
1421
+
1422
+ return [ this [ 0 ] , num ] ;
1405
1423
}
1406
1424
} , function ( ) {
1407
1425
for ( var i = 0 ; i < 2 ; ++ i ) {
@@ -1502,6 +1520,15 @@ function gen_code(ast, options) {
1502
1520
finally { indentation -= incr ; }
1503
1521
} ;
1504
1522
1523
+ function last_char ( str ) {
1524
+ str = str . toString ( ) ;
1525
+ return str . charAt ( str . length - 1 ) ;
1526
+ } ;
1527
+
1528
+ function first_char ( str ) {
1529
+ return str . toString ( ) . charAt ( 0 ) ;
1530
+ } ;
1531
+
1505
1532
function add_spaces ( a ) {
1506
1533
if ( beautify )
1507
1534
return a . join ( " " ) ;
@@ -1510,7 +1537,8 @@ function gen_code(ast, options) {
1510
1537
var next = a [ i + 1 ] ;
1511
1538
b . push ( a [ i ] ) ;
1512
1539
if ( next &&
1513
- ( ( / [ a - z 0 - 9 _ \x24 ] $ / i. test ( a [ i ] . toString ( ) ) && / ^ [ a - z 0 - 9 _ \x24 ] / i. test ( next . toString ( ) ) ) ||
1540
+ ( ( is_identifier_char ( last_char ( a [ i ] ) ) && ( is_identifier_char ( first_char ( next ) )
1541
+ || first_char ( next ) == "\\" ) ) ||
1514
1542
( / [ \+ \- ] $ / . test ( a [ i ] . toString ( ) ) && / ^ [ \+ \- ] / . test ( next . toString ( ) ) ) ) ) {
1515
1543
b . push ( " " ) ;
1516
1544
}
@@ -1570,7 +1598,7 @@ function gen_code(ast, options) {
1570
1598
} ;
1571
1599
1572
1600
function make_num ( num ) {
1573
- var str = num . toString ( 10 ) , a = [ str . replace ( / ^ 0 \. / , "." ) ] , m ;
1601
+ var str = num . toString ( 10 ) , a = [ str . replace ( / ^ 0 \. / , "." ) . replace ( 'e+' , 'e' ) ] , m ;
1574
1602
if ( Math . floor ( num ) === num ) {
1575
1603
if ( num >= 0 ) {
1576
1604
a . push ( "0x" + num . toString ( 16 ) . toLowerCase ( ) , // probably pointless
@@ -2063,4 +2091,4 @@ exports.MAP = MAP;
2063
2091
2064
2092
// keep this last!
2065
2093
exports . ast_squeeze_more = require ( "./squeeze-more" ) . ast_squeeze_more ;
2066
- } ) ;
2094
+ } ) ;
0 commit comments