A string value of file text `src` in module directory `mdir`: a literal, or * a template of literals and ${path.module}. false when any part is something * else. */
| 72 | * a template of literals and ${path.module}. false when any part is something |
| 73 | * else. */ |
| 74 | static bool tf_string(const char *src, const char *mdir, TSNode n, char *out, size_t sz) { |
| 75 | size_t len = 0; |
| 76 | out[0] = '\0'; |
| 77 | while (tf_type(n, "expression") || tf_type(n, "literal_value") || tf_type(n, "template_expr") || |
| 78 | tf_type(n, "string_lit") || tf_type(n, "quoted_template")) { |
| 79 | if (tf_type(n, "string_lit") || tf_type(n, "quoted_template")) { |
| 80 | break; |
| 81 | } |
| 82 | if (ts_node_named_child_count(n) != SKIP_ONE) { |
| 83 | return false; |
| 84 | } |
| 85 | n = ts_node_named_child(n, 0); |
| 86 | } |
| 87 | if (!tf_type(n, "string_lit") && !tf_type(n, "quoted_template")) { |
| 88 | return false; |
| 89 | } |
| 90 | for (uint32_t i = 0; i < ts_node_named_child_count(n); i++) { |
| 91 | TSNode c = ts_node_named_child(n, i); |
| 92 | uint32_t b = ts_node_start_byte(c); |
| 93 | uint32_t e = ts_node_end_byte(c); |
| 94 | if (tf_type(c, "quoted_template_start") || tf_type(c, "quoted_template_end")) { |
| 95 | continue; |
| 96 | } |
| 97 | if (tf_type(c, "template_literal")) { |
| 98 | if (!tf_add(out, sz, &len, src + b, e - b)) { |
| 99 | return false; |
| 100 | } |
| 101 | continue; |
| 102 | } |
| 103 | if (!tf_type(c, "template_interpolation")) { |
| 104 | return false; |
| 105 | } |
| 106 | /* ${path.module} only */ |
| 107 | TSNode expr = {0}; |
| 108 | for (uint32_t j = 0; j < ts_node_named_child_count(c); j++) { |
| 109 | if (tf_type(ts_node_named_child(c, j), "expression")) { |
| 110 | expr = ts_node_named_child(c, j); |
| 111 | } |
| 112 | } |
| 113 | if (ts_node_is_null(expr)) { |
| 114 | return false; |
| 115 | } |
| 116 | uint32_t xb = ts_node_start_byte(expr); |
| 117 | uint32_t xe = ts_node_end_byte(expr); |
| 118 | static const char PATH_MODULE[] = "path.module"; |
| 119 | if (xe - xb != sizeof(PATH_MODULE) - SKIP_ONE || |
| 120 | strncmp(src + xb, PATH_MODULE, xe - xb) != 0) { |
| 121 | return false; |
| 122 | } |
| 123 | /* an absolute marker: the module directory, from the repo root */ |
| 124 | if (!tf_add(out, sz, &len, "\x01", SKIP_ONE) || |
| 125 | !tf_add(out, sz, &len, mdir, strlen(mdir))) { |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 |
no test coverage detected