Skip to content

Commit

Permalink
Remove logic which was moved to new rule 'kdoc'. (#2549)
Browse files Browse the repository at this point in the history
Relates #2547
  • Loading branch information
paul-dingemans authored Feb 13, 2024
1 parent 1b7c449 commit 22ba7c1
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PROJECTION
import com.pinterest.ktlint.rule.engine.core.api.RuleId
Expand Down Expand Up @@ -67,10 +66,6 @@ public class TypeArgumentCommentRule : StandardRule("type-argument-comment") {
}
}
}

KDOC -> {
emit(node.startOffset, "A KDoc is not allowed inside a 'type_argument_list'", false)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.RuleId
Expand Down Expand Up @@ -67,10 +66,6 @@ public class TypeParameterCommentRule : StandardRule("type-parameter-comment") {
}
}
}

KDOC -> {
emit(node.startOffset, "A KDoc is not allowed inside a 'type_parameter_list'", false)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT_LIST
import com.pinterest.ktlint.rule.engine.core.api.RuleId
Expand Down Expand Up @@ -30,52 +27,44 @@ public class ValueArgumentCommentRule : StandardRule("value-argument-comment") {
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
) {
if (node.isPartOfComment() && node.treeParent.elementType in valueArgumentTokenSet) {
when (node.elementType) {
EOL_COMMENT, BLOCK_COMMENT -> {
if (node.treeParent.elementType == VALUE_ARGUMENT) {
// Disallow:
// val foo = foo(
// bar /* some comment */ = "bar"
// )
// or
// val foo = foo(
// bar =
// // some comment
// "bar"
// )
emit(
node.startOffset,
"A (block or EOL) comment inside or on same line after a 'value_argument' is not allowed. It may be placed " +
"on a separate line above.",
false,
)
} else if (node.treeParent.elementType == VALUE_ARGUMENT_LIST) {
if (node.prevLeaf().isWhiteSpaceWithNewline()) {
// Allow:
// val foo = foo(
// // some comment
// bar = "bar"
// )
} else {
// Disallow
// class Foo(
// val bar1: Bar, // some comment 1
// // some comment 2
// val bar2: Bar,
// )
// It is not clear whether "some comment 2" belongs to bar1 as a continuation of "some comment 1" or that it belongs to
// bar2. Note both comments are direct children of the value_argument_list.
emit(
node.startOffset,
"A comment in a 'value_argument_list' is only allowed when placed on a separate line",
false,
)
}
}
}

KDOC -> {
emit(node.startOffset, "A KDoc is not allowed inside a 'value_argument_list'", false)
if (node.treeParent.elementType == VALUE_ARGUMENT) {
// Disallow:
// val foo = foo(
// bar /* some comment */ = "bar"
// )
// or
// val foo = foo(
// bar =
// // some comment
// "bar"
// )
emit(
node.startOffset,
"A (block or EOL) comment inside or on same line after a 'value_argument' is not allowed. It may be placed " +
"on a separate line above.",
false,
)
} else if (node.treeParent.elementType == VALUE_ARGUMENT_LIST) {
if (node.prevLeaf().isWhiteSpaceWithNewline()) {
// Allow:
// val foo = foo(
// // some comment
// bar = "bar"
// )
} else {
// Disallow
// class Foo(
// val bar1: Bar, // some comment 1
// // some comment 2
// val bar2: Bar,
// )
// It is not clear whether "some comment 2" belongs to bar1 as a continuation of "some comment 1" or that it belongs to
// bar2. Note both comments are direct children of the value_argument_list.
emit(
node.startOffset,
"A comment in a 'value_argument_list' is only allowed when placed on a separate line",
false,
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
Expand Down Expand Up @@ -30,82 +28,49 @@ public class ValueParameterCommentRule : StandardRule("value-parameter-comment")
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
) {
if (node.isPartOfComment() && node.treeParent.elementType in valueParameterTokenSet) {
when (node.elementType) {
EOL_COMMENT, BLOCK_COMMENT -> {
if (node.treeParent.elementType == VALUE_PARAMETER) {
// Disallow:
// class Foo(
// bar /* some comment */ = "bar"
// )
// or
// class Foo(
// bar =
// // some comment
// "bar"
// )
emit(
node.startOffset,
"A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed " +
"on a separate line above.",
false,
)
} else if (node.treeParent.elementType == VALUE_PARAMETER_LIST) {
if (node.prevLeaf().isWhiteSpaceWithNewline()) {
// Allow:
// class Foo(
// // some comment
// bar = "bar"
// )
} else {
// Disallow
// class Foo(
// val bar1: Bar, // some comment 1
// // some comment 2
// val bar2: Bar,
// )
// It is not clear whether "some comment 2" belongs to bar1 as a continuation of "some comment 1" or that it belongs to
// bar2. Note both comments are direct children of the value_parameter_list.
emit(
node.startOffset,
"A comment in a 'value_parameter_list' is only allowed when placed on a separate line",
false,
)
}
}
if (node.treeParent.elementType == VALUE_PARAMETER) {
if (node.elementType == KDOC && node.treeParent.firstChildNode == node) {
// Allow KDoc to be the first element of a value parameter. EOL and Block comments are not parsed as the first child of
// the value parameter but as a child of the value parameter list
} else {
// Disallow:
// class Foo(
// bar /* some comment */ = "bar"
// )
// or
// class Foo(
// bar =
// // some comment
// "bar"
// )
emit(
node.startOffset,
"A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line " +
"above.",
false,
)
}

KDOC -> {
if (node.treeParent.elementType == VALUE_PARAMETER) {
if (node == node.treeParent.firstChildNode) {
// Allow
// class Foo(
// /** some comment */
// val bar: Bar,
// )
} else {
// Disallow a kdoc inside a VALUE_PARAMETER.
// class Foo(
// val bar:
// /** some comment */
// Bar,
// )
// or
// class Foo(
// val bar: Bar /** some comment */
// )
emit(
node.startOffset,
"A kdoc in a 'value_parameter' is only allowed when placed on a new line before this element",
false,
)
}
} else {
emit(
node.startOffset,
"A KDoc is not allowed inside a 'value_parameter_list' when not followed by a property",
false,
)
}
} else if (node.treeParent.elementType == VALUE_PARAMETER_LIST) {
if (node.prevLeaf().isWhiteSpaceWithNewline()) {
// Allow:
// class Foo(
// // some comment
// bar = "bar"
// )
} else {
// Disallow
// class Foo(
// val bar1: Bar, // some comment 1
// // some comment 2
// val bar2: Bar,
// )
// It is not clear whether "some comment 2" belongs to bar1 as a continuation of "some comment 1" or that it belongs to
// bar2. Note both comments are direct children of the value_parameter_list.
emit(
node.startOffset,
"A comment in a 'value_parameter_list' is only allowed when placed on a separate line",
false,
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ class FunctionSignatureRuleTest {
.addAdditionalRuleProvider { ValueParameterCommentRule() }
.hasLintViolationsForAdditionalRule(
LintViolation(5, 17, "A comment in a 'value_parameter_list' is only allowed when placed on a separate line", false),
LintViolation(6, 18, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(7, 19, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(8, 23, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(6, 18, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(7, 19, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(8, 23, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(13, 13, "A comment in a 'value_parameter_list' is only allowed when placed on a separate line", false),
).hasNoLintViolationsExceptInAdditionalRules()
}
Expand All @@ -252,9 +252,9 @@ class FunctionSignatureRuleTest {
functionSignatureWrappingRuleAssertThat(code)
.addAdditionalRuleProvider { ValueParameterCommentRule() }
.hasLintViolationsForAdditionalRule(
LintViolation(1, 18, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(2, 19, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(3, 23, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(1, 18, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(2, 19, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(3, 23, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false),
LintViolation(5, 13, "A comment in a 'value_parameter_list' is only allowed when placed on a separate line", false),
).hasNoLintViolationsExceptInAdditionalRules()
// When ValueParameterCommentRule is not loaded or disabled:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import org.junit.jupiter.api.Test
class TypeArgumentCommentRuleTest {
private val typeArgumentCommentRuleAssertThat = assertThatRule { TypeArgumentCommentRule() }

@Test
fun `Given a kdoc inside a type projection`() {
val code =
"""
fun Foo<out /** some comment */ Any>.foo() {}
""".trimIndent()
typeArgumentCommentRuleAssertThat(code)
.hasLintViolationWithoutAutoCorrect(1, 13, "A KDoc is not allowed inside a 'type_argument_list'")
}

@Test
fun `Given a block comment inside a type projection`() {
val code =
Expand All @@ -40,18 +30,6 @@ class TypeArgumentCommentRuleTest {
.hasLintViolationWithoutAutoCorrect(1, 13, "A (block or EOL) comment inside or on same line after a 'type_projection' is not allowed. It may be placed on a separate line above.")
}

@Test
fun `Given a kdoc as child of type argument list`() {
val code =
"""
val fooBar: FooBar<
/** some comment */
Foo, Bar>
""".trimIndent()
typeArgumentCommentRuleAssertThat(code)
.hasLintViolationWithoutAutoCorrect(2, 5, "A KDoc is not allowed inside a 'type_argument_list'")
}

@Test
fun `Given a comment on separate line before type projection ast node`() {
val code =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import org.junit.jupiter.api.Test
class TypeParameterCommentRuleTest {
private val typeParameterCommentRuleAssertThat = assertThatRule { TypeParameterCommentRule() }

@Test
fun `Given a kdoc inside a type parameter`() {
val code =
"""
class Foo<in /** some comment */ Bar>
""".trimIndent()
typeParameterCommentRuleAssertThat(code)
.hasLintViolationWithoutAutoCorrect(1, 14, "A KDoc is not allowed inside a 'type_parameter_list'")
}

@Test
fun `Given a block comment inside a type parameter`() {
val code =
Expand All @@ -40,18 +30,6 @@ class TypeParameterCommentRuleTest {
.hasLintViolationWithoutAutoCorrect(1, 14, "A (block or EOL) comment inside or on same line after a 'type_parameter' is not allowed. It may be placed on a separate line above.")
}

@Test
fun `Given a kdoc as child of type parameter list`() {
val code =
"""
class Foo<
/** some comment */
Bar>
""".trimIndent()
typeParameterCommentRuleAssertThat(code)
.hasLintViolationWithoutAutoCorrect(2, 5, "A KDoc is not allowed inside a 'type_parameter_list'")
}

@Test
fun `Given a comment on separate line before type parameter ast node`() {
val code =
Expand Down
Loading

0 comments on commit 22ba7c1

Please sign in to comment.