Skip to content

Commit e02cec4

Browse files
authored
Public stub adjustment (#15)
* Remove modifier and change it to bool literal instead * Separate Macro to two different class
1 parent 98ccd25 commit e02cec4

8 files changed

+39
-60
lines changed

Sources/SwiftEnvironment/Macros.swift

+6-11
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ public macro EnvironmentValue() = #externalMacro(
1313
)
1414

1515
@attached(member, names: arbitrary)
16-
public macro Stubbed(_ modifier: Modifier = .internalStub) = #externalMacro(
17-
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
16+
public macro Stubbed(public: Bool = false) = #externalMacro(
17+
module: "SwiftEnvironmentMacro", type: "StubFromTypeGeneratorMacro"
1818
)
1919

2020
@attached(member, names: arbitrary)
21-
public macro Stubbed(_ modifier: Modifier = .internalStub, _ values: DefaultType...) = #externalMacro(
22-
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
21+
public macro Stubbed(public: Bool = false, _ values: DefaultType...) = #externalMacro(
22+
module: "SwiftEnvironmentMacro", type: "StubFromTypeGeneratorMacro"
2323
)
2424

2525
@attached(peer, names: suffixed(Stub))
2626
public macro Stubbed(type: StubType) = #externalMacro(
27-
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
27+
module: "SwiftEnvironmentMacro", type: "StubFromProtocolGeneratorMacro"
2828
)
2929

3030
@attached(peer, names: suffixed(Stub))
3131
public macro Stubbed(type: StubType, _ values: DefaultType...) = #externalMacro(
32-
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
32+
module: "SwiftEnvironmentMacro", type: "StubFromProtocolGeneratorMacro"
3333
)
3434

