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
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: implementattributes()to get an node-type specific attributes viewAttributeView: base class to add custom attributes to antlr based nodese.g.
KtClassDeclarationAttributesadds the attribute "Identifier"attribute implementations can be shared via default methods in interfaces, e.g.
HasIdentifierNew XPath attributes exposed:
@Identifier,@Modifiers,@Nameon applicable nodes; visible in PMD DesignerThe attribute
@Imagehas been removed. See also Deprecate getImage/@Image #4787.2. KotlinAstUtil — static navigation utility
New class
net.sourceforge.pmd.lang.kotlin.ast.internal.KotlinAstUtilwith helper methods for use in Java-based Kotlin rules:textOf(KtSimpleIdentifier)— extract text from aSimpleIdentifiernodetextOf(KtPrimaryExpression)— extract text from aPrimaryExpressionisWithin— scope check: is nodendirectly inside a given ancestor instance (not inside any intervening node of the same type)?collectParamNames— collect parameter names from aKtFunctionDeclarationcollectLocalVarNames— collect localval/varnames in scopecollectClassVarFieldNames— collect class-level field namescollectLambdaParamNames— collect explicit lambda parameter names from aKtLambdaLiteral