【问题标题】:Why does Prettier break up my JSON object into multiple lines?为什么 Prettier 将我的 JSON 对象分成多行?
【发布时间】:2020-11-02 00:56:36
【问题描述】:

Prettier 很棒,但它会将我的 javascript 对象分成多行,如下所示:

<Text
    style={
        styles.chineseText
    }
>
    {
        this.state
            .housePayload
            .sitMountain
            .chinese
    }
</Text>

我希望结果类似于:

<Text
    style={
        styles.chineseText
    }
>
    { this.state.housePayload.sitMountain.chinese }
</Text>

我怎样才能做到这一点?我当前的配置文件是:

{
    "trailingComma": "es5",
    "tabs": true,
    "tabWidth": 4,
    "semi": false,
    "singleQuote": true
}

【问题讨论】:

  • 你试过 printWidth 了吗?

标签: javascript jsx prettier


【解决方案1】:

您应该在配置中尝试printWidth。默认值为80,您可以指定任何适合您的数字。例如:

printWidth: 100

来自Doc

这是一种向 Prettier 大致告知您希望台词多长的方法 是。 Prettier 会制作更短和更长的线条,但通常 努力满足指定的 printWidth。

同样需要注意的是:

不要尝试使用 printWidth 就好像它是 ESLint 的 max-len - 它们是 不一样。 max-len 只是说允许的最大行长 是,但不是通常首选的长度——这就是 printWidth 指定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 2019-04-30
    • 2017-05-13
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 2018-12-18
    相关资源
    最近更新 更多