Skip to content

Commit

Permalink
make isDeclaration() match previous behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mgroth0 committed Jan 6, 2025
1 parent 2ecce76 commit 729425e
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SCRIPT_INITIALIZER
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
Expand All @@ -15,6 +16,7 @@ import com.pinterest.ktlint.rule.engine.core.api.prevSibling
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.psi.KtDeclarationImpl
import org.jetbrains.kotlin.psi.stubs.elements.KtTokenSets

/**
Expand Down Expand Up @@ -56,7 +58,11 @@ public class SpacingBetweenDeclarationsWithCommentsRule : StandardRule("spacing-
}
}

private fun ASTNode?.isDeclaration() = this != null && elementType in KtTokenSets.DECLARATION_TYPES
/**
* [KtScriptInitializer] is considered a type of declaration in terms of it being a subtype of [KtDeclarationImpl] even though SCRIPT_INITIALIZER is not included in DECLARATION_TYPES. We consider SCRIPT_INITIALIZER a declaration here to match previous behavior of ktlint in older versions.
*/
private fun ASTNode?.isDeclaration() =
this != null && (elementType in KtTokenSets.DECLARATION_TYPES || elementType == SCRIPT_INITIALIZER)
}

public val SPACING_BETWEEN_DECLARATIONS_WITH_COMMENTS_RULE_ID: RuleId = SpacingBetweenDeclarationsWithCommentsRule().ruleId

0 comments on commit 729425e

Please sign in to comment.