Skip to content
Draft
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
27 changes: 27 additions & 0 deletions rules/S8314/groovy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Static imports should appear before regular imports",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"convention",
"imports"
],
"defaultSeverity": "Critical",
"ruleSpecification": "RSPEC-8314",
"sqKey": "S8314",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH"
},
"attribute": "CONVENTIONAL"
}
}
51 changes: 51 additions & 0 deletions rules/S8314/groovy/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
This rule raises an issue when static imports are declared after regular (non-static) imports in a source file.

== Why is this an issue?

Import statements should follow a consistent ordering convention to improve code readability and maintainability. The widely accepted convention in Java and Groovy is to place all static imports before regular imports.

When imports are not properly ordered, it creates visual inconsistency that can make code harder to scan and understand. While this doesn't affect the functionality of the code, following consistent import ordering helps developers quickly locate and understand dependencies.

Most style guides and IDEs default to this ordering convention, so maintaining it ensures your code aligns with common practices and tools.

=== What is the potential impact?

This issue has minimal impact on functionality but affects code readability and maintainability. Inconsistent import ordering can make it harder for developers to quickly scan and understand code dependencies, especially in files with many imports.

== How to fix it

Move all static imports to appear before regular imports. Group static imports together at the top of the import section.

=== Code examples

==== Noncompliant code example

[source,groovy,diff-id=1,diff-type=noncompliant]
----
import org.codenarc.rule.InlineViolationsParser
import static org.codenarc.rule.InlineViolationsParser.inlineViolation // Noncompliant
----

==== Compliant solution

[source,groovy,diff-id=1,diff-type=compliant]
----
import static org.codenarc.rule.InlineViolationsParser.inlineViolation
import org.codenarc.rule.InlineViolationsParser
----

== Resources

=== Documentation

* Groovy Style Guide - Import Statements - https://groovy-lang.org/style-guide.html#_import_statements[Official Groovy style guide recommendations for import statement ordering]

* Java Code Conventions - Import Statements - https://www.oracle.com/java/technologies/javase/codeconventions-imports.html[Oracle's Java code conventions for import statement organization]

=== Standards

* Google Java Style Guide - Import Statements - https://google.github.io/styleguide/javaguide.html#s3.3-import-statements[Google's Java style guide specifying import ordering conventions]

=== Related rules

* RSPEC-2208 - https://rules.sonarsource.com/java/RSPEC-2208/[Wildcard imports should not be used (Java)]
2 changes: 2 additions & 0 deletions rules/S8314/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}