Skip to content

[kotlin] Add AST improvements, KotlinAstUtil #6669

@stokpop

Description

@stokpop

Summary

This PR adds foundational AST improvements to pmd-kotlin and introduces KotlinAstUtil, a utility class with reusable navigation helpers.

1. Kotlin AST node improvements

  • KotlinInnerNode: implement attributes() to get an node-type specific attributes view

  • AttributeView: base class to add custom attributes to antlr based nodes

  • e.g. KtClassDeclarationAttributes adds the attribute "Identifier"

  • attribute implementations can be shared via default methods in interfaces, e.g. HasIdentifier

  • New XPath attributes exposed: @Identifier, @Modifiers, @Name on applicable nodes; visible in PMD Designer

  • The attribute @Image has been removed. See also Deprecate getImage/@Image #4787.

2. KotlinAstUtil — static navigation utility

New class net.sourceforge.pmd.lang.kotlin.ast.internal.KotlinAstUtil with helper methods for use in Java-based Kotlin rules:

  • textOf(KtSimpleIdentifier) — extract text from a SimpleIdentifier node
  • textOf(KtPrimaryExpression) — extract text from a PrimaryExpression
  • isWithin — scope check: is node n directly inside a given ancestor instance (not inside any intervening node of the same type)?
  • collectParamNames — collect parameter names from a KtFunctionDeclaration
  • collectLocalVarNames — collect local val/var names in scope
  • collectClassVarFieldNames — collect class-level field names
  • collectLambdaParamNames — collect explicit lambda parameter names from a KtLambdaLiteral

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rules

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions