Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(workspace): read template literals with a nested template in a hole (#4655)

Raghav Chamadiya72971ef0b975
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/js_clients.py b/packages/core/src/repowise/core/workspace/extractors/http/js_clients.py
index 9250b99f6..c6d333b20 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/js_clients.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/js_clients.py
@@ -34,10 +34,11 @@ if TYPE_CHECKING:
_FETCH_RE = re.compile(
r"""fetch\s*\(\s*['"`]([^'"`]+)['"`]""",
)
-_FETCH_METHOD_RE = re.compile(
- r"""fetch\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*\{[^}]*method\s*:\s*['"](\w+)['"]""",
- re.DOTALL,
-)
+_OPTIONS_METHOD = r"""\s*,\s*\{[^}]*method\s*:\s*['"](\w+)['"]"""
+_FETCH_METHOD_RE = re.compile(r"""fetch\s*\(\s*['"`]([^'"`]+)['"`]""" + _OPTIONS_METHOD, re.DOTALL)
+# The options after a URL literal the capture above stopped short of: a nested
+# template in a hole (`` fetch(`/a${q ? `?${q}` : ""}`, { method }) ``).
+_OPTIONS_METHOD_RE = re.compile(_OPTIONS_METHOD, re.DOTALL)
# Wrapper call: IDENT("<url>" | `<url>`, ...) where the URL literal is concrete,
# i.e. starts with `/`, a `${...}` base placeholder, or a scheme. The scan is
@@ -68,8 +69,16 @@ def fetch_calls(content: str) -> Iterator[ClientCallMatch]:
for m in _FETCH_RE.finditer(content):
if m.group(1) in method_urls:
continue
+ url = literal_span(content, m, 1)
+ opt = None
+ if len(url) - 2 > len(m.group(1)):
+ opt = _OPTIONS_METHOD_RE.match(content, m.start(1) + len(url) - 1)
yield ClientCallMatch(
- client="fetch", url=literal_span(content, m, 1), offset=m.start(), callee="fetch"
+ client="fetch",
+ url=url,
+ offset=m.start(),
+ callee="fetch",
+ method=opt.group(1).upper() if opt else None,
)
@@ -102,8 +111,10 @@ def wrapper_calls(content: str, clients: frozenset[str]) -> Iterator[ClientCallM
continue
if clients and _receiver(content, head.start(1)) in clients:
continue # `http.post(url, { method })`, a client's own verb
- nl = content.find("\n", m.end())
- window = content[m.end() :] if nl == -1 else content[m.end() : nl]
+ url = literal_span(content, m, 1)
+ after = m.start(1) + len(url) - 1 # past the literal, nested templates included
+ nl = content.find("\n", after)
+ window = content[after:] if nl == -1 else content[after:nl]
method_opt = _METHOD_OPT_RE.search(window)
# Require an HTTP signal: an HTTP-ish callee name or a method option.
if not (_HTTP_NAME_RE.search(callee) or method_opt):
@@ -112,7 +123,7 @@ def wrapper_calls(content: str, clients: frozenset[str]) -> Iterator[ClientCallM
# is, and `apiPost`/`apiDelete` carry it.
yield ClientCallMatch(
client="wrapper",
- url=literal_span(content, m, 1),
+ url=url,
offset=head.start(),
callee=callee,
method=method_opt.group(1).upper() if method_opt else None,
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/paths.py b/packages/core/src/repowise/core/workspace/extractors/http/paths.py
index 9585e6b56..e9f726510 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/paths.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/paths.py
@@ -71,12 +71,19 @@ def _after_authority(url: str) -> str | None:
return url[scheme + 3 :] if scheme >= 0 else None
+# A string literal opening with `?` inside an interpolation: a query suffix.
+# The quote must open a literal, so `a=="x"?b:c` (a closing quote) is not one.
+_QUERY_LITERAL_RE = re.compile(r"""(?:^|[\s?:(,=+|&])[`'"]\?""")
+
+
def flatten_interpolations(path: str) -> str:
"""*path* with each ``${...}`` holding braces of its own reduced to ``${expr}``.
``/rate/${format(d, F, { in: utc })}`` is one parameter segment; the
brace-free patterns of :func:`normalize_http_path` would stop inside it.
An unbalanced interpolation is left for :func:`is_unusable_consumer_path`.
+ A last hole holding a literal that starts with ``?``
+ (`` ${q ? `?${q}` : ""} ``) is the query string, so the path ends before it.
"""
out: list[str] = []
pos = 0
@@ -86,6 +93,8 @@ def flatten_interpolations(path: str) -> str:
break
inner = path[start + 2 : end]
out.append(path[pos:start])
+ if end == len(path) - 1 and _QUERY_LITERAL_RE.search(inner):
+ return "".join(out)
out.append("${expr}" if "{" in inner else path[start : end + 1])
pos = end + 1
out.append(path[pos:])
diff --git a/packages/core/src/repowise/core/workspace/extractors/strings.py b/packages/core/src/repowise/core/workspace/extractors/strings.py
index 419ac49d9..d2f211230 100644
--- a/packages/core/src/repowise/core/workspace/extractors/strings.py
+++ b/packages/core/src/repowise/core/workspace/extractors/strings.py
@@ -238,15 +238,41 @@ def _head_re(heads: tuple[str, ...]) -> re.Pattern[str]:
return re.compile(rf"^(?:{alts})\s*\(")
+def _template_end(text: str, open_idx: int) -> int:
+ """Index of the backtick closing the template literal opened at *open_idx*, or -1.
+
+ A backtick inside a ``${...}`` hole opens a nested literal
+ (`` `/a${q ? `?${q}` : ""}` ``), so each hole is skipped whole.
+ """
+ i, end = open_idx + 1, min(len(text), open_idx + SCAN_LIMIT)
+ while i < end:
+ ch = text[i]
+ if ch == "`":
+ return i
+ if ch == "$" and text.startswith("{", i + 1):
+ i = match_paren(text, i + 1, closer="}")
+ if i < 0:
+ return -1
+ elif ch == "\\":
+ i += 1
+ i += 1
+ return -1
+
+
def _parse_literal(text: str, syntax: StringSyntax) -> tuple[str, str, str] | None:
"""``(lowercased prefix, quote, body)`` when *text* is exactly one string literal."""
- m = _literal_re(syntax.quotes, syntax.prefixes).match(text.strip())
+ text = text.strip()
+ m = _literal_re(syntax.quotes, syntax.prefixes).match(text)
if m is None:
return None
- body = m.group("body")
- if m.group("q") in body:
+ body, quote = m.group("body"), m.group("q")
+ if quote in body and not (
+ quote == "`"
+ and quote in syntax.template_quotes
+ and _template_end(text, m.start("q")) == len(text) - 1
+ ):
return None # the literal ended and something else followed it
- return m.group("prefix").lower(), m.group("q"), body
+ return m.group("prefix").lower(), quote, body
def _interpolates(prefix: str, quote: str, syntax: StringSyntax) -> bool:
@@ -814,13 +840,16 @@ def literal_span(content: str, m: re.Match[str], group: int) -> str:
For a recogniser whose regex captures the text between the quotes: the
slice one character wider on each side is the literal as written, which is
- what :func:`resolve_string` reads. A capture that stopped at a different
- quote (one inside a ``${...}`` expression) is closed with its own opening
- quote, so the text read is the prefix the regex saw and the row is kept.
+ what :func:`resolve_string` reads. A template literal whose capture stopped
+ at a quote inside a ``${...}`` hole is read to its real closing backtick.
+ Any other capture that stopped at a different quote is closed with its own
+ opening quote, so the text read is the prefix the regex saw and the row is kept.
"""
- quote = content[m.start(group) - 1]
- body = content[m.start(group) : m.end(group)]
- return quote + body + quote
+ start, end = m.start(group), m.end(group)
+ quote = content[start - 1]
+ if quote == "`" and "${" in content[start:end]:
+ end = max(end, _template_end(content, start - 1))
+ return quote + content[start:end] + quote
# ---------------------------------------------------------------------------
diff --git a/tests/unit/workspace/test_client_calls.py b/tests/unit/workspace/test_client_calls.py
index fcff47ddb..4f49e9af5 100644
--- a/tests/unit/workspace/test_client_calls.py
+++ b/tests/unit/workspace/test_client_calls.py
@@ -43,6 +43,7 @@ class TestLiterals:
[
(JS_SYNTAX, "'/api/users'", "/api/users"),
(JS_SYNTAX, "`${BASE}/users/${id}`", "${BASE}/users/${id}"),
+ (JS_SYNTAX, '`/a/${id}${q ? `?${q}` : ""}`', '/a/${id}${q ? `?${q}` : ""}'),
(PYTHON_SYNTAX, '"/api/users"', "/api/users"),
(PYTHON_SYNTAX, 'f"{base}/users/{uid}"', "${base}/users/${uid}"),
(PYTHON_SYNTAX, 'r"/raw"', "/raw"),
@@ -73,6 +74,7 @@ class TestLiterals:
(JS_SYNTAX, "path"),
(JS_SYNTAX, '"/a" + buildPath(id)'),
(JS_SYNTAX, "buildPath(id)"),
+ (JS_SYNTAX, "`/a` || `/b`"),
(PYTHON_SYNTAX, 'b"/bytes"'),
(PYTHON_SYNTAX, 'f"/x/{{literal}}"'),
(RUST_SYNTAX, "&url"),
@@ -281,13 +283,25 @@ class TestArgumentScanning:
class TestLiteralSpan:
- def test_a_capture_cut_at_an_inner_quote_keeps_its_prefix(self):
+ def test_a_capture_cut_at_an_inner_quote_reads_the_whole_template(self):
from repowise.core.workspace.extractors.http.js_clients import fetch_calls
content = "fetch(`https://h/v1/links?${new URLSearchParams({ a: 'x' })}`)"
(row,) = fetch_calls(content)
- assert row.url == "`https://h/v1/links?${new URLSearchParams({ a: `"
- assert resolve_string(row.url, JS_SYNTAX) == "https://h/v1/links?${new URLSearchParams({ a: "
+ assert row.url == "`https://h/v1/links?${new URLSearchParams({ a: 'x' })}`"
+ assert resolve_string(row.url, JS_SYNTAX) == row.url[1:-1]
+
+ def test_a_nested_template_keeps_the_fetch_method(self):
+ from repowise.core.workspace.extractors.http.js_clients import fetch_calls, wrapper_calls
+
+ content = (
+ 'fetch(`${API}/repos${q ? `?${q}` : ""}`, { method: "POST" });\n'
+ 'apiCall(`/wins${q ? `?${q}` : ""}`, { method: "DELETE" });\n'
+ )
+ (row,) = fetch_calls(content)
+ assert (row.url, row.method) == ('`${API}/repos${q ? `?${q}` : ""}`', "POST")
+ (row,) = wrapper_calls(content, frozenset())
+ assert (row.url, row.method) == ('`/wins${q ? `?${q}` : ""}`', "DELETE")
class TestConsumerContracts:
@@ -327,6 +341,14 @@ class TestConsumerUrlShapes:
assert flatten_interpolations("/a/${id}/b") == "/a/${id}/b"
assert flatten_interpolations("/a/${broken") == "/a/${broken"
+ def test_a_query_hole_ends_the_path(self):
+ assert flatten_interpolations('/a/${id}${q ? `?${q}` : ""}') == "/a/${id}"
+ assert flatten_interpolations("/a/${id}${qs ? '?' + qs : ''}") == "/a/${id}"
+ assert flatten_interpolations("/a/${b ? `/${c}` : ''}/d") == "/a/${expr}/d"
+ assert flatten_interpolations('/a/${k=="x"?id:0}/d') == '/a/${k=="x"?id:0}/d'
+ # Only a last hole is a query suffix; a mid-path one keeps the tail.
+ assert flatten_interpolations('/a/${c ? `?a` : `/b`}/z') == "/a/${c ? `?a` : `/b`}/z"
+
def test_a_scheme_inside_an_interpolation_is_not_the_host(self):
url = "${prod ? 'https://x.io' : ''}/api/status"
assert absolute_host(url) is None
diff --git a/tests/unit/workspace/test_contracts.py b/tests/unit/workspace/test_contracts.py
index 4d693ce84..4a3eca8e1 100644
--- a/tests/unit/workspace/test_contracts.py
+++ b/tests/unit/workspace/test_contracts.py
@@ -460,15 +460,25 @@ class TestHttpExtractor:
contracts = HttpExtractor().extract(tmp_path, "svc")
assert contracts == []
- def test_truncated_template_literal_dropped(self, tmp_path: Path) -> None:
- # A nested quote inside the template expression truncates the capture,
- # leaving an unbalanced ``${`` — this is noise and must not be emitted.
+ def test_quote_inside_a_template_hole_reads_the_whole_literal(self, tmp_path: Path) -> None:
+ # A quote inside the hole stops the regex capture; the literal is read
+ # to its real end and the query hole is dropped.
self._write_file(tmp_path, "src/api.ts", """
fetchJSON(`/repos/explore${qs ? '?' + qs : ''}`);
""")
consumers = [
c for c in HttpExtractor().extract(tmp_path, "frontend") if c.role == "consumer"
]
+ assert [c.contract_id for c in consumers] == ["http::GET::/repos/explore"]
+
+ def test_unclosed_template_literal_dropped(self, tmp_path: Path) -> None:
+ # A template that never closes leaves an unbalanced ``${``: noise, not emitted.
+ self._write_file(tmp_path, "src/api.ts", """
+ fetchJSON(`/repos/explore${qs ? '?' + qs
+ """)
+ consumers = [
+ c for c in HttpExtractor().extract(tmp_path, "frontend") if c.role == "consumer"
+ ]
assert consumers == []
def test_bare_param_path_dropped(self, tmp_path: Path) -> None:
diff --git a/tests/unit/workspace/test_index_clients.py b/tests/unit/workspace/test_index_clients.py
index 623343bc1..f6fd2bd27 100644
--- a/tests/unit/workspace/test_index_clients.py
+++ b/tests/unit/workspace/test_index_clients.py
@@ -577,6 +577,27 @@ class TestLexicalNoiseIsNotEvidence:
# that the call *after* it still parses, which a desynced scan loses.
assert "http::GET::/plain" in _ids(contracts)
+ def test_a_nested_template_query_suffix_reads_the_route(self):
+ source = (
+ "export class C {\n"
+ " private async fetch<T>(p: string): Promise<T> {\n"
+ " return (await fetch(p)).json();\n"
+ " }\n"
+ " wins(id: string, q: string) {\n"
+ ' return this.fetch<W>(`/snapshots/${id}/wins${q ? `?${q}` : ""}`);\n'
+ " }\n"
+ " file(id: string, qs: string) {\n"
+ ' return this.fetch<F>(`/files/${encodeURIComponent(id)}${qs ? `?${qs}` : ""}`);\n'
+ " }\n"
+ "}\n"
+ )
+ contracts, unresolved, _c = _run(source, "src/lib/c.ts")
+ assert _ids(contracts) == {
+ "http::GET::/snapshots/{param}/wins",
+ "http::GET::/files/{param}",
+ }
+ assert unresolved == 0
+
def test_a_regex_literal_containing_a_paren_does_not_desync(self):
source = (
"export class C {\n"