rspec/rules/S7068/dart/rule.adoc

72 lines
2.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
In Dart, there are no keywords like `public`, `protected`, and `private`. If an identifier starts with an underscore (_), it's private to its library. So every type starting with underscore(``++_T++``) is considered private.
Public API of a library is designed to be used outside the library itself. And this API is supposed to be stable and not change often. Changes to the Public API will most likely break the code using it. This is why it is highly recommended to follow some standard processes of deprecation and prepare the users to the breaking change if the one is coming.
In case private types appear in public API, the users of the API are forced to depend on those private types too, which is highly discouraged. This is why it is very important to decouple the public API from the implementation. Internal implementation is a subject to change often and it shouldn't affect the users of the public API.
To keep this decoupling in control, this rule reports if some private types leaked into public API.
== How to fix it
Make sure your implementation details don't leak into public API. In case it's impossible to refactor the code to get rid of the leakage, consider changing the visibility of a private type and adding it to the public API.
=== Code examples
==== Noncompliant code example
[source,dart]
----
_PrivateClass nonCompliantPublicFunction() { // returns private class
_PrivateClass private = _PrivateClass();
return private;
}
PublicClass nonCompliantPublicFunction2(_PrivateClass private) { // takes an instance of a private type as a parameter
private.runtimeType;
return PublicClass();
}
----
==== Compliant solution
[source,dart]
----
_PrivateClass _privateFunction() {
_PrivateClass private = _PrivateClass();
return private;
}
PublicClass publicFunction() {
_PrivateClass private = _PrivateClass(); // it's okay to use private types inside the implementation as long as they don't leak
return PublicClass();
}
----
== Resources
=== Documentation
* Dart Docs - https://dart.dev/tools/linter-rules/library_private_types_in_public_api[Dart Linter rule - library_private_types_in_public_api]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Invalid use of a private type in a public API.
=== Highlighting
Type usage
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]