Skip to content

Commit

Permalink
Merge pull request #16 from kunny/exclude_android_extensions
Browse files Browse the repository at this point in the history
Exclude Kotlin Android extensions from Wildcard import rule
  • Loading branch information
shyiko authored Oct 11, 2016
2 parents ed3af45 + d60d2cf commit cc4f1b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package com.gihub.shyiko.ktlint.ruleset.standard

import com.github.shyiko.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes

class NoWildcardImportsRule : Rule("no-wildcard-imports") {

override fun visit(node: ASTNode, autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
if (node is LeafPsiElement && node.textMatches("*") && node.isPartOf(KtImportDirective::class)) {
emit(node.startOffset, "Wildcard import", false)
if (node.elementType == KtStubElementTypes.IMPORT_DIRECTIVE) {
val importDirective = node.psi as KtImportDirective
val path = importDirective.importPath?.pathStr
if (path != null && !path.startsWith("kotlinx.android.synthetic") && path.contains('*')) {
emit(node.startOffset, "Wildcard import", false)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class NoWildcardImportsRuleTest {
import a.*
import a.b.c.*
import a.b
import kotlinx.android.synthetic.main.layout_name.*
""".trimIndent()
)).isEqualTo(listOf(
LintError(1, 10, "no-wildcard-imports", "Wildcard import"),
LintError(2, 14, "no-wildcard-imports", "Wildcard import")
LintError(1, 1, "no-wildcard-imports", "Wildcard import"),
LintError(2, 1, "no-wildcard-imports", "Wildcard import")
))
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<version>1.0.0</version>
<configuration>
<usage>
# build "./ktlitn/target/ktlint" (executable jar)
# build "./ktlint/target/ktlint" (executable jar)
./mvnw -Pcapsule clean package -Dmaven.test.skip=true

# test (&amp; check code style)
Expand Down

0 comments on commit cc4f1b7

Please sign in to comment.