Giter VIP home page Giter VIP logo

Comments (13)

tvonhacht avatar tvonhacht commented on May 19, 2024

maybe give this a shot yourself? :)

from prettier-java.

victornoel avatar victornoel commented on May 19, 2024

@thorbenvh8 honestly, this one is a hell to fix, the node tree doesn't have the right abstractions: we would need to differentiate between block statement and an expression, so that we can have expression inside a statement and variable declaration (instead of field declaration) inside a statement too.

We could then apply wrapping to the statement and not to the expression as it is currently done.

from prettier-java.

tvonhacht avatar tvonhacht commented on May 19, 2024

Ok sure I could put that into the parser.

Can you give me two examples when to return what do understand better?

If we have this return block, if we have this return expression. :)

from prettier-java.

victornoel avatar victornoel commented on May 19, 2024

Inside a BLOCK, in node.statements we should have only BLOCK_STATEMENT objects, and inside a BLOCK_STATEMENT we should have what is currently present in BLOCK (i.e. identifier statement, expression statement and field declaration).

So that the new line rule (that checks if we are in a lambda, etc) currently defined for EXPRESSION_STATEMENT can actually be moved to BLOCK_STATEMENT and applied to any of the 3 type of block statement.

from prettier-java.

tvonhacht avatar tvonhacht commented on May 19, 2024

So you want from:

{
  "type": "BLOCK",
  "statements": [
    {
           "type": "IDENTIFIER_STATEMENT"
    },
    {
           "type": "EXPRESSION_STATEMENT"
    },
    {
           "type": "FIELD_DECLARATION"
     }
  ]
}

to:

{
  "type": "BLOCK",
  "statements": [
    {
       "type": "BLOCK_STATEMENT",
       "statement": {
           "type": "IDENTIFIER_STATEMENT"
       }
    },
    {
       "type": "BLOCK_STATEMENT",
       "statement": {
           "type": "EXPRESSION_STATEMENT"
       }
    },
    {
       "type": "BLOCK_STATEMENT",
       "statement": {
           "type": "FIELD_DECLARATION"
       }
    }
  ]
}

from prettier-java.

tvonhacht avatar tvonhacht commented on May 19, 2024

Did you know you can do path.getParent() (parameter can be a number and that means the parent of level like 1 (default), 2, 3 etc.)
Maybe you can use that?

from prettier-java.

victornoel avatar victornoel commented on May 19, 2024

Let's scratch that, I wonder if the bug is not coming from something simpler:

In the example in the description, the variable declaration is parsed as a FIELD_DECLARATION: this doesn't make any sense. Reading the code of chevrotain-java, I can see that a EXPRESSION_STATEMENT actually can contains a LOCAL_VARIABLE_DECLARATION.
If the variable declaration in the example was inside a node of type EXPRESSION_STATEMENT, then the correct printing behaviour would have been applied.

WDYT? Is that a bug in chevrotain-java?

from prettier-java.

tvonhacht avatar tvonhacht commented on May 19, 2024
void localVariables() { final List<Filter> filterList = new ArrayList<>(); }

I get this back from the parser, what would you expect instead?

  {
      type: "METHOD_DECLARATION",
      typeType: { type: "VOID" },
      name: { type: "IDENTIFIER", value: "localVariables" },
      parameters: { parameters: [], type: "FORMAL_PARAMETERS" },
      dimensions: [],
      body: {
        type: "BLOCK",
        statements: [
          {
            type: "FIELD_DECLARATION",
            typeType: {
              modifiers: [{ type: "MODIFIER", value: "final" }],
              type: "TYPE_TYPE",
              dimensions: [],
              value: {
                name: { type: "IDENTIFIER", value: "List" },
                type: "CLASS_OR_INTERFACE_TYPE_ELEMENT",
                typeArguments: {
                  type: "TYPE_ARGUMENTS",
                  value: {
                    type: "TYPE_LIST",
                    list: [
                      {
                        type: "TYPE_ARGUMENT",
                        argument: {
                          type: "IDENTIFIER",
                          value: "Filter"
                        },
                        extends: undefined,
                        super: undefined
                      }
                    ]
                  }
                }
              }
            },
            variableDeclarators: {
              type: "VARIABLE_DECLARATORS",
              list: [
                {
                  type: "VARIABLE_DECLARATOR",
                  id: {
                    type: "VARIABLE_DECLARATOR_ID",
                    id: { type: "IDENTIFIER", value: "filterList" },
                    dimensions: []
                  },
                  init: {
                    type: "SIMPLE_CREATOR",
                    name: {
                      elements: [
                        {
                          type: "IDENTIFIER_NAME_ELEMENT",
                          id: {
                            type: "IDENTIFIER",
                            value: "ArrayList"
                          },
                          typeArguments: {
                            type: "TYPE_ARGUMENTS",
                            value: undefined
                          }
                        }
                      ],
                      type: "IDENTIFIER_NAME"
                    },
                    rest: {
                      type: "CLASS_CREATOR_REST",
                      arguments: { list: [], type: "EXPRESSION_LIST" },
                      body: undefined
                    }
                  }
                }
              ]
            },
            followedEmptyLine: false
          }
        ]
      },
      throws: undefined
    }

Did you check the visitor code? Maybe there is a bug that I return the wrong ast?

from prettier-java.

victornoel avatar victornoel commented on May 19, 2024

I don't understand how, semantically, a field could be contained in a method block.

I would have expected it to contain an EXPRESSION_STATEMENT, itself containing a LOCAL_VARIABLE_DECLARATION or something like that.

I checked quickly the visitor code of chevrotain-java, but honestly it is not very readable, especially in that place. I'm trying to help understanding where the problem is, but I'm mostly not able to fix it myself because the code is too complex.

from prettier-java.

tvonhacht avatar tvonhacht commented on May 19, 2024

Fixed in last release :)

from prettier-java.

victornoel avatar victornoel commented on May 19, 2024

@thorbenvh8 is it really? You only merged my PR in the last release, nothing since then and my PR didn't fix this!

from prettier-java.

tvonhacht avatar tvonhacht commented on May 19, 2024

Ups, I released some more stuff but forgot to push the code. On the road right now, so I will push it later :) thanks for the heads up :)

from prettier-java.

victornoel avatar victornoel commented on May 19, 2024

cool, that's great news then :)

from prettier-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.