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

type.accessLevel returns empty string for all types that are not custom. #325

Closed
Sajjon opened this issue May 25, 2017 · 4 comments
Closed

Comments

@Sajjon
Copy link

Sajjon commented May 25, 2017

What?

accessLevel returns empty string for UIKit, Foundation and probably all Apple types.

Environment

Swift

Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)
Target: x86_64-apple-macosx10.9

Sourcery

0.6.0

Example

protocol AutoDemo {}

private struct Foo {}
struct Bar {}
internal struct Baz {}
public struct Buz {}

extension Foo: AutoDemo {}
extension Bar: AutoDemo {}
extension Baz: AutoDemo {}
extension Buz: AutoDemo {}
extension UIControlEvents: AutoDemo {}
extension NSObject: AutoDemo {}
extension String: AutoDemo {}

Given the stencil

{% macro accessModifierOf type %}{% if type.accessLevel == '' %}PUBLIC{% else %}{{ type.accessLevel }}{% endif %}{% endmacro %}

{% for type in types.implementing.AutoDemo %}
// `{{ type.name }}` is `{% call accessModifierOf type %}`
{% endfor %}

Results in:

// `Bar` is `internal`
// `Baz` is `internal`
// `Buz` is `public`
// `Foo` is `private`
// `NSObject` is `PUBLIC`
// `String` is `PUBLIC`
// `UIControlEvents` is `PUBLIC`

So my fix is using the macro accessModifierOf but printing public and not PUBLIC of course (just wrote that to highlight that type.accessLevel) returns an empty string.

@ilyapuchka
Copy link
Collaborator

ilyapuchka commented May 25, 2017

I believe extensions themselves do not have access level, at least SourceKit does not return such information (try sourcekitten structure --text "protocol AutoDemo{}; extension NSObject: AutoDemo {}"). But declarations inside extension should contain proper access level.
For known types we simply merge extension with type definition, access level (that you see in your output) comes from type definition. For unknown types there is nothing to merge with - thus access level is unknown.

The other problem is that Sourcery does not have any means to distinguish conformance to protocols through extensions in implementing or based collections. I.e. if you extend public class with internal protocol should we expose this protocol in implementing collection? This conformance should be not accessible from other modules but will be accessible in the same module (the same for private and fileprivate scopes).

Depending on your use case maybe there is a better workaround for that. But if public is good default for you - use it. But it will be not a good default in general case.

@ilyapuchka
Copy link
Collaborator

@Sajjon does this answer your question?

@Antondomashnev
Copy link
Collaborator

@Sajjon could you please follow-up for @ilyapuchka's question?

@Sajjon
Copy link
Author

Sajjon commented Aug 7, 2017

@ilyapuchka @Antondomashnev sorry for inactivity! We do not get any information at all since Sourcery does not work for UIKit types, but rather our own types, as discussed here.

So we can close this one.

@Sajjon Sajjon closed this as completed Aug 7, 2017
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

No branches or pull requests

3 participants