Skip to content

Support #if/#elseif/#endif conditional compilation in mock generation#597

Draft
DominatorVbN wants to merge 1 commit into
Brightify:masterfrom
DominatorVbN:fix/if-config-support
Draft

Support #if/#elseif/#endif conditional compilation in mock generation#597
DominatorVbN wants to merge 1 commit into
Brightify:masterfrom
DominatorVbN:fix/if-config-support

Conversation

@DominatorVbN

@DominatorVbN DominatorVbN commented Jun 18, 2026

Copy link
Copy Markdown

Summary

Fixes #471 — conditional compilation blocks (#if/#elseif/#else/#endif) in protocols and classes are now correctly handled by the mock generator.

  • Uses SwiftIfConfig (already included in the swift-syntax package dependency) to evaluate #if conditions against a configurable build configuration.
  • Replaces the // TODO: Implement #if functionality. stub in Crawler.visit(_:IfConfigDeclSyntax) with proper active-clause evaluation — only the active branch's members are crawled.
  • Adds a new build_conditions field to Cuckoofile.toml module configuration so users can declare which custom conditions are active during mock generation:
[modules.MyModule]
sources = ["Sources/**/*.swift"]
output = "GeneratedMocks.swift"
build_conditions = ["DEBUG", "MY_FEATURE_FLAG"]
  • Platform/OS/arch/runtime conditions that can't be known at generation time (e.g. os(iOS)) default to false (inactive), matching the safest behaviour — only explicitly declared conditions are active.

Changes

File Change
Package.swift Add SwiftIfConfig product to CuckooGenerator target dependencies
Generator/Project.swift Mirror the same dependency for the Tuist project
Generator/Sources/Internal/Crawlers/CuckooGeneratorBuildConfiguration.swift New — implements BuildConfiguration protocol for use with SwiftIfConfig
Generator/Sources/Internal/Crawlers/Crawler.swift Use SwiftIfConfig to evaluate #if conditions; propagate buildConfiguration through sub-crawlers
Generator/Sources/CLI/Module.swift Add buildConditions: Set<String> property and build_conditions TOML key
Generator/Sources/CLI/Generator.swift Construct CuckooGeneratorBuildConfiguration from module's buildConditions and pass to Crawler.crawl

Test plan

  • Protocol with #if MY_FLAG block: when MY_FLAG is in build_conditions, the flagged methods appear in the generated mock; when absent, they are excluded
  • #elseif and #else branches are resolved to the correct active clause
  • Nested #if blocks inside #if blocks work correctly
  • Platform conditions like #if os(iOS) produce no members by default (not crashing or including both branches)
  • Existing mocks without any #if blocks are unaffected

Uses SwiftIfConfig (part of swift-syntax) to evaluate #if blocks in
crawled source files. Only the active branch is visited, so conditionally
compiled methods and properties are included or excluded correctly.

Users specify active custom conditions via `build_conditions` in
Cuckoofile.toml (e.g. `build_conditions = ["DEBUG", "MY_FEATURE"]`).
Platform/arch/os queries default to false when not explicitly modelled.

Fixes Brightify#471.
@DominatorVbN DominatorVbN marked this pull request as draft June 18, 2026 06:19
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.

Compiler custom flags are not supported by Cuckoo generator

1 participant