Skip to content

Commit

Permalink
update for swift 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JARMourato committed Jan 31, 2025
1 parent a4f4a7e commit a42da7d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.10
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
Expand All @@ -11,7 +11,7 @@ let package = Package(
.library(name: "Injection", targets: ["Injection"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax", from: "510.0.0"),
.package(url: "https://github.com/apple/swift-syntax", from: "600.0.0-latest"),
],
targets: [
.macro(
Expand Down
4 changes: 2 additions & 2 deletions Sources/Injection/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import SwiftUI

public typealias DependencyKey = EnvironmentKey

public struct DependencyValues {
@preconcurrency public struct DependencyValues {
private var values: [ObjectIdentifier: Any] = [:]

init() {}

@usableFromInline static var shared = DependencyValues()
@usableFromInline nonisolated(unsafe) static var shared = DependencyValues()

public subscript<K>(key: K.Type) -> K.Value where K: DependencyKey {
get { values[ObjectIdentifier(key)] as? K.Value ?? key.defaultValue }
Expand Down
2 changes: 1 addition & 1 deletion Sources/Injection/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/// Creates an unique `DependencyKey` for the variable and adds getters and setters.
/// The initial value of the variable becomes the default value of the `DependencyKey`.
@attached(peer, names: prefixed(___))
@attached(peer, names: prefixed(__Key_))
@attached(accessor, names: named(get), named(set))
public macro DependencyKey() = #externalMacro(module: "InjectionMacros", type: "DependencyKeyMacro")

Expand Down
45 changes: 41 additions & 4 deletions Sources/InjectionMacros/DependencyKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,44 @@ public struct InjectValuesMacro: MemberAttributeMacro {

guard !isStatic else { return [] }

// Note: Implement usage of @Entry for EnvironmentValues for iOS 18
if extensionName == "EnvironmentValues" {
#if os(iOS)
if #available(iOS 18.0, *) {
return [AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: .identifier("Entry"))
)]
}
#elseif os(macOS)
if #available(macOS 15.0, *) {
return [AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: .identifier("Entry"))
)]
}
#elseif os(tvOS)
if #available(tvOS 18.0, *) {
return [AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: .identifier("Entry"))
)]
}
#elseif os(watchOS)
if #available(watchOS 11.0, *) {
return [AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: .identifier("Entry"))
)]
}
#elseif os(visionOS)
if #available(visionOS 2.0, *) {
return [AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: .identifier("Entry"))
)]
}
#endif
}

return [
AttributeSyntax(
Expand Down Expand Up @@ -67,7 +104,7 @@ public struct DependencyKeyMacro: PeerMacro {

return [
"""
private struct ___\(raw: identifier.trimmedDescription): DependencyKey {
private struct __Key_\(raw: identifier.trimmedDescription): DependencyKey {
static let \(binding) \(raw: isOptionalType && !hasDefaultValue ? "= nil" : "")
}
""",
Expand All @@ -88,12 +125,12 @@ extension DependencyKeyMacro: AccessorMacro {
return [
"""
get {
self[___\(raw: identifier.trimmedDescription).self]
self[__Key_\(raw: identifier.trimmedDescription).self]
}
""",
"""
set {
self[___\(raw: identifier.trimmedDescription).self] = newValue
self[__Key_\(raw: identifier.trimmedDescription).self] = newValue
}
""",
]
Expand Down
9 changes: 5 additions & 4 deletions Tests/DependencyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import XCTest
final class DependencyTests: XCTestCase {
func testReadDependency() {
// Given
let assertionValue = 100
DependencyValues.NumberKey.defaultValue = assertionValue
let assertionValue = 10
// When
let readValue = DependencyValues.shared[keyPath: \.number]
// Then
Expand Down Expand Up @@ -37,7 +36,7 @@ final class DependencyTests: XCTestCase {
XCTAssertEqual(value, DependencyValues.NumberKey.defaultValue)
}

func testSwiftUIViewUtilityWriter() {
@MainActor func testSwiftUIViewUtilityWriter() {
// Given
let view = EmptyView()
let assertionValue = 100
Expand All @@ -58,6 +57,8 @@ extension DependencyValues {
}

struct NumberKey: DependencyKey {
static var defaultValue: Int = 10
static var defaultValue: Int {
10
}
}
}
54 changes: 17 additions & 37 deletions Tests/InjectionMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class InjectionMacroTests: XCTestCase {
expandedSource:
"""
extension EnvironmentValues {
@DependencyKey
@Entry
var number: Int = 10
}
""",
Expand All @@ -36,14 +36,14 @@ final class InjectionMacroTests: XCTestCase {
extension EnvironmentValues {
var number: Int {
get {
self [___number.self]
self[__Key_number.self]
}
set {
self [___number.self] = newValue
self[__Key_number.self] = newValue
}
}
private struct ___number: DependencyKey {
private struct __Key_number: DependencyKey {
static let defaultValue: Int = 10
}
}
Expand All @@ -64,30 +64,10 @@ final class InjectionMacroTests: XCTestCase {
expandedSource:
"""
extension EnvironmentValues {
var number: Int {
get {
self [___number.self]
}
set {
self [___number.self] = newValue
}
}
private struct ___number: DependencyKey {
static let defaultValue: Int = 10
}
var text: String {
get {
self [___text.self]
}
set {
self [___text.self] = newValue
}
}
private struct ___text: DependencyKey {
static let defaultValue: String = "Hello"
}
@Entry
var number: Int = 10
@Entry
var text: String = "Hello"
}
""",
macros: [
Expand Down Expand Up @@ -128,14 +108,14 @@ final class InjectionMacroTests: XCTestCase {
extension DependencyValues {
var number: Int {
get {
self [___number.self]
self[__Key_number.self]
}
set {
self [___number.self] = newValue
self[__Key_number.self] = newValue
}
}
private struct ___number: DependencyKey {
private struct __Key_number: DependencyKey {
static let defaultValue: Int = 10
}
}
Expand All @@ -158,26 +138,26 @@ final class InjectionMacroTests: XCTestCase {
extension DependencyValues {
var number: Int {
get {
self [___number.self]
self[__Key_number.self]
}
set {
self [___number.self] = newValue
self[__Key_number.self] = newValue
}
}
private struct ___number: DependencyKey {
private struct __Key_number: DependencyKey {
static let defaultValue: Int = 10
}
var text: String {
get {
self [___text.self]
self[__Key_text.self]
}
set {
self [___text.self] = newValue
self[__Key_text.self] = newValue
}
}
private struct ___text: DependencyKey {
private struct __Key_text: DependencyKey {
static let defaultValue: String = "Hello"
}
}
Expand Down

0 comments on commit a42da7d

Please sign in to comment.