@@ -110,34 +110,33 @@ def __check_if_node_is_valid(node):
110110 return valid
111111
112112
113+ def __handle_skip (stack , skip ):
114+ body = stack [skip ]
115+ children = [body ]
116+ if hasattr (body , 'children' ):
117+ children = body .children
118+ stack = stack [:skip ] + children + stack [skip + 1 :]
119+ node = body
120+ end_line , _ = body .end_pos
121+ return node , end_line
122+
123+
113124def __handle_flow_nodes (node , end_line , stack ):
114125 from_keyword = False
115126 if isinstance (node , tree_nodes .Keyword ):
116127 from_keyword = True
117- if node .value in {'if' , 'elif' , 'with' , 'while' , 'except' }:
118- body = stack [ 2 ]
119- children = [ body ]
120- if hasattr ( body , 'children' ):
121- children = body . children
122- stack = stack [: 2 ] + children + stack [ 3 :]
123- node = body
124- end_line , _ = body . end_pos
128+ if node .value in {'if' , 'elif' , 'with' , 'while' }:
129+ node , end_line = __handle_skip ( stack , 2 )
130+ elif node . value in { 'except' }:
131+ first_node = stack [ 0 ]
132+ if isinstance ( first_node , tree_nodes . Operator ):
133+ node , end_line = __handle_skip ( stack , 1 )
134+ else :
135+ node , end_line = __handle_skip ( stack , 2 )
125136 elif node .value in {'for' }:
126- body = stack [4 ]
127- children = [body ]
128- if hasattr (body , 'children' ):
129- children = body .children
130- stack = stack [:4 ] + children + stack [5 :]
131- node = body
132- end_line , _ = body .end_pos
137+ node , end_line = __handle_skip (stack , 4 )
133138 elif node .value in {'else' }:
134- body = stack [1 ]
135- children = [body ]
136- if hasattr (body , 'children' ):
137- children = body .children
138- stack = stack [:1 ] + children + stack [2 :]
139- node = body
140- end_line , _ = body .end_pos
139+ node , end_line = __handle_skip (stack , 1 )
141140 return end_line , from_keyword , node , stack
142141
143142
0 commit comments