Maximum control-flow nesting depth inside a compound_statement. Counts if/else/for/while/do/switch/try blocks. Lambdas reset the counter -- their body is a separate scope.
(body_node)
| 1242 | |
| 1243 | |
| 1244 | def _max_nesting_depth(body_node) -> int: |
| 1245 | """Maximum control-flow nesting depth inside a compound_statement. |
| 1246 | Counts if/else/for/while/do/switch/try blocks. Lambdas reset the |
| 1247 | counter -- their body is a separate scope.""" |
| 1248 | if body_node is None: |
| 1249 | return 0 |
| 1250 | |
| 1251 | NESTERS = { |
| 1252 | "if_statement", |
| 1253 | "for_statement", |
| 1254 | "for_range_loop", |
| 1255 | "while_statement", |
| 1256 | "do_statement", |
| 1257 | "switch_statement", |
| 1258 | "try_statement", |
| 1259 | } |
| 1260 | LAMBDA = "lambda_expression" |
| 1261 | |
| 1262 | def walk(n, depth): |
| 1263 | best = depth |
| 1264 | for c in n.children: |
| 1265 | if c.type == LAMBDA: |
| 1266 | continue # lambda body is its own scope |
| 1267 | if c.type in NESTERS: |
| 1268 | best = max(best, walk(c, depth + 1)) |
| 1269 | else: |
| 1270 | best = max(best, walk(c, depth)) |
| 1271 | return best |
| 1272 | |
| 1273 | return walk(body_node, 0) |
| 1274 | |
| 1275 | |
| 1276 | def _previous_doxygen_brief(node, src: bytes) -> bool: |