【问题标题】:Why doesn't prettier allow me to break the function arguments into multiline?为什么 prettier 不允许我将函数参数分成多行?
【发布时间】:2019-10-25 12:37:41
【问题描述】:

Prettier 自动变化

    it(
      "Throws error if item has source: false and doesn't have children",
      () => {
        const rawData = [{ slug: 'item-name', source: false }]

        jest.doMock('../sidebar.json', () => rawData)

        expect(() => require('./helper')).toThrow(
          new Error(
            "If you set 'source' to false, you had to add at least one child"
          )
        )
      }
    )

到这里

    it("Throws error if item has source: false and doesn't have children", () => {
      const rawData = [{ slug: 'item-name', source: false }]

      jest.doMock('../sidebar.json', () => rawData)

      expect(() => require('./helper')).toThrow(
        new Error(
          "If you set 'source' to false, you had to add at least one child"
        )
      )
    })

我更漂亮的配置:

semi: false
singleQuote: true
trailingComma: none
printWidth: 80
tabWidth: 2
useTabs: false
proseWrap: always

它不知何故不尊重printWidth 规则并将所有参数写入一行。

我目前的解决方法是// prettier-ignore,但有更好的解决方案吗?

【问题讨论】:

  • 那是因为线宽是 78,看起来,所以它遵守规则:P 尝试将其减少到 75,看看是否有任何效果?
  • @Kobe 真的很抱歉格式错误。但我现在已经编辑了。请注意添加的额外缩进,因为它已经在由两个空格缩进的函数 describe('default', () => { /* 'it' is here */ }) 中。

标签: javascript prettier


【解决方案1】:

Prettier 将测试框架函数调用与其他函数调用区别对待,将它们全部放在同一行。

来源:https://github.com/prettier/prettier/blob/ae176f2e790bb61144eb071acc1c22616fe1401e/src/language-js/print/call-expression.js#L39

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 2023-03-27
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2023-03-25
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多