Skip to content

Commit

Permalink
Merge pull request #132 from Matejkob/create-pull-request/patch
Browse files Browse the repository at this point in the history
Apply swift-format changes
  • Loading branch information
Matejkob authored Dec 3, 2024
2 parents 5a8fa8d + d0b0de4 commit c81a4c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
16 changes: 8 additions & 8 deletions Sources/SpyableMacro/Extractors/Extractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Extractor {
let behindPreprocessorFlagArgument = argumentList.first { argument in
argument.label?.text == "behindPreprocessorFlag"
}

guard let behindPreprocessorFlagArgument else {
// The `behindPreprocessorFlag` argument is missing.
return nil
Expand Down Expand Up @@ -86,7 +86,7 @@ struct Extractor {
let accessLevelArgument = argumentList.first { argument in
argument.label?.text == "accessLevel"
}

guard let accessLevelArgument else {
// The `accessLevel` argument is missing.
return nil
Expand All @@ -104,23 +104,23 @@ struct Extractor {
}

let accessLevelText = memberAccess.declName.baseName.text

switch accessLevelText {
case "public":
return DeclModifierSyntax(name: .keyword(.public))

case "package":
return DeclModifierSyntax(name: .keyword(.package))

case "internal":
return DeclModifierSyntax(name: .keyword(.internal))

case "fileprivate":
return DeclModifierSyntax(name: .keyword(.fileprivate))

case "private":
return DeclModifierSyntax(name: .keyword(.private))

default:
context.diagnose(
Diagnostic(
Expand Down
20 changes: 11 additions & 9 deletions Sources/SpyableMacro/Macro/SpyableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ import SwiftSyntaxMacros
public enum SpyableMacro: PeerMacro {
private static let extractor = Extractor()
private static let spyFactory = SpyFactory()

public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
// Extract the protocol declaration
let protocolDeclaration = try extractor.extractProtocolDeclaration(from: declaration)

// Generate the initial spy class declaration
var spyClassDeclaration = try spyFactory.classDeclaration(for: protocolDeclaration)

// Apply access level modifiers if needed
if let accessLevel = determineAccessLevel(for: node, protocolDeclaration: protocolDeclaration, context: context) {
if let accessLevel = determineAccessLevel(
for: node, protocolDeclaration: protocolDeclaration, context: context)
{
spyClassDeclaration = rewriteSpyClass(spyClassDeclaration, withAccessLevel: accessLevel)
}

// Handle preprocessor flag
if let preprocessorFlag = extractor.extractPreprocessorFlag(from: node, in: context) {
return [wrapInIfConfig(spyClassDeclaration, withFlag: preprocessorFlag)]
}

return [DeclSyntax(spyClassDeclaration)]
}

/// Determines the access level to use for the spy class.
private static func determineAccessLevel(
for node: AttributeSyntax,
Expand All @@ -41,7 +43,7 @@ public enum SpyableMacro: PeerMacro {
return extractor.extractAccessLevel(from: protocolDeclaration)
}
}

/// Applies the specified access level to the spy class declaration.
private static func rewriteSpyClass(
_ spyClassDeclaration: DeclSyntaxProtocol,
Expand All @@ -50,7 +52,7 @@ public enum SpyableMacro: PeerMacro {
let rewriter = AccessLevelModifierRewriter(newAccessLevel: accessLevel)
return rewriter.rewrite(spyClassDeclaration).cast(ClassDeclSyntax.self)
}

/// Wraps a declaration in an `#if` preprocessor directive.
private static func wrapInIfConfig(
_ spyClassDeclaration: ClassDeclSyntax,
Expand Down
2 changes: 1 addition & 1 deletion Tests/SpyableMacroTests/Macro/UT_SpyableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ final class UT_SpyableMacro: XCTestCase {
)
}
}

func testMacroWithAccessLevelArgument() {
let accessLevelMappings = [
(protocolAccessLevel: "public", spyClassAccessLevel: "public"),
Expand Down

0 comments on commit c81a4c3

Please sign in to comment.