@@ -726,7 +726,7 @@ describe("global scope", function() {
726
726
} ) ;
727
727
} ) ;
728
728
729
- describe ( "scope.getBindings " , function ( ) {
729
+ describe ( "scope methods " , function ( ) {
730
730
var traverse = types . traverse ;
731
731
732
732
var scope = [
@@ -739,8 +739,9 @@ describe("scope.getBindings", function () {
739
739
"};"
740
740
] ;
741
741
742
- var ast = parse ( scope . join ( "\n" ) ) ;
743
- it ( "should get local and global scope bindings" , function ( ) {
742
+ it ( "getBindings should get local and global scope bindings" , function ( ) {
743
+ var ast = parse ( scope . join ( "\n" ) ) ;
744
+
744
745
traverse ( ast , function ( node ) {
745
746
var bindings ;
746
747
if ( n . Program . check ( node ) ) {
@@ -760,6 +761,64 @@ describe("scope.getBindings", function () {
760
761
}
761
762
} ) ;
762
763
} ) ;
764
+
765
+ it ( "declareTemporary should use distinct names in nested scopes" , function ( ) {
766
+ var ast = parse ( scope . join ( "\n" ) ) ;
767
+ var globalVarDecl ;
768
+ var barVarDecl ;
769
+ var romVarDecl ;
770
+
771
+ types . visit ( ast , {
772
+ visitProgram : function ( path ) {
773
+ path . get ( "body" ) . unshift (
774
+ globalVarDecl = b . variableDeclaration ( "var" , [
775
+ b . variableDeclarator (
776
+ path . scope . declareTemporary ( "$" ) ,
777
+ b . literal ( "global" )
778
+ ) ,
779
+ b . variableDeclarator (
780
+ path . scope . declareTemporary ( "$" ) ,
781
+ b . literal ( "global" )
782
+ )
783
+ ] )
784
+ ) ;
785
+
786
+ this . traverse ( path ) ;
787
+ } ,
788
+
789
+ visitFunction : function ( path ) {
790
+ var funcId = path . value . id ;
791
+
792
+ var varDecl = b . variableDeclaration ( "var" , [
793
+ b . variableDeclarator (
794
+ path . scope . declareTemporary ( "$" ) ,
795
+ b . literal ( funcId . name + 1 )
796
+ ) ,
797
+ b . variableDeclarator (
798
+ path . scope . declareTemporary ( "$" ) ,
799
+ b . literal ( funcId . name + 2 )
800
+ )
801
+ ] ) ;
802
+
803
+ path . get ( "body" , "body" ) . unshift ( varDecl ) ;
804
+
805
+ if ( funcId . name === "bar" ) {
806
+ barVarDecl = varDecl ;
807
+ } else if ( funcId . name === "rom" ) {
808
+ romVarDecl = varDecl ;
809
+ }
810
+
811
+ this . traverse ( path ) ;
812
+ }
813
+ } ) ;
814
+
815
+ assert . strictEqual ( globalVarDecl . declarations [ 0 ] . id . name , "$0$0" ) ;
816
+ assert . strictEqual ( globalVarDecl . declarations [ 1 ] . id . name , "$0$1" ) ;
817
+ assert . strictEqual ( barVarDecl . declarations [ 0 ] . id . name , "$1$0" ) ;
818
+ assert . strictEqual ( barVarDecl . declarations [ 1 ] . id . name , "$1$1" ) ;
819
+ assert . strictEqual ( romVarDecl . declarations [ 0 ] . id . name , "$1$0" ) ;
820
+ assert . strictEqual ( romVarDecl . declarations [ 1 ] . id . name , "$1$1" ) ;
821
+ } ) ;
763
822
} ) ;
764
823
765
824
describe ( "catch block scope" , function ( ) {
0 commit comments