@@ -173,7 +173,7 @@ Parser.prototype.initializeEvaluating = function() {
173173 expression = expression . object ;
174174 }
175175 if ( expression . type == "Identifier" && this . scope . definitions . indexOf ( expression . name ) == - 1 ) {
176- exprName . unshift ( expression . name ) ;
176+ exprName . unshift ( this . scope . renames [ "$" + expression . name ] || expression . name ) ;
177177 exprName = exprName . join ( "." ) ;
178178 var res = this . applyPluginsBailResult ( "evaluate typeof " + exprName , expr ) ;
179179 if ( res !== undefined ) return res ;
@@ -200,12 +200,13 @@ Parser.prototype.initializeEvaluating = function() {
200200 }
201201 } ) ;
202202 this . plugin ( "evaluate Identifier" , function ( expr ) {
203+ var name = this . scope . renames [ "$" + expr . name ] || expr . name ;
203204 if ( this . scope . definitions . indexOf ( expr . name ) == - 1 ) {
204- var result = this . applyPluginsBailResult ( "evaluate Identifier " + expr . name , expr ) ;
205+ var result = this . applyPluginsBailResult ( "evaluate Identifier " + name , expr ) ;
205206 if ( result ) return result ;
206- return new BasicEvaluatedExpression ( ) . setIdentifier ( expr . name ) . setRange ( expr . range ) ;
207+ return new BasicEvaluatedExpression ( ) . setIdentifier ( name ) . setRange ( expr . range ) ;
207208 } else {
208- return this . applyPluginsBailResult ( "evaluate defined Identifier " + expr . name , expr ) ;
209+ return this . applyPluginsBailResult ( "evaluate defined Identifier " + name , expr ) ;
209210 }
210211 } ) ;
211212 this . plugin ( "evaluate MemberExpression" , function ( expression ) {
@@ -216,7 +217,7 @@ Parser.prototype.initializeEvaluating = function() {
216217 expr = expr . object ;
217218 }
218219 if ( expr . type == "Identifier" ) {
219- exprName . unshift ( expr . name ) ;
220+ exprName . unshift ( this . scope . renames [ "$" + expr . name ] || expr . name ) ;
220221 exprName = exprName . join ( "." ) ;
221222 if ( this . scope . definitions . indexOf ( expr . name ) == - 1 ) {
222223 var result = this . applyPluginsBailResult ( "evaluate Identifier " + exprName , expression ) ;
@@ -456,12 +457,21 @@ Parser.prototype.walkVariableDeclarators = function walkVariableDeclarators(decl
456457 declarators . forEach ( function ( declarator ) {
457458 switch ( declarator . type ) {
458459 case "VariableDeclarator" :
459- if ( declarator . id . type === "Identifier" ) {
460- if ( ! this . applyPluginsBailResult ( "var " + declarator . id . name ) )
461- this . scope . definitions . push ( declarator . id . name ) ;
460+ var initToIdentifier = declarator . init && declarator . init . type === "Identifier" ;
461+ if ( initToIdentifier && declarator . id . type === "Identifier" && ! this . applyPluginsBailResult ( "rename " + declarator . init . name , declarator ) ) {
462+ // renaming with "var a = b;"
463+ this . scope . renames [ "$" + declarator . id . name ] = this . scope . renames [ "$" + declarator . init . name ] || declarator . init . name ;
464+ var idx = this . scope . definitions . indexOf ( declarator . id . name ) ;
465+ if ( idx >= 0 ) this . scope . definitions . splice ( idx , 1 ) ;
466+ } else if ( declarator . id . type === "Identifier" && ! this . applyPluginsBailResult ( "var " + declarator . id . name , declarator ) ) {
467+ this . scope . definitions . push ( declarator . id . name ) ;
468+ if ( declarator . init )
469+ this . walkExpression ( declarator . init ) ;
470+ } else {
471+ this . walkExpression ( declarator . id ) ;
472+ if ( declarator . init )
473+ this . walkExpression ( declarator . init ) ;
462474 }
463- if ( declarator . init )
464- this . walkExpression ( declarator . init ) ;
465475 break ;
466476 }
467477 } , this ) ;
@@ -509,7 +519,7 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
509519 expr = expr . object ;
510520 }
511521 if ( expr . type == "Identifier" && this . scope . definitions . indexOf ( expr . name ) == - 1 ) {
512- exprName . unshift ( expr . name ) ;
522+ exprName . unshift ( this . scope . renames [ "$" + expr . name ] || expr . name ) ;
513523 exprName = exprName . join ( "." ) ;
514524 var result = this . applyPluginsBailResult ( "typeof " + exprName , expression ) ;
515525 if ( result === true )
@@ -524,10 +534,22 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
524534 this . walkExpression ( expression . right ) ;
525535 break ;
526536 case "AssignmentExpression" :
527- if ( expression . left . type !== "Identifier" ||
528- expression . left . name !== "require" )
537+ if ( expression . left . type === "Identifier" && expression . right . type === "Identifier" && ! this . applyPluginsBailResult ( "rename " + expression . right . name , expression . right ) ) {
538+ // renaming "a = b;"
539+ this . scope . renames [ "$" + expression . left . name ] = expression . right . name ;
540+ var idx = this . scope . definitions . indexOf ( expression . left . name ) ;
541+ if ( idx >= 0 ) this . scope . definitions . splice ( idx , 1 ) ;
542+ } else if ( expression . left . type === "Identifier" ) {
543+ if ( ! this . applyPluginsBailResult ( "assign " + ( this . scope . renames [ "$" + expression . left . name ] || expression . left . name , expression ) , expression ) ) {
544+ this . walkExpression ( expression . left ) ;
545+ }
546+ if ( ! this . applyPluginsBailResult ( "assigned " + ( this . scope . renames [ "$" + expression . left . name ] || expression . left . name , expression ) ) ) {
547+ this . walkExpression ( expression . right ) ;
548+ }
549+ } else {
529550 this . walkExpression ( expression . left ) ;
530- this . walkExpression ( expression . right ) ;
551+ this . walkExpression ( expression . right ) ;
552+ }
531553 break ;
532554 case "ConditionalExpression" :
533555 var result = this . applyPluginsBailResult ( "expression ?:" , expression ) ;
@@ -549,28 +571,38 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
549571 this . walkExpressions ( expression . arguments ) ;
550572 break ;
551573 case "CallExpression" :
552- if ( expression . callee . type === "FunctionExpression" && expression . arguments ) {
553- // (function(...) { }(...))
554- var args = expression . arguments . map ( function ( arg , idx ) {
574+ function walkIIFE ( functionExpression , args ) {
575+ var params = functionExpression . params ;
576+ var args = args . map ( function ( arg , idx ) {
555577 var result = this . evaluateExpression ( arg ) ;
556578 if ( result && ! result . isIdentifier ( ) ) result = undefined ;
557- if ( result && ( ! expression . callee . params [ idx ] || expression . callee . params [ idx ] . name !== result . identifier ) )
558- result = undefined ;
559579 if ( ! result ) {
560580 this . walkExpression ( arg ) ;
561581 return ;
562582 }
563583 return result . identifier ;
564584 } , this ) ;
565- this . inScope ( expression . callee . params . filter ( function ( identifier , idx ) {
566- return identifier . name !== args [ idx ] ;
585+ this . inScope ( params . filter ( function ( identifier , idx ) {
586+ return ! args [ idx ] ;
567587 } ) , function ( ) {
568- if ( expression . callee . body . type === "BlockStatement" )
569- this . walkStatement ( expression . callee . body ) ;
588+ args . forEach ( function ( arg , idx ) {
589+ if ( ! arg ) return ;
590+ if ( params [ idx ] . type !== "Identifier" ) return ;
591+ this . scope . renames [ "$" + params [ idx ] . name ] = arg ;
592+ } , this ) ;
593+ if ( functionExpression . body . type === "BlockStatement" )
594+ this . walkStatement ( functionExpression . body ) ;
570595 else
571- this . walkExpression ( expression . callee . body ) ;
596+ this . walkExpression ( functionExpression . body ) ;
572597 } . bind ( this ) ) ;
573-
598+ }
599+ if ( expression . callee . type === "MemberExpression" && expression . callee . object . type === "FunctionExpression" && expression . callee . property . type === "Identifier" && [ "call" , "bind" ] . indexOf ( expression . callee . property . name ) >= 0 && expression . arguments && expression . arguments . length > 1 ) {
600+ // (function(...) { }.call/bind(?, ...))
601+ walkIIFE . call ( this , expression . callee . object , expression . arguments . slice ( 1 ) ) ;
602+ this . walkExpression ( expression . arguments [ 0 ] ) ;
603+ } else if ( expression . callee . type === "FunctionExpression" && expression . arguments ) {
604+ // (function(...) { }(...))
605+ walkIIFE . call ( this , expression . callee , expression . arguments ) ;
574606 } else {
575607
576608 var callee = this . evaluateExpression ( expression . callee ) ;
@@ -594,7 +626,7 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
594626 expr = expr . object ;
595627 }
596628 if ( expr . type == "Identifier" && this . scope . definitions . indexOf ( expr . name ) == - 1 ) {
597- exprName . unshift ( expr . name ) ;
629+ exprName . unshift ( this . scope . renames [ "$" + expr . name ] || expr . name ) ;
598630 exprName = exprName . join ( "." ) ;
599631 var result = this . applyPluginsBailResult ( "expression " + exprName , expression ) ;
600632 if ( result === true )
@@ -606,7 +638,7 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
606638 break ;
607639 case "Identifier" :
608640 if ( this . scope . definitions . indexOf ( expression . name ) == - 1 ) {
609- var result = this . applyPluginsBailResult ( "expression " + expression . name , expression ) ;
641+ var result = this . applyPluginsBailResult ( "expression " + ( this . scope . renames [ "$" + expression . name ] || expression . name ) , expression ) ;
610642 if ( result === true )
611643 break ;
612644 }
@@ -618,7 +650,8 @@ Parser.prototype.inScope = function inScope(params, fn) {
618650 var oldScope = this . scope ;
619651 this . scope = {
620652 inTry : false ,
621- definitions : oldScope . definitions . slice ( )
653+ definitions : oldScope . definitions . slice ( ) ,
654+ renames : Object . create ( oldScope . renames )
622655 } ;
623656 params . forEach ( function ( param ) {
624657 if ( typeof param !== "string" ) {
@@ -722,7 +755,8 @@ Parser.prototype.parse = function parse(source, initialState) {
722755 var oldState = this . state ;
723756 this . scope = {
724757 inTry : false ,
725- definitions : [ ]
758+ definitions : [ ] ,
759+ renames : { }
726760 } ;
727761 var state = this . state = initialState || { } ;
728762 if ( this . applyPluginsBailResult ( "program" , ast ) === undefined )
0 commit comments