【问题标题】:How to avoid VsCode Prettier to break chain functions with arrow functions?如何避免 VsCode Prettier 用箭头函数破坏链函数?
【发布时间】:2020-09-21 13:50:23
【问题描述】:

我正在使用 VSCode 和 Prettier,当我们使用内部箭头函数链接函数调用时,就像 lodash 链一样:

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

更漂亮的闯入:

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

当我们使用strings代替箭头函数时,它不会分成几行,例如:

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

我正在与.prettierrc"prettier": "^2.0.5" 合作:

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

函数内部有箭头函数时,如何避免用prettier换行?

【问题讨论】:

    标签: typescript visual-studio-code prettier


    【解决方案1】:

    没有选项可以覆盖方法链中断行为。在 2.0 版中,Prettier 使用新的启发式方法来确定何时中断方法链。您可以在https://github.com/prettier/prettier/pull/6685/files#diff-207f1974ddc06ae7b574152f9afc878dR893 中看到启发式。

    如果参数不重要,Prettier 会中断方法链。字符串文字被认为是“平凡的”,但箭头函数表达式被认为是“非平凡的”。这就是当您将字符串与箭头函数作为参数传递时,您会看到不同行为的原因。

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2016-03-25
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多