From 71da8212892476ae0b5bed4804417e80be7157c4 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sat, 16 Sep 2023 15:22:08 +0200 Subject: [PATCH 1/3] Ignore anonymous function in rule `function-naming` Closes #2260 --- CHANGELOG.md | 1 + .../ruleset/standard/rules/FunctionNamingRule.kt | 12 +++++++++++- .../ruleset/standard/rules/FunctionNamingRuleTest.kt | 12 ++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e846156782..b5bd8530d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed * Fix ktlint cli parameter `--code-style` [#2238](https://github.com/pinterest/ktlint/pull/2238) +* Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260) ### Changed diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRule.kt index d98d244d74..a1ad1baab4 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRule.kt @@ -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 @@ -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 @@ -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() diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRuleTest.kt index 6f8116f8b9..34ab7fb691 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionNamingRuleTest.kt @@ -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() + } } From 0065813edf4026e42914d04a7d6701730d7b42f6 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sat, 16 Sep 2023 15:32:25 +0200 Subject: [PATCH 2/3] Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` Closes #2260 --- CHANGELOG.md | 1 + .../standard/rules/BlankLineBeforeDeclarationRule.kt | 12 ++++++++++-- .../rules/BlankLineBeforeDeclarationRuleTest.kt | 12 ++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5bd8530d6..3515b0ab33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). * Fix ktlint cli parameter `--code-style` [#2238](https://github.com/pinterest/ktlint/pull/2238) * 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 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt index 31210429d0..b75c306864 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt @@ -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 @@ -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")) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt index 77702f4986..cee323deb7 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt @@ -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() + } } From 719c4cb99458843a57dcb394f6e57f8b4acf415a Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sat, 16 Sep 2023 16:27:01 +0200 Subject: [PATCH 3/3] Fix maven coordinates in documentation and changelog Closes #2264 --- CHANGELOG.md | 20 ++++++++++---------- documentation/release-latest/docs/faq.md | 20 ++++++++++---------- documentation/snapshot/docs/faq.md | 20 ++++++++++---------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3515b0ab33..71a32d2904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,16 +25,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 diff --git a/documentation/release-latest/docs/faq.md b/documentation/release-latest/docs/faq.md index e0f84053c0..8add39d47c 100644 --- a/documentation/release-latest/docs/faq.md +++ b/documentation/release-latest/docs/faq.md @@ -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? diff --git a/documentation/snapshot/docs/faq.md b/documentation/snapshot/docs/faq.md index c2b9be08f8..62923a1f34 100644 --- a/documentation/snapshot/docs/faq.md +++ b/documentation/snapshot/docs/faq.md @@ -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?