Skip to content

Commit a13a037

Browse files
authored
Fix macros (#13)
1 parent 7aea0f3 commit a13a037

File tree

5 files changed

+104
-19
lines changed

5 files changed

+104
-19
lines changed

Sources/StateStructMacros/COWTrackingPropertyMacro.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,19 @@ extension COWTrackingPropertyMacro: PeerMacro {
4141
}
4242
}
4343

44-
var _variableDecl = variableDecl
44+
var _variableDecl = variableDecl.trimmed
4545
_variableDecl.attributes = [.init(.init(stringLiteral: "@TrackingIgnored"))]
4646

4747
_variableDecl = _variableDecl
4848
.renamingIdentifier(with: "_backing_")
49-
.withPrivateModifier()
5049
.modifyingTypeAnnotation({ type in
5150
return "_BackingStorage<\(type.trimmed)>"
5251
})
5352
.modifyingInit({ initializer in
5453
return .init(value: "_BackingStorage.init(\(initializer.value))" as ExprSyntax)
5554
})
5655

57-
_variableDecl.leadingTrivia = .spaces(2)
58-
59-
newMembers.append(_variableDecl.trimmed.formatted(using: .init(indentationWidth: .spaces(2), initialIndentation: [])).as(DeclSyntax.self)!)
56+
newMembers.append(DeclSyntax(_variableDecl))
6057

6158
return newMembers
6259
}

Sources/StateStructMacros/TrackingMacro.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ extension TrackingMacro: MemberMacro {
2323
in context: some MacroExpansionContext
2424
) throws -> [DeclSyntax] {
2525

26+
let isPublic = declaration.modifiers.contains(where: { $0.name.tokenKind == .keyword(.public) })
27+
2628
return [
2729
"""
28-
let _tracking_context: _TrackingContext = .init()
30+
\(raw: isPublic ? "public" : "internal") let _tracking_context: _TrackingContext = .init()
2931
""" as DeclSyntax
3032
]
3133
}
@@ -76,15 +78,6 @@ extension TrackingMacro: ExtensionMacro {
7678
extension \(structDecl.name.trimmed): TrackingObject {
7779
}
7880
""" as DeclSyntax).cast(ExtensionDeclSyntax.self),
79-
// ("""
80-
// extension \(structDecl.name.trimmed) {
81-
//
82-
// func _tracking_propagate(path: PropertyPath) {
83-
// _tracking_context.path = path
84-
// \(raw: operation)
85-
// }
86-
// }
87-
// """ as DeclSyntax).formatted().cast(ExtensionDeclSyntax.self)
8881
]
8982
}
9083
}

Tests/StateStructMacroTests/COWTrackingProperyMacroTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ final class COWTrackingProperyMacroTests: XCTestCase {
2727
@COWTrackingProperty
2828
private var stored_0: Int = 18
2929
30+
@COWTrackingProperty
31+
public var stored_1: Int = 18
32+
3033
func compute() {
3134
}
3235
}
@@ -69,6 +72,40 @@ final class COWTrackingProperyMacroTests: XCTestCase {
6972
}
7073
private var _backing_stored_0: _BackingStorage<Int> = _BackingStorage.init(18)
7174
75+
76+
public var stored_1: Int {
77+
_read {
78+
(_backing_stored_1.value as? TrackingObject)?._tracking_context.path = _tracking_context.path?.pushed(.init("stored_1"))
79+
_Tracking._tracking_modifyStorage {
80+
$0.accessorRead(path: _tracking_context.path?.pushed(.init("stored_1")))
81+
}
82+
yield _backing_stored_1.value
83+
}
84+
set {
85+
(_backing_stored_1.value as? TrackingObject)?._tracking_context.path = _tracking_context.path?.pushed(.init("stored_1"))
86+
_Tracking._tracking_modifyStorage {
87+
$0.accessorSet(path: _tracking_context.path?.pushed(.init("stored_1")))
88+
}
89+
if !isKnownUniquelyReferenced(&_backing_stored_1) {
90+
_backing_stored_1 = .init(newValue)
91+
} else {
92+
_backing_stored_1.value = newValue
93+
}
94+
95+
}
96+
_modify {
97+
(_backing_stored_1.value as? TrackingObject)?._tracking_context.path = _tracking_context.path?.pushed(.init("stored_1"))
98+
_Tracking._tracking_modifyStorage {
99+
$0.accessorModify(path: _tracking_context.path?.pushed(.init("stored_1")))
100+
}
101+
if !isKnownUniquelyReferenced(&_backing_stored_1) {
102+
_backing_stored_1 = .init(_backing_stored_1.value)
103+
}
104+
yield &_backing_stored_1.value
105+
}
106+
}
107+
public var _backing_stored_1: _BackingStorage<Int> = _BackingStorage.init(18)
108+
72109
func compute() {
73110
}
74111
}

Tests/StateStructMacroTests/TrackingMacroTests.swift

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,66 @@ final class TrackingMacroTests: XCTestCase {
1414
super.invokeTest()
1515
}
1616
}
17+
18+
func test_public() {
19+
assertMacro {
20+
"""
21+
@Tracking
22+
public struct MyState {
23+
24+
private var stored_0: Int = 18
25+
26+
var stored_1: String
27+
28+
let stored_2: Int = 0
29+
30+
var age: Int { 0 }
31+
32+
var age2: Int {
33+
get { 0 }
34+
set { }
35+
}
36+
37+
var height: Int
38+
39+
func compute() {
40+
}
41+
}
42+
"""
43+
} expansion: {
44+
"""
45+
public struct MyState {
46+
@COWTrackingProperty
47+
48+
private var stored_0: Int = 18
49+
@COWTrackingProperty
50+
51+
var stored_1: String
52+
@COWTrackingProperty
53+
54+
let stored_2: Int = 0
55+
56+
var age: Int { 0 }
57+
58+
var age2: Int {
59+
get { 0 }
60+
set { }
61+
}
62+
@COWTrackingProperty
63+
64+
var height: Int
65+
66+
func compute() {
67+
}
68+
69+
public let _tracking_context: _TrackingContext = .init()
70+
}
71+
72+
extension MyState: TrackingObject {
73+
}
74+
"""
75+
}
76+
}
1777

1878
func test_macro() {
1979

@@ -67,7 +127,7 @@ final class TrackingMacroTests: XCTestCase {
67127
func compute() {
68128
}
69129
70-
let _tracking_context: _TrackingContext = .init()
130+
internal let _tracking_context: _TrackingContext = .init()
71131
}
72132
73133
extension MyState: TrackingObject {

Tests/StateStructTests/TrackingTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ struct TrackingTests {
250250

251251
#expect(
252252
result.graph.prettyPrint() == """
253-
StateStructTests.Nesting {
254-
_1-(1)
255-
}
253+
StateStructTests.Nesting
256254
"""
257255
)
258256

0 commit comments

Comments
 (0)