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 Angular HttpClient calls and Angular app entry points (#2531)

Raghav Chamadiya6d19a39055db
diff --git a/docs/layers/LANGUAGE_SUPPORT.md b/docs/layers/LANGUAGE_SUPPORT.md
index 448b287b2..ec6866052 100644
--- a/docs/layers/LANGUAGE_SUPPORT.md
+++ b/docs/layers/LANGUAGE_SUPPORT.md
@@ -221,7 +221,7 @@ their relationships:
| C# | ASP.NET (attribute + minimal API), EF Core, gRPC-dotnet, host-builder extensions, CommunityToolkit MVVM |
| Go | net/http, gin, echo, chi, gRPC server registration |
| Rust | Axum, Actix route → handler |
-| JS / TS / Svelte | Next.js App Router, Hono / Fastify / Koa / Elysia, Remix / SvelteKit / Astro, tRPC, Express / NestJS |
+| JS / TS / Svelte | Next.js App Router, Hono / Fastify / Koa / Elysia, Remix / SvelteKit / Astro, tRPC, Express / NestJS, Angular |
| C++ | GoogleTest, Catch2, Boost.Test, doctest, Google Benchmark, libFuzzer |
The dead-code analyzer knows each ecosystem's entry points, generated-file
diff --git a/docs/scale/WORKSPACES.md b/docs/scale/WORKSPACES.md
index 8ba47b587..dd957aa14 100644
--- a/docs/scale/WORKSPACES.md
+++ b/docs/scale/WORKSPACES.md
@@ -231,7 +231,7 @@ Scans source files for HTTP routes, gRPC services, database tables, message topi
| Language | Providers (routes served) | Consumers (calls made) |
|----------|---------------------------|------------------------|
-| JS / TS | Express, Hono, Fastify, Koa, Elysia; NestJS controllers; Next.js App Router; Remix | `fetch`; axios, ky, got, ofetch and their instances; HTTP-named wrappers |
+| JS / TS | Express, Hono, Fastify, Koa, Elysia; NestJS controllers; Next.js App Router; Remix | `fetch`; axios, ky, got, ofetch and their instances; Angular `HttpClient`; HTTP-named wrappers |
| Python | FastAPI, Flask, Django | requests, httpx (aiohttp through wrappers the index confirms) |
| PHP | Laravel | Guzzle, Laravel `Http` |
| Java / Kotlin | Spring, JAX-RS, Micronaut | Feign, `java.net.http`, `RestTemplate`, Ktor (Kotlin) |
@@ -306,7 +306,9 @@ otherwise, and `Route::resource` / `apiResource` expand into the routes they reg
route is served at the app's `setGlobalPrefix` (unless its `exclude` list names the route), then
its URI version (`enableVersioning`, `@Version`), then the `@Controller` prefix. A call through an
axios, ky, got or ofetch instance is read with the instance's `baseURL` / `prefixUrl`, also when
-another file imports the instance. A client call
+another file imports the instance. An Angular `HttpClient` call is read on any receiver typed
+or injected as `HttpClient`, with `environment.apiUrl` folded from `src/environments/` and class
+fields built on it; a base an interceptor prepends is not read. A client call
whose base URL is an unresolved placeholder (`fetch(\`${API_BASE}/users\`)`) matches
on the host-relative path; the link is **exact** when exactly one workspace service
provides that path and a lower-confidence **candidate** when the target is ambiguous.
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py b/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py
index 79999f38e..513268754 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py
@@ -1,7 +1,7 @@
"""Framework-aware synthetic edge detection.
Detects convention-based relationships (Django, FastAPI, Flask, ASP.NET, Rails,
-Laravel, Spring, Express/Nest, Gin/Echo/Chi, Axum/Actix/Rocket, TYPO3, Godot
+Laravel, Spring, Express/Nest, Angular, Gin/Echo/Chi, Axum/Actix/Rocket, TYPO3, Godot
``class_name`` globals, and pytest ``conftest.py``) and adds
``edge_type="framework"`` edges that no static import graph captures.
@@ -19,6 +19,7 @@ from typing import TYPE_CHECKING
from . import (
android_manifest,
+ angular,
aspnet,
django,
express,
@@ -67,6 +68,7 @@ _HANDLERS: list[FrameworkHandler] = [
*android_manifest.HANDLERS,
*flutter.HANDLERS,
*express.HANDLERS,
+ *angular.HANDLERS,
*next_app.HANDLERS,
*hono.HANDLERS,
*remix.HANDLERS,
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/angular.py b/packages/core/src/repowise/core/ingestion/framework_edges/angular.py
new file mode 100644
index 000000000..4aa57ed35
--- /dev/null
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/angular.py
@@ -0,0 +1,180 @@
+"""Angular convention edges: build entries, router config and decorator metadata.
+
+Angular reaches most of an app through ordinary imports: a route's
+``component``, ``loadComponent: () => import(...)`` and ``loadChildren``, an
+NgModule's ``declarations`` and a standalone component's ``imports`` all name
+an imported class, and the TypeScript parser records static and dynamic
+imports alike. What no import says:
+
+* the files the build loads: ``main.ts``, the polyfills, the environment file a
+ build swaps in, the Karma config. ``angular.json`` / Nx ``project.json`` name
+ them (:func:`..framework_facts.angular_entry_files`), and a file calling
+ ``bootstrapApplication`` / ``bootstrapModule`` is one; each is anchored to
+ ``framework:angular``;
+* a name only written inside router config or decorator metadata
+ (``const routes: Routes = [...]`` passed to ``RouterModule.forRoot(routes)``,
+ a guard in ``canActivate``). A local one gets a ``framework_binds`` edge from
+ the file's module symbol, since no call reaches it; an imported one a file
+ edge (usually the import edge already there).
+
+Template selectors (``<app-user>``) are not read: a component a template uses
+is declared in an NgModule or a standalone ``imports`` array, both TypeScript
+references, so a selector edge would add no reachability the graph lacks.
+"""
+
+from __future__ import annotations
+
+import json
+import re
+from typing import TYPE_CHECKING, Any
+
+from ..framework_facts import ANGULAR, ANGULAR_WORKSPACE_FILES, angular_entry_files
+from ..framework_routes import match_paren
+from ..resolvers import ResolverContext
+from .base import (
+ JS_STRING_LITERAL_RE,
+ DetectionContext,
+ FrameworkHandler,
+ _add_edge_if_new,
+ _build_ts_var_to_file,
+ add_symbol_edge,
+ anchor_edge,
+ source_text,
+)
+
+if TYPE_CHECKING:
+ from collections.abc import Iterator
+
+ import networkx as nx
+
+_TS_JS = ("typescript", "javascript")
+
+# Where Angular reads names no call reaches: decorator metadata, router config
+# (a `Routes` array, `forRoot` / `forChild` / `provideRouter`) and bootstrap.
+_SITE_RE = re.compile(
+ r"@(?:NgModule|Component|Directive|Pipe)\s*(?P<meta>\()"
+ r"|(?P<boot>bootstrapApplication|bootstrapModule)\s*(?P<call>\()"
+ r"|(?:forRoot|forChild|provideRouter)\s*(?P<router>\()"
+ r"|Routes?(?:\s*\[\s*\])?\s*=\s*(?P<routes>\[)"
+)
+_SITE_HINTS = ("@NgModule", "@Component", "@Directive", "@Pipe", "bootstrap", "Route")
+# Strings and comments in one left-to-right pass, so an apostrophe in
+# `// don't` is not read as opening a string, nor `//` inside one as a comment.
+_NOISE_RE = re.compile(rf"{JS_STRING_LITERAL_RE.pattern}|//[^\n]*|/\*.*?\*/", re.DOTALL)
+# A name standing for a value: not a member (`m.routes`), a spread is one.
+_NAME_RE = re.compile(r"(?:(?<=\.\.\.)|(?<![\w$.]))[A-Za-z_$][\w$]*")
+# The class a decorator decorates: its own name in its metadata
+# (`useExisting: forwardRef(() => SelfComponent)`) is not a use of it.
+_DECORATED_CLASS_RE = re.compile(
+ r"\s*(?:export\s+)?(?:default\s+)?(?:abstract\s+)?class\s+(?P<name>[A-Za-z_$][\w$]*)"
+)
+
+
+def _blank(m: re.Match[str]) -> str:
+ return "''" if m.group()[0] in "'\"`" else " "
+
+
+_COLON_RE = re.compile(r"\s*:(?!:)")
+
+
+def _is_key(span: str, m: re.Match[str]) -> bool:
+ """Whether the name *m* is an object key (`{ path: ''`), not a ternary branch."""
+ if _COLON_RE.match(span, m.end()) is None:
+ return False
+ i = m.start() - 1
+ while i >= 0 and span[i].isspace():
+ i -= 1
+ return i < 0 or span[i] in "{,"
+
+
+def angular_references(text: str) -> Iterator[tuple[str, bool]]:
+ """``(name, bootstrap)`` per name Angular metadata, router config or bootstrap writes.
+
+ *bootstrap* marks the names of a ``bootstrapApplication`` /
+ ``bootstrapModule`` call, whose file the runtime loads first. A site in a
+ comment or a string is none.
+ """
+ if not any(h in text for h in _SITE_HINTS):
+ return
+ code = _NOISE_RE.sub(_blank, text)
+ for m in _SITE_RE.finditer(code):
+ group = next(g for g in ("meta", "call", "router", "routes") if m.group(g) is not None)
+ close = match_paren(code, m.start(group))
+ if close < 0:
+ continue
+ span = code[m.start(group) + 1 : close]
+ bootstrap = m.group("boot") is not None
+ decorated = _DECORATED_CLASS_RE.match(code, close + 1) if group == "meta" else None
+ own = decorated.group("name") if decorated else None
+ for name in _NAME_RE.finditer(span):
+ if name.group() != own and not _is_key(span, name):
+ yield name.group(), bootstrap
+
+
+# A JSON string (kept) or a comment (dropped).
+_JSON_COMMENT_RE = re.compile(r'(?P<s>"(?:[^"\\]|\\.)*")|//[^\n]*|/\*.*?\*/', re.DOTALL)
+
+
+def _entry_files(ctx: ResolverContext, path_set: set[str]) -> set[str]:
+ """Every file an Angular workspace config in the repo names as loaded."""
+ if ctx.repo_path is None:
+ return set()
+ out: set[str] = set()
+ for path in path_set:
+ if path.rpartition("/")[2] not in ANGULAR_WORKSPACE_FILES:
+ continue
+ try:
+ # Both CLIs read JSON with comments, and a BOM is common on Windows.
+ text = (ctx.repo_path / path).read_text(encoding="utf-8-sig")
+ config = json.loads(_JSON_COMMENT_RE.sub(lambda m: m.group("s") or " ", text))
+ except (OSError, ValueError):
+ continue
+ out.update(angular_entry_files(path, config, path_set))
+ return out
+
+
+def _imports_angular(parsed: Any) -> bool:
+ return any(imp.module_path.startswith("@angular/") for imp in parsed.imports)
+
+
+class _AngularHandler:
+ def detect(self, dctx: DetectionContext) -> bool:
+ if "angular" in dctx.stack_lower:
+ return True
+ return any(
+ p.file_info.language in _TS_JS and _imports_angular(p)
+ for p in dctx.parsed_files.values()
+ )
+
+ def add_edges(
+ self,
+ graph: nx.DiGraph,
+ parsed_files: dict[str, Any],
+ ctx: ResolverContext,
+ path_set: set[str],
+ ) -> int:
+ count = sum(anchor_edge(graph, ANGULAR, p) for p in sorted(_entry_files(ctx, path_set)))
+ source_map = getattr(ctx, "source_map", None) or {}
+ for path, parsed in parsed_files.items():
+ if parsed.file_info.language not in _TS_JS or not _imports_angular(parsed):
+ continue
+ refs = list(angular_references(source_text(path, parsed, source_map)))
+ if not refs:
+ continue
+ if any(bootstrap for _, bootstrap in refs):
+ count += anchor_edge(graph, ANGULAR, path)
+ local = {s.name: s.id for s in parsed.symbols if not s.parent_name}
+ module_sym = f"{path}::__module__"
+ imported: dict[str, str] | None = None
+ for name in {name for name, _ in refs}:
+ if name in local:
+ count += add_symbol_edge(graph, module_sym, local[name])
+ continue
+ if imported is None:
+ imported = _build_ts_var_to_file(parsed, path, ctx, path_set)
+ if name in imported:
+ count += _add_edge_if_new(graph, path, imported[name])
+ return count
+
+
+HANDLERS: list[FrameworkHandler] = [_AngularHandler()]
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/base.py b/packages/core/src/repowise/core/ingestion/framework_edges/base.py
index 2850d2223..35a12e552 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/base.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/base.py
@@ -11,6 +11,7 @@ dispatcher iterates over.
from __future__ import annotations
+import re
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any, Protocol
@@ -21,6 +22,9 @@ if TYPE_CHECKING:
from ..framework_facts import FrameworkFacts
from ..resolvers import ResolverContext
+# One JS / TS string literal, blanked so a name inside one is not read as a reference.
+JS_STRING_LITERAL_RE = re.compile(r"'(?:[^'\\]|\\.)*'|\"(?:[^\"\\]|\\.)*\"|`(?:[^`\\]|\\.)*`")
+
@dataclass(frozen=True)
class DetectionContext:
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/express.py b/packages/core/src/repowise/core/ingestion/framework_edges/express.py
index 546da17a5..f57fe9a14 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/express.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/express.py
@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any
from ..framework_routes import express_routes, js_router_bindings, match_paren
from ..resolvers import ResolverContext
from .base import (
+ JS_STRING_LITERAL_RE,
DetectionContext,
FrameworkHandler,
_add_edge_if_new,
@@ -29,7 +30,6 @@ _NEST_MODULE_RE = re.compile(r"@Module\s*\(\s*\{([^}]*)\}\s*\)", re.DOTALL)
_NEST_ARRAY_FIELD_RE = re.compile(r"\b(?:controllers|providers|imports|exports)\s*:\s*\[([^\]]*)\]")
_IDENT_RE = re.compile(r"\b([A-Z]\w*)\b")
-_STRING_LITERAL_RE = re.compile(r"'(?:[^'\\]|\\.)*'|\"(?:[^\"\\]|\\.)*\"|`(?:[^`\\]|\\.)*`")
_ARG_IDENT_RE = re.compile(r"[A-Za-z_$][\w$]*")
@@ -86,7 +86,7 @@ def _add_express_edges(
close = match_paren(text, route.paren_offset)
if close == -1:
continue
- arg_blob = _STRING_LITERAL_RE.sub(
+ arg_blob = JS_STRING_LITERAL_RE.sub(
"", text[route.paren_offset + 1 : close]
)
for ident in _ARG_IDENT_RE.finditer(arg_blob):
diff --git a/packages/core/src/repowise/core/ingestion/framework_facts.py b/packages/core/src/repowise/core/ingestion/framework_facts.py
index 6240f8c8c..27dab75ac 100644
--- a/packages/core/src/repowise/core/ingestion/framework_facts.py
+++ b/packages/core/src/repowise/core/ingestion/framework_facts.py
@@ -1,16 +1,19 @@
-"""What each PHP framework loads by convention, named once.
+"""What each framework loads by convention, named once.
A framework recognised here is recognised the same way everywhere: tech-stack
detection reads :func:`detect_php_framework`, and the framework-edge handlers
anchor :attr:`FrameworkFacts.entry_globs` to a ``framework:`` node so dead-code
-liveness sees the files the runtime loads without any importer.
+liveness sees the files the runtime loads without any importer. Angular names
+its entry files in its workspace config instead (:func:`angular_entry_files`).
"""
from __future__ import annotations
import fnmatch
+import posixpath
import re
from collections.abc import Iterable
+from collections.abc import Set as AbstractSet
from dataclasses import dataclass
from functools import cache
@@ -119,3 +122,70 @@ PHP_FRAMEWORKS: tuple[FrameworkFacts, ...] = (TYPO3, SYMFONY, LARAVEL)
def detect_php_framework(manifest: ComposerManifest) -> FrameworkFacts | None:
"""The first framework in precedence order that *manifest* declares."""
return next((facts for facts in PHP_FRAMEWORKS if facts.matches(manifest)), None)
+
+
+ANGULAR = FrameworkFacts(name="Angular", anchor="framework:angular")
+
+#: Workspace config naming what an Angular build loads: Angular CLI's
+#: ``angular.json`` and an Nx project's ``project.json``.
+ANGULAR_WORKSPACE_FILES = frozenset({"angular.json", "project.json"})
+# Build and test options whose value is a file loaded with no importer. A
+# package (`"polyfills": ["zone.js"]`) is dropped by the path check.
+_ANGULAR_ENTRY_OPTIONS = frozenset({"main", "browser", "server", "polyfills", "karmaConfig", "scripts"})
+# Options holding objects that name such a file: `fileReplacements: [{ with }]`,
+# `ssr: { entry }`, `scripts: [{ input }]`.
+_ANGULAR_ENTRY_KEYS = ("with", "entry", "input")
+
+
+def _nx_root(config_path: str, paths: AbstractSet[str]) -> str:
+ """The nearest directory above an Nx ``project.json`` holding ``nx.json``, else the repo root."""
+ parts = config_path.split("/")[:-1]
+ for depth in range(len(parts), 0, -1):
+ base = "/".join(parts[:depth])
+ if f"{base}/nx.json" in paths:
+ return base
+ return ""
+
+
+def angular_entry_files(config_path: str, config: object, paths: AbstractSet[str]) -> list[str]:
+ """The members of *paths* the workspace config at *config_path* names as loaded.
+
+ Entry options (``main``, ``browser``, ``polyfills``, ``scripts``,
+ ``ssr.entry``, ...) and each ``fileReplacements`` ``with``
+ (``environment.prod.ts``, swapped in by the build), read only from targets
+ an Angular builder runs. ``angular.json`` paths are relative to its
+ directory, an Nx ``project.json``'s to the workspace root; a value is kept
+ only when it names a file.
+ """
+ values: list[object] = []
+
+ def collect(value: object) -> None:
+ for item in value if isinstance(value, list) else [value]:
+ if isinstance(item, dict):
+ values.extend(item.get(k) for k in _ANGULAR_ENTRY_KEYS)
+ else:
+ values.append(item)
+
+ def walk(node: object) -> None:
+ if isinstance(node, dict):
+ builder = node.get("builder") or node.get("executor")
+ if isinstance(builder, str) and "angular" not in builder:
+ return # an Nx target another toolchain builds (`@nx/node:build`)
+ for key, value in node.items():
+ if key in _ANGULAR_ENTRY_OPTIONS or key in ("fileReplacements", "ssr"):
+ collect(value)
+ else:
+ walk(value)
+ elif isinstance(node, list):
+ for item in node:
+ walk(item)
+
+ walk(config)
+ if config_path.rpartition("/")[2] == "project.json":
+ base = _nx_root(config_path, paths)
+ else:
+ base = posixpath.dirname(config_path)
+ found = {
+ posixpath.normpath(posixpath.join(base, v)) for v in values if isinstance(v, str) and v
+ }
+ return sorted(found & paths)
diff --git a/packages/core/src/repowise/core/workspace/contracts.py b/packages/core/src/repowise/core/workspace/contracts.py
index 1e20c9c71..1a0d0862a 100644
--- a/packages/core/src/repowise/core/workspace/contracts.py
+++ b/packages/core/src/repowise/core/workspace/contracts.py
@@ -60,7 +60,9 @@ CONTRACTS_FILENAME = "contracts.json"
# Laravel broadcasting sockets; JS/TS constants fold.
# Version 12 adds NestJS controllers, axios/ky/got/ofetch instances with their
# base, and Prisma, TypeORM, Sequelize, Drizzle and Knex tables.
-CONTRACTS_VERSION = 12
+# Version 13 adds Angular HttpClient calls, with bases folded from environment
+# files and class fields.
+CONTRACTS_VERSION = 13
#: ``meta["kind"]`` of a topic contract: the destination a broker call names.
#: A queue is read by one consumer group; a topic, subject or channel fans
diff --git a/packages/core/src/repowise/core/workspace/extractors/calls.py b/packages/core/src/repowise/core/workspace/extractors/calls.py
index 6ea9b3ad0..29a46d11f 100644
--- a/packages/core/src/repowise/core/workspace/extractors/calls.py
+++ b/packages/core/src/repowise/core/workspace/extractors/calls.py
@@ -18,6 +18,7 @@ from typing import TYPE_CHECKING, Protocol
from .strings import (
call_arguments,
+ inline_names,
match_paren,
resolve_argument,
resolve_string,
@@ -50,15 +51,28 @@ class Call:
class FileStrings:
- """One file's string syntax, and its constants, read the first time they are needed."""
+ """One file's string syntax, and its constants, read the first time they are needed.
- __slots__ = ("_code", "_constants", "content", "syntax")
+ ``settled`` holds names whose text is already resolved elsewhere (an
+ imported module's member, a class field), holes allowed. They are inlined
+ after the file's own constants have had their turn, so a local binding
+ still wins.
+ """
+
+ __slots__ = ("_code", "_constants", "content", "settled", "syntax")
- def __init__(self, content: str, syntax: StringSyntax, code: str | None = None) -> None:
+ def __init__(
+ self,
+ content: str,
+ syntax: StringSyntax,
+ code: str | None = None,
+ settled: dict[str, str] | None = None,
+ ) -> None:
self.content = content
self.syntax = syntax
self._code = code
self._constants: dict[str, str] | None = None
+ self.settled = settled if settled is not None else {}
@property
def constants(self) -> dict[str, str]:
@@ -81,6 +95,11 @@ class FileStrings:
text = resolve_string(expr, self.syntax)
if text is None or "${" in text:
text = resolve_string(expr, self.syntax, self.constants)
+ if self.settled:
+ if text is None:
+ return self.settled.get(expr.strip())
+ if "${" in text:
+ return inline_names(text, self.settled)
return text
@@ -169,6 +188,47 @@ def receiver_at(content: str, start: int, qualifiers: re.Pattern[str] | None = N
return qualifiers is not None and qualifiers.search(head) is not None
+_RECEIVER_NAME_RE = re.compile(r"(?<![\w$.])(?:this\s*\.\s*)?(?P<name>[A-Za-z_$][\w$]*)\s*$")
+
+
+def receiver_name(content: str, dot: int) -> str | None:
+ """The name a member call is made on, read back from its ``.`` at *dot*.
+
+ ``api`` for ``api.get`` and ``this.api.get``; ``None`` for a longer chain
+ (``window.api.get``) or no name at all.
+ """
+ m = _RECEIVER_NAME_RE.search(content, max(0, dot - 80), dot)
+ return m.group("name") if m else None
+
+
+# What stands before a type to declare a receiver of it: `http: ` (a parameter,
+# parameter property or field) or `http = inject(` (a field).
+_TYPED_RE = re.compile(r"(?P<name>[A-Za-z_$][\w$]*)\s*[?!]?\s*:\s*$")
+_INJECTED_RE = re.compile(r"(?P<name>[A-Za-z_$][\w$]*)\s*(?::[^=\n]+)?=\s*inject\s*\(\s*$")
+_DECLARATION_WINDOW = 120
+
+
+def typed_receivers(content: str, type_re: re.Pattern[str]) -> list[str]:
+ """TypeScript names declared with a type *type_re* matches, or injected with it.
+
+ ``private http: HttpClient``, ``http = inject(HttpClient)``,
+ ``@Inject('X') billing: ClientKafka``. The type must stand alone: not a
+ longer name, a member (``ns.HttpClient``) or a generic.
+ """
+ names: list[str] = []
+ for m in type_re.finditer(content):
+ after = content[m.end() : m.end() + 1]
+ if after and (after.isalnum() or after in "_$.<"):
+ continue
+ if not receiver_at(content, m.start()):
+ continue
+ before = content[max(0, m.start() - _DECLARATION_WINDOW) : m.start()]
+ found = _TYPED_RE.search(before) or _INJECTED_RE.search(before)
+ if found is not None and found.group("name") not in names:
+ names.append(found.group("name"))
+ return names
+
+
# Whitespace and comments between decorators and the member they decorate.
_GAP_RE = re.compile(r"(?:\s+|//[^\n]*|/\*.*?\*/)*", re.DOTALL)
_DECORATOR_NAME_RE = re.compile(r"@[\w$.]+")
@@ -208,4 +268,6 @@ __all__ = [
"decorated_member",
"file_strings",
"receiver_at",
+ "receiver_name",
+ "typed_receivers",
]
diff --git a/packages/core/src/repowise/core/workspace/extractors/data/sequelize.py b/packages/core/src/repowise/core/workspace/extractors/data/sequelize.py
index 574013415..6d44a06e7 100644
--- a/packages/core/src/repowise/core/workspace/extractors/data/sequelize.py
+++ b/packages/core/src/repowise/core/workspace/extractors/data/sequelize.py
@@ -17,7 +17,7 @@ import re
from typing import TYPE_CHECKING
from ..base import ScanContext, line_at
-from ..calls import Call, call_sites, decorated_member, file_strings
+from ..calls import Call, call_sites, decorated_member, file_strings, receiver_name
from ..langs import JS_TS
from ..strings import Arg, call_arguments, match_paren, on_comment_line, select_argument
from .dialect import CONVENTION_CONFIDENCE, EXPLICIT_CONFIDENCE, dedup_emit
@@ -32,8 +32,6 @@ _SEQUELIZE = ("sequelize",)
_DEFINE = Call(re.compile(r"\.\s*define\s*\("), JS_TS, _SEQUELIZE)
# `User.init(attributes, { sequelize, ... })`: the options name the connection.
_INIT = Call(re.compile(r"\.\s*init\s*\("), JS_TS, _SEQUELIZE)
-# The receiver a `.define(` / `.init(` is called on, read back from the dot.
-_RECEIVER_RE = re.compile(r"(?<![\w$.])(?:this\s*\.\s*)?(?P<name>[A-Za-z_$][\w$]*)\s*$")
_CONNECTION_RE = re.compile(r"(?<![\w$])sequelize(?![\w$])")
_BOUND_RE = re.compile(r"(?P<name>[A-Za-z_$][\w$]*)\s*=\s*new\s+Sequelize\s*\(")
_CLASS_RE = re.compile(r"\bclass\s+(?P<name>[A-Za-z_$][\w$]*)")
@@ -65,11 +63,6 @@ def _table(model: str, options: list[str], strings: FileStrings) -> tuple[str, f
return name, CONVENTION_CONFIDENCE
-def _receiver(content: str, dot: int) -> str | None:
- m = _RECEIVER_RE.search(content, max(0, dot - 80), dot)
- return m.group("name") if m else None
-
-
class SequelizeDialect:
name = "sequelize"
extensions = JS_TS
@@ -81,7 +74,7 @@ class SequelizeDialect:
connections = {"sequelize", *(m.group("name") for m in _BOUND_RE.finditer(content))}
found: list[tuple[str, float, int]] = []
for call, args, m, strings in call_sites(ctx, (_DEFINE, _INIT)):
- receiver = _receiver(content, m.start())
+ receiver = receiver_name(content, m.start())
if receiver is None:
continue
if call is _DEFINE:
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/angular.py b/packages/core/src/repowise/core/workspace/extractors/http/angular.py
new file mode 100644
index 000000000..530152b58
--- /dev/null
+++ b/packages/core/src/repowise/core/workspace/extractors/http/angular.py
@@ -0,0 +1,141 @@
+"""Angular's ``HttpClient`` receivers, and the environment files apps take a base from.
+
+``HttpClient`` is injected, never called by its own name: a service calls
+``this.http.get(url)`` on a receiver declared with the type
+(``constructor(private http: HttpClient)``, ``private readonly api: HttpClient``)
+or injected into a field (``http = inject(HttpClient)``). :func:`http_client_receivers`
+names those receivers, so :mod:`.node_clients` reads their calls like any
+other client's.
+
+An Angular CLI app keeps its API base in ``src/environments/environment.ts``,
+an object-literal const another module imports (``environment.apiUrl``); the
+build swaps the file per configuration, and the file read here is the one the
+source imports. Its string members travel through the orchestrator's mount
+pass keyed by module path and fold into the importing file's URLs. A member
+that does not resolve stays a ``${environment.apiUrl}`` base token.
+
+Ceiling: an interceptor that prepends a base (``req.clone({ url: base +
+req.url })``) applies to every call at run time, but which calls it reaches is
+decided by provider registration, so it is not guessed; those calls keep the
+path they spell.
+"""
+
+from __future__ import annotations
+
+import json
+import posixpath
+import re
+from functools import lru_cache
+from typing import TYPE_CHECKING
+
+from ..calls import typed_receivers
+from ..langs import JS_EXTENSION_RE
+from ..strings import JS_SYNTAX, resolve_string, string_constants
+
+if TYPE_CHECKING:
+ from collections.abc import Iterable, Mapping
+
+ from ..base import ScanContext
+
+ANGULAR_HTTP = "@angular/common/http"
+
+
+@lru_cache(maxsize=16)
+def _type_re(type_name: str) -> re.Pattern[str]:
+ return re.compile(re.escape(type_name))
+
+
+def http_client_receivers(content: str, type_name: str) -> list[str]:
+ """The receivers *content* declares with ``HttpClient``, imported as *type_name*."""
+ return typed_receivers(content, _type_re(type_name))
+
+
+# --- environment files -----------------------------------------------------
+
+_ENV_KEY = "environment:"
+# Present when any file of the repo is an environment module, so a file is
+# only looked up then.
+_ENV_ANY = "environment-modules"
+_ENV_DIR = "environments"
+_EXPORTED_CONST_RE = re.compile(r"export\s+const\s+(?P<name>[A-Za-z_$][\w$]*)")
+
+
+def _module_path(rel_path: str) -> str:
+ return JS_EXTENSION_RE.sub("", rel_path)
+
+
+def environment_mounts(ctx: ScanContext) -> dict[str, str]:
+ """The string members an environment module exports, as one mount entry.
+
+ Only a module in an ``environments/`` directory counts: Angular CLI's
+ convention, and the one directory whose objects a build swaps in whole.
+ """
+ parent = posixpath.basename(posixpath.dirname(ctx.rel_path))
+ if parent != _ENV_DIR or "export" not in ctx.content:
+ return {}
+ exported = {m.group("name") for m in _EXPORTED_CONST_RE.finditer(ctx.content)}
+ if not exported:
+ return {}
+ constants = string_constants(ctx.content, JS_SYNTAX)
+ members: dict[str, str] = {}
+ for name, rhs in constants.items():
+ if name.partition(".")[0] in exported and "." in name:
+ text = resolve_string(rhs, JS_SYNTAX, constants)
+ if text is not None:
+ members[name] = text
+ if not members:
+ return {}
+ return {_ENV_KEY + _module_path(ctx.rel_path): json.dumps(members, sort_keys=True), _ENV_ANY: ""}
+
+
+def _environment_module(
+ rel_path: str, spec: str, mounts: Mapping[str, str]
+) -> str | None:
+ """The mount value of the environment module *spec* names from *rel_path*.
+
+ A relative specifier resolves against the importing file. A ``paths``
+ alias naming the environments (``@env/environment``,
+ ``src/environments/environment``) is looked up as
+ ``<ancestor>/environments/<name>``, nearest ancestor first, which is where
+ the alias points in an Angular CLI or Nx app. Any other specifier is a
+ package (``@ngrx/environment``) and is not looked up.
+ """
+ if spec.startswith("."):
+ path = posixpath.normpath(posixpath.join(posixpath.dirname(rel_path), spec))
+ return mounts.get(_ENV_KEY + path)
+ folder, _, leaf = spec.rstrip("/").rpartition("/")
+ if "env" not in folder:
+ return None
+ parts = rel_path.split("/")[:-1]
+ for depth in range(len(parts), -1, -1):
+ base = "/".join(parts[:depth])
+ value = mounts.get(f"{_ENV_KEY}{base + '/' if base else ''}{_ENV_DIR}/{leaf}")
+ if value is not None:
+ return value
+ return None
+
+
+def imported_environment(
+ ctx: ScanContext, imports: Iterable[tuple[str, str, str]]
+) -> dict[str, str]:
+ """``local.member -> text`` for each environment object *ctx* imports.
+
+ *imports* is ``(local, imported, specifier)`` per binding.
+ """
+ if _ENV_ANY not in ctx.mounts:
+ return {}
+ out: dict[str, str] = {}
+ for local, imported, spec in imports:
+ if imported == "default":
+ continue
+ value = _environment_module(ctx.rel_path, spec, ctx.mounts)
+ if value is None:
+ continue
+ for member, text in json.loads(value).items():
+ name, _, rest = member.partition(".")
+ if name == imported:
+ out[f"{local}.{rest}"] = text
+ return out
+
+
+__all__ = ["ANGULAR_HTTP", "environment_mounts", "http_client_receivers", "imported_environment"]
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/dialect.py b/packages/core/src/repowise/core/workspace/extractors/http/dialect.py
index cecdf71d3..ba284f267 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/dialect.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/dialect.py
@@ -16,6 +16,7 @@ from .paths import (
absolute_host,
consumer_meta,
extract_path_from_url,
+ flatten_interpolations,
is_unusable_consumer_path,
normalize_http_path,
strip_leading_base_expr,
@@ -129,6 +130,8 @@ def build_consumer_contract(
host = absolute_host(url)
path = extract_path_from_url(url)
path, base_token = strip_leading_base_expr(path)
+ if "${" in path:
+ path = flatten_interpolations(path)
path = strip_trailing_query_expr(path)
norm_path = normalize_http_path(path)
if is_unusable_consumer_path(norm_path):
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 41e9f09a7..39d1612a6 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
@@ -14,8 +14,10 @@ import re
from collections.abc import Iterator
from typing import TYPE_CHECKING
+from ..calls import receiver_name
from ..langs import JS_TS
from ..strings import JS_SYNTAX, literal_span, string_constants
+from .angular import environment_mounts
from .client_calls import ClientCallMatch, consumer_contracts, matches_in
from .node_clients import client_calls, client_mounts
@@ -72,6 +74,14 @@ def _callee(content: str, start: int, end: int) -> str:
return content[start:end]
+def _receiver(content: str, start: int) -> str | None:
+ """The name a member call at *start* is made on (``http`` in ``this.http.get``)."""
+ dot = start - 1
+ while dot >= 0 and content[dot] in " \t\n":
+ dot -= 1
+ return receiver_name(content, dot) if dot >= 0 and content[dot] == "." else None
+
+
def wrapper_calls(content: str, clients: frozenset[str]) -> Iterator[ClientCallMatch]:
"""``fetchJSON(`${BASE}/path`, { method: "POST" })`` and its relatives.
@@ -81,6 +91,8 @@ def wrapper_calls(content: str, clients: frozenset[str]) -> Iterator[ClientCallM
callee = m.group(1)
if callee in _WRAPPER_SKIP or _callee(content, m.start(1), m.end(1)) in clients:
continue
+ if clients and _receiver(content, m.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]
method_opt = _METHOD_OPT_RE.search(window)
@@ -104,7 +116,7 @@ class JsClientsDialect:
extensions = JS_TS
def collect_mounts(self, ctx: ScanContext) -> dict[str, str]:
- return client_mounts(ctx)
+ return client_mounts(ctx) | environment_mounts(ctx)
def extract(self, ctx: ScanContext) -> list[Contract]:
content = ctx.content
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/node_clients.py b/packages/core/src/repowise/core/workspace/extractors/http/node_clients.py
index a0001897b..691c1f6c5 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/node_clients.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/node_clients.py
@@ -1,4 +1,4 @@
-"""Node HTTP client libraries: axios, ky, got and ofetch.
+"""HTTP client libraries for JS / TS: axios, ky, got, ofetch and Angular's HttpClient.
A call goes straight through the library (``axios.post(url)``, ``ky.get(url)``,
``got(url, { method })``) or through an instance created with a base
@@ -15,6 +15,12 @@ A base resolves through the strings layer. One it cannot read
(``process.env.API_URL``) stays a ``${...}`` placeholder, which the consumer
builder records as the call's base token, exactly as for a hand-written
``${API_URL}/users``. ``ofetch(url)`` itself is the ``fetch`` reader's.
+
+Angular's ``HttpClient`` is a type, not a callable: its clients are the
+receivers declared with it (:func:`.angular.http_client_receivers`). URLs also fold
+what the file imports from an environment module (``environment.apiUrl``) and
+class fields set once (``private base = `${environment.apiUrl}/users```), so
+``this.http.get(`${this.base}/${id}`)`` reads whole.
"""
from __future__ import annotations
@@ -26,8 +32,9 @@ from typing import TYPE_CHECKING
from ..base import line_at
from ..calls import Call, call_sites, file_strings, receiver_at
-from ..langs import JS_TS
-from ..strings import NAME_RE, Arg, call_arguments, select_argument
+from ..langs import JS_EXTENSION_RE, JS_TS
+from ..strings import NAME_RE, Arg, call_arguments, js_class_fields, select_argument
+from .angular import ANGULAR_HTTP, http_client_receivers, imported_environment
from .client_calls import VERBS, is_rooted_url, method_from_argument
from .dialect import build_consumer_contract
from .wrappers import GENERIC_ARGS
@@ -37,28 +44,46 @@ if TYPE_CHECKING:
from ..base import ScanContext
from ..calls import FileStrings
+ from ..strings import ClassFields
@dataclass(frozen=True)
class _Library:
name: str
- factories: tuple[str, ...]
- base_key: str
- verbs: bool # `client.get(url)`; ofetch has none
+ factories: tuple[str, ...] = ()
+ base_key: str = "" # the option naming an instance's base
+ verbs: bool = True # `client.get(url)`; ofetch has none
exports: tuple[str, ...] = ("default",) # what an import of the client names
-
-
-_LIBRARIES = {
- lib.name: lib
- for lib in (
- _Library("axios", ("create",), "baseURL", True),
- _Library("ky", ("create", "extend"), "prefixUrl", True),
- _Library("got", ("extend",), "prefixUrl", True),
- _Library("ofetch", ("create",), "baseURL", False, ("ofetch", "$fetch")),
- )
-}
+ specifier: str = "" # the package, when it is not *name*
+ # The import names a type, and the receivers declared with it are the
+ # clients (Angular's injected `HttpClient`), not the import itself. Such a
+ # client has verbs only; `client(url)` is not a call.
+ typed: bool = False
+ request: str = "" # `request({ url, method })` ("config") or `request('GET', url)` ("method")
+ body_first: bool = False # post / put / patch take a body before their options
+
+ @property
+ def package(self) -> str:
+ return self.specifier or self.name
+
+
+_LIBRARY_LIST = (
+ _Library("axios", ("create",), "baseURL", request="config", body_first=True),
+ _Library("ky", ("create", "extend"), "prefixUrl"),
+ _Library("got", ("extend",), "prefixUrl"),
+ _Library("ofetch", ("create",), "baseURL", verbs=False, exports=("ofetch", "$fetch")),
+ _Library(
+ "angular",
+ exports=("HttpClient",),
+ specifier=ANGULAR_HTTP,
+ typed=True,
+ request="method",
+ body_first=True,
+ ),
+)
+_LIBRARIES = {lib.package: lib for lib in _LIBRARY_LIST}
# A library's specifier in quotes, so `ky` is not read out of every word holding it.
-_SPECIFIERS = tuple(f"{q}{name}{q}" for name in _LIBRARIES for q in "'\"")
+_SPECIFIERS = tuple(f"{q}{package}{q}" for package in _LIBRARIES for q in "'\"")
# axios is also a global (a script tag, Laravel's `window.axios`, Nuxt's `$axios`).
_AXIOS_GLOBALS = ("axios.", "axios(")
# Qualifiers a client is called through: its own class, the browser global, Vue.
@@ -101,7 +126,6 @@ _EXPORT_LIST_RE = re.compile(r"export(?<![\w$]export)\s*\{(?P<names>[^{}]*)\}")
_EXPORT_DEFAULT_RE = re.compile(
r"export(?<![\w$]export)\s+default\s+(?P<name>[A-Za-z_$][\w$]*)\s*;?\s*$", re.MULTILINE
)
-_EXTENSION_RE = re.compile(r"\.(?:[cm]?[jt]sx?)$")
def _named(entries: str) -> list[tuple[str, str]]:
@@ -145,7 +169,7 @@ def _is_package(spec: str) -> bool:
def _module_name(path: str) -> str:
"""What a specifier naming *path* ends with: its stem, or its directory's for an index."""
- parts = _EXTENSION_RE.sub("", path).rstrip("/").split("/")
+ parts = JS_EXTENSION_RE.sub("", path).rstrip("/").split("/")
if parts[-1] == "index" and len(parts) > 1:
return parts[-2]
return parts[-1]
@@ -156,12 +180,12 @@ def _key(module: str, export: str) -> str:
def _encode(client: _Client) -> str:
- return f"{client.library.name}\n{client.base}"
+ return f"{client.library.package}\n{client.base}"
def _decode(value: str) -> _Client:
- name, _, base = value.partition("\n")
- return _Client(_LIBRARIES[name], base)
+ package, _, base = value.partition("\n")
+ return _Client(_LIBRARIES[package], base)
def _alternation(names: tuple[str, ...]) -> str:
@@ -187,6 +211,8 @@ def _calls_for(names: tuple[str, ...]) -> tuple[Call, ...]:
def _base(args: list[str], library: _Library, strings: FileStrings) -> str | None:
"""The base a factory call's options name; ``None`` when they name none."""
+ if not library.base_key:
+ return None
raws = select_argument(args, Arg(keys=(library.base_key,)))
if not raws:
return None
@@ -203,14 +229,19 @@ class _FileClients:
exported: dict[str, _Client] # what it exports, by exported name (`default` too)
-def _file_clients(ctx: ScanContext) -> _FileClients:
+def _file_clients(
+ ctx: ScanContext, imports: list[tuple[str, str, str]], strings: FileStrings | None
+) -> _FileClients:
content = ctx.content
clients: dict[str, _Client] = {}
- for local, imported, spec in _imports(content):
+ for local, imported, spec in imports:
library = _LIBRARIES.get(spec)
if library is not None:
- if imported in library.exports:
- clients[local] = _Client(library, "", direct=True)
+ if imported not in library.exports:
+ continue
+ receivers = http_client_receivers(content, local) if library.typed else [local]
+ for name in receivers:
+ clients[name] = _Client(library, "", direct=True)
elif not _is_package(spec):
value = ctx.mounts.get(_key(_module_name(spec), imported))
if value is not None:
@@ -224,7 +255,6 @@ def _file_clients(ctx: ScanContext) -> _FileClients:
if not clients or (".create" not in content and ".extend" not in content):
return _FileClients(clients, exported)
- strings = file_strings(ctx)
locals_: dict[str, _Client] = {}
for m in _factory_re(tuple(clients)).finditer(content):
parent = clients[m.group("parent")]
@@ -264,7 +294,7 @@ def client_mounts(ctx: ScanContext) -> dict[str, str]:
return {}
if not any(s in content for s in _SPECIFIERS) and "axios.create" not in content:
return {} # no library here, so no instance of one (imported ones are not re-read)
- exported = _file_clients(ctx).exported
+ exported = _file_clients(ctx, _imports(content), file_strings(ctx)).exported
module = _module_name(ctx.rel_path)
out = {_key(module, name): _encode(client) for name, client in exported.items()}
if out:
@@ -279,17 +309,23 @@ _BODY_VERBS = frozenset({"post", "put", "patch"})
def _request(
verb: str | None, args: list[str], client: _Client, strings: FileStrings
) -> tuple[str, str] | None:
- """``(method, url)`` a call names: ``x.post(url)``, ``x(url, { method })``, ``x({ url, method })``.
+ """``(method, url)`` a call names: ``x.post(url)``, ``x(url, { method })``,
+ ``x({ url, method })``, ``x.request('GET', url)``.
A base in the call's own options (``axios.get(url, { baseURL })``) wins over
the client's, as it does at run time.
"""
if not args:
return None
+ library = client.library
if verb is not None and verb != "request":
url_raws, method = args[:1], verb.upper()
- body = client.library.name == "axios" and verb in _BODY_VERBS
- options = args[2:3] if body else args[1:2]
+ options = args[2:3] if library.body_first and verb in _BODY_VERBS else args[1:2]
+ elif verb == "request" and library.request == "method":
+ named = method_from_argument(args[0])
+ if named is None:
+ return None # a variable, or an `HttpRequest` object
+ url_raws, method, options = args[1:2], named, args[2:3]
else:
config = args[0].startswith("{")
if verb == "request" and not config:
@@ -301,10 +337,43 @@ def _request(
url = strings.text(url_raws[0]) if url_raws else None
if url is None:
return None
- base = _base(options, client.library, strings)
+ base = _base(options, library, strings)
return method, _join(client.base if base is None else base, url)
+class _Fields:
+ """The class fields in scope at a call, settled into the file's strings.
+
+ Only the innermost class holding the call is in scope, so a field of one
+ class never folds into another's URL. Holes are allowed, and a field built
+ on another (``users = `${this.base}/users```) resolves on the second
+ round, once the first has settled ``this.base``.
+ """
+
+ def __init__(self, strings: FileStrings) -> None:
+ self._strings = strings
+ self._classes: list[ClassFields] | None = None
+ self._current: ClassFields | None = None
+
+ def enter(self, pos: int) -> None:
+ if self._classes is None:
+ self._classes = js_class_fields(self._strings.content)
+ found = next((c for c in self._classes if c.start <= pos < c.end), None)
+ if found is self._current:
+ return
+ settled = self._strings.settled
+ for name in self._current.fields if self._current else ():
+ settled.pop(name, None)
+ self._current = found
+ if found is None:
+ return
+ for _ in range(2):
+ for name, rhs in found.fields.items():
+ text = self._strings.text(rhs)
+ if text is not None:
+ settled[name] = text
+
+
def _join(base: str, url: str) -> str:
if not base or url.startswith(("http://", "https://", "//")):
return url
@@ -320,20 +389,29 @@ def client_calls(ctx: ScanContext) -> tuple[list[Contract], frozenset[str]]:
or (_MOUNT_ANY in ctx.mounts and ("import" in content or "require" in content))
):
return [], frozenset()
- clients = _file_clients(ctx).callable
+ strings = file_strings(ctx)
+ if strings is None:
+ return [], frozenset()
+ imports = _imports(content)
+ strings.settled.update(imported_environment(ctx, imports))
+ clients = _file_clients(ctx, imports, strings).callable
# `ofetch(url)` is a fetch call the fetch reader already reads.
names = tuple(n for n, c in clients.items() if not (c.direct and not c.library.verbs))
if not names:
return [], frozenset()
+ fields = _Fields(strings)
out: list[Contract] = []
- for _call, args, m, strings in call_sites(ctx, _calls_for(names)):
+ for _call, args, m, _strings in call_sites(ctx, _calls_for(names), strings):
+ if any("this." in a for a in args[:3]):
+ fields.enter(m.start()) # read only for a call whose URL may name a field
if not receiver_at(content, m.start(), _QUALIFIER_RE):
continue
client = clients[m.group("recv")]
+ library = client.library
verb = m.group("verb")
- if verb in VERBS and not client.library.verbs:
+ if (verb in VERBS and not library.verbs) or (verb is None and library.typed):
continue
- if verb == "request" and client.library.name != "axios":
+ if verb == "request" and not library.request:
continue
request = _request(verb, args, client, strings)
if request is 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 03bd89439..259dcb25d 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/paths.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/paths.py
@@ -9,6 +9,8 @@ from __future__ import annotations
import re
+from ..strings import match_paren
+
def normalize_http_path(path: str) -> str:
"""Normalize an HTTP path for matching.
@@ -48,9 +50,36 @@ def _after_authority(url: str) -> str | None:
"""
if url.startswith("//"):
return url[2:]
- if "://" in url:
- return url.split("://", 1)[1]
- return None
+ start = 0
+ if url.startswith("${"):
+ # A scheme inside a leading interpolation (`${prod ? 'https://x' : ''}/api`)
+ # is not this URL's; one after it (`${proto}://host/api`) is.
+ start = match_paren(url, 1, closer="}") + 1
+ if start == 0:
+ return None
+ scheme = url.find("://", start)
+ return url[scheme + 3 :] if scheme >= 0 else None
+
+
+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`.
+ """
+ out: list[str] = []
+ pos = 0
+ while (start := path.find("${", pos)) >= 0:
+ end = match_paren(path, start + 1, closer="}")
+ if end < 0:
+ break
+ inner = path[start + 2 : end]
+ out.append(path[pos:start])
+ out.append("${expr}" if "{" in inner else path[start : end + 1])
+ pos = end + 1
+ out.append(path[pos:])
+ return "".join(out)
def extract_path_from_url(url: str) -> str:
diff --git a/packages/core/src/repowise/core/workspace/extractors/langs.py b/packages/core/src/repowise/core/workspace/extractors/langs.py
index 5931cee21..78cb94aa1 100644
--- a/packages/core/src/repowise/core/workspace/extractors/langs.py
+++ b/packages/core/src/repowise/core/workspace/extractors/langs.py
@@ -7,6 +7,8 @@ registry and a dialect file reads as "this recogniser is for Rust" at a glance.
from __future__ import annotations
+import re
+
from repowise.core.ingestion.languages.registry import REGISTRY as _REGISTRY
JS_TS = _REGISTRY.extensions_for(["javascript", "typescript"])
@@ -19,3 +21,6 @@ GO = _REGISTRY.extensions_for(["go"])
CSHARP = _REGISTRY.extensions_for(["csharp"])
RUST = _REGISTRY.extensions_for(["rust"])
PROTO = _REGISTRY.extensions_for(["proto"])
+
+# A JS / TS module file's extension, which an import specifier leaves off.
+JS_EXTENSION_RE = re.compile(r"\.(?:[cm]?[jt]sx?)$")
diff --git a/packages/core/src/repowise/core/workspace/extractors/strings.py b/packages/core/src/repowise/core/workspace/extractors/strings.py
index 9c1c5ed67..6fb602673 100644
--- a/packages/core/src/repowise/core/workspace/extractors/strings.py
+++ b/packages/core/src/repowise/core/workspace/extractors/strings.py
@@ -385,6 +385,16 @@ def resolve_string(
return None
+def inline_names(text: str, names: dict[str, str]) -> str:
+ """Resolved template *text* with each ``${name}`` *names* holds replaced by its text.
+
+ For values settled outside the file's own constants, whose text may keep a
+ hole of its own (``${environment.apiUrl}/users``); :func:`_fold` inlines
+ only hole-free constants.
+ """
+ return _TEMPLATE_INTERP_RE.sub(lambda m: names.get(m.group(1).strip(), m.group(0)), text)
+
+
def string_constants(
content: str, syntax: StringSyntax, code: str | None = None
) -> dict[str, str]:
@@ -427,6 +437,74 @@ def string_constants(
return {name: text for name, text in seen.items() if text is not None}
+_CLASS_OPEN_RE = re.compile(r"class(?<![\w$]class)\s+[A-Za-z_$][\w$]*[^{;]*\{")
+# A class field and what it is set to: a declaration on its own line
+# (`private base = x`, `base: string = x`) or `this.base = x` anywhere, with
+# `op` set for a compound assignment (`this.base += x`).
+_FIELD_DECL_RE = re.compile(
+ r"^[ \t]*(?P<mods>(?:(?:private|protected|public|readonly|static|override)[ \t]+)*)"
+ r"(?P<name>[A-Za-z_$][\w$]*)[ \t]*[?!]?(?:[ \t]*:[^=\n]+)?=(?![=>])[ \t]*(?P<rhs>[^\n]+)$",
+ re.MULTILINE,
+)
+_THIS_ASSIGN_RE = re.compile(
+ r"this\s*\.\s*(?P<name>[A-Za-z_$][\w$]*)\s*(?P<op>[-+*/%?|&]*)=(?![=>])[ \t]*(?P<rhs>[^\n;]*)"
+)
+_BARE_NAME_RE = re.compile(r"[A-Za-z_$][\w$]*")
+
+
+@dataclass(frozen=True)
+class ClassFields:
+ """One class body's span and its fields: ``this.name -> rhs``, raw text."""
+
+ start: int
+ end: int
+ fields: dict[str, str]
+
+
+def js_class_fields(content: str) -> list[ClassFields]:
+ """The fields each class in *content* sets exactly once, per class body.
+
+ A declaration counts only directly in the body (not a method's local
+ ``url = x``) and not when ``static`` (read as ``A.base``, not ``this.base``).
+ A field set twice, or by ``+=``, is not a constant, nor one set to a bare
+ name (``this.base = base``), usually a constructor parameter shadowing any
+ constant of that name. Innermost classes come first.
+ """
+ spans: list[tuple[int, int]] = []
+ for c in _CLASS_OPEN_RE.finditer(content):
+ close = match_paren(content, c.end() - 1, len(content), closer="}")
+ if close > 0:
+ spans.append((c.end(), close))
+ out: list[ClassFields] = []
+ for start, end in sorted(spans, key=lambda s: s[1] - s[0]):
+ inner = [s for s in spans if start < s[0] and s[1] < end]
+ seen: dict[str, str | None] = {}
+ depth, at = 0, start
+ for m in _FIELD_DECL_RE.finditer(content, start, end):
+ depth += content.count("{", at, m.start()) - content.count("}", at, m.start())
+ at = m.start()
+ if depth == 0 and "static" not in m.group("mods"):
+ _record(seen, content, m)
+ for m in _THIS_ASSIGN_RE.finditer(content, start, end):
+ if not any(a <= m.start() < b for a, b in inner):
+ _record(seen, content, m)
+ fields = {name: rhs for name, rhs in seen.items() if rhs}
+ if fields:
+ out.append(ClassFields(start, end, fields))
+ return out
+
+
+def _record(seen: dict[str, str | None], content: str, m: re.Match[str]) -> None:
+ if on_comment_line(content, m.start()):
+ return
+ name = "this." + m.group("name")
+ rhs = m.group("rhs").strip()
+ if JS_SYNTAX.assignment_strip is not None:
+ rhs = JS_SYNTAX.assignment_strip.sub("", rhs).strip()
+ refused = name in seen or m.groupdict().get("op") or _BARE_NAME_RE.fullmatch(rhs)
+ seen[name] = None if refused else rhs
+
+
# One object or enum entry: comments before it, an identifier or quoted key,
# then `:` or `=`.
_MEMBER_RE = re.compile(
@@ -442,6 +520,13 @@ _MEMBER_VALUE_START = frozenset("'\"`{$_") | frozenset(
)
+# A literal member value with a comment after it (`url: '/api' // was '/v1'`).
+_LITERAL_THEN_COMMENT_RE = re.compile(
+ r"""^(?P<lit>(?P<q>['"`])(?:(?!(?P=q))[^\\]|\\.)*(?P=q))\s*(?://[^\n]*|/\*.*?\*/)\s*$""",
+ re.DOTALL,
+)
+
+
def map_entry(entry: str) -> tuple[str, str, int] | None:
"""``(key, value source, value offset)`` of one ``key: value`` / ``key = value`` entry.
@@ -478,6 +563,8 @@ def _fold_members(
continue
key, value, value_at = member
name = f"{prefix}.{key}"
+ if value[0] in _QUOTES and "/" in value and len(value) <= _MEMBER_VALUE_MAX:
+ value = _LITERAL_THEN_COMMENT_RE.sub(r"\g<lit>", value)
if value[0] not in _MEMBER_VALUE_START or len(value) > _MEMBER_VALUE_MAX:
seen[name] = None
continue
@@ -772,8 +859,11 @@ __all__ = [
"RUST_SYNTAX",
"SCAN_LIMIT",
"Arg",
+ "ClassFields",
"StringSyntax",
"call_arguments",
+ "inline_names",
+ "js_class_fields",
"literal_span",
"map_entry",
"match_paren",
diff --git a/packages/core/src/repowise/core/workspace/extractors/topic/nestjs.py b/packages/core/src/repowise/core/workspace/extractors/topic/nestjs.py
index cae06c43b..0e4e786fc 100644
--- a/packages/core/src/repowise/core/workspace/extractors/topic/nestjs.py
+++ b/packages/core/src/repowise/core/workspace/extractors/topic/nestjs.py
@@ -16,7 +16,7 @@ import re
from functools import lru_cache
from typing import TYPE_CHECKING
-from ..calls import call_sites
+from ..calls import call_sites, typed_receivers
from ..langs import JS_TS
from ..strings import Arg, map_entry, select_argument, split_top_level
from .dialect import TopicCall, topic_contract
@@ -50,9 +50,7 @@ _HANDLERS = (
)
# `private readonly client: ClientProxy`, `@Inject('X') billing: ClientKafka`.
-_PROXY_FIELD_RE = re.compile(
- r"\b(?P<name>[A-Za-z_$][\w$]*)\s*[?!]?\s*:\s*Client(?:Proxy|Kafka|RMQ|Nats|Redis|Mqtt)\b"
-)
+_PROXY_TYPE_RE = re.compile(r"Client(?:Proxy|Kafka|RMQ|Nats|Redis|Mqtt)")
@lru_cache(maxsize=256)
@@ -102,7 +100,7 @@ class NestMicroservicesDialect:
if not any(r in content for r in _MICROSERVICES):
return [] # handlers and ClientProxy both come from this package
calls = list(_HANDLERS)
- names = {m.group("name") for m in _PROXY_FIELD_RE.finditer(content)}
+ names = set(typed_receivers(content, _PROXY_TYPE_RE))
if names:
calls.extend(_proxy_calls(tuple(sorted(names))))
out: list[Contract] = []
diff --git a/tests/unit/ingestion/test_angular_framework_edges.py b/tests/unit/ingestion/test_angular_framework_edges.py
new file mode 100644
index 000000000..23ed5ba56
--- /dev/null
+++ b/tests/unit/ingestion/test_angular_framework_edges.py
@@ -0,0 +1,229 @@
+"""Angular build entries, router config and decorator metadata as graph edges.
+
+The end-to-end cases go through traverse -> parse -> graph -> framework edges
+-> dead code, the path a real index takes.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+from repowise.core.analysis.dead_code import DeadCodeAnalyzer
+from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder
+from repowise.core.ingestion.framework_edges.angular import angular_references
+from repowise.core.ingestion.framework_facts import angular_entry_files
+
+_ANGULAR_JSON = {
+ "projects": {
+ "web": {
+ "architect": {
+ "build": {
+ "builder": "@angular-devkit/build-angular:application",
+ "options": {"browser": "src/main.ts", "polyfills": ["zone.js", "src/polyfills.ts"]},
+ "configurations": {
+ "production": {
+ "fileReplacements": [
+ {
+ "replace": "src/environments/environment.ts",
+ "with": "src/environments/environment.prod.ts",
+ }
+ ]
+ }
+ },
+ },
+ "test": {
+ "builder": "@angular-devkit/build-angular:karma",
+ "options": {"main": "src/test.ts", "karmaConfig": "karma.conf.js"},
+ },
+ }
+ }
+ }
+}
+
+_APP = {
+ "src/main.ts": (
+ "import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';\n"
+ "import { AppModule } from './app/app.module';\n"
+ "platformBrowserDynamic().bootstrapModule(AppModule);\n"
+ ),
+ "src/polyfills.ts": "import 'zone.js';\n",
+ "src/test.ts": "import 'zone.js/testing';\n",
+ "karma.conf.js": "module.exports = function (config) {};\n",
+ "src/environments/environment.ts": "export const environment = { production: false };\n",
+ "src/environments/environment.prod.ts": "export const environment = { production: true };\n",
+ "src/app/app.module.ts": (
+ "import { NgModule } from '@angular/core';\n"
+ "import { RouterModule, Routes } from '@angular/router';\n"
+ "import { HomeComponent } from './home.component';\n"
+ "const requireAuth = () => true;\n"
+ "const lazy = () => import('./users.module').then(m => m.UsersModule);\n"
+ "const routes: Routes = [\n"
+ " { path: '', component: HomeComponent, canActivate: [requireAuth] },\n"
+ " { path: 'users', loadChildren: lazy },\n"
+ "];\n"
+ "@NgModule({ imports: [RouterModule.forRoot(routes)], declarations: [HomeComponent] })\n"
+ "export class AppModule {}\n"
+ "function neverUsed() { return 1; }\n"
+ ),
+ "src/app/home.component.ts": (
+ "import { Component } from '@angular/core';\n"
+ "import { environment } from '../environments/environment';\n"
+ "@Component({ selector: 'app-home', template: '' })\n"
+ "export class HomeComponent { prod = environment.production; }\n"
+ ),
+ "src/app/users.module.ts": (
+ "import { NgModule } from '@angular/core';\n@NgModule({})\nexport class UsersModule {}\n"
+ ),
+ "src/app/orphan.component.ts": (
+ "import { Component } from '@angular/core';\n"
+ "@Component({ selector: 'app-orphan', template: '' })\nexport class OrphanComponent {}\n"
+ ),
+}
+
+
+def _write(repo: Path, files: dict[str, str]) -> Path:
+ for rel, text in files.items():
+ path = repo / rel
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(text, encoding="utf-8")
+ return repo
+
+
+def _graph(repo: Path) -> tuple[object, set[tuple[str, str, str]]]:
+ builder = GraphBuilder(repo)
+ parser = ASTParser()
+ source_map: dict[str, bytes] = {}
+ for fi in FileTraverser(repo).traverse():
+ src = Path(fi.abs_path).read_bytes()
+ builder.add_file(parser.parse_file(fi, src))
+ source_map[fi.path] = src
+ builder.set_source_map(source_map)
+ builder.build()
+ builder.add_framework_edges(["Angular"])
+ report = DeadCodeAnalyzer(
+ builder.graph(), {}, parsed_files=builder._parsed_files, source_map=source_map, repo_root=repo
+ ).analyze({"detect_unused_internals": True})
+ dead = {(f.kind.value, f.file_path, f.symbol_name or "") for f in report.findings}
+ return builder.graph(), dead
+
+
+class TestReferences:
+ def test_names_in_metadata_router_config_and_bootstrap(self) -> None:
+ text = (
+ "bootstrapApplication(AppComponent, appConfig);\n"
+ "export const routes: Routes = [{ path: 'a', component: A, canActivate: [guard] },"
+ " { path: 'b', component: mobile ? Small : Large }, ...childRoutes];\n"
+ "@Component({ selector: 'x', imports: [Child, 'NotAName'] /* Old */ })\n"
+ "export class Own {}\n"
+ )
+ assert sorted(set(angular_references(text))) == [
+ ("A", False),
+ ("AppComponent", True),
+ ("Child", False),
+ ("Large", False),
+ ("Small", False),
+ ("appConfig", True),
+ ("childRoutes", False),
+ ("guard", False),
+ ("mobile", False),
+ ]
+
+ def test_a_site_in_a_comment_or_string_is_none(self) -> None:
+ text = (
+ "// bootstrapApplication(Old)\nconst s = 'bootstrapApplication(Fake)';\n"
+ "@NgModule({\n // don't forget\n declarations: [AppComponent],\n"
+ " providers: [{ provide: 'x', useValue: 'Helper' }] })\nexport class M {}\n"
+ )
+ assert sorted(set(angular_references(text))) == [("AppComponent", False)]
+
+ def test_a_class_naming_itself_in_its_metadata_is_not_a_use(self) -> None:
+ text = (
+ "@Component({ providers: [{ provide: X, useExisting: forwardRef(() => Self) }] })\n"
+ "export class Self {}\n"
+ )
+ assert sorted(set(angular_references(text))) == [("X", False), ("forwardRef", False)]
+
+ def test_a_file_with_no_site_reads_nothing(self) -> None:
+ assert list(angular_references("const routes = [];\nfoo(Bar);\n")) == []
+
+
+class TestEntryFiles:
+ def test_build_and_test_entries_and_file_replacements(self) -> None:
+ paths = {"src/main.ts", "src/polyfills.ts", "src/test.ts", "karma.conf.js",
+ "src/environments/environment.prod.ts", "src/app/app.module.ts"} # fmt: skip
+ assert angular_entry_files("angular.json", _ANGULAR_JSON, paths) == [
+ "karma.conf.js",
+ "src/environments/environment.prod.ts",
+ "src/main.ts",
+ "src/polyfills.ts",
+ "src/test.ts",
+ ]
+
+ def test_nx_paths_are_workspace_relative_and_other_toolchains_are_skipped(self) -> None:
+ project = {
+ "targets": {
+ "build": {"executor": "@nx/angular:application", "options": {"main": "apps/web/src/main.ts"}},
+ "serve-api": {"executor": "@nx/node:build", "options": {"main": "apps/web/server.ts"}},
+ }
+ }
+ paths = {"apps/web/src/main.ts", "apps/web/server.ts"}
+ assert angular_entry_files("apps/web/project.json", project, paths) == ["apps/web/src/main.ts"]
+
+ def test_an_angular_json_in_a_subdirectory_resolves_against_it(self) -> None:
+ config = {
+ "projects": {
+ "web": {
+ "architect": {
+ "build": {
+ "builder": "@angular/build:application",
+ "options": {
+ "browser": "src/main.ts",
+ "ssr": {"entry": "src/server.ts"},
+ "scripts": ["src/legacy.js", {"input": "src/lazy.js", "inject": False}],
+ },
+ }
+ }
+ }
+ }
+ }
+ paths = {"frontend/src/main.ts", "frontend/src/server.ts", "frontend/src/legacy.js",
+ "frontend/src/lazy.js", "src/main.ts"} # fmt: skip
+ assert angular_entry_files("frontend/angular.json", config, paths) == [
+ "frontend/src/lazy.js",
+ "frontend/src/legacy.js",
+ "frontend/src/main.ts",
+ "frontend/src/server.ts",
+ ]
+
+
+def test_an_angular_app_s_convention_files_are_not_dead_code(tmp_path: Path) -> None:
+ repo = _write(tmp_path, {**_APP, "angular.json": json.dumps(_ANGULAR_JSON)})
+ graph, dead = _graph(repo)
+ for path in ("src/main.ts", "src/polyfills.ts", "src/test.ts", "karma.conf.js",
+ "src/environments/environment.prod.ts"): # fmt: skip
+ assert graph.has_edge("framework:angular", path), path
+ assert graph.get_edge_data("src/app/app.module.ts::__module__", "src/app/app.module.ts::routes")[
+ "edge_type"
+ ] == "framework_binds"
+ flagged = {(kind, path, name) for kind, path, name in dead if kind != "zombie_package"}
+ # Only what nothing names: the orphan component and the unused function.
+ assert flagged == {
+ ("unreachable_file", "src/app/orphan.component.ts", ""),
+ ("unused_export", "src/app/orphan.component.ts", "OrphanComponent"),
+ ("unused_internal", "src/app/app.module.ts", "neverUsed"),
+ }
+
+
+def test_a_commented_workspace_config_with_a_bom_is_read(tmp_path: Path) -> None:
+ config = "\ufeff// generated\n" + json.dumps(_ANGULAR_JSON).replace('"projects"', '/* x */ "projects"', 1)
+ repo = _write(tmp_path, {**_APP, "angular.json": config})
+ graph, _dead = _graph(repo)
+ assert graph.has_edge("framework:angular", "src/polyfills.ts")
+
+
+def test_without_a_workspace_config_the_bootstrap_file_is_still_an_entry(tmp_path: Path) -> None:
+ repo = _write(tmp_path, {k: v for k, v in _APP.items() if k.startswith("src/")})
+ graph, _dead = _graph(repo)
+ assert graph.has_edge("framework:angular", "src/main.ts")
+ assert not graph.has_edge("framework:angular", "src/polyfills.ts")
diff --git a/tests/unit/workspace/test_angular_http.py b/tests/unit/workspace/test_angular_http.py
new file mode 100644
index 000000000..804ea4623
--- /dev/null
+++ b/tests/unit/workspace/test_angular_http.py
@@ -0,0 +1,309 @@
+"""Angular HttpClient consumers, with the base an environment file or a class field names."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+from repowise.core.workspace.extractors.http import HttpExtractor
+from repowise.core.workspace.matching import match_contracts
+
+_IMPORT = "import { HttpClient } from '@angular/common/http';\n"
+_ENV = "export const environment = {\n production: false,\n apiUrl: 'http://localhost:4000/api',\n};\n"
+
+
+def _write(repo: Path, files: dict[str, str]) -> Path:
+ for rel, text in files.items():
+ path = repo / rel
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(text, encoding="utf-8")
+ return repo
+
+
+def _calls(repo: Path) -> list[tuple[str, str, str, str]]:
+ return sorted(
+ (c.meta["client"], c.meta["method"], c.meta["path"], c.meta.get("base_token", ""))
+ for c in HttpExtractor().extract(repo, "web")
+ if c.role == "consumer"
+ )
+
+
+def _service(body: str, ctor: str = "private http: HttpClient") -> str:
+ return f"{_IMPORT}export class Api {{\n constructor({ctor}) {{}}\n{body}}}\n"
+
+
+class TestReceivers:
+ def test_verbs_on_a_constructor_parameter_property(self, tmp_path: Path) -> None:
+ body = (
+ " list() { return this.http.get<User[]>('/api/users'); }\n"
+ " make(u: User) { return this.http.post<User>('/api/users', u, { params }); }\n"
+ " edit(id: string) { return this.http.patch(`/api/users/${id}`, {}); }\n"
+ " drop(id: string) { return this.http.delete<void>(`/api/users/${id}`); }\n"
+ )
+ assert _calls(_write(tmp_path, {"src/api.ts": _service(body)})) == [
+ ("angular", "DELETE", "/api/users/{param}", ""),
+ ("angular", "GET", "/api/users", ""),
+ ("angular", "PATCH", "/api/users/{param}", ""),
+ ("angular", "POST", "/api/users", ""),
+ ]
+
+ def test_inject_and_typed_fields_under_any_name(self, tmp_path: Path) -> None:
+ text = (
+ "import { HttpClient as Http } from '@angular/common/http';\n"
+ "export class A {\n private readonly api = inject(Http);\n"
+ " private rest!: Http;\n"
+ " a() { return this.api.get('/api/a'); }\n b() { return this.rest.put('/api/b', x); }\n}\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": text})) == [
+ ("angular", "GET", "/api/a", ""),
+ ("angular", "PUT", "/api/b", ""),
+ ]
+
+ def test_request_takes_the_method_first(self, tmp_path: Path) -> None:
+ body = (
+ " a() { return this.http.request('POST', '/api/a', { body }); }\n"
+ " b(req: HttpRequest<unknown>) { return this.http.request(req); }\n"
+ " c(m: string) { return this.http.request(m, '/api/c'); }\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": _service(body)})) == [
+ ("angular", "POST", "/api/a", ""),
+ ]
+
+ def test_no_import_no_client(self, tmp_path: Path) -> None:
+ text = (
+ "export class A {\n constructor(private http: HttpClient) {}\n"
+ " a() { return this.http.get('/api/a'); }\n}\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": text})) == []
+
+ def test_a_longer_type_name_and_a_bare_call_are_not_clients(self, tmp_path: Path) -> None:
+ text = (
+ f"{_IMPORT}export class A {{\n constructor(private http: MyHttpClient, private h: HttpClient) {{}}\n"
+ " a() { this.http.get('/api/a'); this.h.get('/api/c'); return h('/api/b'); }\n}\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": text})) == [("angular", "GET", "/api/c", "")]
+
+ def test_head_options_lowercase_request_and_nested_generics(self, tmp_path: Path) -> None:
+ body = (
+ " a() { return this.http.head('/api/a'); }\n"
+ " b() { return this.http.options('/api/b'); }\n"
+ " c() { return this.http.request('get', '/api/c'); }\n"
+ " d() { return this.http.get<Page<User>>('/api/d'); }\n"
+ " e() { return this.http.jsonp('/api/e', 'cb'); }\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": _service(body)})) == [
+ ("angular", "GET", "/api/c", ""),
+ ("angular", "GET", "/api/d", ""),
+ ("angular", "HEAD", "/api/a", ""),
+ ("angular", "OPTIONS", "/api/b", ""),
+ ]
+
+ def test_a_method_option_does_not_make_a_second_wrapper_row(self, tmp_path: Path) -> None:
+ body = " pay() { return this.http.post('/api/pay', { method: 'card' }); }\n"
+ assert _calls(_write(tmp_path, {"src/a.ts": _service(body)})) == [
+ ("angular", "POST", "/api/pay", ""),
+ ]
+
+
+class TestEnvironmentBase:
+ def test_an_imported_environment_folds(self, tmp_path: Path) -> None:
+ body = " a() { return this.http.get(`${environment.apiUrl}/users/${id}`); }\n"
+ repo = _write(
+ tmp_path,
+ {
+ "src/environments/environment.ts": _ENV,
+ "src/app/api.ts": "import { environment } from '../environments/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/api/users/{param}", "")]
+
+ def test_a_path_alias_finds_the_nearest_environments_directory(self, tmp_path: Path) -> None:
+ body = " a() { return this.http.get(environment.apiUrl + '/orders'); }\n"
+ other = _ENV.replace("/api", "/other")
+ repo = _write(
+ tmp_path,
+ {
+ "apps/web/src/environments/environment.ts": _ENV,
+ "apps/admin/src/environments/environment.ts": other,
+ "src/environments/environment.ts": _ENV.replace("/api", "/root"),
+ "apps/web/src/app/api.ts": "import { environment } from '@env/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/api/orders", "")]
+
+ def test_an_aliased_import_and_a_nested_member(self, tmp_path: Path) -> None:
+ env = "export const environment = { api: { base: '/api/v2' } };\n"
+ body = " a() { return this.http.get(`${env.api.base}/x`); }\n"
+ repo = _write(
+ tmp_path,
+ {
+ "src/environments/environment.ts": env,
+ "src/app/api.ts": "import { environment as env } from '../environments/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/api/v2/x", "")]
+
+ def test_a_package_named_environment_is_not_looked_up(self, tmp_path: Path) -> None:
+ body = " a() { return this.http.get(`${environment.apiUrl}/x`); }\n"
+ repo = _write(
+ tmp_path,
+ {
+ "src/environments/environment.ts": _ENV,
+ "src/app/api.ts": "import { environment } from '@ngrx/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/x", "apiUrl")]
+
+ def test_an_unreadable_member_stays_a_base_token(self, tmp_path: Path) -> None:
+ env = "export const environment = { apiUrl: process.env['API_URL'] };\n"
+ body = " a() { return this.http.get(`${environment.apiUrl}/users`); }\n"
+ repo = _write(
+ tmp_path,
+ {
+ "src/environments/environment.ts": env,
+ "src/app/api.ts": "import { environment } from '../environments/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/users", "apiUrl")]
+
+ def test_a_const_outside_environments_does_not_travel(self, tmp_path: Path) -> None:
+ body = " a() { return this.http.get(`${environment.apiUrl}/users`); }\n"
+ repo = _write(
+ tmp_path,
+ {
+ "src/config/environment.ts": _ENV,
+ "src/app/api.ts": "import { environment } from '../config/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/users", "apiUrl")]
+
+
+class TestClassFields:
+ def test_a_field_built_on_the_environment(self, tmp_path: Path) -> None:
+ body = (
+ " private readonly base = `${environment.apiUrl}/users`;\n"
+ " one(id: string) { return this.http.get(`${this.base}/${id}`); }\n"
+ " all() { return this.http.get(this.base); }\n"
+ )
+ repo = _write(
+ tmp_path,
+ {
+ "src/environments/environment.ts": _ENV,
+ "src/app/api.ts": "import { environment } from '../environments/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [
+ ("angular", "GET", "/api/users", ""),
+ ("angular", "GET", "/api/users/{param}", ""),
+ ]
+
+ def test_a_field_set_in_the_constructor(self, tmp_path: Path) -> None:
+ text = (
+ f"{_IMPORT}import {{ environment }} from '../environments/environment';\n"
+ "export class A {\n private baseUrl: string;\n"
+ " constructor(private http: HttpClient) {\n this.baseUrl = environment.apiUrl;\n }\n"
+ " a() { return this.http.get(`${this.baseUrl}/auth.json`); }\n}\n"
+ )
+ repo = _write(tmp_path, {"src/environments/environment.ts": _ENV, "src/app/a.ts": text})
+ assert _calls(repo) == [("angular", "GET", "/api/auth.json", "")]
+
+ def test_a_field_built_on_another_field(self, tmp_path: Path) -> None:
+ body = (
+ " private base = environment.apiUrl;\n"
+ " private users = `${this.base}/users`;\n"
+ " a(id: string) { return this.http.get(`${this.users}/${id}`); }\n"
+ )
+ repo = _write(
+ tmp_path,
+ {
+ "src/environments/environment.ts": _ENV,
+ "src/app/api.ts": "import { environment } from '../environments/environment';\n" + _service(body),
+ },
+ )
+ assert _calls(repo) == [("angular", "GET", "/api/users/{param}", "")]
+
+ def test_fields_that_are_not_constants_are_refused(self, tmp_path: Path) -> None:
+ text = (
+ f"{_IMPORT}const base = '/wrong';\nconst url = '/wrong';\n"
+ "export class A {\n private url: string;\n static prefix = '/static';\n"
+ " private grow = '/api';\n"
+ " constructor(private http: HttpClient, base: string) { this.base = base; }\n"
+ " more() { this.grow += '/v2'; }\n"
+ " a() {\n let url = '/x';\n url = '/y';\n"
+ " this.http.get(`${this.base}/a`);\n this.http.get(`${this.url}/b`);\n"
+ " this.http.get(`${this.prefix}/c`);\n return this.http.get(`${this.grow}/d`);\n }\n}\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": text})) == [
+ ("angular", "GET", "/a", "base"),
+ ("angular", "GET", "/b", "url"),
+ ("angular", "GET", "/c", "prefix"),
+ ("angular", "GET", "/d", "grow"),
+ ]
+
+ def test_a_field_belongs_to_its_own_class(self, tmp_path: Path) -> None:
+ text = (
+ f"{_IMPORT}export class A {{\n private base = '/api/a';\n"
+ " constructor(private http: HttpClient) {}\n"
+ " a() { return this.http.get(`${this.base}/x`); }\n}\n"
+ "export class B extends Base {\n constructor(private http: HttpClient) { super(); }\n"
+ " b() { return this.http.get(`${this.base}/y`); }\n}\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": text})) == [
+ ("angular", "GET", "/api/a/x", ""),
+ ("angular", "GET", "/y", "base"),
+ ]
+
+ def test_a_field_set_twice_is_not_folded(self, tmp_path: Path) -> None:
+ body = (
+ " private base = '/api/v1';\n"
+ " use2() { this.base = '/api/v2'; }\n"
+ " a() { return this.http.get(`${this.base}/users`); }\n"
+ )
+ assert _calls(_write(tmp_path, {"src/a.ts": _service(body)})) == [
+ ("angular", "GET", "/users", "base"),
+ ]
+
+
+def test_an_angular_service_links_to_nest_and_laravel(tmp_path: Path) -> None:
+ nest = _write(
+ tmp_path / "api",
+ {
+ "src/main.ts": "app.setGlobalPrefix('api');\n",
+ "src/users.controller.ts": (
+ "import { Controller, Get } from '@nestjs/common';\n"
+ "@Controller('users')\nexport class UsersController {\n @Get(':id')\n one() {}\n}\n"
+ ),
+ },
+ )
+ laravel = _write(
+ tmp_path / "tickets",
+ {
+ "routes/api.php": (
+ "<?php\nuse App\\Http\\Controllers\\TicketController;\n"
+ "Route::post('/tickets/{ticket}/refund', [TicketController::class, 'refund']);\n"
+ ),
+ },
+ )
+ body = (
+ " private readonly users = `${environment.apiUrl}/users`;\n"
+ " user(id: string) { return this.http.get<User>(`${this.users}/${id}`); }\n"
+ " refund(id: string) { return this.http.post(`/api/tickets/${id}/refund`, {}); }\n"
+ " private other = '/api/v1';\n swap() { this.other = '/api/v2'; }\n"
+ " guess(id: string) { return this.http.get(`${this.other}/orders/${id}`); }\n"
+ )
+ web = _write(
+ tmp_path / "web",
+ {
+ "src/environments/environment.ts": _ENV,
+ "src/app/api.service.ts": "import { environment } from '../environments/environment';\n" + _service(body),
+ },
+ )
+ contracts = (
+ HttpExtractor().extract(nest, "api")
+ + HttpExtractor().extract(laravel, "tickets")
+ + HttpExtractor().extract(web, "web")
+ )
+ links = sorted((link.provider_repo, link.consumer_repo, link.contract_id) for link in match_contracts(contracts))
+ assert links == [
+ ("api", "web", "http::GET::/api/users/{param}"),
+ ("tickets", "web", "http::POST::/api/tickets/{param}/refund"),
+ ]
diff --git a/tests/unit/workspace/test_client_calls.py b/tests/unit/workspace/test_client_calls.py
index e04fe8afc..3658574af 100644
--- a/tests/unit/workspace/test_client_calls.py
+++ b/tests/unit/workspace/test_client_calls.py
@@ -15,6 +15,11 @@ from repowise.core.workspace.extractors.http.client_calls import (
consumer_contracts,
method_from_argument,
)
+from repowise.core.workspace.extractors.http.paths import (
+ absolute_host,
+ extract_path_from_url,
+ flatten_interpolations,
+)
from repowise.core.workspace.extractors.strings import (
CSHARP_SYNTAX,
GO_SYNTAX,
@@ -294,3 +299,20 @@ class TestConsumerContracts:
rows = [ClientCallMatch(client="w", url='"database_url"', offset=0, method="GET")]
assert consumer_contracts(self._ctx("x"), rows, GO_SYNTAX, path_only=True) == []
assert len(consumer_contracts(self._ctx("x"), rows, GO_SYNTAX)) == 1
+
+
+class TestConsumerUrlShapes:
+ def test_nested_braces_in_an_interpolation_stay_one_segment(self):
+ assert flatten_interpolations("/rate/${format(d, F, { in: utc })}/x") == "/rate/${expr}/x"
+ assert flatten_interpolations("/a/${id}/b") == "/a/${id}/b"
+ assert flatten_interpolations("/a/${broken") == "/a/${broken"
+
+ 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
+ assert extract_path_from_url(url) == url
+ assert absolute_host("https://x.io/${p}") == "x.io"
+
+ def test_a_scheme_after_a_leading_interpolation_is_the_urls(self):
+ assert absolute_host("${proto}://h.com/api/u") == "h.com"
+ assert extract_path_from_url("${proto}://h.com/api/u") == "/api/u"
diff --git a/tests/unit/workspace/test_node_clients.py b/tests/unit/workspace/test_node_clients.py
index ff6e7f585..4ac1e2814 100644
--- a/tests/unit/workspace/test_node_clients.py
+++ b/tests/unit/workspace/test_node_clients.py
@@ -240,3 +240,20 @@ def test_a_base_in_the_calls_own_options_wins(tmp_path: Path) -> None:
("axios", "POST", "/v2/jobs", ""),
("axios", "GET", "/api/users", ""),
}
+
+
+def test_an_axios_call_folds_a_class_field(tmp_path: Path) -> None:
+ text = (
+ "import axios from 'axios';\nexport class S {\n private base = '/api/v1';\n"
+ " a() { return axios.get(`${this.base}/users`); }\n}\n"
+ )
+ assert _calls(_write(tmp_path, {"a.ts": text})) == {("axios", "GET", "/api/v1/users", "")}
+
+
+def test_an_instance_verb_with_a_method_option_is_not_a_wrapper_row(tmp_path: Path) -> None:
+ text = (
+ "import axios from 'axios';\nconst api = axios.create({ baseURL: '/api' });\n"
+ "api.post('/pay', { method: 'card' });\n"
+ )
+ rows = [c for c in HttpExtractor().extract(_write(tmp_path, {"a.ts": text}), "web") if c.role == "consumer"]
+ assert [(c.meta["client"], c.contract_id) for c in rows] == [("axios", "http::POST::/api/pay")]
diff --git a/tests/unit/workspace/test_strings.py b/tests/unit/workspace/test_strings.py
index 543120f19..93a503801 100644
--- a/tests/unit/workspace/test_strings.py
+++ b/tests/unit/workspace/test_strings.py
@@ -9,6 +9,7 @@ from repowise.core.workspace.extractors.strings import (
PHP_SYNTAX,
PYTHON_SYNTAX,
Arg,
+ inline_names,
resolve_argument,
resolve_string,
select_argument,
@@ -110,6 +111,19 @@ class TestJsConstants:
def test_an_example_in_a_comment_is_not_a_binding(self) -> None:
assert _js("// const Q = 'a';\n", "Q") is None
+ def test_a_comment_after_the_last_member_value_is_not_the_value(self) -> None:
+ src = "export const env = {\n apiUrl: '/assets/data' // 'http://localhost:3000'\n};\n"
+ assert _js(src, "env.apiUrl") == "/assets/data"
+
+ def test_a_url_member_keeps_its_slashes(self) -> None:
+ src = "const env = { api: 'http://localhost:4000' /* dev */, b: 'https://x.io//y' };\n"
+ assert (_js(src, "env.api"), _js(src, "env.b")) == ("http://localhost:4000", "https://x.io//y")
+
+
+def test_inline_names_fills_settled_holes_and_keeps_the_rest() -> None:
+ names = {"this.base": "${environment.apiUrl}/users"}
+ assert inline_names("${this.base}/${id}", names) == "${environment.apiUrl}/users/${id}"
+
class TestPhpConstants:
def test_a_class_constant_folds_through_self(self) -> None: