Skip to content

Commit

Permalink
Merge branch 'master' into 2257-indent
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-dingemans authored Sep 17, 2023
2 parents 0d24f4a + 719c4cb commit 4396750
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
22 changes: 12 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

* Fix ktlint cli parameter `--code-style` [#2238](https://github.com/pinterest/ktlint/pull/2238)
* Fix indent of multiline object declaration inside class `indent` [#2257](https://github.com/pinterest/ktlint/issue/2257)
* Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260)

### Changed

Expand All @@ -24,16 +26,16 @@ This project adheres to [Semantic Versioning](https://semver.org/).

| Old Maven coordinates | New Maven coordinates |
|----------------------------------------------------|--------------------------------------------------------|
| com.pinterest.ktlint | com.pinterest.ktlint.ktlint-cli |
| com.pinterest.ktlint.ktlint-reporter-baseline | com.pinterest.ktlint.ktlint-cli-reporter-baseline |
| com.pinterest.ktlint.ktlint-reporter-checkstyle | com.pinterest.ktlint.ktlint-cli-reporter-checkstyle |
| com.pinterest.ktlint.ktlint-cli-reporter | com.pinterest.ktlint.ktlint-cli-reporter-core |
| com.pinterest.ktlint.ktlint-reporter-format | com.pinterest.ktlint.ktlint-cli-reporter-format |
| com.pinterest.ktlint.ktlint-reporter-html | com.pinterest.ktlint.ktlint-cli-reporter-html |
| com.pinterest.ktlint.ktlint-reporter-json | com.pinterest.ktlint.ktlint-cli-reporter-json |
| com.pinterest.ktlint.ktlint-reporter-plain | com.pinterest.ktlint.ktlint-cli-reporter-plain |
| com.pinterest.ktlint.ktlint-reporter-plain-summary | com.pinterest.ktlint.ktlint-cli-reporter-plain-summary |
| com.pinterest.ktlint.ktlint-reporter-sarif | com.pinterest.ktlint.ktlint-cli-reporter-sarif |
| com.pinterest.ktlint | com.pinterest.ktlint:ktlint-cli |
| com.pinterest.ktlint:ktlint-reporter-baseline | com.pinterest.ktlint:ktlint-cli-reporter-baseline |
| com.pinterest.ktlint:ktlint-reporter-checkstyle | com.pinterest.ktlint:ktlint-cli-reporter-checkstyle |
| com.pinterest.ktlint:ktlint-cli-reporter | com.pinterest.ktlint:ktlint-cli-reporter-core |
| com.pinterest.ktlint:ktlint-reporter-format | com.pinterest.ktlint:ktlint-cli-reporter-format |
| com.pinterest.ktlint:ktlint-reporter-html | com.pinterest.ktlint:ktlint-cli-reporter-html |
| com.pinterest.ktlint:ktlint-reporter-json | com.pinterest.ktlint:ktlint-cli-reporter-json |
| com.pinterest.ktlint:ktlint-reporter-plain | com.pinterest.ktlint:ktlint-cli-reporter-plain |
| com.pinterest.ktlint:ktlint-reporter-plain-summary | com.pinterest.ktlint:ktlint-cli-reporter-plain-summary |
| com.pinterest.ktlint:ktlint-reporter-sarif | com.pinterest.ktlint:ktlint-cli-reporter-sarif |


* Add binary compatibility validator - [#2131](https://github.com/pinterest/ktlint/pull/2131), by @mateuszkwiecinski
Expand Down
20 changes: 10 additions & 10 deletions documentation/release-latest/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ The Maven coordinates of modules below have been changed:

| Old Maven coordinates | New Maven coordinates |
|------------------------------------------------------|----------------------------------------------------------|
| `com.pinterest.ktlint` | `com.pinterest.ktlint.ktlint-cli` |
| `com.pinterest.ktlint.ktlint-reporter-baseline` | `com.pinterest.ktlint.ktlint-cli-reporter-baseline` |
| `com.pinterest.ktlint.ktlint-reporter-checkstyle` | `com.pinterest.ktlint.ktlint-cli-reporter-checkstyle` |
| `com.pinterest.ktlint.ktlint-cli-reporter` | `com.pinterest.ktlint.ktlint-cli-reporter-core` |
| `com.pinterest.ktlint.ktlint-reporter-format` | `com.pinterest.ktlint.ktlint-cli-reporter-format` |
| `com.pinterest.ktlint.ktlint-reporter-html` | `com.pinterest.ktlint.ktlint-cli-reporter-html` |
| `com.pinterest.ktlint.ktlint-reporter-json` | `com.pinterest.ktlint.ktlint-cli-reporter-json` |
| `com.pinterest.ktlint.ktlint-reporter-plain` | `com.pinterest.ktlint.ktlint-cli-reporter-plain` |
| `com.pinterest.ktlint.ktlint-reporter-plain-summary` | `com.pinterest.ktlint.ktlint-cli-reporter-plain-summary` |
| `com.pinterest.ktlint.ktlint-reporter-sarif` | `com.pinterest.ktlint.ktlint-cli-reporter-sarif` |
| `com.pinterest:ktlint` | `com.pinterest.ktlint:ktlint-cli` |
| `com.pinterest.ktlint:ktlint-reporter-baseline` | `com.pinterest.ktlint:ktlint-cli-reporter-baseline` |
| `com.pinterest.ktlint:ktlint-reporter-checkstyle` | `com.pinterest.ktlint:ktlint-cli-reporter-checkstyle` |
| `com.pinterest.ktlint:ktlint-cli-reporter` | `com.pinterest.ktlint:ktlint-cli-reporter-core` |
| `com.pinterest.ktlint:ktlint-reporter-format` | `com.pinterest.ktlint:ktlint-cli-reporter-format` |
| `com.pinterest.ktlint:ktlint-reporter-html` | `com.pinterest.ktlint:ktlint-cli-reporter-html` |
| `com.pinterest.ktlint:ktlint-reporter-json` | `com.pinterest.ktlint:ktlint-cli-reporter-json` |
| `com.pinterest.ktlint:ktlint-reporter-plain` | `com.pinterest.ktlint:ktlint-cli-reporter-plain` |
| `com.pinterest.ktlint:ktlint-reporter-plain-summary` | `com.pinterest.ktlint:ktlint-cli-reporter-plain-summary` |
| `com.pinterest.ktlint:ktlint-reporter-sarif` | `com.pinterest.ktlint:ktlint-cli-reporter-sarif` |

## How do I enable or disable a rule?

Expand Down
20 changes: 10 additions & 10 deletions documentation/snapshot/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ The Maven coordinates of modules below have been changed:

| Old Maven coordinates | New Maven coordinates |
|------------------------------------------------------|----------------------------------------------------------|
| `com.pinterest.ktlint` | `com.pinterest.ktlint.ktlint-cli` |
| `com.pinterest.ktlint.ktlint-reporter-baseline` | `com.pinterest.ktlint.ktlint-cli-reporter-baseline` |
| `com.pinterest.ktlint.ktlint-reporter-checkstyle` | `com.pinterest.ktlint.ktlint-cli-reporter-checkstyle` |
| `com.pinterest.ktlint.ktlint-cli-reporter` | `com.pinterest.ktlint.ktlint-cli-reporter-core` |
| `com.pinterest.ktlint.ktlint-reporter-format` | `com.pinterest.ktlint.ktlint-cli-reporter-format` |
| `com.pinterest.ktlint.ktlint-reporter-html` | `com.pinterest.ktlint.ktlint-cli-reporter-html` |
| `com.pinterest.ktlint.ktlint-reporter-json` | `com.pinterest.ktlint.ktlint-cli-reporter-json` |
| `com.pinterest.ktlint.ktlint-reporter-plain` | `com.pinterest.ktlint.ktlint-cli-reporter-plain` |
| `com.pinterest.ktlint.ktlint-reporter-plain-summary` | `com.pinterest.ktlint.ktlint-cli-reporter-plain-summary` |
| `com.pinterest.ktlint.ktlint-reporter-sarif` | `com.pinterest.ktlint.ktlint-cli-reporter-sarif` |
| `com.pinterest:ktlint` | `com.pinterest.ktlint:ktlint-cli` |
| `com.pinterest.ktlint:ktlint-reporter-baseline` | `com.pinterest.ktlint:ktlint-cli-reporter-baseline` |
| `com.pinterest.ktlint:ktlint-reporter-checkstyle` | `com.pinterest.ktlint:ktlint-cli-reporter-checkstyle` |
| `com.pinterest.ktlint:ktlint-cli-reporter` | `com.pinterest.ktlint:ktlint-cli-reporter-core` |
| `com.pinterest.ktlint:ktlint-reporter-format` | `com.pinterest.ktlint:ktlint-cli-reporter-format` |
| `com.pinterest.ktlint:ktlint-reporter-html` | `com.pinterest.ktlint:ktlint-cli-reporter-html` |
| `com.pinterest.ktlint:ktlint-reporter-json` | `com.pinterest.ktlint:ktlint-cli-reporter-json` |
| `com.pinterest.ktlint:ktlint-reporter-plain` | `com.pinterest.ktlint:ktlint-cli-reporter-plain` |
| `com.pinterest.ktlint:ktlint-reporter-plain-summary` | `com.pinterest.ktlint:ktlint-cli-reporter-plain-summary` |
| `com.pinterest.ktlint:ktlint-reporter-sarif` | `com.pinterest.ktlint:ktlint-cli-reporter-sarif` |

## How do I enable or disable a rule?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_INITIALIZER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EQ
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.LBRACE
Expand Down Expand Up @@ -116,10 +117,17 @@ public class BlankLineBeforeDeclarationRule :
return
}

if (node.elementType == FUN && node.prevCodeSibling()?.elementType == EQ) {
// Allow:
// val foo =
// fun(): String {
// return "foo"
// }
return
}

node
.takeIf { it.psi is KtDeclaration }
// (node.psi as KtDeclaration)
// .node
?.takeIf {
val prevLeaf = it.prevLeaf()
prevLeaf != null && (!prevLeaf.isWhiteSpace() || !prevLeaf.text.startsWith("\n\n"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IMPORT_DIRECTIVE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.ruleset.standard.StandardRule
import com.pinterest.ktlint.ruleset.standard.rules.internal.regExIgnoringDiacriticsAndStrokesOnLetters
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand Down Expand Up @@ -43,7 +46,8 @@ public class FunctionNamingRule : StandardRule("function-naming") {
?.takeUnless {
node.isFactoryMethod() ||
node.isTestMethod() ||
node.hasValidFunctionName()
node.hasValidFunctionName() ||
node.isAnonymousFunction()
}?.let {
val identifierOffset =
node
Expand Down Expand Up @@ -76,6 +80,12 @@ public class FunctionNamingRule : StandardRule("function-naming") {
.orEmpty()
.matches(VALID_FUNCTION_NAME_REGEXP)

private fun ASTNode.isAnonymousFunction() =
VALUE_PARAMETER_LIST ==
findChildByType(FUN_KEYWORD)
?.nextCodeSibling()
?.elementType

private companion object {
val VALID_FUNCTION_NAME_REGEXP = "[a-z][A-Za-z\\d]*".regExIgnoringDiacriticsAndStrokesOnLetters()
val VALID_TEST_FUNCTION_NAME_REGEXP = "(`.*`)|([a-z][A-Za-z\\d_]*)".regExIgnoringDiacriticsAndStrokesOnLetters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,16 @@ class BlankLineBeforeDeclarationRuleTest {
.hasLintViolation(3, 5, "Expected a blank line for this declaration")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2260 - Given an anonymous function as right hand side in an assignment`() {
val code =
"""
val foo =
fun(): String {
return "foo"
}
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,16 @@ class FunctionNamingRuleTest {
""".trimIndent()
functionNamingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2260 - Given an anonymous function in a property assignment`() {
val code =
"""
val foo =
fun(): String {
return "foo"
}
""".trimIndent()
functionNamingRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 4396750

Please sign in to comment.