Skip to content

Conversation

paul-dingemans
Copy link
Collaborator

Description

Replace functions below with property accessors. Most of the new property accessors have a temporary name using sufix 20 to avoid binary compatability issues. In KtLint 2.0 the functions will be removed, and the property accessors will be renamed.

  • ASTNode.nextLeaf(includeEmpty: Boolean = false, skipSubtree: Boolean = false): ASTNode?
    • Replace calls astNode.nextLeaf() with astNode.nextLeaf
    • No replacements have been added for includeEmpty=true or skipSubtree=true
  • ASTNode.prevLeaf(includeEmpty: Boolean = false): ASTNode?
    • Replace calls astNode.prevLeaf() with astNode.prevLeaf
    • No replacement has been added for includeEmpty=true
  • ASTNode.nextCodeLeaf(includeEmpty: Boolean = false): ASTNode?
    • Replace calls astNode.nextCodeLeaf() with astNode.nextCodeLeaf
    • No replacements have been added for includeEmpty=true or skipSubtree=true
  • ASTNode.prevCodeLeaf(includeEmpty: Boolean = false): ASTNode?
    • Replace calls astNode.prevCodeLeaf() with astNode.prevCodeLeaf
    • No replacement has been added for includeEmpty=true
  • ASTNode.nextSibling(): ASTNode?
    • Replace calls astNode.nextSibling() with astNode.nextSibling20
  • ASTNode.prevSibling(): ASTNode?
    • Replace calls astNode.prevSibling() with astNode.prevSibling20
  • ASTNode.nextCodeSibling(): ASTNode?
    • Replace calls astNode.nextCodeSibling() with astNode.nextCodeSibling20
  • ASTNode.prevCodeSibling(): ASTNode?
    • Replace calls astNode.prevCodeSibling() with astNode.prevCodeSibling20
  • ASTNode.firstChildLeafOrSelf(): ASTNode
    • Replace calls astNode.firstChildLeafOrSelf() with astNode.firstChildLeafOrSelf20
  • ASTNode.lastChildLeafOrSelf(): ASTNode
    • Replace calls astNode.lastChildLeafOrSelf() with astNode.lastChildLeafOrSelf20
  • ASTNode.parent(elementType: IElementType, strict: Boolean = true): ASTNode?
    • Replace calls astNode.parent(elementType) with astNode.findParentByType(elementType)
    • No replacement has been added for strict=false
  • ASTNode.parent(strict: Boolean = true, predicate: (ASTNode) -> Boolean): ASTNode?
    • Replace calls astNode.parent(strict = true) { ... } with astNode.parent { ... }
    • No replacement has been added for strict=false
  • ASTNode.isPartOfString(): Boolean
    • Replace calls astNode.isPartOfString() with astNode.isPartOfString20
  • ASTNode?.isWhiteSpace(): Boolean
    • Replace calls astNode.isWhiteSpace() with astNode.isWhiteSpace20
  • ASTNode?.isWhiteSpaceWithNewline(): Boolean
    • Replace calls astNode.isWhiteSpaceWithNewline() with astNode.isWhiteSpaceWithNewline20
  • ASTNode?.isWhiteSpaceWithoutNewline(): Boolean
    • Replace calls astNode.isWhiteSpaceWithoutNewline() with astNode.isWhiteSpaceWithoutNewline20
  • ASTNode.isRoot(): Boolean
    • Replace calls astNode.isRoot() with astNode.isRoot20
  • ASTNode.isLeaf(): Boolean
    • Replace calls astNode.isLeaf() with astNode.isLeaf20
  • ASTNode.isPartOfComment(): Boolean
    • Replace calls astNode.isPartOfComment() with astNode.isPartOfComment20
  • ASTNode.children(): Sequence<ASTNode>
    • Replace calls astNode.children() with astNode.children20
  • ASTNode.recursiveChildren(includeSelf: Boolean = false): Sequence<ASTNode>
    • Replace calls astNode.recursiveChildren(includeSelf = false) with astNode.recursiveChildren20
    • No replacement has been added for includeSelf=true
  • ASTNode.indent(includeNewline: Boolean = true): String
  • Replace calls astNode.indent() and astNode.indent(includeNewline = true) with astNode.indent20
  • Replace calls astNode.indent(includeNewline = false) with astNode.indentWithoutNewlinePrefix
  • ASTNode.leavesIncludingSelf(forward: Boolean = true): Sequence<ASTNode>
    • Replace calls astNode.leavesIncludingSelf() and astNode.leavesIncludingSelf(forward = true) with astNode.leavesForwardsIncludingSelf
    • Replace calls astNode.leavesIncludingSelf(forward = false) with astNode.leavesBackwardsIncludingSelf
  • ASTNode.leavesOnLine(): Sequence<ASTNode>
    • Replace calls astNode.leavesOnLine with astNode.leavesOnLine20
  • ASTNode.leavesOnLine(excludeEolComment: Boolean): Sequence<ASTNode>
    • Replace calls astNode.leavesOnLine(excludeEolComment = true) with astNode.leavesOnLine20.dropTrailingEolComment()
  • ASTNode.lineLengthWithoutNewlinePrefix(): Int
    • Replace calls astNode.lineLengthWithoutNewlinePrefix() with astNode.leavesOnLine20.lineLength
  • ASTNode.lineLength(excludeEolComment: Boolean = false): Int
    • Replace calls astNode.lineLength() and astNode.lineLength(excludeEolComment = false) with astNode.leavesOnLine20.lineLength
    • Replace calls astNode.lineLength(excludeEolComment = true)withastNode.leavesOnLine20.dropTrailingEolComment().lineLength`
  • Sequence<ASTNode>.lineLengthWithoutNewlinePrefix(): Int
    • Replace calls astNode.lineLengthWithoutNewlinePrefix()withastNode.lineLength`
  • ASTNode.findChildByTypeRecursively(elementType: IElementType, includeSelf: Boolean): ASTNode?
    • Replace calls astNode.findChildByTypeRecursively(elementType, false)withastNode.findChildByTypeRecursively(elementType)`
    • No replacement has been added for includeSelf=true
  • ASTNode.endOffset(): Int
    • Replace calls astNode.endOffset()withastNode.endOffset20`
  • fun ASTNode.isKtAnnotated(): Boolean
    • Replace calls astNode.isKtAnnotated()withastNode.isKtAnnotated20`
  • ASTNode?.isDeclaration(): Boolean
    • Replace calls astNode.isDeclaration()withastNode.isDeclaration20`

