diff --git a/CHANGELOG.md b/CHANGELOG.md index 717dcd5ddb..57a63f5935 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Added ### Fixed +- Do not add a space after the typealias name (`type-parameter-list-spacing`) ([#1435](https://github.com/pinterest/ktlint/issues/1435)) ### Changed * Set Kotlin development version to `1.6.21` and Kotlin version to `1.6.21`. diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRule.kt index 5b7a38e745..47f9502ecf 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRule.kt @@ -4,9 +4,12 @@ import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.CLASS import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY import com.pinterest.ktlint.core.ast.ElementType.CONSTRUCTOR_KEYWORD +import com.pinterest.ktlint.core.ast.ElementType.EQ +import com.pinterest.ktlint.core.ast.ElementType.FUN import com.pinterest.ktlint.core.ast.ElementType.GT import com.pinterest.ktlint.core.ast.ElementType.LT import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR +import com.pinterest.ktlint.core.ast.ElementType.TYPEALIAS import com.pinterest.ktlint.core.ast.ElementType.TYPE_PARAMETER_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.nextCodeSibling @@ -31,10 +34,10 @@ public class TypeParameterListSpacingRule : Rule("type-parameter-list-spacing") return } - if (node.treeParent.elementType == CLASS) { - visitClassDeclaration(node, autoCorrect, emit) - } else { - visitFunctionDeclaration(node, autoCorrect, emit) + when (node.treeParent.elementType) { + CLASS -> visitClassDeclaration(node, autoCorrect, emit) + TYPEALIAS -> visitTypeAliasDeclaration(node, autoCorrect, emit) + FUN -> visitFunctionDeclaration(node, autoCorrect, emit) } visitInsideTypeParameterList(node, autoCorrect, emit) } @@ -77,6 +80,26 @@ public class TypeParameterListSpacingRule : Rule("type-parameter-list-spacing") ?.let { singleSpaceExpected(it, autoCorrect, emit) } } + private fun visitTypeAliasDeclaration( + node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit + ) { + // No white space expected between typealias keyword name and parameter list + // typealias Bar + node + .prevSibling { true } + ?.takeIf { it.elementType == WHITE_SPACE } + ?.let { noWhitespaceExpected(it, autoCorrect, emit) } + + // No white space expected between parameter type list and equals sign + // typealias Bar = ... + node + .nextSibling { true } + ?.takeIf { it.elementType == WHITE_SPACE && it.nextCodeSibling()?.elementType == EQ } + ?.let { singleSpaceExpected(it, autoCorrect, emit) } + } + private fun visitFunctionDeclaration( node: ASTNode, autoCorrect: Boolean, diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRuleTest.kt index efa81076f2..ed229e00e3 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/TypeParameterListSpacingRuleTest.kt @@ -307,4 +307,21 @@ class TypeParameterListSpacingRuleTest { ) assertThat(TypeParameterListSpacingRule().format(code)).isEqualTo(formattedCode) } + + @Test + fun `Given a typealias definition with a type parameter list followed by a parameter list then the redundant spaces are removed`() { + val code = + """ + typealias Bar = () -> T + """.trimIndent() + val formattedCode = + """ + typealias Bar = () -> T + """.trimIndent() + assertThat(TypeParameterListSpacingRule().lint(code)).containsExactly( + LintError(1, 14, "type-parameter-list-spacing", "No whitespace expected at this position"), + LintError(1, 19, "type-parameter-list-spacing", "Expected a single space") + ) + assertThat(TypeParameterListSpacingRule().format(code)).isEqualTo(formattedCode) + } }