-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
Context.kt
101 lines (85 loc) · 3.16 KB
/
Context.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
package io.gitlab.arturbosch.detekt.api
import io.gitlab.arturbosch.detekt.api.internal.isSuppressedBy
import org.jetbrains.kotlin.psi.KtFile
/**
* A context describes the storing and reporting mechanism of [Finding]'s inside a [Rule].
* Additionally it handles suppression and aliases management.
*
* The detekt engine retrieves the findings after each KtFile visit and resets the context
* before the next KtFile.
*/
interface Context {
val findings: List<Finding>
/**
* Reports a single new violation.
*/
@Deprecated("Overloaded version with extra ruleSetId parameter should be used.")
fun report(finding: Finding, aliases: Set<String> = emptySet())
/**
* Reports a single new violation.
* By contract the implementation can check if
* this finding is already suppressed and should not get reported.
* An alias set can be given to additionally check if an alias was used when suppressing.
* Additionally suppression by rule set id is supported.
*/
fun report(finding: Finding, aliases: Set<String> = emptySet(), ruleSetId: RuleSetId? = null) {
report(finding, aliases, null)
}
/**
* Same as [report] but reports a list of findings.
*/
@Deprecated("Overloaded version with extra ruleSetId parameter should be used.")
fun report(findings: List<Finding>, aliases: Set<String> = emptySet())
/**
* Same as [report] but reports a list of findings.
*/
fun report(findings: List<Finding>, aliases: Set<String> = emptySet(), ruleSetId: RuleSetId? = null) {
report(findings, aliases, null)
}
/**
* Clears previous findings.
* Normally this is done on every new [KtFile] analyzed and should be called by clients.
*/
fun clearFindings()
}
/**
* Default [Context] implementation.
*/
open class DefaultContext : Context {
/**
* Returns a copy of violations for this rule.
*/
override val findings: List<Finding>
get() = _findings.toList()
private var _findings: MutableList<Finding> = mutableListOf()
/**
* Reports a single code smell finding.
*
* Before adding a finding, it is checked if it is not suppressed
* by @Suppress or @SuppressWarnings annotations.
*/
override fun report(finding: Finding, aliases: Set<String>, ruleSetId: RuleSetId?) {
val ktElement = finding.entity.ktElement
if (ktElement == null || !ktElement.isSuppressedBy(finding.id, aliases, ruleSetId)) {
_findings.add(finding)
}
}
/**
* Reports a list of code smell findings.
*
* Before adding a finding, it is checked if it is not suppressed
* by @Suppress or @SuppressWarnings annotations.
*/
override fun report(findings: List<Finding>, aliases: Set<String>, ruleSetId: RuleSetId?) {
findings.forEach { report(it, aliases, ruleSetId) }
}
final override fun clearFindings() {
_findings = mutableListOf()
}
override fun report(finding: Finding, aliases: Set<String>) {
report(finding, aliases, null)
}
override fun report(findings: List<Finding>, aliases: Set<String>) {
report(findings, aliases, null)
}
}