更漂亮的设置可以保留函数声明正下方的空间

6 javascript whitespace code-formatting typescript prettier

例如,如果我们在 .ts 文件中包含以下内容:

export const handleHMRMessages = (cache: any, store: CPReduxStore, conn: WebSocket) => {

  conn.onclose = () => {
    store.dispatch(actions.dev.hmrDisconnected());
  };
}
Run Code Online (Sandbox Code Playgroud)

prettier 将删除 func 主体中第一行代码上方的所有回车符:

export const handleHMRMessages = (cache: any, store: CPReduxStore, conn: WebSocket) => {
  conn.onclose = () => {
    store.dispatch(actions.dev.hmrDisconnected());
  };
}
Run Code Online (Sandbox Code Playgroud)

所以我一直在声明上方添加注释以保留空间:

export const handleHMRMessages = (cache: any, store: CPReduxStore, conn: WebSocket) => {
  // add comment here as needed
  conn.onclose = () => {
    store.dispatch(actions.dev.hmrDisconnected());
  };
}
Run Code Online (Sandbox Code Playgroud)

是否有一个更漂亮的设置,可以在函数声明下面始终没有更多或更少的空间?

mat*_*ell 2

不。Prettier 是一个固执己见的格式化程序,可配置性有限,并且是故意这样设计的。此处列出了可用的选项。