简体   繁体   English

如何避免 VsCode Prettier 在新行中破坏链函数。?

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

I'm working with VSCode, Prettier and TSLint.我正在使用 VSCode、Prettier 和 TSLint。

When I do have chained functions call with more than 2 calls like当我确实有超过 2 个调用的链式函数调用时

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

Prettier breaks into Prettier闯入

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

I already set the TSLint rule我已经设置了 TSLint 规则

{
  "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?我该怎么做才能避免换行但仍然使用 TSLint?

[EDIT] In Prettier v2.0.4 this issue is fixed. [编辑] 在 Prettier v2.0.4 中,此问题已修复。 Update to latest version更新到最新版本

This is an issue in prettier .这是prettier 中的一个问题 The PR's to add this feature has not yet been merged from what i understand.据我所知,添加此功能的 PR 尚未合并。

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注释进行格式化。

// 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.这些 ignore 语句有多种变体,比如可以在 ranger 中忽略,或者甚至可以忽略特定文件。 Do check out the official prettier documentations to know more of it's implementation.请查看官方更漂亮的文档以了解更多它的实现。

Note that in Prettier v2.0.4 this issue is fixed.请注意,在 Prettier v2.0.4 中,此问题已得到修复。 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.现在,只要你的代码行在你的配置中指定的长度或默认的 80 以内,它就会留在一行中。 Otherwise, it will be wrapped to multiple lines.否则,它将换行到多行。

I had to upgrade my prettier in order for these changes to be put into affect.为了使这些更改生效,我不得不升级我的 prettier。

$ yarn upgrade -g prettier --latest

I'm working with VSCode, Prettier and TSLint.我正在使用 VSCode、Prettier 和 TSLint。

When I do have chained functions call with more than 2 calls like当我确实有超过 2 个调用的链接函数调用时

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我已经设置了 TSLint 规则

{
  "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?我该怎么做才能避免换行但仍在使用 TSLint?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM