File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ /**
4
+ * Returns the list of parents for the node, starting from the closest parent.
5
+ * @param {ASTNode } node - root node to start parent traversal
6
+ * @returns {Array.<ASTNode> } List of nodes or empty
7
+ */
8
+ function getNodeAncestors ( node ) {
9
+ const ancestors = [ ] ;
10
+ if ( ! node ) {
11
+ return ancestors ;
12
+ }
13
+
14
+ // eslint-disable-next-line no-cond-assign
15
+ for ( let parent = node ; parent = parent . parent ; ) {
16
+ ancestors . push ( parent ) ;
17
+ }
18
+ return ancestors ;
19
+ }
20
+
21
+ module . exports = getNodeAncestors ;
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const assert = require ( 'assert' ) ;
4
+ const getNodeAncestors = require ( '../../lib/util/getNodeAncestors' ) ;
5
+
6
+ describe ( 'getNodeAncestors' , ( ) => {
7
+ it ( 'returns empty array for null node' , ( ) => {
8
+ assert . deepEqual ( getNodeAncestors ( null ) , [ ] ) ;
9
+ } ) ;
10
+ it ( 'returns list of parents' , ( ) => {
11
+ const node = {
12
+ type : 'Identifier' ,
13
+ parent : {
14
+ type : 'FunctionDeclaration' ,
15
+ parent : {
16
+ type : 'Program'
17
+ }
18
+ }
19
+ } ;
20
+ const actual = getNodeAncestors ( node ) ;
21
+ const expected = [ 'FunctionDeclaration' , 'Program' ] ;
22
+
23
+ actual . forEach ( ( parent , i ) => assert . equal ( parent . type , expected [ i ] ) ) ;
24
+ } ) ;
25
+ } ) ;
You can’t perform that action at this time.
0 commit comments