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 S7084: Dependencies should be sorted (sort_pub_dependencies) #4300

Merged
merged 2 commits into from
Sep 26, 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/S7084/dart/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Dependencies should be sorted",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "2min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7084",
"sqKey": "S7084",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "FORMATTED"
}
}
120 changes: 120 additions & 0 deletions rules/S7084/dart/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Dependencies should be sorted in alphabetical ascending order.

== Why is this an issue?

Keeping `pubspec.yaml` dependencies in order facilitates lookup and maintenance. There may be multiple criteria to sort dependencies, which make sense in the context of the Dart project on-hand.

However, none of the criteria can be a perfect-fit for all projects. In order to promote consistency across projects, and to follow the https://en.wikipedia.org/wiki/Principle_of_least_astonishment[Principle of least astonishment], it's best to sort dependencies in alphabetical ascending order.

While it may not be the most optimal sorting criteria for all specific scenarios, it's the most neutral, easily inferrable at first glance, as well as the least surprising.

The rule applies to the following three sections of the `pubspec.yaml` file:

* `dependencies`
* `dev_dependencies`
* `dependency_overrides`

=== What is the potential impact?

Sorting dependencies in a different order may lead, for example, to duplicated dependencies, where a developer assumed a dependency would appear in a certain section of the `pubspec.yaml` file, while it actually appears in a different section.

== How to fix it

=== Code examples

==== Noncompliant code example

[source,yaml,diff-id=1,diff-type=noncompliant]
----
name: test

dependencies:
linter: any
analyzer: any # Noncompliant : should come before linter
watcher: ^1.1.0
path: ^1.9.0

dev_dependencies:
flutter_test:
sdk: flutter

async: 2.11.0 # Noncompliant" should come before flutter_test
boolean_selector: 2.1.1
clock: 1.1.1

dependency_overrides:
http: ^1.0.0

url_protocol:
git:
url: https://github.com/LucasXu0/flutter_url_protocol.git
commit: 77a8420

supabase_flutter: # Noncompliant: should come before url_protocol
git:
url: https://github.com/supabase/supabase-flutter
ref: 9b05eea
path: packages/supabase_flutter

----

==== Compliant solution

[source,yaml,diff-id=1,diff-type=compliant]
----
name: test

dependencies:
analyzer: any
linter: any
path: ^1.9.0
watcher: ^1.1.0

dev_dependencies:
async: 2.11.0
boolean_selector: 2.1.1
clock: 1.1.1
flutter_test:
sdk: flutter

dependency_overrides:
http: ^1.0.0

supabase_flutter:
git:
url: https://github.com/supabase/supabase-flutter
ref: 9b05eea
path: packages/supabase_flutter

url_protocol:
git:
url: https://github.com/LucasXu0/flutter_url_protocol.git
commit: 77a8420
----

== Resources

=== Documentation

* Dart Docs - https://dart.dev/tools/linter-rules/sort_pub_dependencies[Linter rule - sort_pub_dependencies]
* Dart Docs - https://dart.dev/tools/pub/pubspec[Packages - The pubspec file]
* Wikipedia - https://en.wikipedia.org/wiki/Principle_of_least_astonishment[Principle of least astonishment]


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

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

=== Message

Dependencies not sorted alphabetically.

=== Highlighting

The name of the first dependency that is not sorted alphabetically, but not its version: e.g. `analyzer` in `analyzer: any`.

'''

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