Functions below are deprecated without replacement:

  • ASTNode.isPartOfCompositeElementOfType(iElementType: IElementType): Boolean
  • ASTNode.findCompositeParentElementOfType(iElementType: IElementType): ASTNode?
  • ASTNode.isCodeLeaf(): Boolean
  • ASTNode.logStructure(): ASTNode
  • ASTNode.isValOrVarKeyword(): Boolean
  • ASTNode.isKtExpression(): Boolean
  • ASTNode.isKtLoopExpression(): Boolean

Newly added property accessors:

  • ASTNode.isCode get(): Boolean: true when [ASTNode] is not a whitespace element, and is not part of a comment
  • ASTNode.parent get(): ASTNode?: The parent of the [ASTNode]. This counterpart of the PSI treeParent function is nullable as the root node does not have a parent.
  • ASTNode?.isWhiteSpaceWithoutNewlineOrNull get(): Boolean: true when [ASTNode] is null, or when [ASTNode] is a whitespace element containing a newline

Newly added functions:

  • ASTNode.replaceTextWith(text: String): Replace text [ASTNode] with [text]. [ASTNode] must be a [LeafElement].

Closes #2919

Checklist

Before submitting the PR, please check following (checks which are not relevant may be ignored):

  • Commit message are well written. In addition to a short title, the commit message also explain why a change is made.
  • At least one commit message contains a reference Closes #<xxx> or Fixes #<xxx> (replace<xxx> with issue number)
  • Tests are added
  • KtLint format has been applied on source code itself and violations are fixed
  • PR title is short and clear (it is used as description in the release changelog)
  • PR description added (background information)

Documentation is updated. See difference between snapshot and release documentation

  • Snapshot documentation in case documentation is to be released together with a code change
  • Release documentation in case documentation is related to a released version of ktlint and has to be published as soon as the change is merged to master

…e function "isWhiteSpaceWithNewline" in KtLint 2.0
…lace function "isWhiteSpaceWithoutNewline" in Ktlint 2.0
…perty accessor "indent20", to replace function "children" in Ktlint 2.0
…ors "leavesForwardsIncludingSelf", and "leavesBackwardsIncludingSelf"
…y property accessor "leavesOnLine20" to replace function "leavesOnLine(Boolean)" in Ktlint 2.0
…f" in Ktlint 2.0.

Note that the property accessor replaces the default usage of the function with parameter values includeEmpty equals false, and skipSubtree equals false.
…lint 2.0.

Note that the property accessor replaces the default usage of the function with parameter values includeEmpty equals false, and skipSubtree equals false.
…unction "firstChildLeafOrSelf" in Ktlint 2.0
…erty accessor "nextSibling20" to replace function "ASTNode.nextSibling()" in Ktlint 2.0.
…Node.parent(IElementType,Boolean): ASTNode?"
…ASTNode?", and add "public fun ASTNode.parent((ASTNode) -> Boolean): ASTNode?"

Refactor use case where parameter "strict" was used by replacing it with a call to "isPartOf(TokenSet)"
… ASTNode?", and add "ASTNode.findChildByTypeRecursively(IElementType): ASTNode?"

Refactor calls to deprecated method
… ASTNode?", and add "ASTNode.findChildByTypeRecursively(IElementType): ASTNode?"

Refactor calls to deprecated method
…ElementType" are not the adjacent sibling of the current ASTNode
…erty accessor "prevSibling20" to replace function "ASTNode.prevSibling()" in Ktlint 2.0.
…and property accessors to avoid migration to psi
# Conflicts:
#	ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt
#	ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/UnnecessaryParenthesesBeforeTrailingLambdaRule.kt
@paul-dingemans paul-dingemans merged commit b45b52f into master Jul 2, 2025
20 checks passed
@paul-dingemans paul-dingemans deleted the 2919 branch July 2, 2025 18:24
@paul-dingemans paul-dingemans added this to the 1.7.0 milestone Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor AstNodeExtensions - use properties with getters
1 participant