Skip to content

Commit

Permalink
Suppress remove curly braces from template #157 (#263)
Browse files Browse the repository at this point in the history
Adds a way to respect standard suppress code style annotations.
  • Loading branch information
Aleksandr authored and Tapchicoma committed Nov 11, 2019
1 parent 681df30 commit baac14c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
48 changes: 48 additions & 0 deletions ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.TreeCopyHandler
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.KtAnnotated
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import sun.reflect.ReflectionFactory

object KtLint {
Expand Down Expand Up @@ -466,6 +470,14 @@ object KtLint {
}
}
}
// Extract all Suppress annotations and create SuppressionHints
val psi = node.psi
if (psi is KtAnnotated) {
createSuppressionHintFromAnnotations(psi, suppressAnnotations, suppressAnnotationRuleMap)
?.let {
result.add(it)
}
}
}
result.addAll(
open.map {
Expand All @@ -490,6 +502,42 @@ object KtLint {
comment.replace(Regex("\\s"), " ").replace(" {2,}", " ").split(" ")

private fun <T> List<T>.tail() = this.subList(1, this.size)

private val suppressAnnotationRuleMap = mapOf(
"RemoveCurlyBracesFromTemplate" to "string-template"
)

private val suppressAnnotations = setOf("Suppress", "SuppressWarnings")

/**
* Creates [SuppressionHint] from annotations of given [psi]
* Returns null if no [targetAnnotations] present or no mapping exists
* between annotations' values and ktlint rules
*/
private fun createSuppressionHintFromAnnotations(
psi: KtAnnotated,
targetAnnotations: Collection<String>,
annotationValueToRuleMapping: Map<String, String>
): SuppressionHint? {

return psi.annotationEntries
.filter {
it.calleeExpression?.constructorReferenceExpression
?.getReferencedName() in targetAnnotations
}.flatMap(KtAnnotationEntry::getValueArguments)
.mapNotNull { it.getArgumentExpression()?.text?.removeSurrounding("\"") }
.mapNotNull(annotationValueToRuleMapping::get)
.let { suppressedRules ->
if (suppressedRules.isNotEmpty()) {
SuppressionHint(
IntRange(psi.startOffset, psi.endOffset),
suppressedRules.toSet()
)
} else {
null
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,45 @@ fun main() {

println("${h["x-forwarded-proto"] ?: "http"}")
println("${if (diff > 0) "expanded" else if (diff < 0) "shrank" else "changed"}")

@Suppress("RemoveCurlyBracesFromTemplate")
println("${s0}")
@Suppress("RemoveCurlyBracesFromTemplate", "Unused")
println("${s0}")
@Suppress("RemoveCurlyBracesFromTemplate")
val t = "${s0}"
}

class B(val k: String) {
override fun toString(): String = "${super.toString()}, ${super.hashCode().toString()}, k=$k"

@Suppress("RemoveCurlyBracesFromTemplate")
val a
get() = "${s0}"
}

@Suppress("RemoveCurlyBracesFromTemplate")
class C {
override fun toString(): String = "${s0}"
}

// Ensure that suppression scope is as wide as it should be
class D {
@Suppress("RemoveCurlyBracesFromTemplate")
override fun toString(): String = "${s0}"

fun test() = "${s0}"
}

@SuppressWarnings("RemoveCurlyBracesFromTemplate")
class E {
override fun toString(): String = "${s0}"
}

// expect
// 2:29:Redundant "toString()" call in string template
// 3:28:Redundant "toString()" call in string template
// 6:15:Redundant curly braces
// 7:15:Redundant curly braces
// 21:79:Redundant "toString()" call in string template
// 28:79:Redundant "toString()" call in string template
// 45:20:Redundant curly braces

0 comments on commit baac14c

Please sign in to comment.