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

2

25

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?

Gustie answered 20/1, 2019 at 0:41 Comment(0)
T
4

Note that in Prettier v2.0.4 this issue is fixed. Now, as long as your line of code is within the length specified in your config or the default 80, it will be left in one line. Otherwise, it will be wrapped to multiple lines.

I had to upgrade my prettier in order for these changes to be put into affect.

$ yarn upgrade -g prettier --latest
Trabzon answered 18/4, 2020 at 17:9 Comment(0)
T
12

[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.

Trenttrento answered 20/1, 2019 at 3:56 Comment(0)
T
4

Note that in Prettier v2.0.4 this issue is fixed. Now, as long as your line of code is within the length specified in your config or the default 80, it will be left in one line. Otherwise, it will be wrapped to multiple lines.

I had to upgrade my prettier in order for these changes to be put into affect.

$ yarn upgrade -g prettier --latest
Trabzon answered 18/4, 2020 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.