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

Fix bug in parseImportsLayout: trim spaces in entries #1770

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

* Trim spaces in the `.editorconfig` property `ij_kotlin_imports_layout`'s entries ([#1770](https://github.com/pinterest/ktlint/pull/1770))

### Changed

## [0.48.1] - 2023-01-03
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal const val ALIAS_CHAR = "^"
* Adapted from https://github.com/JetBrains/intellij-kotlin/blob/73b5a484198f02518c9ece2fb453d27cead680fb/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinPackageEntryTableAccessor.kt#L27-L43
*/
internal fun parseImportsLayout(importsLayout: String): List<PatternEntry> {
val importsList = importsLayout.split(",").onEach { it.trim() }
val importsList = importsLayout.split(",").map { it.trim() }

if (importsList.first() == BLANK_LINE_CHAR || importsList.last() == BLANK_LINE_CHAR) {
throw IllegalArgumentException("Blank lines are not supported in the beginning or end of import list")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.pinterest.ktlint.ruleset.standard.internal.importordering.parseImport
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class ImportLayoutParserTest {
@Test
Expand All @@ -21,9 +23,17 @@ class ImportLayoutParserTest {
}.isInstanceOf(IllegalArgumentException::class.java)
}

@Test
fun `parses correctly`() {
val expected = listOf(
@ParameterizedTest(name = "Imports layout: {0}")
@ValueSource(
strings = [
"android.**,|,org.junit.**,|,^android.**,*,kotlin.io.Closeable.*,^",
"android.**, |, org.junit.**, |, ^android.**, *, kotlin.io.Closeable.*, ^",
],
)
fun `Given some imports layout then parse it correctly`(importsLayout: String) {
val actual = parseImportsLayout(importsLayout)

assertThat(actual).containsExactly(
PatternEntry("android.*", withSubpackages = true, hasAlias = false),
PatternEntry.BLANK_LINE_ENTRY,
PatternEntry("org.junit.*", withSubpackages = true, hasAlias = false),
Expand All @@ -33,8 +43,5 @@ class ImportLayoutParserTest {
PatternEntry("kotlin.io.Closeable.*", withSubpackages = false, hasAlias = false),
PatternEntry.ALL_OTHER_ALIAS_IMPORTS_ENTRY,
)
val actual = parseImportsLayout("android.**,|,org.junit.**,|,^android.**,*,kotlin.io.Closeable.*,^")

assertThat(actual).isEqualTo(expected)
}
}