prettier-plugin-apex icon indicating copy to clipboard operation
prettier-plugin-apex copied to clipboard

Incorrect indent when methods are chained to inline new instance

Open paustint opened this issue 3 years ago • 3 comments

Sorry for the cryptic title!

Everything is indented as if the new keyword does not exist.

# Prettier options (if any):
{
  "trailingComma": "none",
  "apexInsertFinalNewline": false,
  "singleQuote": true,
  "printWidth": 140,
  "overrides": [
    {
      "files": "**/classes/**/*.cls",
      "options": { "tabWidth": 4 }
    },
    {
      "files": "**/lwc/**/*.html",
      "options": { "parser": "lwc", "tabWidth": 4 }
    },
    {
      "files": "*.{cmp,page,component}",
      "options": { "parser": "html", "tabWidth": 4 }
    }
  ]
}

Input:

new Flow.Interview.Termination_Handle_Legal_Comments_and_Chatter_Posts(new Map<String, Object>{ 'varCaseId' => currCase.Id, 'varAccountId' => currCase.AccountId, 'varType' => chatterType }).start();

Actual output:

new Flow.Interview.Termination_Handle_Legal_Comments_and_Chatter_Posts(
        new Map<String, Object>{ 'varCaseId' => currCase.Id, 'varAccountId' => currCase.AccountId, 'varType' => chatterType }
    )
    .start();

Additional example

new Flow.Interview.Termination_Handle_Legal_Comments_and_Chatter_Posts(
        new Map<String, Object>{
            'varCaseId' => currCase.Id,
            'varAccountId' => currCase.AccountId,
            'varSalesRep' => currCase.Sales_Rep__c,
            'varType' => chatterType
        }
    )
    .start();

Expected output:

// prettier-ignore
new Flow.Interview.Termination_Handle_Legal_Comments_and_Chatter_Posts(
        new Map<String, Object>{ 'varCaseId' => currCase.Id, 'varAccountId' => currCase.AccountId, 'varType' => chatterType }
)
.start();

Additional information (please fill this out):

  • OS: OSX
  • Version: "prettier-plugin-apex": "^1.10.0"

paustint avatar Oct 29 '21 22:10 paustint

Thanks for the issue! This is indeed a difficult one to write a title for 🤣 I'll take a look at this soon-ish, right now I'm rewriting this library in TypeScript so formatting changes need to take a bit of a back seat, but hopefully that won't take too long.

dangmai avatar Oct 29 '21 22:10 dangmai

@dangmai - No problem! I just wanted to let you know about the bug with no expectations beyond that. You are an excellent maintainer and THANK YOU for this library and the ongoing work you put into it.

As for Typescript, that is awesome! The only JavaScript I write these days is for Salesforce, but aside from that I am 100% sold on using TypeScript for everything because it is so amazing. 😍

paustint avatar Oct 29 '21 22:10 paustint

Looking at this issue now, there's actually some complexity to it. Your expected output has .start() on the same level as the parent expression, which makes it difficult to understand that it's a child expression. I would actually rather go with how Prettier upstream (for JS) handles this kind of complexity, example here.

Basically, if there's only 1 method call, it's always attached directly to the parent expression with no line break, no matter how long it is. If there are more than 1, new lines with another level of indentation is added.

Going down this route will also make it less of a cognitive switch when looking at formatted JS code and formatted Apex code, because they will have the same formatting behavior.

dangmai avatar May 13 '22 16:05 dangmai