One `from module import names` statement of the package's top level. */
| 117 | |
| 118 | /* One `from module import names` statement of the package's top level. */ |
| 119 | static void py_from_import(const CBMExtractCtx *ctx, TSNode stmt, py_sb_t *sb) { |
| 120 | TSNode mod = ts_node_child_by_field_name(stmt, "module_name", (uint32_t)strlen("module_name")); |
| 121 | if (ts_node_is_null(mod)) { |
| 122 | return; |
| 123 | } |
| 124 | int level = 0; |
| 125 | const char *ms = ""; |
| 126 | size_t ml = 0; |
| 127 | if (strcmp(ts_node_type(mod), "relative_import") == 0) { |
| 128 | uint32_t nc = ts_node_child_count(mod); |
| 129 | for (uint32_t i = 0; i < nc; i++) { |
| 130 | TSNode c = ts_node_child(mod, i); |
| 131 | const char *k = ts_node_type(c); |
| 132 | if (strcmp(k, "import_prefix") == 0) { |
| 133 | const char *ps; |
| 134 | size_t pl; |
| 135 | py_text(ctx, c, &ps, &pl); |
| 136 | for (size_t j = 0; j < pl; j++) { |
| 137 | level += ps[j] == '.'; |
| 138 | } |
| 139 | } else if (strcmp(k, "dotted_name") == 0) { |
| 140 | py_text(ctx, c, &ms, &ml); |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | py_text(ctx, mod, &ms, &ml); |
| 145 | } |
| 146 | if (ml > 0 && !py_field_ok(ms, ml)) { |
| 147 | return; |
| 148 | } |
| 149 | uint32_t nc = ts_node_child_count(stmt); |
| 150 | for (uint32_t i = 0; i < nc; i++) { |
| 151 | const char *field = ts_node_field_name_for_child(stmt, i); |
| 152 | if (!field || strcmp(field, "name") != 0) { |
| 153 | continue; |
| 154 | } |
| 155 | TSNode c = ts_node_child(stmt, i); |
| 156 | TSNode name = c; |
| 157 | TSNode alias = c; |
| 158 | if (strcmp(ts_node_type(c), "aliased_import") == 0) { |
| 159 | name = ts_node_child_by_field_name(c, "name", (uint32_t)strlen("name")); |
| 160 | alias = ts_node_child_by_field_name(c, "alias", (uint32_t)strlen("alias")); |
| 161 | if (ts_node_is_null(name) || ts_node_is_null(alias)) { |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | const char *ns; |
| 166 | size_t nl; |
| 167 | const char *as; |
| 168 | size_t al; |
| 169 | py_text(ctx, name, &ns, &nl); |
| 170 | py_text(ctx, alias, &as, &al); |
| 171 | if (!py_field_ok(ns, nl) || !py_field_ok(as, al)) { |
| 172 | continue; |
| 173 | } |
| 174 | char lv[PY_INT_DIGITS]; |
| 175 | int lvn = snprintf(lv, sizeof(lv), "%d", level); |
| 176 | const char *f[] = {lv, ms, ns, as}; |
no test coverage detected