Resolve a sibling-file import: a bare path/name (no leading "./") that names * a file relative to the importer's directory. This covers build/markup * grammars whose import string is a sibling filename or directory rather than a * dotted module path: * - SCSS `@use 'vars'` → sibling `_vars.scss` (partial underscore) * - Just `import 'common.just'` → sibling `common.jus
| 2019 | * File/Module-node QN (extension is stripped by fqn_module). Returns a borrowed |
| 2020 | * node or NULL. Several filename conventions are tried in turn. */ |
| 2021 | static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx, |
| 2022 | const char *source_rel, |
| 2023 | const char *source_file_qn, |
| 2024 | const char *module_path, bool php_only) { |
| 2025 | if (!module_path || !module_path[0]) { |
| 2026 | return NULL; |
| 2027 | } |
| 2028 | /* Directory of the importing file (empty for repo-root files). */ |
| 2029 | char *dir = path_dirname(source_rel ? source_rel : ""); |
| 2030 | if (!dir) { |
| 2031 | return NULL; |
| 2032 | } |
| 2033 | |
| 2034 | /* Candidate relative paths, in priority order. */ |
| 2035 | char cands[5][PKGMAP_PATH_BUF]; |
| 2036 | int ncand = 0; |
| 2037 | const char *base = module_path; |
| 2038 | /* Skip a leading "./". */ |
| 2039 | if (base[0] == '.' && base[1] == '/') { |
| 2040 | base += 2; |
| 2041 | } |
| 2042 | /* 1. Direct sibling: dir/<module_path>. */ |
| 2043 | snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, dir[0] ? "/" : "", base); |
| 2044 | /* 2. SCSS partial: dir/[subdir/]_<basename>.scss (underscore-prefixed). */ |
| 2045 | { |
| 2046 | const char *slash = strrchr(base, '/'); |
| 2047 | const char *bn = slash ? slash + 1 : base; |
| 2048 | char *dpart = slash ? cbm_strndup(base, (size_t)(slash - base)) : strdup(""); |
| 2049 | if (dpart && bn[0] != '_') { |
| 2050 | snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s%s_%s.scss", dir, dir[0] ? "/" : "", |
| 2051 | dpart[0] ? dpart : "", dpart[0] ? "/" : "", bn); |
| 2052 | } |
| 2053 | free(dpart); |
| 2054 | } |
| 2055 | /* 3. Meson subdir: dir/<module_path>/meson.build. */ |
| 2056 | snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s/meson.build", dir, dir[0] ? "/" : "", base); |
| 2057 | /* 4. Basename sibling: dir/<basename(module_path)>. Covers include paths |
| 2058 | * that carry a non-relative prefix (Hyprlang `source = ~/.config/.../x.conf`, |
| 2059 | * absolute include paths) but reference a file sitting beside the importer. */ |
| 2060 | { |
| 2061 | const char *slash = strrchr(base, '/'); |
| 2062 | if (slash && slash[1]) { |
| 2063 | snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, dir[0] ? "/" : "", slash + 1); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | const cbm_gbuf_node_t *found = NULL; |
| 2068 | for (int i = 0; i < ncand; i++) { |
| 2069 | char *qn = cbm_pipeline_fqn_module(ctx->project_name, cands[i]); |
| 2070 | if (!qn) { |
| 2071 | continue; |
| 2072 | } |
| 2073 | const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(ctx->gbuf, qn); |
| 2074 | free(qn); |
| 2075 | if (n && (!php_only || node_in_php_file(n)) && import_targetable_label(n->label) && |
| 2076 | (!source_file_qn || !n->qualified_name || |
| 2077 | strcmp(n->qualified_name, source_file_qn) != 0)) { |
| 2078 | found = n; |
no test coverage detected