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

Added fix in AnnotationNewLineRule #662

Merged
merged 7 commits into from
Dec 25, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ class AnnotationNewLineRule(private val configRules: List<RulesConfig>) : Rule("
}

node.getAllChildrenWithType(ANNOTATION_ENTRY).forEach {
if (!it.isFollowedByNewlineWithComment() || !it.isBeginByNewline()) {
deleteSpaces(it, !it.isFollowedByNewlineWithComment(), !it.isBeginByNewline())
if (!it.isFollowedByNewlineWithComment() || !it.isBeginNewLineWithComment()) {
deleteSpaces(it, !it.isFollowedByNewlineWithComment())
}
}
}

// fixme added handle for left side!
private fun deleteSpaces(node: ASTNode,
rightSide: Boolean,
leftSide: Boolean) {
rightSide: Boolean) {
Warnings.ANNOTATION_NEW_LINE.warnAndFix(configRules, emitWarn, isFixMode, "${node.text} not on a single line",
node.startOffset, node) {
if (rightSide) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.OVERRIDE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.PRIVATE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.PROTECTED_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.PUBLIC_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isLeaf
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.lineNumber
import com.pinterest.ktlint.core.ast.parent
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand Down Expand Up @@ -169,6 +170,14 @@ fun ASTNode.isBeginByNewline() =
(it.treePrev.elementType == IMPORT_LIST && it.treePrev.isLeaf() && it.treePrev.treePrev.isLeaf())
} ?: false

/**
* Checks if there is a newline before this element or before comment before. See [isBeginByNewline] for motivation on parents check.
*/
fun ASTNode.isBeginNewLineWithComment() =
isBeginByNewline() || siblings(forward = false).takeWhile { !it.textContains('\n') }.toList().run {
all { it.isWhiteSpace() || it.isPartOfComment() } && isNotEmpty()
}

/**
* checks if the node has corresponding child with elementTyp
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,33 @@ class AnnotationNewLineRuleWarnTest : LintTestBase(::AnnotationNewLineRule) {

@Test
@Tag(WarningNames.ANNOTATION_NEW_LINE)
fun `should fix annotation on same line with import`() {
fun `should warn annotation for several annotations`() {
lintMethod(
"""
|import qwe.qwe;@ExperimentalStdlibApi
| @Hello
| override fun checkNode() {}
""".trimMargin(),
LintError(1, 16, ruleId, "${Warnings.ANNOTATION_NEW_LINE.warnText()} @ExperimentalStdlibApi not on a single line", true),
)
}

@Test
@Tag(WarningNames.ANNOTATION_NEW_LINE)
fun `should fix annotation on same line with package`() {
lintMethod(
"""
|package dfgh.dfgh;@ExperimentalStdlibApi
| @Hello
| override fun checkNode() {}
|@ExperimentalStdlibApi /* */ @Hello
|override fun checkNode() {}
|
|/* */ @Goo
|class A {}
|
|@A1
|/* */ @A2
|@A3
|class A {}
|
|
|@Foo class Foo {}
|
|@Foo
|class Foo {}
|
|@Foo @Goo val loader: DataLoader
|
|@Foo
|@goo val loader: DataLoader
""".trimMargin(),
LintError(1, 19, ruleId, "${Warnings.ANNOTATION_NEW_LINE.warnText()} @ExperimentalStdlibApi not on a single line", true),
LintError(1, 1, ruleId, "${Warnings.ANNOTATION_NEW_LINE.warnText()} @ExperimentalStdlibApi not on a single line", true),
LintError(1, 32, ruleId, "${Warnings.ANNOTATION_NEW_LINE.warnText()} @Hello not on a single line", true),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
@Hello() // hello
@Hi
class A {
}
}
@Foo
/* */ @Goo
/* */ @Qwe
class AA {}
/** */ @Foo
/** */ @Gg
class Bb {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@Hello() // hello
@Hi
class A {
}
}
@Foo /* */ @Goo /* */ @Qwe class AA {}
/** */ @Foo /** */ @Gg class Bb {}