可以让 Prettier 允许在新行上链接函数吗?

Possible to make Prettier allow chaining functions on new lines?

我正在编写一些 Firebase 函数。开箱即用,它带有 EsLint 和 eslint-plugin-promise(很棒)。似乎 eslint-plugin-promise 也引入了 prettier。我不习惯 prettier,但我在 .prettierrc 文件中配置了一些我喜欢的东西,但我无法弄清楚这个 Promise 链接问题。

我在新行上链接 Promise 调用,但 prettier 迫使我将它们放在一行中。

我的代码:

module.exports = functions.firestore
    .document('thing/{thingId}')
    .onCreate((snap, context) => {
        // stuff
    });

Prettier 正在重新格式化为:

module.exports = functions.firestore.document('thing/{thingId}').onCreate((snap, context) => {
    // stuff
}

我认为 Prettier 的版本更差。它更难阅读,也更难区分。

知道如何关闭它吗?我现在刚刚禁用了 Prettier,我不喜欢这样做,因为我喜欢 Prettier 所做的大部分工作。

是的,您可以增加 printWidth,这样可以让更多的呼叫适应一条线路。

不,我不这么认为,至少不是你想要的方式。 不过,您确实可以选择忽略下一个块。

Prettier offers an escape hatch to ignore a block of code or prevent entire files from being formatted.

You can find more info on this method here

所以这

matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)

// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)

格式为此(例如)

matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)

这个行为在 Prettier 2 中有所改变。