Skip to content

Bugfix/ipconfig skips protocols#346

Open
AnthonyTTaylor wants to merge 3 commits intouber:masterfrom
AnthonyTTaylor:bugfix/ipconfig-skips-protocols
Open

Bugfix/ipconfig skips protocols#346
AnthonyTTaylor wants to merge 3 commits intouber:masterfrom
AnthonyTTaylor:bugfix/ipconfig-skips-protocols

Conversation

@AnthonyTTaylor
Copy link
Copy Markdown

@AnthonyTTaylor AnthonyTTaylor commented Apr 6, 2026

This PR fixes a regression where protocols (and other mockable entities) inside #if blocks were not being discovered for mock generation. The issue was introduced by changes in PR #328 that made the visitor treat all non-fileMacro #if blocks as conditional import
blocks, even when they contained declarations like protocols.

Problem

For non-fileMacro #if blocks, the visitor now always:

  1. Parses the block as a ConditionalImportBlock
  2. Returns .skipChildren

This is correct for import-only blocks, but incorrect for blocks containing declarations. In those cases, protocols, classes, and other declarations are never visited, so mockable entities are not discovered.

Solution
Unified handling of all #if blocks with context preservation:

Added IfConfigContext — A new struct that tracks which #if clause an entity was found in, storing the block offset, clause type (#if/#elseif/#else), and clause index.

Refactored #if block processing — A new processTopLevelIfConfig method iterates through all clauses in an #if block, collecting imports as conditional blocks while also discovering and tagging protocols/classes with their IfConfigContext.

Reconstructed #if directives in output — The template renderer groups entities by their IfConfigContext and wraps generated mocks in the same #if/#elseif/#else/#endif structure as the source protocols.

Only treat #if config blocks as conditional imports when they contain imports exclusively. This preserves existing import parsing while allowing the visitor to traverse blocks that contain @mockable protocols or other declarations.
Copy link
Copy Markdown
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think the containsOnlyImports approach is a bit tricky.
Ideally, I'd like to generate mocks while preserving the structure of the #if directive.

Without #if, isn't it just a coincidence that mock implementations can be used?

@AnthonyTTaylor AnthonyTTaylor force-pushed the bugfix/ipconfig-skips-protocols branch from 900f675 to bd45f5f Compare April 10, 2026 22:18
@AnthonyTTaylor
Copy link
Copy Markdown
Author

AnthonyTTaylor commented Apr 10, 2026

Had to add a missing closing bracket - sorry about the delay.

Hmm, I think the containsOnlyImports approach is a bit tricky.
Ideally, I'd like to generate mocks while preserving the structure of the #if directive.

Without #if, isn't it just a coincidence that mock implementations can be used?

I'll dive in and see about preserving the structure of the #if

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.

2 participants