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.

fix(health): skip for...of over constant collections in TS/JS perf detector (#545)

Raghav Chamadiyaa781e901d078
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
index 115d98541..3fbabb7ef 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
@@ -120,6 +120,27 @@ class TsJsPerfDialect(BasePerfDialect):
return self._dotted_path(right)
return None
+ def is_constant_loop(self, node: Node) -> bool:
+ """True if a ``for...of`` / ``for...in`` iterates a compile-time-constant
+ bound: an inline **array literal** (``for (const p of ["/a", "/b"])`` —
+ the author enumerated a fixed set, so there is no data-dependent N+1
+ blow-up) or an **ALL_CAPS** named constant (``for (const f of
+ DREAMS_FILENAMES)``). Mirrors the Python dialect's literal-collection
+ skip. C-style ``for (;;)`` / ``while`` / ``do`` are cursors — never
+ constant here (they are pagination / polling)."""
+ if node.type not in self._ITERATION_LOOP_KINDS:
+ return False
+ right = node.child_by_field_name("right")
+ if right is None:
+ return False
+ if right.type == "array":
+ return True
+ if right.type == "identifier" and right.text is not None:
+ name = right.text.decode("utf-8", "replace")
+ if name.isupper() and len(name) > 1:
+ return True
+ return False
+
def sink_kind(
self,
root: str,
diff --git a/tests/unit/health/test_perf_io_in_loop.py b/tests/unit/health/test_perf_io_in_loop.py
index 56686a654..90cf3db0b 100644
--- a/tests/unit/health/test_perf_io_in_loop.py
+++ b/tests/unit/health/test_perf_io_in_loop.py
@@ -149,6 +149,28 @@ _CASES = [
[],
"Map.delete is not a db sink",
),
+ (
+ "typescript",
+ b"import fs from 'fs';\n"
+ b'function f() {\n for (const p of ["/proc/net/tcp", "/proc/net/tcp6"]) {\n'
+ b' fs.readFileSync(p, "utf8");\n }\n}\n',
+ [],
+ "for-of over an inline array literal is constant-bounded -> skipped",
+ ),
+ (
+ "typescript",
+ b"import fs from 'fs';\nconst NAMES = ['DREAMS.md', 'dreams.md'];\n"
+ b"function f() {\n for (const n of NAMES) {\n fs.readFileSync(n);\n }\n}\n",
+ [],
+ "for-of over an ALL_CAPS named constant is constant-bounded -> skipped",
+ ),
+ (
+ "typescript",
+ b"import fs from 'fs';\n"
+ b'function f(paths) {\n for (const p of paths) {\n fs.readFileSync(p, "utf8");\n }\n}\n',
+ [("io_in_loop", "filesystem")],
+ "for-of over a data-dependent variable still fires (contrast to constant)",
+ ),
(
"typescript",
b"import axios from 'axios';\n"