Skip to content

Conversation

@luizgustavo09
Copy link

@luizgustavo09 luizgustavo09 commented Dec 1, 2025

Description

Fixes #211

Member attributes like @available, @objc, and @discardableResult are now placed on methods and variables instead of the mock class.

Changes

  • Added attributes field to MethodModel and VariableModel
  • Updated parsers to capture and pass member attributes
  • Modified templates to render @available above and any other inline
  • Updated test fixtures

Testing

  • All 129 tests passing
  • Added MemberAttributeTests covering attributes on methods and variables

@luizgustavo09 luizgustavo09 force-pushed the fix-available-on-function branch from c0d2381 to 756faef Compare December 1, 2025 18:49
@luizgustavo09 luizgustavo09 marked this pull request as ready for review December 2, 2025 17:29
Copy link
Collaborator

@fummicc1 fummicc1 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! I left a comment.

Please let me request a review to other collaborators. I'm not fully familiar with the current specifications related to this change.

Copy link
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

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

It's better than before.

let shouldFilterMemberAttributes = item is MethodModel || item is VariableModel

if shouldFilterMemberAttributes {
let memberAttributes = ["@available", "@objc", "@discardableResult"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why were only these three chosen?

If using filters, you'll need to cover all combinations of Model types and attributes.

Copy link
Author

Choose a reason for hiding this comment

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

It's the same discussed here

We're thinking about we don't need to filter because there's no use case where the attribute should be on class scope

Copy link
Author

Choose a reason for hiding this comment

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

I'm not filtering anymore, assuming the every attribute must be nested with the method/var

Comment on lines 60 to 68
let availableAttr = (attributes ?? [])
.filter { $0.contains("@available") }
.joined(separator: "\n")
let availableStr = availableAttr.isEmpty ? "" : "\(1.tab)\(availableAttr)\n"

let objcAttr = (attributes ?? [])
.filter { $0.contains("@objc") }
.joined(separator: " ")
let objcStr = objcAttr.isEmpty ? "" : "\(objcAttr) "
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be endless to add processing for each attribute individually.

Copy link
Author

Choose a reason for hiding this comment

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

I agree, but the problem is that the @available attribute is usually above the method/variable, etc.
@objc, @discardableResult remains in line with the declaration.

What is your suggestion for these cases? Could we handle only the @available attribute?

Copy link
Author

Choose a reason for hiding this comment

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

changed to handle only @available, I don't remember if swift has another use case where the attribute stay above

@luizgustavo09
Copy link
Author

Question:
Some tests like this
protocol NonSimpleFuncs { @discardableResult func returnSelf() -> Self }

was expecting this
func returnSelf() -> Self {...
without the attribute. But imo the result must be @discardableResult func returnSelf() -> Self

@luizgustavo09 luizgustavo09 force-pushed the fix-available-on-function branch from 955d020 to e2df490 Compare December 5, 2025 15:16
@sidepelican
Copy link
Collaborator

Actually, @available cannot be used for stored properties.

The following code won't compile, right?

struct S {
    @available(macOS 99, *) // stored properties cannot be marked potentially unavailable with '@available'
    var a: Int
}

@sidepelican
Copy link
Collaborator

#330 (comment)

But imo the result must be @discardableResult func returnSelf() -> Self

This is something I'm fine with either way.
The functions correctly when the mock class used generically.

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.

@available attribute on function mocked incorrectly

3 participants