3535
public enum StubType {
@@ -40,11 +40,6 @@ public enum StubType {
4040
case openSubclass(AnyClass.Type)
4141
}
4242

43-
public enum Modifier {
44-
case publicStub
45-
case internalStub
46-
}
47-
4843
public struct DefaultType {
4944
public static func value<T>(for: T.Type, _: T) -> DefaultType {
5045
DefaultType()

Sources/SwiftEnvironmentMacro/EnvironmentValueMacroPlugin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ import SwiftSyntaxMacros
1212
@main
1313
struct EnvironmentValueMacroPlugin: CompilerPlugin {
1414
let providingMacros: [Macro.Type] = [
15-
EnvironmentValueMacro.self, StubGeneratorMacro.self
15+
EnvironmentValueMacro.self, StubFromProtocolGeneratorMacro.self, StubFromTypeGeneratorMacro.self
1616
]
1717
}

Sources/SwiftEnvironmentMacro/Extensions/AttributeSyntax+Extensions.swift

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// File.swift
3-
//
3+
//
44
//
55
// Created by Nayanda Haberty on 21/4/24.
66
//
@@ -9,26 +9,20 @@ import Foundation
99
import SwiftSyntax
1010

1111
extension AttributeSyntax {
12-
var hasModifiers: Bool {
13-
guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?
14-
.first?.as(LabeledExprSyntax.self) else { return false }
15-
return firstArgument.trimmedDescription == ".publicStub"
16-
|| firstArgument.trimmedDescription == ".internalStub"
17-
}
18-
1912
var isPublicStub: Bool {
20-
guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?
21-
.first?.as(LabeledExprSyntax.self) else { return false }
22-
return firstArgument.trimmedDescription == ".publicStub"
13+
guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?.first,
14+
firstArgument.label?.trimmedDescription == "public" else {
15+
return false
16+
}
17+
return firstArgument.expression.trimmedDescription == "true"
2318
}
2419

2520
var defaultValueArguments: [String: String] {
2621
get throws {
2722
let arguments = arguments?.as(LabeledExprListSyntax.self)?
2823
.filter { $0.label == nil }
2924
.compactMap { $0.as(LabeledExprSyntax.self) }
30-
guard var arguments else { return [:] }
31-
arguments = hasModifiers ? Array(arguments.suffix(from: 1)) : arguments
25+
guard let arguments else { return [:] }
3226
return try arguments
3327
.map { try $0.extractDefaultValueArguments() }
3428
.mapToTypeDefaultValueArguments()

Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+PeerMacro.swift renamed to Sources/SwiftEnvironmentMacro/Stubbed/StubFromProtocolGeneratorMacro.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftSyntaxBuilder
1010
import SwiftSyntaxMacros
1111
import SwiftSyntax
1212

13-
extension StubGeneratorMacro: PeerMacro {
13+
struct StubFromProtocolGeneratorMacro: PeerMacro {
1414
public static func expansion(of node: AttributeSyntax, providingPeersOf declaration: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
1515
guard let protocolSyntax = declaration.as(ProtocolDeclSyntax.self) else {
1616
return []

Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+MemberMacro.swift renamed to Sources/SwiftEnvironmentMacro/Stubbed/StubFromTypeGeneratorMacro.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftSyntaxBuilder
1010
import SwiftSyntaxMacros
1111
import SwiftSyntax
1212

13-
extension StubGeneratorMacro: MemberMacro {
13+
public struct StubFromTypeGeneratorMacro: MemberMacro {
1414
public static func expansion(of node: AttributeSyntax, providingMembersOf declaration: some DeclGroupSyntax, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
1515
guard let name = declaration.className ?? declaration.structName else {
1616
return []

Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro.swift

-10
This file was deleted.

Tests/SwiftEnvironmentTests/StubFromProtocolGeneratorMacroTests.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,79 +16,79 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase {
1616
assertMacroExpansion(
1717
simpleProtocol,
1818
expandedSource: simpleProtocolExpansion,
19-
macros: ["Stubbed": StubGeneratorMacro.self]
19+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
2020
)
2121
}
2222

2323
func test_givenOptionalProtocol_shouldGenerateDefault() {
2424
assertMacroExpansion(
2525
optionalProtocol,
2626
expandedSource: optionalProtocolExpansion,
27-
macros: ["Stubbed": StubGeneratorMacro.self]
27+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
2828
)
2929
}
3030

3131
func test_givenArrayProtocol_shouldGenerateDefault() {
3232
assertMacroExpansion(
3333
arrayProtocol,
3434
expandedSource: arrayProtocolExpansion,
35-
macros: ["Stubbed": StubGeneratorMacro.self]
35+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
3636
)
3737
}
3838

3939
func test_givenDictionaryProtocol_shouldGenerateDefault() {
4040
assertMacroExpansion(
4141
dictionaryProtocol,
4242
expandedSource: dictionaryProtocolExpansion,
43-
macros: ["Stubbed": StubGeneratorMacro.self]
43+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
4444
)
4545
}
4646

4747
func test_givenClassProtocol_shouldGenerateDefault() {
4848
assertMacroExpansion(
4949
classProtocol,
5050
expandedSource: classProtocolExpansion,
51-
macros: ["Stubbed": StubGeneratorMacro.self]
51+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
5252
)
5353
}
5454

5555
func test_givenSuperClassProtocol_shouldGenerateDefault() {
5656
assertMacroExpansion(
5757
superClassProtocol,
5858
expandedSource: superClassProtocolExpansion,
59-
macros: ["Stubbed": StubGeneratorMacro.self]
59+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
6060
)
6161
}
6262

6363
func test_givenInjectValueProtocol_shouldGenerateDefault() {
6464
assertMacroExpansion(
6565
injectValueProtocol,
6666
expandedSource: injectValueProtocolExpansion,
67-
macros: ["Stubbed": StubGeneratorMacro.self]
67+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
6868
)
6969
}
7070

7171
func test_givenStructProtocol_shouldGenerateDefault() {
7272
assertMacroExpansion(
7373
structProtocol,
7474
expandedSource: structProtocolExpansion,
75-
macros: ["Stubbed": StubGeneratorMacro.self]
75+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
7676
)
7777
}
7878

7979
func test_givenAllArgsProtocol_shouldGenerateDefault() {
8080
assertMacroExpansion(
8181
allArgsProtocol,
8282
expandedSource: allArgsProtocolExpansion,
83-
macros: ["Stubbed": StubGeneratorMacro.self]
83+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
8484
)
8585
}
8686

8787
func test_givenAnyObjectProtocol_shouldGenerateDefault() {
8888
assertMacroExpansion(
8989
anyObjectProtocol,
9090
expandedSource: anyObjectProtocolExpansion,
91-
macros: ["Stubbed": StubGeneratorMacro.self]
91+
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
9292
)
9393
}
9494
}

Tests/SwiftEnvironmentTests/StubFromTypeGeneratorTests.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,71 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase {
1616
assertMacroExpansion(
1717
simpleStruct,
1818
expandedSource: simpleStructExpansion,
19-
macros: ["Stubbed": StubGeneratorMacro.self]
19+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
2020
)
2121
}
2222

2323
func test_givenSimpleClass_shouldGenerateDefault() {
2424
assertMacroExpansion(
2525
simpleClass,
2626
expandedSource: simpleClassExpansion,
27-
macros: ["Stubbed": StubGeneratorMacro.self]
27+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
2828
)
2929
}
3030

3131
func test_givenSimpleClassWithInit_shouldGenerateDefault() {
3232
assertMacroExpansion(
3333
simpleClassWithInit,
3434
expandedSource: simpleClassWithInitExpansion,
35-
macros: ["Stubbed": StubGeneratorMacro.self]
35+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
3636
)
3737
}
3838

3939
func test_givenSimpleClassWithRandomInit_shouldGenerateDefault() {
4040
assertMacroExpansion(
4141
simpleClassWithRandomInit,
4242
expandedSource: simpleClassWithRandomInitExpansion,
43-
macros: ["Stubbed": StubGeneratorMacro.self]
43+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
4444
)
4545
}
4646

4747
func test_givenSimpleClassWithDifferentInit_shouldGenerateDefault() {
4848
assertMacroExpansion(
4949
simpleClassWithDifferentInit,
5050
expandedSource: simpleClassWithDifferentInitExpansion,
51-
macros: ["Stubbed": StubGeneratorMacro.self]
51+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
5252
)
5353
}
5454

5555
func test_givenSimpleStructWithClosure_shouldGenerateDefault() {
5656
assertMacroExpansion(
5757
simpleStructWithClosure,
5858
expandedSource: simpleStructWithClosureExpansion,
59-
macros: ["Stubbed": StubGeneratorMacro.self]
59+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
6060
)
6161
}
6262

6363
func test_givenSimpleClasstWithClosure_shouldGenerateDefault() {
6464
assertMacroExpansion(
6565
simpleClassWithClosure,
6666
expandedSource: simpleClassWithClosureExpansion,
67-
macros: ["Stubbed": StubGeneratorMacro.self]
67+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
6868
)
6969
}
7070

7171
func test_givenSimpleStructWithOptional_shouldGenerateDefault() {
7272
assertMacroExpansion(
7373
simpleStructWithOptional,
7474
expandedSource: simpleStructWithOptionalExpansion,
75-
macros: ["Stubbed": StubGeneratorMacro.self]
75+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
7676
)
7777
}
7878

7979
func test_givenSimpleStructWithTypeAlias_shouldGenerateDefault() {
8080
assertMacroExpansion(
8181
typeAliasStruct,
8282
expandedSource: typeAliasStructExpansion,
83-
macros: ["Stubbed": StubGeneratorMacro.self]
83+
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
8484
)
8585
}
8686
}
@@ -111,7 +111,7 @@ struct Some {
111111
"""
112112

113113
private let simpleClassWithClosure: String = """
114-
@Stubbed(.publicStub)
114+
@Stubbed(public: true)
115115
class Some {
116116
let voidClosure: () -> Void
117117
let argVoidClosure: (Int) -> Void
@@ -193,7 +193,7 @@ struct Some {
193193
"""
194194

195195
private let simpleClassWithDifferentInit: String = """
196-
@Stubbed(.publicStub)
196+
@Stubbed(public: true)
197197
class Some {
198198
let int: Int
199199
var double: Double
@@ -260,7 +260,7 @@ class Some {
260260
"""
261261

262262
private let simpleClassWithInit: String = """
263-
@Stubbed(.publicStub)
263+
@Stubbed(public: true)
264264
class Some {
265265
let int: Int
266266
var double: Double
@@ -317,7 +317,7 @@ class Some {
317317
"""
318318

319319
private let simpleStruct: String = """
320-
@Stubbed(.publicStub)
320+
@Stubbed(public: true)
321321
struct Some {
322322
let int: Int
323323
var double: Double

0 commit comments

Comments
 (0)