Skip to content

Commit

Permalink
Do not add a space after the typealias name (`type-parameter-list-spa…
Browse files Browse the repository at this point in the history
…cing`)
  • Loading branch information
t-kameyama committed Apr 26, 2022
1 parent a52ff9a commit 0ae0c9c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 <T>
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<T> = ...
node
.nextSibling { true }
?.takeIf { it.elementType == WHITE_SPACE && it.nextCodeSibling()?.elementType == EQ }
?.let { singleSpaceExpected(it, autoCorrect, emit) }
}

private fun visitFunctionDeclaration(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> = () -> T
""".trimIndent()
val formattedCode =
"""
typealias Bar<T> = () -> 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)
}
}

0 comments on commit 0ae0c9c

Please sign in to comment.