@@ -38,35 +38,46 @@ def apply_fn(node):
3838 if node .kind in self .passable_nodes : return CONT
3939 elif node .kind == 'operator' : word = node .op
4040 elif node .kind == 'pipe' : word = node .pipe
41- elif node .kind == 'redirect' : word = node .type + ' '
41+ elif node .kind == 'redirect' :
42+ if type (node .input ) == bashlex .ast .node :
43+ word += str (NodeVisitor (node .input ))
44+ elif node .input is not None :
45+ word += str (node .input )
46+ word += str (node .type )
47+ if type (node .output ) == bashlex .ast .node :
48+ word += str (NodeVisitor (node .output ))
49+ elif node .output is not None :
50+ word += str (node .output )
51+ self ._string = self ._string + ' ' + word
52+ return DONT_DESCEND
4253 elif node .kind == 'compound' :
4354 for part in node .list :
44- word += str (NodeVisitor (part )) + ' '
55+ word += ' ' + str (NodeVisitor (part ))
4556 if hasattr (node , 'redirects' ):
4657 for part in node .redirects :
47- word += str (NodeVisitor (part )) + ' '
48- self ._string = self ._string + word + ' '
58+ word += ' ' + str (NodeVisitor (part ))
59+ self ._string = self ._string + ' ' + word
4960 return DONT_DESCEND
5061 elif node .kind == 'commandsubstitution' :
5162 word = '$('
5263 cmd = node .command
5364 for part in cmd .parts :
5465 word += str (NodeVisitor (part )) + ' '
5566 word = word [:- 1 ] + ')'
56- self ._string = self ._string + word + ' '
67+ self ._string = self ._string + ' ' + word
5768 return DONT_DESCEND
5869
5970 elif hasattr (node , 'word' ):
6071 word = node .word
61- self ._string = self ._string + word + ' '
72+ self ._string = self ._string + ' ' + word
6273 return DONT_DESCEND
6374 else :
6475 raise ValueError ('Error! Unsupported node kind encountered when converting NodeVisitor to string: ' , node .kind )
65- self ._string = self ._string + word + ' '
76+ self ._string = self ._string + ' ' + word
6677 return CONT
6778
6879 self .apply (apply_fn )
69- return self ._string [: - 1 ] # remove trailing space cause wrong
80+ return self ._string . strip () # remove surrounding spaces cause wrong
7081
7182
7283 def __type__ (self ):
@@ -243,8 +254,10 @@ def children(self, root = None):
243254 elif k == 'redirect' :
244255 if isinstance (root .output , bashlex .ast .node ):
245256 return [ root .output ]
246- if root .heredoc :
257+ elif root .heredoc :
247258 return [ root .heredoc [child_num ] ]
259+ else :
260+ return []
248261 elif hasattr (root , 'list' ):
249262 return root .list
250263 else :
0 commit comments