diff --git a/Sources/SwiftEnvironment/Macros.swift b/Sources/SwiftEnvironment/Macros.swift index 9fcf9cf..417f7de 100644 --- a/Sources/SwiftEnvironment/Macros.swift +++ b/Sources/SwiftEnvironment/Macros.swift @@ -13,23 +13,23 @@ public macro EnvironmentValue() = #externalMacro( ) @attached(member, names: arbitrary) -public macro Stubbed(_ modifier: Modifier = .internalStub) = #externalMacro( - module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro" +public macro Stubbed(public: Bool = false) = #externalMacro( + module: "SwiftEnvironmentMacro", type: "StubFromTypeGeneratorMacro" ) @attached(member, names: arbitrary) -public macro Stubbed(_ modifier: Modifier = .internalStub, _ values: DefaultType...) = #externalMacro( - module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro" +public macro Stubbed(public: Bool = false, _ values: DefaultType...) = #externalMacro( + module: "SwiftEnvironmentMacro", type: "StubFromTypeGeneratorMacro" ) @attached(peer, names: suffixed(Stub)) public macro Stubbed(type: StubType) = #externalMacro( - module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro" + module: "SwiftEnvironmentMacro", type: "StubFromProtocolGeneratorMacro" ) @attached(peer, names: suffixed(Stub)) public macro Stubbed(type: StubType, _ values: DefaultType...) = #externalMacro( - module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro" + module: "SwiftEnvironmentMacro", type: "StubFromProtocolGeneratorMacro" ) public enum StubType { @@ -40,11 +40,6 @@ public enum StubType { case openSubclass(AnyClass.Type) } -public enum Modifier { - case publicStub - case internalStub -} - public struct DefaultType { public static func value(for: T.Type, _: T) -> DefaultType { DefaultType() diff --git a/Sources/SwiftEnvironmentMacro/EnvironmentValueMacroPlugin.swift b/Sources/SwiftEnvironmentMacro/EnvironmentValueMacroPlugin.swift index 405136d..650cfd7 100644 --- a/Sources/SwiftEnvironmentMacro/EnvironmentValueMacroPlugin.swift +++ b/Sources/SwiftEnvironmentMacro/EnvironmentValueMacroPlugin.swift @@ -12,6 +12,6 @@ import SwiftSyntaxMacros @main struct EnvironmentValueMacroPlugin: CompilerPlugin { let providingMacros: [Macro.Type] = [ - EnvironmentValueMacro.self, StubGeneratorMacro.self + EnvironmentValueMacro.self, StubFromProtocolGeneratorMacro.self, StubFromTypeGeneratorMacro.self ] } diff --git a/Sources/SwiftEnvironmentMacro/Extensions/AttributeSyntax+Extensions.swift b/Sources/SwiftEnvironmentMacro/Extensions/AttributeSyntax+Extensions.swift index 2ae4130..9dc38c2 100644 --- a/Sources/SwiftEnvironmentMacro/Extensions/AttributeSyntax+Extensions.swift +++ b/Sources/SwiftEnvironmentMacro/Extensions/AttributeSyntax+Extensions.swift @@ -1,6 +1,6 @@ // // File.swift -// +// // // Created by Nayanda Haberty on 21/4/24. // @@ -9,17 +9,12 @@ import Foundation import SwiftSyntax extension AttributeSyntax { - var hasModifiers: Bool { - guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)? - .first?.as(LabeledExprSyntax.self) else { return false } - return firstArgument.trimmedDescription == ".publicStub" - || firstArgument.trimmedDescription == ".internalStub" - } - var isPublicStub: Bool { - guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)? - .first?.as(LabeledExprSyntax.self) else { return false } - return firstArgument.trimmedDescription == ".publicStub" + guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?.first, + firstArgument.label?.trimmedDescription == "public" else { + return false + } + return firstArgument.expression.trimmedDescription == "true" } var defaultValueArguments: [String: String] { @@ -27,8 +22,7 @@ extension AttributeSyntax { let arguments = arguments?.as(LabeledExprListSyntax.self)? .filter { $0.label == nil } .compactMap { $0.as(LabeledExprSyntax.self) } - guard var arguments else { return [:] } - arguments = hasModifiers ? Array(arguments.suffix(from: 1)) : arguments + guard let arguments else { return [:] } return try arguments .map { try $0.extractDefaultValueArguments() } .mapToTypeDefaultValueArguments() diff --git a/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+PeerMacro.swift b/Sources/SwiftEnvironmentMacro/Stubbed/StubFromProtocolGeneratorMacro.swift similarity index 99% rename from Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+PeerMacro.swift rename to Sources/SwiftEnvironmentMacro/Stubbed/StubFromProtocolGeneratorMacro.swift index d2f11b6..4c05d59 100644 --- a/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+PeerMacro.swift +++ b/Sources/SwiftEnvironmentMacro/Stubbed/StubFromProtocolGeneratorMacro.swift @@ -10,7 +10,7 @@ import SwiftSyntaxBuilder import SwiftSyntaxMacros import SwiftSyntax -extension StubGeneratorMacro: PeerMacro { +struct StubFromProtocolGeneratorMacro: PeerMacro { public static func expansion(of node: AttributeSyntax, providingPeersOf declaration: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [DeclSyntax] { guard let protocolSyntax = declaration.as(ProtocolDeclSyntax.self) else { return [] diff --git a/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+MemberMacro.swift b/Sources/SwiftEnvironmentMacro/Stubbed/StubFromTypeGeneratorMacro.swift similarity index 98% rename from Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+MemberMacro.swift rename to Sources/SwiftEnvironmentMacro/Stubbed/StubFromTypeGeneratorMacro.swift index 8f01cd5..4ffc0ad 100644 --- a/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro+MemberMacro.swift +++ b/Sources/SwiftEnvironmentMacro/Stubbed/StubFromTypeGeneratorMacro.swift @@ -10,7 +10,7 @@ import SwiftSyntaxBuilder import SwiftSyntaxMacros import SwiftSyntax -extension StubGeneratorMacro: MemberMacro { +public struct StubFromTypeGeneratorMacro: MemberMacro { public static func expansion(of node: AttributeSyntax, providingMembersOf declaration: some DeclGroupSyntax, in context: some MacroExpansionContext) throws -> [DeclSyntax] { guard let name = declaration.className ?? declaration.structName else { return [] diff --git a/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro.swift b/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro.swift deleted file mode 100644 index 85bb0ea..0000000 --- a/Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro.swift +++ /dev/null @@ -1,10 +0,0 @@ -// -// File.swift -// -// -// Created by Nayanda Haberty on 1/4/24. -// - -import Foundation - -public struct StubGeneratorMacro { } diff --git a/Tests/SwiftEnvironmentTests/StubFromProtocolGeneratorMacroTests.swift b/Tests/SwiftEnvironmentTests/StubFromProtocolGeneratorMacroTests.swift index 3b489fd..1381640 100644 --- a/Tests/SwiftEnvironmentTests/StubFromProtocolGeneratorMacroTests.swift +++ b/Tests/SwiftEnvironmentTests/StubFromProtocolGeneratorMacroTests.swift @@ -16,7 +16,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleProtocol, expandedSource: simpleProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -24,7 +24,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( optionalProtocol, expandedSource: optionalProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -32,7 +32,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( arrayProtocol, expandedSource: arrayProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -40,7 +40,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( dictionaryProtocol, expandedSource: dictionaryProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -48,7 +48,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( classProtocol, expandedSource: classProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -56,7 +56,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( superClassProtocol, expandedSource: superClassProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -64,7 +64,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( injectValueProtocol, expandedSource: injectValueProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -72,7 +72,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( structProtocol, expandedSource: structProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -80,7 +80,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( allArgsProtocol, expandedSource: allArgsProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } @@ -88,7 +88,7 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase { assertMacroExpansion( anyObjectProtocol, expandedSource: anyObjectProtocolExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromProtocolGeneratorMacro.self] ) } } diff --git a/Tests/SwiftEnvironmentTests/StubFromTypeGeneratorTests.swift b/Tests/SwiftEnvironmentTests/StubFromTypeGeneratorTests.swift index 8a4ebdb..753cefd 100644 --- a/Tests/SwiftEnvironmentTests/StubFromTypeGeneratorTests.swift +++ b/Tests/SwiftEnvironmentTests/StubFromTypeGeneratorTests.swift @@ -16,7 +16,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleStruct, expandedSource: simpleStructExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -24,7 +24,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleClass, expandedSource: simpleClassExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -32,7 +32,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleClassWithInit, expandedSource: simpleClassWithInitExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -40,7 +40,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleClassWithRandomInit, expandedSource: simpleClassWithRandomInitExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -48,7 +48,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleClassWithDifferentInit, expandedSource: simpleClassWithDifferentInitExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -56,7 +56,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleStructWithClosure, expandedSource: simpleStructWithClosureExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -64,7 +64,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleClassWithClosure, expandedSource: simpleClassWithClosureExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -72,7 +72,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( simpleStructWithOptional, expandedSource: simpleStructWithOptionalExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } @@ -80,7 +80,7 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase { assertMacroExpansion( typeAliasStruct, expandedSource: typeAliasStructExpansion, - macros: ["Stubbed": StubGeneratorMacro.self] + macros: ["Stubbed": StubFromTypeGeneratorMacro.self] ) } } @@ -111,7 +111,7 @@ struct Some { """ private let simpleClassWithClosure: String = """ -@Stubbed(.publicStub) +@Stubbed(public: true) class Some { let voidClosure: () -> Void let argVoidClosure: (Int) -> Void @@ -193,7 +193,7 @@ struct Some { """ private let simpleClassWithDifferentInit: String = """ -@Stubbed(.publicStub) +@Stubbed(public: true) class Some { let int: Int var double: Double @@ -260,7 +260,7 @@ class Some { """ private let simpleClassWithInit: String = """ -@Stubbed(.publicStub) +@Stubbed(public: true) class Some { let int: Int var double: Double @@ -317,7 +317,7 @@ class Some { """ private let simpleStruct: String = """ -@Stubbed(.publicStub) +@Stubbed(public: true) struct Some { let int: Int var double: Double