Skip to content

[kotlin] Request for an XPath function as simple partial replacement of missing typeIs function #4858

Open

Description

@jborgers

Is your feature request related to a problem? Please describe.
Because of a missing typeIf function in Kotlin, we are checking if the expected package of the type is imported, to come close to typeIs functionality. This is a verbose expression in XPath on the verbose parse tree and often needed.

Describe the solution you'd like
A function called hasImport with expected package as argument.
Example: /KotlinFile[pmd-kotlin:hasImport('javax.xml.xpath')]

Describe alternatives you've considered

  • copy-paste every time: we want to avoid this and be more concise
  • create a proper concise AST out of the kotlin parse tree: don't know how to do this, expected a lot of work
  • create a real typeIs function for Kotlin: expected to be difficult and much work

Additional context

  • This would be a small step to improve productivity of creating rule. Maybe one of the above could be next step.
  • I do have a initial version in a my repo fork in the 'pmd/7.0.0x-kotlin/hasImport' branch.
  • Not sure this is the way to go, so feedback requested.

Activity

  1. added
    an:enhancement
    on Mar 11, 2024
  2. changed the title [-]Request for Kotlin XPath function as simple partly replacement of missing typeIs function[/-] [+][kotlin] Request for an XPath function as simple partial replacement of missing typeIs function[/+] on Mar 11, 2024
  3. adangel commented on Mar 14, 2024

    @adangel
    Member

    Just for reference, the boilerplate XPath expression looks like this:

    /KotlinFile[ImportList/ImportHeader[Identifier
        [SimpleIdentifier[1][T-Identifier[@Text = 'javax']]]
        [SimpleIdentifier[2][T-Identifier[@Text = 'xml']]]
        [SimpleIdentifier[3][T-Identifier[@Text = 'xpath']]]
      ]]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions