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

Do not add a space after the typealias name (type-parameter-list-spacing) #1450

Merged
merged 1 commit into from
Apr 26, 2022
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
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)
}
}