Skip to content

Commit

Permalink
Fixed no-semi removing ; after companion object (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Oct 2, 2018
1 parent bc1d51f commit 7241459
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,4 @@ class ChainWrappingRule : Rule("chain-wrapping") {

private fun ASTNode.isPartOfWhenCondition() =
treeParent?.treeParent?.treeParent?.elementType == KtNodeTypes.WHEN_CONDITION_EXPRESSION

private fun PsiElement.nextLeafIgnoringWhitespaceAndComments() =
this.nextLeaf { it.node.elementType != KtTokens.WHITE_SPACE && !it.isPartOf(PsiComment::class) }

private fun PsiElement.prevLeafIgnoringWhitespaceAndComments() =
this.prevLeaf { it.node.elementType != KtTokens.WHITE_SPACE && !it.isPartOf(PsiComment::class) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtEnumEntry

class NoSemicolonsRule : Rule("no-semi") {
Expand All @@ -20,6 +21,10 @@ class NoSemicolonsRule : Rule("no-semi") {
!node.isPartOf(KtEnumEntry::class)) {
val nextLeaf = PsiTreeUtil.nextLeaf(node, true)
if (doesNotRequirePreSemi(nextLeaf)) {
if (node.psi.prevLeafIgnoringWhitespaceAndComments()?.node?.elementType == KtTokens.OBJECT_KEYWORD) {
// https://github.com/shyiko/ktlint/issues/281
return
}
emit(node.startOffset, "Unnecessary semicolon", true)
if (autoCorrect) {
node.treeParent.removeChild(node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.shyiko.ktlint.ruleset.standard

import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
import kotlin.reflect.KClass

internal fun PsiElement.isPartOf(clazz: KClass<out PsiElement>) = getNonStrictParentOfType(clazz.java) != null
Expand All @@ -18,6 +22,10 @@ internal fun PsiElement.isPartOfMultiLineString(): Boolean {
}
internal fun PsiElement.prevLeaf(): PsiElement? = PsiTreeUtil.prevLeaf(this)
internal fun PsiElement.nextLeaf(): PsiElement? = PsiTreeUtil.nextLeaf(this)
internal fun PsiElement.nextLeafIgnoringWhitespaceAndComments() =
this.nextLeaf { it.node.elementType != KtTokens.WHITE_SPACE && !it.isPartOf(PsiComment::class) }
internal fun PsiElement.prevLeafIgnoringWhitespaceAndComments() =
this.prevLeaf { it.node.elementType != KtTokens.WHITE_SPACE && !it.isPartOf(PsiComment::class) }
internal fun ASTNode.visit(cb: (node: ASTNode) -> Unit) {
cb(this)
this.getChildren(null).forEach { it.visit(cb) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ class NoSemicolonsRuleTest {
""".trimIndent()
)
}

@Test
fun testSemiIsPreservedAfterCompanionObject() {
// github issue #281
assertThat(NoSemicolonsRule().lint(
"""
class A {
companion object;
companion object ;
}
class A {
companion object {
val s = ""
};
}
""".trimIndent()
)).isEqualTo(listOf(
LintError(8, 6, "no-semi", "Unnecessary semicolon")
))
}
}

0 comments on commit 7241459

Please sign in to comment.