Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude Kotlin Android extensions from Wildcard import rule #16

Merged
merged 1 commit into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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