Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit protocol modifiers #80

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

bradleymackey
Copy link

@bradleymackey bradleymackey commented Jan 19, 2024

  • Allows protocols with any modifiers, like public, fileprivate etc. to inherit this access on the spy and all the generated members. This is done by taking the modifiers on the original protocol definition and applying it to all generated members.
    • private protocol members will inherit an access level fileprivate instead. A protocol declared private at file-scope is effectively treated as fileprivate. This access level on the generated members is required to satisfy the protocol requirements
  • Generate an init with the same access level, allowing the spy to be created outside of the current module (as the default init level is internal).
  • Adds tests for all use cases.
  • Closes Declare the spy as public if the protocol is declared public #73 , fixes Use protocol's access control on the spy's declaration #72

Thanks for the great library! This is the best spy/mock generator out there atm.

@Matejkob
Copy link
Owner

Hi @bradleymackey, welcome to the project!

Thank you for your contribution! I'll try to review it over the weekend! 🙌

Copy link

codecov bot commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (288c860) 96.69% compared to head (13b69a5) 96.92%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #80      +/-   ##
==========================================
+ Coverage   96.69%   96.92%   +0.23%     
==========================================
  Files          17       18       +1     
  Lines         695      748      +53     
==========================================
+ Hits          672      725      +53     
  Misses         23       23              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@Matejkob Matejkob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firstly, I want to apologize for the delay in providing feedback on your PR. Your effort and dedication are truly appreciated.

Upon reviewing the code, an alternative implementation occurred to me. I suggest considering the use of SyntaxRewriter. This would allow us to traverse the spy's final declaration, adjusting each DeclModifierListSyntax to the required access level.

This method could streamline our code by eliminating the need to extend each method in every factory with an additional modifiers parameter. The result would likely be a cleaner and more concise codebase.

I'm eager to hear your thoughts on this suggestion. I understand it might be a bit disheartening to revise what you've already accomplished so well, but I believe the end product will justify the effort.

Should you choose to adopt this approach, be aware of certain edge cases, like ensuring DeclModifierListSyntax is not modified when its parent node is FunctionParameterSyntax, as FunctionParameterSyntax cannot have an access level modifier.

Here's an example of how you could incorporate such a syntax rewriter in SpyableMacor.swift:

var spyClassDeclaration = try spyFactory.classDeclaration(for: protocolDeclaration)

// Considerations: Should extractor.extractAccessLevel return nil? Are we supporting all possible modifiers?
if let accessLevel = extractor.extractAccessLevel(from: protocolDeclaration) {
   let accessLevelModifierRewriter = AccessLevelModifierRewriter(newAccessLevel: accessLevel)

   spyClassDeclaration = accessLevelModifierRewriter.rewrite(spyClassDeclaration).cast(ClassDeclSyntax.self)
}

Looking forward to your input and next steps!

@damian-kolasinski
Copy link

Hey @Matejkob could you merge this improvement by any chance?

@bradleymackey
Copy link
Author

Oops, this one fell off my radar, apologies for not addressing the feedback as requested. I'm using Mockolo now for my personal projects as it has a lot of features I need anyway with a bit more control over certain aspects of the mock generation.

I'll see about having a look at addressing these improvements in a week or so, but no promises as I may be a little busy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use protocol's access control on the spy's declaration
3 participants