-
Notifications
You must be signed in to change notification settings - Fork 501
/
SuppressionLocatorBuilder.kt
201 lines (186 loc) · 7.53 KB
/
SuppressionLocatorBuilder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.pinterest.ktlint.core.internal
import com.pinterest.ktlint.core.ast.prevLeaf
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.psi.KtAnnotated
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.ValueArgument
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
/**
* Detects if given `ruleId` at given `offset` is suppressed.
*/
internal typealias SuppressionLocator = (offset: Int, ruleId: String, isRoot: Boolean) -> Boolean
internal object SuppressionLocatorBuilder {
/**
* No suppression is detected. Always returns `false`.
*/
val noSuppression: SuppressionLocator = { _, _, _ -> false }
private val suppressAnnotationRuleMap = mapOf(
"RemoveCurlyBracesFromTemplate" to "string-template"
)
private val suppressAnnotations = setOf("Suppress", "SuppressWarnings")
private const val suppressAllKtlintRules = "ktlint-all"
private val commentRegex = Regex("\\s")
/**
* Builds [SuppressionLocator] for given [rootNode] of AST tree.
*/
fun buildSuppressedRegionsLocator(
rootNode: ASTNode
): SuppressionLocator {
val hintsList = collect(rootNode)
return if (hintsList.isEmpty()) {
noSuppression
} else {
toSuppressedRegionsLocator(hintsList)
}
}
private fun toSuppressedRegionsLocator(hintsList: List<SuppressionHint>): SuppressionLocator =
{ offset, ruleId, isRoot ->
hintsList
.filter { offset in it.range }
.any { hint -> hint.disabledRules.isEmpty() || hint.disabledRules.contains(ruleId) }
}
/**
* @param range zero-based range of lines where lint errors should be suppressed
* @param disabledRules empty set means "all"
*/
private data class SuppressionHint(
val range: IntRange,
val disabledRules: Set<String> = emptySet()
)
private fun collect(
rootNode: ASTNode
): List<SuppressionHint> {
val result = ArrayList<SuppressionHint>()
val open = ArrayList<SuppressionHint>()
rootNode.collect { node ->
if (node is PsiComment) {
val text = node.getText()
if (text.startsWith("//")) {
val commentText = text.removePrefix("//").trim()
parseHintArgs(commentText, "ktlint-disable")?.let { args ->
val lineStart = (
node.prevLeaf { it is PsiWhiteSpace && it.textContains('\n') } as
PsiWhiteSpace?
)?.let { it.node.startOffset + it.text.lastIndexOf('\n') + 1 } ?: 0
result.add(SuppressionHint(IntRange(lineStart, node.startOffset), HashSet(args)))
}
} else {
val commentText = text.removePrefix("/*").removeSuffix("*/").trim()
parseHintArgs(commentText, "ktlint-disable")?.apply {
open.add(SuppressionHint(IntRange(node.startOffset, node.startOffset), HashSet(this)))
}
?: parseHintArgs(commentText, "ktlint-enable")?.apply {
// match open hint
val disabledRules = HashSet(this)
val openHintIndex = open.indexOfLast { it.disabledRules == disabledRules }
if (openHintIndex != -1) {
val openingHint = open.removeAt(openHintIndex)
result.add(
SuppressionHint(
IntRange(openingHint.range.first, node.startOffset - 1),
disabledRules
)
)
}
}
}
}
// 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 {
SuppressionHint(IntRange(it.range.first, rootNode.textLength), it.disabledRules)
}
)
return result
}
private fun ASTNode.collect(block: (node: ASTNode) -> Unit) {
block(this)
this
.getChildren(null)
.forEach { it.collect(block) }
}
private fun parseHintArgs(
commentText: String,
key: String
): List<String>? {
if (commentText.startsWith(key)) {
val parsedComment = splitCommentBySpace(commentText)
// assert exact match
if (parsedComment[0] == key) {
return parsedComment.tail()
}
}
return null
}
private fun splitCommentBySpace(
comment: String
) = comment
.replace(commentRegex, " ")
.replace(" {2,}", " ")
.split(" ")
private fun <T> List<T>.tail() = this.subList(1, this.size)
/**
* 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? =
psi
.annotationEntries
.filter {
it.calleeExpression
?.constructorReferenceExpression
?.getReferencedName() in targetAnnotations
}.flatMap(KtAnnotationEntry::getValueArguments)
.mapNotNull { it.toRuleId(annotationValueToRuleMapping) }
.let { suppressedRules ->
when {
suppressedRules.isEmpty() -> null
suppressedRules.contains(suppressAllKtlintRules) ->
SuppressionHint(
IntRange(psi.startOffset, psi.endOffset),
emptySet()
)
else ->
SuppressionHint(
IntRange(psi.startOffset, psi.endOffset),
suppressedRules.toSet()
)
}
}
private fun ValueArgument.toRuleId(annotationValueToRuleMapping: Map<String, String>): String? =
getArgumentExpression()
?.text
?.removeSurrounding("\"")
?.let {
when {
it == "ktlint" -> {
// Disable all rules
suppressAllKtlintRules
}
it.startsWith("ktlint:") -> {
// Disable specific rule
it.removePrefix("ktlint:")
}
else -> {
// Disable specific rule if it the annotion value is mapped to a specific rule
annotationValueToRuleMapping[it]
}
}
}
}