Skip to content

feat(lint): db-call-in-loop rule + loop_depth on call records#63

Merged
smochan merged 1 commit into
feat/lint-query-and-literal-rulesfrom
feat/lint-db-call-in-loop
Jun 10, 2026
Merged

feat(lint): db-call-in-loop rule + loop_depth on call records#63
smochan merged 1 commit into
feat/lint-query-and-literal-rulesfrom
feat/lint-db-call-in-loop

Conversation

@smochan

@smochan smochan commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Summary

Stacked on #62. Phase 1 continues (gap-analysis findings #8/#10 — N+1 / DB-call-in-loop):

  • Parsers (typescript.py, python.py): _collect_calls DFS now carries loop_depth; CALLS edge metadata gains loop_depth/in_loop. TS also treats function args to .map/.forEach/.filter/.reduce/.flatMap as implicit loop bodies.
  • New lint rule db-call-in-loop (high): DB-API call patterns (TS db.select|insert|update|delete|execute|query; Py session|cursor|db . query|execute|add|commit) flagged when loop_depth >= 1.
  • Reviewer fix on top of the implementation: the iteration-method branch was dropping the callee subtree, losing CALLS edges for chains like getUsers().map(cb) — fixed + regression test.

Implemented by a Sonnet subagent against a Fable-authored spec; reviewed, corrected, and verified by the orchestrating session.

Test plan

  • 15 new tests (lint rule both languages, forEach callback, non-loop counterexamples, parser loop_depth metadata, chained-receiver regression)
  • Full suite 680 passed; ruff + mypy --strict clean; self-lint 0 findings

🤖 Generated with Claude Code

Parser changes:
- typescript.py _collect_calls: stack entries now carry loop_depth;
  for_statement/for_in_statement/while_statement/do_statement children
  get depth+1; .map/.forEach/.filter/.reduce/.flatMap function-typed
  argument children also get depth+1; CALLS edge metadata gains
  loop_depth and in_loop fields.
- python.py _collect_calls: same depth-threading for for_statement and
  while_statement; stop-at-nested-defs behaviour preserved.

Lint changes:
- analysis/lint.py: new _check_db_call_in_loop checker fires when
  ctx.loop_depth >= 1 and the call chain matches the per-language DB
  pattern (TS: db/database.select|insert|..., Python:
  session/cursor/db.query|execute|...). New _iter_children_with_depth
  helper extends the lint DFS to bump depth inside .forEach/.map/etc
  callbacks. Registered in _CHECK_REGISTRY; added to DEFAULT_LINT_RULES
  with severity high.

Fixtures & tests:
- tests/fixtures/lint_sample/src/loops.ts: for...of + .forEach cases
  that fire, plus a non-loop counterexample that must not.
- tests/fixtures/lint_sample/src/batch.py: Python for-loop case that
  fires and a non-loop counterexample.
- tests/fixtures/df0_typescript/loop_calls.ts: small fixture for parser
  metadata assertions.
- tests/test_lint.py: 8 new tests covering TS for-of, forEach callback,
  Python for-loop, non-loop negative, severity, and message content.
- tests/test_lint_cli.py: updated --fail-on gate test to account for new
  high-severity findings in the shared fixture.
- tests/test_df0_typescript.py: 3 new tests asserting loop_depth/in_loop
  metadata on CALLS edges.
- tests/test_df0_python.py: 4 new tests for Python loop depth threading
  including nested loops.
@smochan smochan merged commit 900c813 into feat/lint-query-and-literal-rules Jun 10, 2026
4 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant