简体   繁体   English

我想知道如何防止 Prettier 以这种奇怪的形式格式化我的 js 代码

[英]I wish to know how I can prevent Prettier to format my js code in this strange form

I started to use Prettier inside my Microsoft Visual Studio Code (MVSC), but, when formatting a javascript file, I get some strange formatting.我开始在我的 Microsoft Visual Studio Code (MVSC) 中使用 Prettier,但是,在格式化 javascript 文件时,我得到了一些奇怪的格式。

For example, if I have this code:例如,如果我有这个代码:

    if ($("#epgprogramdescription" + channelListIndexOffset + ":nth-of-type(" + (programListIndexOffset + 1) + ")" ).length) {
        $("#epgprogramdescription" + channelListIndexOffset + ":nth-of-type(" + programListIndexOffset + ")").removeClass("epgtablerowselected");
        $("#epgprogramdescription" + channelListIndexOffset + ":nth-of-type(" + (programListIndexOffset + 1) + ")").addClass("epgtablerowselected");
    } else {
        programListIndexOffset--;
    }

It get it formatted as:它被格式化为:

    if (
        $(
            "#epgprogramdescription" +
                channelListIndexOffset +
                ":nth-of-type(" +
                (programListIndexOffset + 1) +
                ")"
        ).length
    ) {
        $(
            "#epgprogramdescription" +
                channelListIndexOffset +
                ":nth-of-type(" +
                programListIndexOffset +
                ")"
        ).removeClass("epgtablerowselected");
        $(
            "#epgprogramdescription" +
                channelListIndexOffset +
                ":nth-of-type(" +
                (programListIndexOffset + 1) +
                ")"
        ).addClass("epgtablerowselected");
    } else {
        programListIndexOffset--;
    }

Any way to configure Prettier to maintain the original formatting?有什么方法可以配置 Prettier 以保持原始格式?

Thanks.谢谢。

Prettier gonna always try to give line break your code if it exceeds your print width configuration.如果您的代码超出您的打印宽度配置,Prettier 将始终尝试给它换行。

You can change that by accessing vscode settings您可以通过访问 vscode 设置来更改它

  1. Go to File -> Preferences -> Settings转到文件 -> 首选项 -> 设置
  2. Search for "Prettier"搜索“更漂亮”
  3. Look for configuration called "Prettier: Print Width"寻找名为“Prettier: Print Width”的配置
  4. And change it to suit your needs !并根据您的需要进行更改!

Give it bigger value if you want your code to stay in the same line.如果您希望代码保持在同一行,则赋予它更大的价值。 Prettier only break your code into new lines if it exceeds the given configuration如果代码超过给定的配置,Prettier 只会将您的代码分成新行

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

相关问题 如何让 Prettier 格式化我的代码并继续缩进? - How do I make Prettier format my code with indentation continuation? 我怎样才能防止更漂亮的行为 - How can I prevent prettier behavior 如何防止代码包装在 Prettier / ESLint - How do I prevent code wrapping in Prettier / ESLint 如何配置 Prettier 格式可用的 React 代码? - How do I configure Prettier format usable React code? VS Code:如何阻止 Prettier 在我的 JS 文件末尾添加新行? - VS Code: How do I stop Prettier from adding a new line at the end of my JS file? 我想知道如何在后面的代码中访问模态表单元素。我也想知道如何在模态表单上应用jquery验证器 - I want to know that how can i access my modal form elements in my code behind .Also I want to know how can I apply jquery validator on modal form 我希望使我的代码验证一个字段,以确保它是四位数。 我怎样才能做到这一点? - I wish to make my code validate a field to make sure it is a number of four digits. How can I do this? 如何使用更漂亮的配置改进代码格式 - How can i improve code formatting using prettier configuration 如何防止HTML代码覆盖2个js文件? - How do I prevent 2 js files to be overwrite in my HTML code? 如何禁用 eslint 上的错误(更漂亮/更漂亮)? - How can I disable the error (prettier/prettier) on eslint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM