Unify #if handling and fix top-level #if regression#5
Draft
fummicc1 wants to merge 1 commit into
Draft
Conversation
Conditional compilation (#if/#elseif/#else) was modeled twice -- IfMacroModel for member-level blocks and ConditionalImportBlock for imports -- with the directive-emitting switch duplicated between IfMacroTemplate and ImportsHandler. A side effect of that split (introduced in uber#328) is a regression: EntityVisitor treated every non-fileMacro top-level #if as import-only and returned .skipChildren, so protocols/classes declared inside a top-level #if were never discovered and no mock was generated. This unifies the structure and fixes the regression: - Add a generic ConditionalBlock<Leaf> and centralize directive emission on IfClauseType.directiveLine(indent:)/endIfLine(indent:), removing the two duplicated switches. - Reshape IfMacroModel as a thin wrapper over ConditionalBlock<(String, Model)>; member-level #if output is byte-identical. - Replace ConditionalImportBlock with ConditionalBlock<ImportContent>. - Parse a top-level #if into a TopLevelConditionalLeaf union (import / entity / nested), discovering declarations and tagging them with their clause. Entities flow through normal resolution (so their members -- including nested member-level #if -- are fully generated), and renderConditionalBlocks re-wraps each generated mock in the original #if/#elseif/#else directives. SwiftIfConfig is intentionally not used: it resolves the active branch for a known configuration, whereas mock generation must preserve every branch. Tests: all 160 existing tests stay green (member-level and import #if behavior unchanged); adds TestConditionalCompilation covering top-level #if protocols, #if/#elseif/#else, mixed import+declaration, nested #if, and class members (incl. a member-level #if) inside a top-level #if. Claude-Session: https://claude.ai/code/session_018WBhuzn7RkfYMWKA9gYF14
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
Conditional compilation (
#if/#elseif/#else) was modeled twice in mockolo:IfMacroModelfor member-level blocks andConditionalImportBlockfor imports,with the directive-emitting
switchduplicated betweenIfMacroTemplateandImportsHandler.A side effect of that split (from uber#328) is a regression:
EntityVisitortreated every non-fileMacro top-level
#ifas import-only and returned.skipChildren, so protocols/classes declared inside a top-level#ifwerenever discovered and no mock was generated.
What this does
ConditionalBlock<Leaf>and movedirective emission to
IfClauseType.directiveLine(indent:)/endIfLine(indent:), deleting the two duplicated switches.IfMacroModelbecomes a thin wrapper overConditionalBlock<(String, Model)>(member-level
#ifoutput is byte-identical).ConditionalBlock<ImportContent>(the oldConditionalImportBlockis removed).#ifis parsed into aTopLevelConditionalLeafunion (import/entity/nested). Declarationsare discovered and tagged with their clause; they flow through normal
resolution (so members — including nested member-level
#if— are fullygenerated), and a new
renderConditionalBlocksre-wraps each generated mock inthe original
#if/#elseif/#elsedirectives.Why not SwiftIfConfig
SwiftIfConfig(BuildConfiguration/removingInactive/ActiveSyntaxVisitor)resolves the active branch for a known configuration and discards inactive
ones. Mock generation must preserve every branch so the generated file
compiles under any configuration, so the raw
IfConfigDeclSyntax.clausestraversal remains the correct tool here.
Tests
#ifbehavior isunchanged.
TestConditionalCompilationcovers: a protocol inside a top-level#if;#if/#elseif/#elseeach declaring a protocol; a mixed import + declarationin one
#if; nested top-level#if; and a class with members (incl. amember-level
#if) inside a top-level#if.Notes
Targets the same regression as uber#346, but takes the unify-the-model route rather
than tagging entities onto the import block.
https://claude.ai/code/session_018WBhuzn7RkfYMWKA9gYF14