Skip to content

Conversation

@Kyle-Ye
Copy link
Collaborator

@Kyle-Ye Kyle-Ye commented Sep 30, 2025

No description provided.

Introduce a new OpenSwiftUIMacros executable target and accompanying test target, and register them in Package.swift. Add a Macro folder in OpenSwiftUI with EntryMacro and __EntryDefaultValue macro declarations that reference the new OpenSwiftUIMacros module. Implement the macros in Sources/OpenSwiftUIMacros (EntryMacro, EntryDefaultValueMacro, and plugin entry point) to generate accessor and peer key types for @entry and to provide a getter-only expansion for @__EntryDefaultValue. Add comprehensive tests (OpenSwiftUIMacrosTests) to verify macro expansions for string and integer defaults.
The generated EnvironmentKey structs were emitted outside the EnvironmentValues
extension, which did not match expected structure. Move the private key
types into the extension and adjust their defaultValue to computed
properties (with getter) to satisfy the macro/test expectations. This
ensures myCustomValue and intValue keys are scoped correctly and tests
reflect the updated shape.
Add tests for two new cases: inferred type ("@entry var inferType = 0") and optional type ("@entry var optionalType: Int?"). Update the macro implementation to accept bindings that have either a type annotation or an initializer, infer simple literal types from initializers (Int, Double, String, Bool), and provide a default nil value for optional type annotations. This fixes failing tests by allowing @entry to work with type inference and Optional default nil semantics, and by returning clear errors for unsupported cases.
Add support for type inference when using @entry with simple initializer calls like CustomType(). The macro previously only inferred basic literals (String/Bool) and rejected other expressions; this caused tests like adding a CustomType struct with @entry var inferCustomType = CustomType() to fail. Now the macro recognizes FunctionCallExpr with a DeclReferenceExpr callee and uses the referenced identifier as the inferred type.

Also add a unit test (testEntryMacroWithCustomTypeInference) that verifies expansion for the CustomType example, and update the error message for complex expressions to instruct users to provide explicit types when necessary.
Improve @entry macro handling for function-call initializers by validating and heuristically inferring types for different call forms. Add a new test to assert behavior for member function calls (A.b()) and emit a clear diagnostic when the macro cannot reliably infer a return type. This change also tightens error messages for complex expressions and ensures optional/member call cases either get a sensible heuristic or require an explicit type annotation so tests reflect the macro's limitations.
…ault value

Refine Entry macro expansion to stop attempting fragile, case-by-case type inference from initializers (especially member function calls). Instead: always preserve the initializer expression as the defaultValue when present, handle the "only type annotation" case by producing nil for optional types or emitting an error for non-optional types, and emit the generated PatternBinding correctly. This fixes an incorrect emitted type like "static var defaultValue: B = A.B()" and updates tests to assert the expanded form (ignore @__EntryDefaultValue for now) and add a positive test for A.B() returning C.
Add a dedicated testEntryMacros map and several new assertMacroExpansion cases to follow the testEntryMacroExpansion example. These additions cover a basic @entry expansion with a custom default value, and a case ensuring member function call inference works when assigning A.B() as a default. This change was needed to extend test coverage for EntryMacro type inference and annotation generation and to isolate the Entry macro mapping for these tests.
@Kyle-Ye Kyle-Ye requested a review from Copilot September 30, 2025 14:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the @Entry macro in OpenSwiftUI, which provides a convenient way to define environment values with automatic key generation. The macro transforms annotated properties into computed properties that access environment keys, similar to SwiftUI's environment system.

  • Implements EntryMacro and EntryDefaultValueMacro for transforming environment value declarations
  • Adds comprehensive test coverage for various type scenarios and edge cases
  • Integrates macro infrastructure into the package structure with proper dependencies

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
Sources/OpenSwiftUIMacros/EntryMacro.swift Core macro implementation that generates accessors and peer key structs
Sources/OpenSwiftUIMacros/EntryDefaultValueMacro.swift Helper macro for generating computed property accessors from stored properties
Sources/OpenSwiftUIMacros/OpenSwiftUIMacrosPlugin.swift Macro plugin registration for compiler integration
Sources/OpenSwiftUICore/Macro/EntryMacro.swift Public macro declarations with external macro references
Tests/OpenSwiftUIMacrosTests/EntryMacroTests.swift Comprehensive test suite covering various macro expansion scenarios
Package.swift Package configuration updates to include macro targets and dependencies
CLAUDE.md Documentation update noting XCTest requirement for macro tests

""",
macros: testMacros
)
assertMacroExpansion(
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

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

Test functions should not include 'test' prefix according to coding guidelines. The function should be named 'entryMacroExpansion' instead of 'testEntryMacroExpansion'.

Copilot generated this review using guidance from repository custom instructions.
@codecov
Copy link

codecov bot commented Sep 30, 2025

Codecov Report

❌ Patch coverage is 0% with 107 lines in your changes missing coverage. Please review.
✅ Project coverage is 24.15%. Comparing base (8e2c48d) to head (230b4e1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
Sources/OpenSwiftUIMacros/EntryMacro.swift 0.00% 88 Missing ⚠️
...ces/OpenSwiftUIMacros/EntryDefaultValueMacro.swift 0.00% 15 Missing ⚠️
...es/OpenSwiftUIMacros/OpenSwiftUIMacrosPlugin.swift 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #520      +/-   ##
==========================================
- Coverage   24.23%   24.15%   -0.08%     
==========================================
  Files         496      499       +3     
  Lines       28519    28626     +107     
==========================================
+ Hits         6912     6916       +4     
- Misses      21607    21710     +103     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Kyle-Ye Kyle-Ye merged commit a723a34 into main Oct 1, 2025
10 of 11 checks passed
@Kyle-Ye Kyle-Ye deleted the feature/entry_macro branch October 1, 2025 02:13
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