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

Create rule S7053: Relative lib imports should not be used (avoid_relative_lib_imports) #4206

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions rules/S7053/dart/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Relative lib imports should not be used",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7053",
"sqKey": "S7053",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "IDENTIFIABLE"
}
}
64 changes: 64 additions & 0 deletions rules/S7053/dart/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Relative lib imports should be avoided.

== Why is this an issue?

The https://dart.dev/guides/libraries/create-packages[`lib` directory] of a Dart project is where the main code of the package is stored. Code living directly under the `lib` directory, when not explicitly declared as private with the ``++__++`` name convention, is considered the public API of the package, and can be imported via https://dart.dev/guides/packages#importing-libraries-from-packages[package imports].
Copy link
Contributor

Choose a reason for hiding this comment

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

To be verified if _ is shown correctly in SQ and SC


When importing code from a `lib` directory of a package available in the local file structure, it is recommended to use package imports instead of relative imports.

This is because relative imports may cause duplicate imports and may lead to confusion.

[source,dart]
----
import 'package:my_package/my_module.dart'; // OK
import '../my_module.dart'; // Noncompliant: duplicate of above
----

== How to fix it

Convert the relative import to a package import.

=== Code examples

==== Noncompliant code example

[source,dart,diff-id=1,diff-type=noncompliant]
----
import '../my_module.dart';
----

==== Compliant solution

[source,dart,diff-id=1,diff-type=compliant]
----
import 'package:my_package/my_module.dart';
----

== Resources

=== Documentation

* Dart Docs - https://dart.dev/tools/linter-rules/avoid_relative_lib_imports[Dart Linter rule - avoid_relative_lib_imports]
* Dart Docs - https://dart.dev/guides/libraries/create-packages[Packages - Create packages]
* Dart Docs - https://dart.dev/guides/packages#importing-libraries-from-packages[Packages - Importing libraries from packages]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

* Can't use a relative path to import a library in 'lib'.

=== Highlighting

The string defining the path of the relative import.

'''
== Comments And Links
(visible only on this page)

endif::env-github,rspecator-view[]
2 changes: 2 additions & 0 deletions rules/S7053/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading