Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to --print-ast #5417

Closed
samuelneff opened this issue Oct 15, 2022 · 6 comments · Fixed by #5418
Closed

Enhancements to --print-ast #5417

samuelneff opened this issue Oct 15, 2022 · 6 comments · Fixed by #5418
Labels

Comments

@samuelneff
Copy link

I love the --print-ast tool while working on new rules. I added some additional output to make it even easier to use.

Is this something we would want to include? If so I'll make a PR.

Expected Behavior

  • Add part of the element's .text property so it's clear what element is being dumped.
  • When present output the element's .name (or equivalent) property instead (see class and function names not shown in example).

Current Behavior

Only the element types are dumped.

Context

Developing tools to make developing tools easier.

Example

This is showing .text only. Class and function names are not shown so I'll modify to include .name (or equivalent) when present before submitting a PR.

    20:KtClass                                                   @KotlinCoreEnvironmentTest
      20:KtDeclarationModifierList                               @KotlinCoreEnvironmentTest
      20:KtAnnotationEntry                                       @KotlinCoreEnvironmentTest
      20:KtConstructorCalleeExpression                           KotlinCoreEnvironmentTest
      20:KtTypeReference                                         KotlinCoreEnvironmentTest
      20:KtUserType                                              KotlinCoreEnvironmentTest
      20:KtNameReferenceExpression                               KotlinCoreEnvironmentTest
      21:KtPrimaryConstructor                                    (val env: KotlinCoreEnvironment)
        21:KtParameterList                                       (val env: KotlinCoreEnvironment)
        21:KtParameter                                           val env: KotlinCoreEnvironment
          21:KtTypeReference                                     KotlinCoreEnvironment
          21:KtUserType                                          KotlinCoreEnvironment
          21:KtNameReferenceExpression                           KotlinCoreEnvironment
      21:KtClassBody                                             {
        22:KtProperty                                            val subject = UnusedPrivateMember()
          22:KtCallExpression                                    UnusedPrivateMember()
          22:KtNameReferenceExpression                           UnusedPrivateMember
          22:KtValueArgumentList                                 ()
        35:KtClass                                               @Nested
          35:KtDeclarationModifierList                           @Nested
          35:KtAnnotationEntry                                   @Nested
          35:KtConstructorCalleeExpression                       Nested
          35:KtTypeReference                                     Nested
          35:KtUserType                                          Nested
          35:KtNameReferenceExpression                           Nested
          36:KtClassBody                                         {
            38:KtNamedFunction                                   @Test
              38:KtDeclarationModifierList                       @Test
              38:KtAnnotationEntry                               @Test
              38:KtConstructorCalleeExpression                   Test
              38:KtTypeReference                                 Test
              38:KtUserType                                      Test
              38:KtNameReferenceExpression                       Test
            39:KtParameterList                                   ()
              39:KtBlockExpression                               {
                40:KtProperty                                    val code = """
                  40:KtDotQualifiedExpression                    """
                  40:KtStringTemplateExpression                  """
                  40:KtLiteralStringTemplateEntry
                41:KtLiteralStringTemplateEntry                  interface UserPlugin {
                  41:KtLiteralStringTemplateEntry
                42:KtLiteralStringTemplateEntry                  fun plug(application: Application)
                  42:KtLiteralStringTemplateEntry
                43:KtLiteralStringTemplateEntry                  fun unplug()
                  43:KtLiteralStringTemplateEntry
                44:KtLiteralStringTemplateEntry                  }
                  44:KtLiteralStringTemplateEntry
                45:KtLiteralStringTemplateEntry
                  45:KtCallExpression                            trimIndent()
                  45:KtNameReferenceExpression                   trimIndent
                  45:KtValueArgumentList                         ()
              46:KtDotQualifiedExpression                        assertThat(subject.lint(code)).isEmpty()
                46:KtCallExpression                              assertThat(subject.lint(code))
                46:KtNameReferenceExpression                     assertThat
                46:KtValueArgumentList                           (subject.lint(code))
                46:KtValueArgument                               subject.lint(code)
                46:KtDotQualifiedExpression                      subject.lint(code)
                46:KtNameReferenceExpression                     subject
                46:KtCallExpression                              lint(code)
                46:KtNameReferenceExpression                     lint
                46:KtValueArgumentList                           (code)
                46:KtValueArgument                               code
                46:KtNameReferenceExpression                     code
                46:KtCallExpression                              isEmpty()
                46:KtNameReferenceExpression                     isEmpty
                46:KtValueArgumentList                           ()
@cortinico
Copy link
Member

cortinico commented Oct 15, 2022

Is this something we would want to include? If so I'll make a PR.

TIL we do have a --print-ast flag, I was not aware of it.
In general I find the PSI viewer: https://www.jetbrains.com/help/idea/psi-viewer.html good enough and sufficient. I'm unsure we want to expand on the --print-ast feature as it's a bit outside the scope of Detekt.

@schalkms
Copy link
Member

I agree with Nico here. I think the flag should be removed for housekeeping reasons, since the PsiViewer plugin provides the same features.

As a side note, the AstPrinter exists since the early stages of detekt (2018). I have never used this flag even though I knew about the existence of this flag. The print-ast flag is only linked in the Contributing.md guide, but nowhere else.

@schalkms
Copy link
Member

If we decide to remove this flag, I provided a PR #5418.

@3flex
Copy link
Member

3flex commented Oct 15, 2022

I also use the PSI Viewer plugin, and have never used the AST printer.

@samuelneff
Copy link
Author

Sounds like there's consensus on removing the flag. 👍

PsiViewer is nice. I don't suppose there is any way to get the resolved type information though?

@cortinico
Copy link
Member

+1 for removing it.

PsiViewer is nice. I don't suppose there is any way to get the resolved type information though?

IIRC PsiViewer displays some amount of resolved types as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants