30

Is there a way to disable the printWidth rule warning in prettier?

I want to be able to determine my own line length for readability. In certain cases I want a line break, and in other cases I don't.

I tried this in my .prettierrc file :

{
  "singleQuote": true,
  "printWidth" : "off"
}

But this does not work.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225

2 Answers2

23

The short answer is no, you cannot disable it entirely.

There are, however, a few workarounds, but they have caveats.

To quote an answer from this issue on github: https://github.com/prettier/prettier/issues/3468. printWidth is not a rule but an input into the algorithm they use to generate their output. Meaning, it is required to be there.

One workaround is to set printWidth to a really high number, but although this will prevent lines from breaking, changing this property will affect your entire codebase, causing other lines throughout it to combine into a single line, which is most likely not desired.

Your second option is to disable prettier for a block of code with the // prettier-ignore syntax. The downside to this is that you'll disable all prettier features for this section of the code. Also, I personally don't find it very "clean" to have comments like that all over your code. You can read about how to use the ignore functionality here: https://prettier.io/docs/en/ignore.html

thorn0
  • 9,362
  • 3
  • 68
  • 96
Jakob S. Rames
  • 314
  • 2
  • 15
2

If you want to disable prettier rules once, just do:

// prettier-ignore
const date = new Date()   // prettier disabled on this line
Dreamoon
  • 369
  • 5
  • 8
  • 6
    @OliverWatkins Oliver is right, his code would not be, "prettier", It would be "uglier" instead. This option defeats the entire point of using a formatting tool. IMO this is not just a bad solution, but it really isn't a solution at all, as it doesn't disable the rule, it just disables prettier entirely for 1 line. What he suppose to do put this on every line? – JΛYDΞV Aug 03 '21 at 10:58