-
Notifications
You must be signed in to change notification settings - Fork 501
/
VisitorProvider.kt
104 lines (98 loc) · 4.52 KB
/
VisitorProvider.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
package com.pinterest.ktlint.core.internal
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleRunner
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.disabledRulesProperty
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.ktlintDisabledRulesProperty
import com.pinterest.ktlint.core.api.EditorConfigProperties
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties.EditorConfigProperty
/**
* The VisitorProvider is created for each file being scanned. As the [RuleRunnerSorter] logs the order in which the
* rules are executed, a singleton instance of the class is used to prevent that the logs are flooded with duplicate
* log lines.
*/
private val ruleRunnerSorter = RuleRunnerSorter()
internal class VisitorProvider(
private val params: KtLint.ExperimentalParams,
/**
* Creates a new [RuleRunnerSorter]. Only to be used in unit tests where the same set of rules are used with distinct [Rule.VisitorModifier]s.
*/
recreateRuleSorter: Boolean = false
) : UsesEditorConfigProperties {
override val editorConfigProperties: List<EditorConfigProperty<*>> = listOf(
ktlintDisabledRulesProperty,
disabledRulesProperty
)
/**
* The list of [ruleRunners] is sorted based on the [Rule.VisitorModifier] of the rules.
*/
private val ruleRunners: List<RuleRunner> =
if (recreateRuleSorter) {
RuleRunnerSorter()
} else {
ruleRunnerSorter
}.getSortedRuleRunners(params.ruleRunners, params.debug)
internal fun visitor(editorConfigProperties: EditorConfigProperties): ((rule: Rule, fqRuleId: String) -> Unit) -> Unit {
val enabledRuleRunners =
ruleRunners
.filter { ruleRunner -> isNotDisabled(editorConfigProperties, ruleRunner.qualifiedRuleId) }
if (enabledRuleRunners.isEmpty()) {
if (params.debug && enabledRuleRunners.isEmpty()) {
println(
"[DEBUG] Skipping file as no enabled rules are found to be executed"
)
}
return { _ -> }
}
val ruleRunnersToBeSkipped =
ruleRunners
.filter { ruleRunner ->
val runAfterRule = ruleRunner.runAfterRule
runAfterRule != null &&
runAfterRule.runOnlyWhenOtherRuleIsEnabled &&
enabledRuleRunners.none { it.qualifiedRuleId == runAfterRule.ruleId.toQualifiedRuleId() }
}
if (params.debug && ruleRunnersToBeSkipped.isNotEmpty()) {
ruleRunnersToBeSkipped
.forEach {
println(
"[DEBUG] Skipping rule with id '${it.qualifiedRuleId}'. This rule has to run after rule with " +
"id '${it.runAfterRule?.ruleId?.toQualifiedRuleId()}' and will not run in case that rule is " +
"disabled."
)
}
}
val ruleRunnersToExecute = enabledRuleRunners - ruleRunnersToBeSkipped.toSet()
if (ruleRunnersToExecute.isEmpty()) {
if (params.debug) {
println(
"[DEBUG] Skipping file as no enabled rules are found to be executed"
)
}
return { _ -> }
}
return { visit ->
ruleRunnersToExecute.forEach {
visit(it.getRule(), it.shortenedQualifiedRuleId)
}
}
}
private fun isNotDisabled(editorConfigProperties: EditorConfigProperties, qualifiedRuleId: String): Boolean {
val ktlintDisabledRulesProperty =
if (editorConfigProperties.containsKey(ktlintDisabledRulesProperty.type.name) ||
!editorConfigProperties.containsKey(disabledRulesProperty.type.name)
) {
// New property takes precedence when defined, or, when both old and new property are not defined.
editorConfigProperties.getEditorConfigValue(ktlintDisabledRulesProperty)
} else {
editorConfigProperties.getEditorConfigValue(disabledRulesProperty)
}
return ktlintDisabledRulesProperty
.split(",")
.none {
// The rule set id in the disabled_rules setting may be omitted for rules in the standard rule set
it.toQualifiedRuleId() == qualifiedRuleId
}
}
}