Skip to content

Fix nested class boundaries truncating outer class scope (#1040) - #1044

Merged
squid-protocol merged 4 commits into
mainfrom
fix/1040-nested-class-boundaries
Aug 5, 2026
Merged

squid-protocol merged 4 commits into
mainfrom
fix/1040-nested-class-boundaries

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

  • 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 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.
  • Fixes the method-linking loop to assign each function to its innermost enclosing class only, so a nested class's methods aren't double-counted into every enclosing outer class too now that scopes correctly nest.

Two bugs found and fixed along the way (both required by this change)

  • class_pattern's leading \s* can swallow a blank line sitting before class, landing match.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.
  • Sharing the string/comment shielding logic between _slice_by_braces and the new class-scope resolver (_build_brace_safe_stream) surfaced a latent bug: func_start was matched against lexed_code (pre-macro-shield) instead of safe_code (post-macro-shield) -- so dead #if/#else branch 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'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. 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

  • Two new regression tests: nested classes for both brace-style (C-family) and indentation-style (Python) languages, verifying the outer class keeps every method declared after the nested class, and the nested class's methods aren't double-counted into the outer class.
  • python -m pytest tests/ -- 5437 passed, no regressions
  • python tests/tools/crucible_check.py -- zero diff against the (regenerated) golden-master corpus, both full_precision and zero_dependency
  • python tests/tools/audit_check.py -- ruff baseline regenerated (pure line-shifts + the macro-shield fix's own new findings reviewed), mypy/dead-key clean
  • Manually verified Godot's object.h against the real file: _instance_binding_reference etc. are genuinely real, correctly-sized inline methods, confirming the new output is correct and the old baseline was wrong

Closes #1040.

🤖 Generated with Claude Code

squid-protocol and others added 2 commits August 4, 2026 20:07
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
Comment thread gitgalaxy/core/detector.py Fixed
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

squid-protocol and others added 2 commits August 4, 2026 20:45
…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]>
@squid-protocol
squid-protocol merged commit 5fbb50e into main Aug 5, 2026
28 checks passed
@squid-protocol
squid-protocol deleted the fix/1040-nested-class-boundaries branch August 5, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(core): Nested class boundaries truncate outer class scope

2 participants