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
|
package com.github.shyiko.ktlint.core
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtImportDirective
import org.testng.annotations.Test
import java.util.ArrayList
class ErrorSuppressionTest {
@Test
fun testErrorSuppression() {
class NoWildcardImportsRule : Rule("no-wildcard-imports") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, corrected: Boolean) -> Unit
) {
if (node is LeafPsiElement && node.textMatches("*") &&
PsiTreeUtil.getNonStrictParentOfType(node, KtImportDirective::class.java) != null) {
emit(node.startOffset, "Wildcard import", false)
}
}
}
fun lint(text: String) =
ArrayList<LintError>().apply {
KtLint.lint(text, listOf(RuleSet("standard", NoWildcardImportsRule()))) { e -> add(e) }
}
assertThat(lint(
"""
import a.* // ktlint-disable
import a.* // will trigger an error
""".trimIndent()
)).isEqualTo(listOf(
LintError(2, 10, "no-wildcard-imports", "Wildcard import")
))
assertThat(lint(
"""
import a.* // ktlint-disable no-wildcard-imports
import a.* // will trigger an error
""".trimIndent()
)).isEqualTo(listOf(
LintError(2, 10, "no-wildcard-imports", "Wildcard import")
))
assertThat(lint(
"""
/* ktlint-disable */
import a.*
import a.*
/* ktlint-enable */
import a.* // will trigger an error
""".trimIndent()
)).isEqualTo(listOf(
LintError(5, 10, "no-wildcard-imports", "Wildcard import")
))
assertThat(lint(
"""
/* ktlint-disable no-wildcard-imports */
import a.*
import a.*
/* ktlint-enable no-wildcard-imports */
import a.* // will trigger an error
""".trimIndent()
)).isEqualTo(listOf(
LintError(5, 10, "no-wildcard-imports", "Wildcard import")
))
}
}
|