typescript - 如何避免 VsCode Prettier 用箭头函数破坏链函数?
问题描述
我正在使用 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",
}
当函数内部有箭头函数时,如何避免更漂亮的换行?
解决方案
没有选项可以覆盖方法链中断行为。在 2.0 版中,Prettier 使用一种新的启发式方法来确定何时中断方法链。您可以在https://github.com/prettier/prettier/pull/6685/files#diff-207f1974ddc06ae7b574152f9afc878dR893中看到启发式。
如果参数不平凡,Prettier 会中断方法链。字符串文字被认为是“平凡的”,但箭头函数表达式被认为是“非平凡的”。这就是您在将字符串与箭头函数作为参数传递时看到不同行为的原因。
推荐阅读
- jquery - 如果已在另一个下拉列表中选择了该值,如何删除下拉列表中的值
- java - 如何通过 API Spring Boot 获取特定字段
- python - 计算不同数据框中两个列表之间的匹配python
- android - 我们如何为回收器视图的每个列表项(每一行)设置 onClickListener,为不同的列表对象设置不同的活动
- android - 使用 API 16 和 17 向集合添加值时出现 Firestore 错误
- php - 如何在modal中从mysql中获取对应的id数据?
- javascript - React:如何从外部函数更新我的组件状态?
- mysql - 返回mysql连接的函数
- mocking - 如何验证链式方法调用?
- html - 无法更改 PWA Vuex 项目中的网站图标