Skip to content

Commit 919bcb7

Browse files
committed
Inline property decorators everywhere
1 parent 2f238c5 commit 919bcb7

File tree

7 files changed

+424
-812
lines changed

7 files changed

+424
-812
lines changed

Sources/SafeDI/Decorators/Instantiated.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
///
2323
/// An example of the macro in use:
2424
///
25-
/// @Instantiated
26-
/// private let dependency: DependencyType
25+
/// @Instantiated private let dependency: DependencyType
2726
///
2827
/// Note that the access level of the dependency in the above example does not affect the dependency tree – a `private` dependency can still be `@Received` by `@Instantiable`-decorated types further down the dependency tree.
2928
@attached(peer)

Tests/SafeDICoreTests/FileVisitorTests.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ final class FileVisitorTests: XCTestCase {
3636
fatalError("SafeDI doesn't inspect the initializer body")
3737
}
3838
39-
@Forwarded
40-
private let user: User
39+
@Forwarded private let user: User
4140
42-
@Received
43-
let networkService: NetworkService
41+
@Received let networkService: NetworkService
4442
}
4543
"""))
4644
XCTAssertEqual(
@@ -95,11 +93,9 @@ final class FileVisitorTests: XCTestCase {
9593
fatalError("SafeDI doesn't inspect the initializer body")
9694
}
9795
98-
@Forwarded
99-
private let user: User
96+
@Forwarded private let user: User
10097
101-
@Received
102-
let networkService: NetworkService
98+
@Received let networkService: NetworkService
10399
}
104100
105101
@Instantiable

Tests/SafeDIMacrosTests/InjectableMacroTests.swift

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ import SafeDICore
5353
assertMacro {
5454
"""
5555
public struct ExampleService {
56-
@Instantiated(fulfilledByType: "SomethingElse")
57-
let something: Something
56+
@Instantiated(fulfilledByType: "SomethingElse") let something: Something
5857
}
5958
"""
6059
} expansion: {
@@ -74,8 +73,7 @@ import SafeDICore
7473
self.instantiatedA = instantiatedA
7574
}
7675
77-
@Instantiated(fulfilledByType: "Module.ConcreteType")
78-
let instantiatedA: InstantiatedA
76+
@Instantiated(fulfilledByType: "Module.ConcreteType") let instantiatedA: InstantiatedA
7977
}
8078
"""
8179
} expansion: {
@@ -84,6 +82,7 @@ import SafeDICore
8482
init(instantiatedA: InstantiatedA) {
8583
self.instantiatedA = instantiatedA
8684
}
85+
8786
let instantiatedA: InstantiatedA
8887
}
8988
"""
@@ -100,8 +99,7 @@ import SafeDICore
10099
self.instantiatedA = instantiatedA
101100
}
102101
103-
@Instantiated
104-
var instantiatedA: InstantiatedA
102+
@Instantiated var instantiatedA: InstantiatedA
105103
}
106104
"""
107105
} diagnostics: {
@@ -111,11 +109,10 @@ import SafeDICore
111109
self.instantiatedA = instantiatedA
112110
}
113111
114-
@Instantiated
115-
var instantiatedA: InstantiatedA
116-
┬──
117-
╰─ 🛑 Dependency can not be mutable unless it is decorated with a property wrapper. Mutations to a dependency are not propagated through the dependency tree.
118-
✏️ Replace `var` with `let`
112+
@Instantiated var instantiatedA: InstantiatedA
113+
┬──
114+
╰─ 🛑 Dependency can not be mutable unless it is decorated with a property wrapper. Mutations to a dependency are not propagated through the dependency tree.
115+
✏️ Replace `var` with `let`
119116
}
120117
"""
121118
} fixes: {
@@ -125,7 +122,7 @@ import SafeDICore
125122
self.instantiatedA = instantiatedA
126123
}
127124
128-
@Instantiated let instantiatedA: InstantiatedA
125+
@Instantiated let instantiatedA: InstantiatedA
129126
}
130127
"""
131128
} expansion: {
@@ -152,8 +149,7 @@ import SafeDICore
152149
}
153150
154151
@ObservedObject
155-
@Instantiated
156-
var instantiatedA: InstantiatedA
152+
@Instantiated var instantiatedA: InstantiatedA
157153
158154
var body: some View {
159155
Text("\\(ObjectIdentifier(instantiatedA))")
@@ -186,17 +182,15 @@ import SafeDICore
186182
assertMacro {
187183
"""
188184
public struct ExampleService {
189-
@Instantiated(fulfilledByType: "LoginViewController")
190-
let loginViewControllerBuilder: Instantiator<UIViewController>
185+
@Instantiated(fulfilledByType: "LoginViewController") let loginViewControllerBuilder: Instantiator<UIViewController>
191186
}
192187
"""
193188
} diagnostics: {
194189
"""
195190
public struct ExampleService {
196-
@Instantiated(fulfilledByType: "LoginViewController")
191+
@Instantiated(fulfilledByType: "LoginViewController") let loginViewControllerBuilder: Instantiator<UIViewController>
197192
┬────────────────────────────────────────────────────
198193
╰─ 🛑 The argument `fulfilledByType` can not be used on an `Instantiator` or `SendableInstantiator`. Use an `ErasedInstantiator` or `SendableErasedInstantiator` instead
199-
let loginViewControllerBuilder: Instantiator<UIViewController>
200194
}
201195
"""
202196
}
@@ -206,17 +200,15 @@ import SafeDICore
206200
assertMacro {
207201
"""
208202
public struct ExampleService {
209-
@Instantiated
210-
let loginViewControllerBuilder: ErasedInstantiator<UIViewController>
203+
@Instantiated let loginViewControllerBuilder: ErasedInstantiator<UIViewController>
211204
}
212205
"""
213206
} diagnostics: {
214207
"""
215208
public struct ExampleService {
216-
@Instantiated
209+
@Instantiated let loginViewControllerBuilder: ErasedInstantiator<UIViewController>
217210
┬────────────
218211
╰─ 🛑 `ErasedInstantiator` and `SendableErasedInstantiator` require use of the argument `fulfilledByType`
219-
let loginViewControllerBuilder: ErasedInstantiator<UIViewController>
220212
}
221213
"""
222214
}
@@ -230,8 +222,7 @@ import SafeDICore
230222
self.instantiatedA = instantiatedA
231223
}
232224
233-
@Received
234-
static let instantiatedA: InstantiatedA
225+
@Received static let instantiatedA: InstantiatedA
235226
}
236227
"""
237228
} diagnostics: {
@@ -241,10 +232,9 @@ import SafeDICore
241232
self.instantiatedA = instantiatedA
242233
}
243234
244-
@Received
235+
@Received static let instantiatedA: InstantiatedA
245236
┬────────
246237
╰─ 🛑 This macro can not decorate `static` variables
247-
static let instantiatedA: InstantiatedA
248238
}
249239
"""
250240
}
@@ -275,8 +265,7 @@ import SafeDICore
275265
}
276266
277267
static let fulfilledByType = "ConcreteType"
278-
@Instantiated(fulfilledByType: fulfilledByType)
279-
let instantiatedA: InstantiatedA
268+
@Instantiated(fulfilledByType: fulfilledByType) let instantiatedA: InstantiatedA
280269
}
281270
"""
282271
} diagnostics: {
@@ -287,10 +276,9 @@ import SafeDICore
287276
}
288277
289278
static let fulfilledByType = "ConcreteType"
290-
@Instantiated(fulfilledByType: fulfilledByType)
279+
@Instantiated(fulfilledByType: fulfilledByType) let instantiatedA: InstantiatedA
291280
┬──────────────────────────────────────────────
292281
╰─ 🛑 The argument `fulfilledByType` must be a string literal
293-
let instantiatedA: InstantiatedA
294282
}
295283
"""
296284
}
@@ -305,8 +293,7 @@ import SafeDICore
305293
}
306294
307295
static let fulfilledByType = "ConcreteType"
308-
@Instantiated(fulfilledByType: "\\(Self.fulfilledByType)")
309-
let instantiatedA: InstantiatedA
296+
@Instantiated(fulfilledByType: "\\(Self.fulfilledByType)") let instantiatedA: InstantiatedA
310297
}
311298
"""
312299
} diagnostics: {
@@ -317,10 +304,9 @@ import SafeDICore
317304
}
318305
319306
static let fulfilledByType = "ConcreteType"
320-
@Instantiated(fulfilledByType: "\(Self.fulfilledByType)")
307+
@Instantiated(fulfilledByType: "\(Self.fulfilledByType)") let instantiatedA: InstantiatedA
321308
┬────────────────────────────────────────────────────────
322309
╰─ 🛑 The argument `fulfilledByType` must be a string literal
323-
let instantiatedA: InstantiatedA
324310
}
325311
"""#
326312
}
@@ -334,8 +320,7 @@ import SafeDICore
334320
self.instantiatedA = instantiatedA
335321
}
336322
337-
@Instantiated(fulfilledByType: "ConcreteType?")
338-
let instantiatedA: InstantiatedA
323+
@Instantiated(fulfilledByType: "ConcreteType?") let instantiatedA: InstantiatedA
339324
}
340325
"""
341326
} diagnostics: {
@@ -345,10 +330,9 @@ import SafeDICore
345330
self.instantiatedA = instantiatedA
346331
}
347332
348-
@Instantiated(fulfilledByType: "ConcreteType?")
333+
@Instantiated(fulfilledByType: "ConcreteType?") let instantiatedA: InstantiatedA
349334
┬──────────────────────────────────────────────
350335
╰─ 🛑 The argument `fulfilledByType` must refer to a simple type
351-
let instantiatedA: InstantiatedA
352336
}
353337
"""
354338
}
@@ -362,8 +346,7 @@ import SafeDICore
362346
self.instantiatedA = instantiatedA
363347
}
364348
365-
@Instantiated(fulfilledByType: "ConcreteType!")
366-
let instantiatedA: InstantiatedA
349+
@Instantiated(fulfilledByType: "ConcreteType!") let instantiatedA: InstantiatedA
367350
}
368351
"""
369352
} diagnostics: {
@@ -373,10 +356,9 @@ import SafeDICore
373356
self.instantiatedA = instantiatedA
374357
}
375358
376-
@Instantiated(fulfilledByType: "ConcreteType!")
359+
@Instantiated(fulfilledByType: "ConcreteType!") let instantiatedA: InstantiatedA
377360
┬──────────────────────────────────────────────
378361
╰─ 🛑 The argument `fulfilledByType` must refer to a simple type
379-
let instantiatedA: InstantiatedA
380362
}
381363
"""
382364
}
@@ -388,19 +370,17 @@ import SafeDICore
388370
@Instantiable
389371
public struct ExampleService {
390372
static let instantiatedAName: StaticString = "instantiatedA"
391-
@Received(fulfilledByDependencyNamed: instantiatedAName, ofType: InstantiatedA.self)
392-
let receivedA: ReceivedA
373+
@Received(fulfilledByDependencyNamed: instantiatedAName, ofType: InstantiatedA.self) let receivedA: ReceivedA
393374
}
394375
"""
395376
} diagnostics: {
396377
"""
397378
@Instantiable
398379
public struct ExampleService {
399380
static let instantiatedAName: StaticString = "instantiatedA"
400-
@Received(fulfilledByDependencyNamed: instantiatedAName, ofType: InstantiatedA.self)
381+
@Received(fulfilledByDependencyNamed: instantiatedAName, ofType: InstantiatedA.self) let receivedA: ReceivedA
401382
┬───────────────────────────────────────────────────────────────────────────────────
402383
╰─ 🛑 The argument `fulfilledByDependencyNamed` must be a string literal
403-
let receivedA: ReceivedA
404384
}
405385
"""
406386
}
@@ -411,18 +391,16 @@ import SafeDICore
411391
"""
412392
@Instantiable
413393
public struct ExampleService {
414-
@Received(fulfilledByDependencyNamed: "instantiatedA", ofType: "InstantiatedA")
415-
let receivedA: ReceivedA
394+
@Received(fulfilledByDependencyNamed: "instantiatedA", ofType: "InstantiatedA") let receivedA: ReceivedA
416395
}
417396
"""
418397
} diagnostics: {
419398
"""
420399
@Instantiable
421400
public struct ExampleService {
422-
@Received(fulfilledByDependencyNamed: "instantiatedA", ofType: "InstantiatedA")
401+
@Received(fulfilledByDependencyNamed: "instantiatedA", ofType: "InstantiatedA") let receivedA: ReceivedA
423402
┬──────────────────────────────────────────────────────────────────────────────
424403
╰─ 🛑 The argument `ofType` must be a type literal
425-
let receivedA: ReceivedA
426404
}
427405
"""
428406
}
@@ -434,26 +412,17 @@ import SafeDICore
434412
@Instantiable
435413
public struct ExampleService {
436414
static let erasedToConcreteExistential = true
437-
@Received(
438-
fulfilledByDependencyNamed: "receivedA",
439-
ofType: ReceivedA.self,
440-
erasedToConcreteExistential: erasedToConcreteExistential
441-
)
442-
let receivedA: AnyReceivedA
415+
@Received(fulfilledByDependencyNamed: "receivedA", ofType: ReceivedA.self, erasedToConcreteExistential: erasedToConcreteExistential) let receivedA: AnyReceivedA
443416
}
444417
"""
445418
} diagnostics: {
446419
"""
447420
@Instantiable
448421
public struct ExampleService {
449422
static let erasedToConcreteExistential = true
450-
@Received(
423+
@Received(fulfilledByDependencyNamed: "receivedA", ofType: ReceivedA.self, erasedToConcreteExistential: erasedToConcreteExistential) let receivedA: AnyReceivedA
424+
┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
451425
╰─ 🛑 The argument `erasedToConcreteExistential` must be a bool literal
452-
fulfilledByDependencyNamed: "receivedA",
453-
ofType: ReceivedA.self,
454-
erasedToConcreteExistential: erasedToConcreteExistential
455-
)
456-
let receivedA: AnyReceivedA
457426
}
458427
"""
459428
}

0 commit comments

Comments
 (0)