簡體   English   中英

如何避免 VsCode Prettier 用箭頭函數破壞鏈函數?

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

我正在使用 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",
}

當函數內部有箭頭 function 時,如何避免更漂亮的換行?

沒有選項可以覆蓋方法鏈中斷行為。 在 2.0 版中,Prettier 使用一種新的啟發式方法來確定何時中斷方法鏈。 您可以在https://github.com/prettier/prettier/pull/6685/files#diff-207f1974ddc06ae7b574152f9afc878dR893中看到啟發式。

如果 arguments 不平凡,Prettier 會中斷方法鏈。 字符串文字被認為是“平凡的”,但箭頭 function 表達式被認為是“不平凡的”。 這就是您在將字符串與箭頭函數作為參數傳遞時看到不同行為的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM