Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 579
Daniel Santos Avatar asked Jan 20 '19 00:01

Daniel Santos


People also ask

How do I temporarily disable prettier in Vscode?

prettier-vscode", If you want to disable Prettier for a specific language, you can set the editor. defaultFormatter to null .


2 Answers

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

like image 90
Yedhin Avatar answered Oct 13 '22 23:10

Yedhin


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
like image 42
Eric Wiener Avatar answered Oct 13 '22 21:10

Eric Wiener