Extract methods inside a class body
| 8552 | |
| 8553 | // Extract methods inside a class body |
| 8554 | static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn, |
| 8555 | const CBMLangSpec *spec) { |
| 8556 | TSNode body = find_class_member_body(class_node, ctx->language); |
| 8557 | if (ts_node_is_null(body)) { |
| 8558 | return; |
| 8559 | } |
| 8560 | |
| 8561 | member_iter_t it; |
| 8562 | member_iter_init(&it, body, ctx->language, false); |
| 8563 | TSNode child; |
| 8564 | while (member_iter_next(&it, &child)) { |
| 8565 | |
| 8566 | if (ctx->language == CBM_LANG_OBJC && |
| 8567 | strcmp(ts_node_type(child), "implementation_definition") == 0) { |
| 8568 | extract_objc_impl_methods(ctx, child, class_qn, spec); |
| 8569 | continue; |
| 8570 | } |
| 8571 | |
| 8572 | // Squirrel wraps each class member in a member_declaration node; the |
| 8573 | // method is the inner function_declaration. Peek through the wrapper. |
| 8574 | if (ctx->language == CBM_LANG_SQUIRREL && |
| 8575 | strcmp(ts_node_type(child), "member_declaration") == 0) { |
| 8576 | TSNode inner = cbm_find_child_by_kind(child, "function_declaration"); |
| 8577 | if (!ts_node_is_null(inner)) { |
| 8578 | child = inner; |
| 8579 | } |
| 8580 | } |
| 8581 | |
| 8582 | // Python wraps @classmethod / @staticmethod / @property methods in |
| 8583 | // a decorated_definition node. Peek through it to find the inner |
| 8584 | // function_definition so we still emit a Method entry. |
| 8585 | TSNode method_node = child; |
| 8586 | if (strcmp(ts_node_type(child), "decorated_definition") == 0) { |
| 8587 | TSNode def = ts_node_child_by_field_name(child, TS_FIELD("definition")); |
| 8588 | if (ts_node_is_null(def) || !cbm_kind_in_set(def, spec->function_node_types)) { |
| 8589 | continue; |
| 8590 | } |
| 8591 | method_node = def; |
| 8592 | } |
| 8593 | |
| 8594 | // TS/JS class-field arrow functions: `handleClick = () => {...}` is a |
| 8595 | // public_field_definition whose `value` is an arrow_function (a common |
| 8596 | // React event-handler pattern). It is not in function_node_types, so it |
| 8597 | // would otherwise be dropped. Peek through to the inner arrow and take |
| 8598 | // the method name from the field's `name` child (#new_ts_class_field_arrow). |
| 8599 | if (strcmp(ts_node_type(child), "public_field_definition") == 0) { |
| 8600 | TSNode value = ts_node_child_by_field_name(child, TS_FIELD("value")); |
| 8601 | if (ts_node_is_null(value) || !cbm_kind_in_set(value, spec->function_node_types)) { |
| 8602 | continue; |
| 8603 | } |
| 8604 | TSNode fname = ts_node_child_by_field_name(child, TS_FIELD("name")); |
| 8605 | if (ts_node_is_null(fname)) { |
| 8606 | continue; |
| 8607 | } |
| 8608 | push_method_def(ctx, value, class_node, class_qn, spec, fname); |
| 8609 | continue; |
| 8610 | } |
| 8611 |
no test coverage detected