Skip to content

Commit

Permalink
Report class and object violations at the identifier
Browse files Browse the repository at this point in the history
Reporting them at the declaration itself will lead to highlighting the whole class/object with kdoc when post-processing the issue results. detekt/detekt-intellij-plugin#81
  • Loading branch information
arturbosch committed Apr 14, 2020
1 parent eb83202 commit ad1cd66
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.gitlab.arturbosch.detekt.api.internal.searchClass
import io.gitlab.arturbosch.detekt.api.internal.searchName
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType

/**
Expand Down Expand Up @@ -32,6 +33,11 @@ data class Entity(
return from(element, location)
}

/**
* Create an entity at the location of the identifier of given named declaration.
*/
fun atName(element: KtNamedDeclaration): Entity = from(element.nameIdentifier ?: element)

/**
* Use this factory method if the location can be calculated much more precisely than
* using the given PsiElement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class TestProvider2(override val ruleSetId: String = "Test2") : RuleSetProvider
class FindName : Rule() {
override val issue: Issue = Issue(javaClass.simpleName, Severity.Minor, "", Debt.TWENTY_MINS)
override fun visitClassOrObject(classOrObject: KtClassOrObject) {
report(CodeSmell(issue, Entity.from(classOrObject), message = ""))
report(CodeSmell(issue, Entity.atName(classOrObject), message = ""))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class EqualsWithHashCodeExist(config: Config = Config.empty) : Rule(config) {
queue.push(ViolationHolder())
super.visitClassOrObject(classOrObject)
if (queue.pop().violation()) {
report(CodeSmell(issue, Entity.from(classOrObject), "A class should always override hashCode " +
report(CodeSmell(issue, Entity.atName(classOrObject), "A class should always override hashCode " +
"when overriding equals and the other way around."))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class IteratorHasNextCallsNextMethod(config: Config = Config.empty) : Rule(confi
if (classOrObject.getSuperNames().contains("Iterator")) {
val hasNextMethod = classOrObject.findFunctionByName("hasNext")
if (hasNextMethod != null && callsNextMethod(hasNextMethod)) {
report(CodeSmell(issue, Entity.from(classOrObject), "Calling hasNext() on an Iterator should " +
report(CodeSmell(issue, Entity.atName(classOrObject), "Calling hasNext() on an Iterator should " +
"have no side-effects. Calling next() is a side effect."))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class IteratorNotThrowingNoSuchElementException(config: Config = Config.empty) :
if (classOrObject.getSuperNames().contains("Iterator")) {
val nextMethod = classOrObject.findFunctionByName("next")
if (nextMethod != null && !nextMethod.throwsNoSuchElementExceptionThrown()) {
report(CodeSmell(issue, Entity.from(classOrObject),
report(CodeSmell(issue, Entity.atName(classOrObject),
"This implementation of Iterator does not correctly implement the next() method as " +
"it doesn't throw a NoSuchElementException when no elements remain in the Iterator."))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ClassNaming(config: Config = Config.empty) : Rule(config) {
if (!classOrObject.identifierName().matches(classPattern)) {
report(CodeSmell(
issue,
Entity.from(classOrObject.nameIdentifier ?: classOrObject),
Entity.atName(classOrObject),
message = "Class and Object names should match the pattern: $classPattern"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ForbiddenClassName(config: Config = Config.empty) : Rule(config) {
forbiddenEntries.forEach { message += " $it," }
message.trimEnd { it == ',' }

report(CodeSmell(issue, Entity.from(classOrObject), message))
report(CodeSmell(issue, Entity.atName(classOrObject), message))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ class UnnecessaryInheritance(config: Config = Config.empty) : Rule(config) {
}

private fun report(classOrObject: KtClassOrObject, message: String) {
report(CodeSmell(issue, Entity.from(classOrObject), message))
report(CodeSmell(issue, Entity.atName(classOrObject), message))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Entity.atName - detekt-api
---

[detekt-api](../../index.html) / [io.gitlab.arturbosch.detekt.api](../index.html) / [Entity](index.html) / [atName](./at-name.html)

# atName

`fun atName(element: KtNamedDeclaration): `[`Entity`](index.html)

Create an entity at the location of the identifier of given named declaration.

Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ Stores information about a specific code fragment.

### Companion Object Functions

| [atName](at-name.html) | Create an entity at the location of the identifier of given named declaration.`fun atName(element: KtNamedDeclaration): `[`Entity`](./index.html) |
| [from](from.html) | Factory function which retrieves all needed information from the PsiElement itself.`fun from(element: PsiElement, offset: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` = 0): `[`Entity`](./index.html)<br>Use this factory method if the location can be calculated much more precisely than using the given PsiElement.`fun from(element: PsiElement, location: `[`Location`](../-location/index.html)`): `[`Entity`](./index.html) |

0 comments on commit ad1cd66

Please sign in to comment.