@@ -74,7 +74,6 @@ describe('Visitor', () => {
74
74
checkVisitorFnArgs ( ast , arguments ) ;
75
75
visited . push ( [ 'enter' , path . slice ( ) ] ) ;
76
76
} ,
77
-
78
77
leave ( node , key , parent , path ) {
79
78
checkVisitorFnArgs ( ast , arguments ) ;
80
79
visited . push ( [ 'leave' , path . slice ( ) ] ) ;
@@ -95,6 +94,34 @@ describe('Visitor', () => {
95
94
] ) ;
96
95
} ) ;
97
96
97
+ it ( 'validates ancestors argument' , ( ) => {
98
+ const ast = parse ( '{ a }' , { noLocation : true } ) ;
99
+ const nodesInPath = [ ] ;
100
+
101
+ visit ( ast , {
102
+ enter ( node , key , parent , path , ancestors ) {
103
+ const inArray = typeof key === 'number' ;
104
+ const expectedAncestors = nodesInPath . slice ( 0 , path . length - 1 ) ;
105
+ expect ( ancestors ) . to . deep . equal ( expectedAncestors ) ;
106
+
107
+ if ( inArray ) {
108
+ nodesInPath . push ( parent ) ;
109
+ }
110
+ nodesInPath . push ( node ) ;
111
+ } ,
112
+ leave ( node , key , parent , path , ancestors ) {
113
+ const inArray = typeof key === 'number' ;
114
+ const expectedAncestors = nodesInPath . slice ( 0 , path . length - 1 ) ;
115
+ expect ( ancestors ) . to . deep . equal ( expectedAncestors ) ;
116
+
117
+ if ( inArray ) {
118
+ nodesInPath . pop ( ) ;
119
+ }
120
+ nodesInPath . pop ( ) ;
121
+ } ,
122
+ } ) ;
123
+ } ) ;
124
+
98
125
it ( 'allows editing a node both on enter and on leave' , ( ) => {
99
126
const ast = parse ( '{ a, b, c { a, b, c } }' , { noLocation : true } ) ;
100
127
0 commit comments