I need to understand the full import graph of the TypeScript smell detection area. Specifically:
What imports from _smell_helpers.py? (both inside and outside the detectors package)
What imports from _smell_effects.py?
What imports from _smell_detectors.py?
What imports from smells.py (the public API)?
What does desloppify/languages/typescript/syntax/scanner.py look like? (the scan_code function)
What does desloppify/core/text_api.py export strip_c_style_comments?
Are there any __init__.py files in the detectors package that re-export things?
Search across the entire codebase for imports of these modules. I need to know every consumer to plan a safe refactor.
Assistant
I'll help you understand the full import graph of the TypeScript smell detection area. Let me search systematically across the codebase for all imports and usage patterns.
User
Thoroughness: medium
I need to understand the test file structure for TS smell detectors:
Read desloppify/languages/typescript/tests/smells/test_ts_smell_helpers.py — what does it import and test?
Read desloppify/languages/typescript/tests/test_ts_smells.py — what does it import and test?
Are there any other test files that test smell detection? Search for files with "smell" in the name under tests/
Check if there's a conftest.py in the tests/smells/ directory
Look at what set_project_root fixture provides (check conftest files)
Assistant
Now let me search for imports of the smell detection modules:
Assistant
I'll help you understand the TS smell detector test file structure. Let me start by finding and reading the relevant test files.
Assistant
Now let me search for conftest files to understand the fixtures:
Assistant
Now let me read the actual smell detection files to understand the structure and what's exported:
Assistant
Let me read the conftest files to understand the fixtures:
Assistant
Let me use Bash to list the detectors directory first:
Assistant
Now let me read all the key smell detection files:
Assistant
Perfect! Now let me check if there's a conftest in the typescript tests directory:
Assistant
Now let me check the __init__.py and scanner files:
Assistant
Great! Now let me check the full structure and look at the imports in these test files to see what they're testing:
Assistant
Now let me search for files that import from these smell modules outside the detectors package:
Assistant
Perfect! Now let me get the imports from the other test file:
Assistant
Let me use Grep instead:
Assistant
Now let me check the key files that use the smell modules:
Assistant
Excellent! Now let me verify if there's a conftest in the smells directory:
Assistant
Now let me check the react.py to see how it imports things:
Assistant
Let me check the text_utils implementation to understand strip_c_style_comments:
Assistant
Now let me check the test files to see if there are other imports:
Assistant
Now let me search comprehensively for all files that import from these specific smell modules:
Assistant
Perfect! Now let me check more of the source file to see what scan_code is re-exported from _smell_helpers:
Assistant
Now let me create a comprehensive summary document. Let me gather a bit more info on what each module exports:
Assistant
Assistant
User
Assistant
I'll verify the proposed plan by examining every file, import, and reference mentioned. Let me start by reading all the key files.
Assistant
Let me find the actual file paths first.
Assistant
Good -- the actual path is desloppify/languages/typescript/detectors/, not desloppify/lang/typescript/detectors/. Let me now read all four key files.
Assistant
Good. Now let me verify the consumer imports mentioned in the plan.
Assistant
Let me filter to just the source files (excluding build/, .desloppify.backup, etc.) for the real import consumers.
Assistant
Now let me read the test files and the other consumer files to understand the full import picture.
Assistant
Now let me check the build directory to see if it's a stale copy that could cause confusion, and also check for any other test files that reference these modules.
Assistant
Good. Now let me verify two critical things: (1) that the duplicate functions in smells.py truly shadow the imports, and (2) the exact import names used in smells.py vs the local definitions.
Assistant
Now I can see the full picture. Key observation: smells.py imports _detect_async_no_await, _detect_error_no_throw, _detect_swallowed_errors from _smell_helpers, but does NOT import _detect_empty_if_chains or _detect_dead_useeffects from . Instead, it defines its own local copies of those two functions (lines 344-408 and 411-464). This means:
Assistant
Now let me compare these implementations with the _smell_effects.py versions. The smells.py local copies are essentially identical to _smell_effects.py but with scan_code and _strip_ts_comments called directly (no DI). This confirms the plan is correct -- the _smell_effects.py DI pattern is unnecessary overhead, and the smells.py versions already use the direct-call pattern we want.
Now let me check the __init__.py file of the detectors package and look at how things flow up.
Assistant
Now let me check whether _build_ts_line_state is used anywhere outside smells.py, and verify the test_direct_coverage_modules.py usage of _smell_effects.
Assistant
Good. Now I have the complete picture. Let me also check if there's a build/ directory copy that needs to be kept in sync.
Assistant
Now let me also check the syntax scanner import chain since _smell_helpers.py re-exports scan_code and many consumers depend on that re-export path.
Assistant
Good. Now I have all the information needed to validate the plan and produce the detailed implementation steps. Let me verify one last thing -- the _track_brace_body function's current flow. It's in _smell_helpers.py as a wrapper around _smell_effects.py's track_brace_body, but _smell_detectors.py imports it from _smell_helpers. After the refactoring, it needs to stay accessible.
Assistant
Perfect! Now I have all the information I need. Let me compile my findings:
TypeScript Smell Detector Test Structure
Based on my analysis of the test files, here's the comprehensive breakdown:
1. test_ts_smell_helpers.py (in /desloppify/languages/typescript/tests/smells/)
Imports:
_detect_catch_return_default, _detect_monster_functions, _detect_stub_functions, _detect_window_globals, _find_function_start from desloppify.languages.typescript.detectors._smell_detectors
_detect_async_no_await, _detect_dead_useeffects, _detect_empty_if_chains, _detect_error_no_throw, _detect_swallowed_errors, _strip_ts_comments, _track_brace_body, _ts_match_is_in_string from desloppify.languages.typescript.detectors._smell_helpers
TS_SMELL_CHECKS from desloppify.languages.typescript.detectors.smells
@pytest.fixture()
def set_project_root(tmp_path: Path):
"""Set PROJECT_ROOT to tmp_path via RuntimeContext for the duration of a test."""
ctx = RuntimeContext(project_root=tmp_path)
with runtime_scope(ctx):
clear_source_file_cache_for_tests()
yield tmp_path
clear_source_file_cache_for_tests()
This fixture:
Creates a RuntimeContext with tmp_path as the project root
Uses runtime_scope() context manager to activate it
write_py(tmp_path) fixture: helper to write Python code to files
smell_ids(entries) function: extracts smell IDs from detection results
find_smell(entries, smell_id) function: finds specific smell entry
5. No conftest in /tests/smells/ directory
The /desloppify/languages/typescript/tests/smells/ directory contains onlytest_ts_smell_helpers.py — no conftest exists there. Tests inherit fixtures from:
Root conftest.py (the set_project_root fixture)
Any pytest plugins
Summary
test_ts_smell_helpers.py: Unit tests for individual helper functions (comment stripping, string detection, brace tracking) and detector functions (async, errors, functions, effects) — directly invokes low-level functions
test_ts_smells.py: Integration tests for detect_smells() main API — tests end-to-end detection across files, uses set_project_root fixture to mock PROJECT_ROOT, tests filtering behavior and output sorting
The set_project_root fixture provides runtime context isolation by wrapping tests with RuntimeContext and activating it via runtime_scope(), ensuring each test gets a clean, temporary project root without filesystem side effects.
Perfect! Now I have all the information I need. Let me create a comprehensive report:
TypeScript Smell Detection Import Graph Analysis
Based on my thorough codebase search, here's the complete import graph for the TypeScript smell detection area:
1. What imports from _smell_helpers.py?
Inside the detectors package:
_smell_detectors.py imports:
_strip_ts_comments
_track_brace_body
_ts_match_is_in_string
smells.py imports:
_detect_async_no_await
_detect_error_no_throw
_detect_swallowed_errors
_strip_ts_comments
_ts_match_is_in_string
scan_code (re-exported from scanner.py)
Outside the detectors package:
react.py (detector) imports:
_strip_ts_comments
scan_code
fixers/syntax_scan.py imports:
scan_code
fixers/if_chain.py imports:
scan_code
tests/smells/test_ts_smell_helpers.py imports:
_detect_async_no_await
_detect_dead_useeffects
_detect_empty_if_chains
_detect_error_no_throw
_detect_swallowed_errors
_strip_ts_comments
_track_brace_body
_ts_match_is_in_string
Total consumers: 7 files
2. What imports from _smell_effects.py?
Only internal to detectors package:
_smell_helpers.py imports (all with _impl suffix internally):
detect_dead_useeffects as _detect_dead_useeffects_impl
detect_empty_if_chains as _detect_empty_if_chains_impl
detect_error_no_throw as _detect_error_no_throw_impl
detect_swallowed_errors as _detect_swallowed_errors_impl
track_brace_body as _track_brace_body_impl
No external consumers - this is a pure internal implementation module that wraps the parametric versions for use by _smell_helpers.py.
Total consumers: 1 file (only _smell_helpers.py)
3. What imports from _smell_detectors.py?
Inside the detectors package:
smells.py imports:
_detect_catch_return_default
_detect_high_cyclomatic_complexity
_detect_monster_functions
_detect_nested_closures
_detect_stub_functions
_detect_switch_no_default
_detect_window_globals
Outside the detectors package:
tests/smells/test_ts_smell_helpers.py imports:
_detect_catch_return_default
_detect_monster_functions
_detect_stub_functions
_detect_window_globals
_find_function_start
Total consumers: 2 files
4. What imports from smells.py (the public API)?
Inside language config:
__init__.py imports:
smells as smells_detector_mod (entire module as alias)
phases.py imports:
smells as smells_detector_mod (entire module as alias)
commands.py imports:
smells as smells_detector_mod (entire module as alias)
/Users/user_c042661f/Documents/desloppify/desloppify/core/text_api.py (and its internal /Users/user_c042661f/Documents/desloppify/desloppify/core/_internal/text_utils.py)
Design a refactoring plan for the TypeScript smell detection area. Here's the full context from exploration:
Current State (Problems)
File Layout
_smell_helpers.py (198 lines) — A confused facade: re-exports scan_code, defines utilities, directly implements _detect_async_no_await, wraps 4 detectors from _smell_effects.py via DI
_smell_effects.py (297 lines) — Internal impl: detect_error_no_throw, detect_empty_if_chains, detect_dead_useeffects, detect_swallowed_errors, track_brace_body. Functions take scan_code_fn/strip_ts_comments_fn DI params that are always the same values
smells.py (644 lines) — Orchestrator: TS_SMELL_CHECKS registry, _build_ts_line_state (120-line FSM), DUPLICATE implementations of _detect_empty_if_chains (lines 344-408) and _detect_dead_useeffects (lines 411-464) that SHADOW the imported wrappers, detect_smells() public API, _detect_non_ts_asset_smells, _script_is_documented
Import Graph (consumers beyond the detectors/ package)
react.py imports: _strip_ts_comments, scan_code from _smell_helpers
fixers/syntax_scan.py imports: scan_code from _smell_helpers
fixers/if_chain.py imports: scan_code from _smell_helpers
smells.py imports from both _smell_helpers and _smell_detectors
Tests import from _smell_helpers, _smell_detectors, and smells
smells.py is imported by __init__.py, phases.py, commands.py at language-config level
Specific Issues
_smell_effects.py has ZERO external consumers (only _smell_helpers.py). The DI pattern (scan_code_fn, strip_ts_comments_fn) adds complexity without benefit — tests never use different implementations.
smells.py has duplicate copies of _detect_empty_if_chains and _detect_dead_useeffects that shadow the imported versions — confusing.
_detect_async_no_await lives in _smell_helpers.py instead of with other detectors — inconsistent.
The facade pattern in _smell_helpers.py is half-applied: wraps 4 functions, directly implements 1, and 2 of the wrappers are dead code.
Stale docstrings still reference "dead-function" / "dead functions".
smells.py at 644 lines mixes too many concerns.
Target Architecture
_smell_helpers.py — Pure utilities (no detector functions):
_smell_detectors.py module docstring: update "dead functions" to "stub functions"
Please verify this plan is sound, identify any risks or edge cases I'm missing, and produce a detailed step-by-step implementation order.
_smell_helpers
The local _detect_empty_if_chains (line 344) shadows the imported wrapper that would come from _smell_helpers
The local _detect_dead_useeffects (line 411) shadows the imported wrapper
But wait -- smells.py doesn't even import these two from _smell_helpers. Let me confirm by looking at the import block more carefully.
The import from _smell_helpers on lines 23-30 imports: _detect_async_no_await, _detect_error_no_throw, _detect_swallowed_errors, _strip_ts_comments, _ts_match_is_in_string, scan_code. There is NO import of _detect_empty_if_chains or _detect_dead_useeffects from _smell_helpers. So the local definitions in smells.py are the ONLY versions used in detect_smells(). The wrappers in _smell_helpers.py (lines 138-162) are used by the tests, but the orchestrator uses its own copies.
Let me also verify that the test file's imports of _detect_empty_if_chains and _detect_dead_useeffects come from _smell_helpers.
So the tests import from _smell_helpers, which delegates to _smell_effects. But the orchestrator (smells.py) uses locally-defined copies. This means there are THREE copies of the logic for _detect_empty_if_chains and _detect_dead_useeffects:
_smell_effects.py (canonical impl with DI params)
_smell_helpers.py (wrapper that passes DI params)
smells.py (independent re-implementation, no DI)
The smells.py versions call scan_code directly instead of via DI -- which is what the target architecture proposes for everything. Let me verify whether the smells.py local copies are identical to or divergent from _smell_effects.py.
Now I have the complete picture. Let me produce the final analysis and implementation plan.
Plan Verification and Detailed Implementation Order
1. The smells.py local copies vs _smell_effects.py copies are functionally identical but use direct imports instead of DI.
The smells.py versions of _detect_empty_if_chains and _detect_dead_useeffects call scan_code and _strip_ts_comments directly. The _smell_effects.py versions take them as keyword arguments (scan_code_fn, strip_ts_comments_fn). When merging into _smell_detectors.py, we should use the direct-call style (matching smells.py's versions), which is what the plan proposes.
Line 72: import desloppify.languages.typescript.detectors._smell_effects as ts_smell_effects
Line 178: ts_smell_effects.detect_swallowed_errors
This test is a smoke-test that just verifies modules are importable and functions are callable. After deleting _smell_effects.py, this test needs updating to reference _smell_detectors instead. This was missing from the original plan's import update list.
3. _track_brace_body placement decision.
The plan says _track_brace_body stays in _smell_helpers.py. This is correct because _smell_detectors.py imports it from _smell_helpers.py, and the tests import it from _smell_helpers.py. However, the current implementation in _smell_helpers.py delegates to _smell_effects.py via DI. After deleting _smell_effects.py, _smell_helpers.py needs to inline the track_brace_body implementation (currently 22 lines in _smell_effects.py lines 266-287). The plan mentions "inline implementation, remove DI" which is correct.
4. _build_ts_line_state is only used inside smells.py.
Moving it to _smell_helpers.py increases the public surface of _smell_helpers.py without a current consumer outside smells.py. It is still a reasonable move for separation of concerns (FSM utility vs orchestration), but it is optional. It does reduce smells.py by ~120 lines which is the stated goal.
5. build/ directory has stale copies.
The build/lib/ directory contains old copies of all four files. These should be cleaned up (or the build directory rebuilt) after the refactoring, but this is outside the scope of the code change itself.
6. Choosing which version of the duplicated detectors to keep.
For _detect_empty_if_chains: the smells.py version (lines 344-408) and _smell_effects.py version (lines 54-133) are functionally identical. The _smell_effects.py version does not take DI params for this function (unlike detect_dead_useeffects). They are the same code. Use either one.
For _detect_dead_useeffects: the smells.py version (lines 411-464) calls scan_code directly; the _smell_effects.py version (lines 136-201) takes scan_code_fn and strip_ts_comments_fn as keyword args. Use the smells.py version (direct calls) since we are eliminating DI.
Risks and Mitigations
Risk 1: Behavioral divergence between duplicate copies.
I compared the smells.py and _smell_effects.py versions line by line. They are functionally identical (same regex patterns, same logic, same output format). The only differences are: (a) DI vs direct imports, and (b) minor whitespace/formatting. No behavioral risk.
Risk 2: Tests currently exercise the _smell_helpers wrappers, which go through _smell_effects DI path.
After the refactoring, the tests will import from _smell_detectors and call functions that use direct imports. If any test relied on the DI mechanism (e.g., mocking scan_code_fn), it would break. Checking test_ts_smell_helpers.py: no tests mock the DI parameters. All tests call the high-level wrapper functions. No risk.
Risk 3: The scan_code re-export chain.
Currently: syntax/scanner.py -> _smell_helpers.py -> _smell_detectors.py, smells.py, react.py, fixers/. After refactoring, _smell_helpers.py continues to re-export scan_code. _smell_detectors.py will need to import scan_code -- it can continue getting it from _smell_helpers (current behavior), or import directly from syntax.scanner. The plan should specify this. Recommendation: keep importing from _smell_helpers to avoid proliferating direct references to the scanner module. The _smell_helpers module serves as the canonical import point for scan_code within the detectors package.
Risk 4: Circular imports.
After the refactoring, _smell_detectors.py imports from _smell_helpers.py (utilities), and smells.py imports from both. No circular dependency. _smell_helpers.py imports only from syntax.scanner and core.text_api. Clean.
Step-by-Step Implementation Order
The key principle: make each step independently testable. Run tests after each step.
Step 1: Inline _track_brace_body in _smell_helpers.py (remove DI delegation)
Replace lines 185-198 (the wrapper that delegates to _track_brace_body_impl with DI) with the direct implementation from _smell_effects.py lines 266-287, but importing scan_code directly instead of via parameter. Specifically:
python
def _track_brace_body(
lines: list[str], start_line: int, *, max_scan: int = 2000
) -> int | None:
"""Find the closing brace that matches the first opening brace from start_line."""
depth = 0
found_open = False
for line_idx in range(start_line, min(start_line + max_scan, len(lines))):
for _, ch, in_string in scan_code(lines[line_idx]):
if in_string:
continue
if ch == "{":
depth += 1
found_open = True
elif ch == "}":
depth -= 1
if found_open and depth == 0:
return line_idx
return None
Remove the import of track_brace_body as _track_brace_body_impl from _smell_effects.
Add at the top of the file (after existing imports):
python
import os
Add the following functions (adapted from _smell_effects.py with DI parameters removed):
_detect_error_no_throw (lines 22-51 of _smell_effects.py, plus the module-level constants _ERROR_HANDLER_BASENAMES, _PRECEDING_SKIP_PATTERNS, _HANDLED_RE)
_detect_empty_if_chains (use the version from smells.py lines 344-408, which already uses direct calls)
_detect_dead_useeffects (use the version from smells.py lines 411-464, which already uses direct calls)
_detect_swallowed_errors (lines 204-263 of _smell_effects.py, replacing scan_code_fn with scan_code and strip_ts_comments_fn with _strip_ts_comments)
Also move _detect_async_no_await from _smell_helpers.py (lines 79-128) into _smell_detectors.py.
Update _smell_detectors.py module docstring from "dead functions" to "stub functions".
Test: python -m pytest desloppify/ -q (full test suite to catch any straggling references)
Step 8: Clean up build/ directory (optional)
The build/lib/ directory has stale copies. Either delete the entire build/ directory or rebuild. This is not strictly part of the refactoring but avoids confusion.
Summary of Changes Per File
File
Action
Lines Before
Lines After (est.)
_smell_helpers.py
Inline _track_brace_body, add _build_ts_line_state, remove all detectors and facade imports
198
~220
_smell_detectors.py
Add 5 detectors + constants from _smell_effects.py and _smell_helpers.py, update docstring
/Users/user_c042661f/Documents/desloppify/desloppify/languages/typescript/detectors/_smell_detectors.py - Receives all detector functions; becomes the single source for all smell detection logic
/Users/user_c042661f/Documents/desloppify/desloppify/languages/typescript/detectors/smells.py - Remove duplicate detectors and _build_ts_line_state, update imports to pull from _smell_detectors
/Users/user_c042661f/Documents/desloppify/desloppify/languages/typescript/detectors/_smell_helpers.py - Inline _track_brace_body, receive _build_ts_line_state, strip all detector functions to become pure utilities
/Users/user_c042661f/Documents/desloppify/desloppify/languages/typescript/detectors/_smell_effects.py - Delete entirely after merging contents into _smell_detectors.py
/Users/user_c042661f/Documents/desloppify/desloppify/languages/typescript/tests/smells/test_ts_smell_helpers.py - Update import sources for all detector functions