How to avoid VsCode Prettier to break chain functions in new lines.?

Daniel Santos picture Daniel Santos · Jan 20, 2019 · Viewed 8.5k times · Source

I'm working with VSCode, Prettier and TSLint.

When I do have chained functions call with more than 2 calls like

let m = moment().startOf("day").subtract(30, "days");

Prettier breaks into

let m = moment()
    .startOf("day")
    .subtract(30, "days")

I already set the TSLint rule

{
  "defaultSeverity": "warning",
  "extends": ["tslint:recommended"],
  "linterOptions": {
    "exclude": ["node_modules/**"]
  },
  "rules": {
    // ...
    "newline-per-chained-call": false
  }
}

and the fallowing settings

"prettier.tslintIntegration": true

But the chained functions still breking into new lines.

What can I do to avoid the line breaking but still using the TSLint?

Answer

Yedhin picture Yedhin · Jan 20, 2019

[EDIT] In Prettier v2.0.4 this issue is fixed. Update to latest version

This is an issue in prettier. The PR's to add this feature has not yet been merged from what i understand.

Currently to get what you want, what i can suggest is to ignore the next node in the abstract syntax tree from formatting using the // prettier-ignore comments.

// prettier-ignore  
let m = moment().startOf("day").subtract(30, "days");   

There are variations of these ignore statements, like one could ignore within a ranger or one could even ignore a particular file too. Do check out the official prettier documentations to know more of it's implementation.