rspec/rules/S7062/dart/rule.adoc
github-actions[bot] 85f1fb28c3
Create rule S7062: Implementation imports shouldn't be used (#4227)
Co-authored-by: leveretka <leveretka@users.noreply.github.com>
2024-09-09 10:01:19 +02:00

69 lines
1.9 KiB
Plaintext

== Why is this an issue?
In Dart, there's a convention to distinguish between public API and the implementation details. It states:
* Files inside `lib/src` are considered internal implementation detail, and shouldn't be referenced
* Other files inside the `lib` directory are publicly accessible and are safe to be imported
Introducing the dependency on the internal implementation of other packages is not recommended as it is the subject to change and may introduce maintainability issues and break your code unexpectedly.
=== Exceptions
It is still safe to reference libraries from `lib/src` within the same package.
== How to fix it
Refactor your code, so you don't need to reference other package's implementation detail. Check if the needed functionality is available through the Public API.
=== Code examples
==== Noncompliant code example
[source,dart,diff-id=1,diff-type=noncompliant]
----
import 'package:other/src/impl.dart'; // Noncompliant here
void main() {
doSomethingPrivate(); // this function is coming from 'other' package
}
----
You can try to find some public API that has the same function:
==== Compliant solution
[source,dart,diff-id=1,diff-type=compliant]
----
import 'package:other/api.dart';
void main() {
doSomethingPublic(); // this function is a public API wrapper of 'doSomethingPrivate'
}
----
== Resources
=== Documentation
* Dart Docs -https://dart.dev/tools/linter-rules/implementation_imports[Dart Linter rule - implementation_imports]
* Dart Docs - https://dart.dev/tools/pub/package-layout#implementation-files[Implementation files]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Import of a library in the 'lib/src' directory of another package.
=== Highlighting
Import statement
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]