diff --git a/rules/S7053/dart/metadata.json b/rules/S7053/dart/metadata.json new file mode 100644 index 00000000000..5edc4d22733 --- /dev/null +++ b/rules/S7053/dart/metadata.json @@ -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" + } +} diff --git a/rules/S7053/dart/rule.adoc b/rules/S7053/dart/rule.adoc new file mode 100644 index 00000000000..a3054666d23 --- /dev/null +++ b/rules/S7053/dart/rule.adoc @@ -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[] diff --git a/rules/S7053/metadata.json b/rules/S7053/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S7053/metadata.json @@ -0,0 +1,2 @@ +{ +}