简体   繁体   English

ESLint & Prettier 配置每个函数后的换行符?

[英]ESLint & Prettier config for line break after each functions?

I'm currently set up my project using ESLint and Prettier.我目前正在使用 ESLint 和 Prettier 设置我的项目。

I would like to know if there's a way to format this code...我想知道是否有格式化此代码的方法...

const Demo = () => {
  const [state, setState] = useState(false);
  const [num, setNum] = useState(10);
  useEffect(() => {
    //...
  },[])
  useEffect(() => {
    //...
  },[state])
  const handleSubmit = () => {
  }
  return (
  //...
  )

}

into this...进入这个...

const Demo = () => {
  const [state, setState] = useState(false);
  const [num, setNum] = useState(10);

  useEffect(() => {
    //...
  },[])

  useEffect(() => {
    //...
  },[state])

  const handleSubmit = () => {
  }

  return (
  //...
  )

}

Here's my.prettierrc file looks like这是 my.prettierrc 文件的样子

{
  "printWidth": 100,
  "semi": false,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "all",
  "endOfLine": "auto"
}

I tried like almost every config but I can't format the code desired way.我尝试了几乎所有的配置,但我无法以所需的方式格式化代码。 I appreciate it if someone could help to figure this out.如果有人可以帮助解决这个问题,我将不胜感激。

Noting that eslint is for styling and prettier is for formatting, so the responsibility for this lies with prettier.请注意,eslint 用于样式设置,而 prettier 用于格式化,因此由 prettier 负责。

I have looked into this before, prettier does not have that option, here is a list of the options which are available我之前研究过这个,prettier 没有那个选项,这里是可用选项的列表

However i just read that padding-line-between-statements is possible with eslint which may do the job FYI TS-ESlint recommend not using this rule - again formatting is the job of a formatter, not a linter like eslint.然而,我刚刚读到, 语句之间的填充行可以用 eslint 实现,这可能会完成FYI TS-ESlint 建议不要使用此规则- 再次格式化是格式化程序的工作,而不是像 eslint 这样的 linter。

This would be how to implement the rule in your eslintrc file, the configuration is an object which has 3 properties;这将是如何在你的 eslintrc 文件中实现规则,配置是一个 object,它有 3 个属性; blankLine, prev and next.空白行、上一个和下一个。 For example, { blankLine: "always", prev: "var", next: "return" } means "one or more blank lines are required between a variable declaration and a return statement."例如,{ blankLine: "always", prev: "var", next: "return" } 表示“变量声明和返回语句之间需要一个或多个空行”。

"padding-line-between-statements": [
    "error",
    { blankLine: "always", prev: "export", next: "*" },
    { blankLine: "always", prev: "import", next: "*" },
    { blankLine: "always", prev: "var", next: "*" },
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM