简体   繁体   English

如何避免 VsCode Prettier 用箭头函数破坏链函数?

[英]How to avoid VsCode Prettier to break chain functions with arrow functions?

I'm working with VSCode and Prettier and when we have chained functions call with arrow functions inside like a lodash chain:我正在使用 VSCode 和 Prettier,当我们在内部像 lodash 链一样使用箭头函数进行链接函数调用时:

let total = _(credits).filter(c => c.active).sumBy(c => c.fee);

Prettier breaks into:更漂亮的闯入:

discount = _(credits)
    .filter(c => c.active)
    .sumBy(c => c.fee);

When the we use strings insteads arrow functions, it does not breaks into several lines, for instance:当我们使用strings而不是箭头函数时,它不会分成几行,例如:

let total = _(credits).filter('c => c.active').sumBy('c => c.fee');

I'm working with following .prettierrc and "prettier": "^2.0.5" :我正在使用以下.prettierrc"prettier": "^2.0.5"

{
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 280,
  "tabWidth": 4,
  "arrowParens": "avoid",
}

How can I avoid the line breaking with prettier when there is a arrow function inside the functions?当函数内部有箭头 function 时,如何避免更漂亮的换行?

There is no option to override method chain breaking behaviour.没有选项可以覆盖方法链中断行为。 With version 2.0, Prettier uses a new heuristic to determine when to break method chains.在 2.0 版中,Prettier 使用一种新的启发式方法来确定何时中断方法链。 You can see the heuristic in https://github.com/prettier/prettier/pull/6685/files#diff-207f1974ddc06ae7b574152f9afc878dR893 .您可以在https://github.com/prettier/prettier/pull/6685/files#diff-207f1974ddc06ae7b574152f9afc878dR893中看到启发式。

Prettier breaks method chains if the arguments are non-trivial.如果 arguments 不平凡,Prettier 会中断方法链。 A string literal is considered as "trivial", but an arrow function expression is considered as "non-trivial".字符串文字被认为是“平凡的”,但箭头 function 表达式被认为是“不平凡的”。 That's the reason you're seeing different behaviour when you pass strings vs arrow functions as parameters.这就是您在将字符串与箭头函数作为参数传递时看到不同行为的原因。

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

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