-
Notifications
You must be signed in to change notification settings - Fork 29
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]. | ||
|
||
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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