Repository navigation
Fix nested class boundaries truncating outer class scope (#1040) - #1044
Merged
Merged
Conversation
The class-scoping logic assigned each class's scope as "from its declaration to the *next* class declaration's start" -- so a nested class declared inside another one truncated the outer class's scope right where the nested class began, silently dropping every method declared after it from the outer class's method_count/state_entanglement/lcom_score. Resolves each class's real end via brace-depth (or, for indentation-scoped languages, dedent-depth) tracking instead -- the same Mode B/Mode C dispatch this engine already uses for function slicing. Also fixes the method-linking loop to assign each function to its innermost enclosing class only (nesting-aware scopes mean an outer class's span now correctly contains everything a nested class's span does too, so without this a nested class's methods would double-count into every enclosing class). Two bugs found and fixed along the way, both required by this change: - class_pattern's regex is anchored on match.start(0), but its leading `\s*` can itself swallow a blank line sitting before "class", landing match.start(0) on that blank line instead of the declaration's real line -- which corrupted the new indent-depth math. Anchored on the class NAME's own position (match.start(1)) instead, which always sits on the class's real line regardless of leading whitespace/blank lines. - Extracting the string/comment shielding logic out of _slice_by_braces into a reusable helper (_build_brace_safe_stream, needed so the new class-scope resolver and _slice_by_braces share one shielding implementation instead of duplicating ~60 lines of regex) surfaced a latent bug: func_start was being matched against `lexed_code` (the pre-macro-shield string) instead of `safe_code` (the post-macro-shield string) -- so dead #if/#else branch shielding for C-family languages was silently a no-op, contrary to the #814/#815 comment's own stated intent to match against the shielded stream. Fixed to match the documented intent. Only one file across the ~80-repo golden-master corpus exercises this (Godot's object.h, heavy `#ifdef TOOLS_ENABLED`/`#ifdef DEBUG_ENABLED` usage) -- previously reported as one bogus 464-line "initialize_class" function; now correctly extracts ~20 real, properly named, correctly-sized methods. Golden master fixtures regenerated via update_golden_master.py to bless this as the corrected baseline. Also extracted _build_indentation_safe_stream out of _slice_by_indentation for the same reuse reason (behavior-preserving there -- no equivalent second bug found on that path). Adds regression tests for both brace-style (C-family) and indentation- style (Python) nested classes, verifying the outer class keeps every method declared after the nested class, and that the nested class's own method isn't double-counted into the outer class too. Closes #1040. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…-boundaries # Conflicts: # tests/ruff_audit_baseline.json
Contributor
…lized local variable' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Joe Esquibel <[email protected]>
The CodeQL "Potentially uninitialized local variable" autofix hoisted class_safe_stream/use_indentation_scoping's computation out of the "if class_matches:" block (correctly -- they're referenced unconditionally in the loop below, so gating their computation on class_matches being non-empty was never actually safe) but left the now-empty "if class_matches:" line behind with only comments as its body, which is invalid Python syntax. Removes the vestigial "if", keeping the explanatory comment as a plain block comment above the hoisted computation. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
method_count/state_entanglement/lcom_score.Two bugs found and fixed along the way (both required by this change)
class_pattern's leading\s*can swallow a blank line sitting beforeclass, landingmatch.start(0)on that blank line instead of the declaration's real line -- corrupting the new indent-depth math. Anchored on the class NAME's own position (match.start(1)) instead, which is reliable regardless of leading whitespace._slice_by_bracesand the new class-scope resolver (_build_brace_safe_stream) surfaced a latent bug:func_startwas matched againstlexed_code(pre-macro-shield) instead ofsafe_code(post-macro-shield) -- so dead#if/#elsebranch shielding for C-family languages was silently a no-op, contrary to the Extraction hardening: javascript #814/Extraction hardening: typescript #815 comment's own documented intent. Fixed to match that intent. Only one file across the ~80-repo golden-master corpus exercises this (Godot'sobject.h, heavy#ifdef TOOLS_ENABLED/#ifdef DEBUG_ENABLEDusage) -- previously reported as one bogus 464-lineinitialize_classfunction; now correctly extracts ~20 real, properly named, correctly-sized methods. Verified against the actual file in the crucible corpus before blessing the new baseline.Golden master fixtures regenerated via
update_golden_master.py(both full-precision and zero-dependency) to bless this as the corrected baseline.Test plan
python -m pytest tests/-- 5437 passed, no regressionspython tests/tools/crucible_check.py-- zero diff against the (regenerated) golden-master corpus, both full_precision and zero_dependencypython tests/tools/audit_check.py-- ruff baseline regenerated (pure line-shifts + the macro-shield fix's own new findings reviewed), mypy/dead-key cleanobject.hagainst the real file:_instance_binding_referenceetc. are genuinely real, correctly-sized inline methods, confirming the new output is correct and the old baseline was wrongCloses #1040.
🤖 Generated with Claude Code