Skip to content

Commit

Permalink
Add 'allowed_wildcard_imports' configuration to editorconfig to make …
Browse files Browse the repository at this point in the history
…'no-wildcard-imports' more granularly configurable
  • Loading branch information
asmadsen committed Nov 5, 2021
1 parent d044ffe commit 54edcd3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## Unreleased

### Added
- New `allowed_wildcard_imports` configuration to be able to allow certain wildcard imports.

### Fixed

### Changed

This is to make developing with libraries like ktor or kotlin-react more pleasant. These libraries rely heavily on extension functions, which in turn adds a lot of imports.

### Removed

## [0.43.0] - 2021-11-02
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ ij_kotlin_imports_layout=android.**,|,^org.junit.**,kotlin.io.Closeable.*,|,*,^
# backticks to be longer than the maximum line length. (Since 0.41.0)
[**/test/**.kt]
ktlint_ignore_back_ticked_identifier=true

# Comma-separated list of allowed wildcard imports that will override the no-wildcard-imports rule.
# This can be used for allowing wildcard imports from libraries like Ktor where extension functions are used in a way that creates a lot of imports.
allowed_wildcard_imports=io.ktor.application.*,io.ktor.routing.*,react.*
```

### Overriding Editorconfig properties for specific directories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface EditorConfig {
val indentSize: Int
val tabWidth: Int
val maxLineLength: Int
val allowedWildcardImports: Set<String>

@Deprecated(
message = "Not used anymore by rules, please use 'insert_final_newline' directly via get()"
Expand All @@ -33,12 +34,14 @@ interface EditorConfig {
val tabWidth = map["indent_size"]?.toIntOrNull()
val maxLineLength = map["max_line_length"]?.toIntOrNull() ?: -1
val insertFinalNewline = map["insert_final_newline"]?.toBoolean() ?: true
val allowedWildcardImports = map["allowed_wildcard_imports"]?.split(',')?.toSet() ?: emptySet()
return object : EditorConfig {
override val indentStyle = indentStyle
override val indentSize = indentSize
override val tabWidth = tabWidth ?: indentSize
override val maxLineLength = maxLineLength
override val insertFinalNewline = insertFinalNewline
override val allowedWildcardImports = allowedWildcardImports
override fun get(key: String): String? = map[key]
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.IMPORT_DIRECTIVE
import com.pinterest.ktlint.core.ast.isRoot
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtImportDirective

class NoWildcardImportsRule : Rule("no-wildcard-imports") {
private var allowedWildcardImports: Set<String> = emptySet()

override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.isRoot()) {
val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY)!!
allowedWildcardImports = editorConfig.allowedWildcardImports
}
if (node.elementType == IMPORT_DIRECTIVE) {
val importDirective = node.psi as KtImportDirective
val path = importDirective.importPath
if (path != null && path.isAllUnder && !path.pathStr.startsWith("kotlinx.android.synthetic")) {
if (path != null && path.isAllUnder && !path.pathStr.startsWith("kotlinx.android.synthetic") && !allowedWildcardImports.contains(path.pathStr)) {
emit(node.startOffset, "Wildcard import", false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,29 @@ class NoWildcardImportsRuleTest {
)
)
}

@Test
fun testAllowedWildcardImports() {
assertThat(
NoWildcardImportsRule().lint(
"""
import a.*
import a.b.c.*
import a.b
import kotlinx.android.synthetic.main.layout_name.*
import foo.bar.`**`
import react.*
import react.dom.*
import kotlinx.css.*
""".trimIndent(),
userData = mapOf("allowed_wildcard_imports" to "react.*,react.dom.*")
)
).isEqualTo(
listOf(
LintError(1, 1, "no-wildcard-imports", "Wildcard import"),
LintError(2, 1, "no-wildcard-imports", "Wildcard import"),
LintError(8, 1, "no-wildcard-imports", "Wildcard import")
)
)
}
}

0 comments on commit 54edcd3

Please sign in to comment.